@@ -40,17 +40,13 @@ struct base_data {
4040 int ofs_first , ofs_last ;
4141};
4242
43- #if !defined(NO_PTHREADS ) && defined(NO_THREAD_SAFE_PREAD )
44- /* pread() emulation is not thread-safe. Disable threading. */
45- #define NO_PTHREADS
46- #endif
47-
4843struct thread_local {
4944#ifndef NO_PTHREADS
5045 pthread_t thread ;
5146#endif
5247 struct base_data * base_cache ;
5348 size_t base_cache_used ;
49+ int pack_fd ;
5450};
5551
5652/*
@@ -91,7 +87,8 @@ static off_t consumed_bytes;
9187static unsigned deepest_delta ;
9288static git_SHA_CTX input_ctx ;
9389static uint32_t input_crc32 ;
94- static int input_fd , output_fd , pack_fd ;
90+ static int input_fd , output_fd ;
91+ static const char * curr_pack ;
9592
9693#ifndef NO_PTHREADS
9794
@@ -134,18 +131,26 @@ static inline void unlock_mutex(pthread_mutex_t *mutex)
134131 */
135132static void init_thread (void )
136133{
134+ int i ;
137135 init_recursive_mutex (& read_mutex );
138136 pthread_mutex_init (& counter_mutex , NULL );
139137 pthread_mutex_init (& work_mutex , NULL );
140138 if (show_stat )
141139 pthread_mutex_init (& deepest_delta_mutex , NULL );
142140 pthread_key_create (& key , NULL );
143141 thread_data = xcalloc (nr_threads , sizeof (* thread_data ));
142+ for (i = 0 ; i < nr_threads ; i ++ ) {
143+ thread_data [i ].pack_fd = open (curr_pack , O_RDONLY );
144+ if (thread_data [i ].pack_fd == -1 )
145+ die_errno (_ ("unable to open %s" ), curr_pack );
146+ }
147+
144148 threads_active = 1 ;
145149}
146150
147151static void cleanup_thread (void )
148152{
153+ int i ;
149154 if (!threads_active )
150155 return ;
151156 threads_active = 0 ;
@@ -154,6 +159,8 @@ static void cleanup_thread(void)
154159 pthread_mutex_destroy (& work_mutex );
155160 if (show_stat )
156161 pthread_mutex_destroy (& deepest_delta_mutex );
162+ for (i = 0 ; i < nr_threads ; i ++ )
163+ close (thread_data [i ].pack_fd );
157164 pthread_key_delete (key );
158165 free (thread_data );
159166}
@@ -288,13 +295,13 @@ static const char *open_pack_file(const char *pack_name)
288295 output_fd = open (pack_name , O_CREAT |O_EXCL |O_RDWR , 0600 );
289296 if (output_fd < 0 )
290297 die_errno (_ ("unable to create '%s'" ), pack_name );
291- pack_fd = output_fd ;
298+ nothread_data . pack_fd = output_fd ;
292299 } else {
293300 input_fd = open (pack_name , O_RDONLY );
294301 if (input_fd < 0 )
295302 die_errno (_ ("cannot open packfile '%s'" ), pack_name );
296303 output_fd = -1 ;
297- pack_fd = input_fd ;
304+ nothread_data . pack_fd = input_fd ;
298305 }
299306 git_SHA1_Init (& input_ctx );
300307 return pack_name ;
@@ -542,7 +549,7 @@ static void *unpack_data(struct object_entry *obj,
542549
543550 do {
544551 ssize_t n = (len < 64 * 1024 ) ? len : 64 * 1024 ;
545- n = pread (pack_fd , inbuf , n , from );
552+ n = pread (get_thread_data () -> pack_fd , inbuf , n , from );
546553 if (n < 0 )
547554 die_errno (_ ("cannot pread pack file" ));
548555 if (!n )
@@ -1490,7 +1497,7 @@ static void show_pack_info(int stat_only)
14901497int cmd_index_pack (int argc , const char * * argv , const char * prefix )
14911498{
14921499 int i , fix_thin_pack = 0 , verify = 0 , stat_only = 0 ;
1493- const char * curr_pack , * curr_index ;
1500+ const char * curr_index ;
14941501 const char * index_name = NULL , * pack_name = NULL ;
14951502 const char * keep_name = NULL , * keep_msg = NULL ;
14961503 char * index_name_buf = NULL , * keep_name_buf = NULL ;
0 commit comments