Skip to content

Commit 53f52cd

Browse files
committed
Merge branch 'nd/index-pack-one-fd-per-thread'
Enable threaded index-pack on platforms without thread-unsafe pread() emulation. * nd/index-pack-one-fd-per-thread: index-pack: work around thread-unsafe pread()
2 parents 9af098c + 3953949 commit 53f52cd

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

Makefile

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,6 @@ all::
185185
# Define NO_STRUCT_ITIMERVAL if you don't have struct itimerval
186186
# This also implies NO_SETITIMER
187187
#
188-
# Define NO_THREAD_SAFE_PREAD if your pread() implementation is not
189-
# thread-safe. (e.g. compat/pread.c or cygwin)
190-
#
191188
# Define NO_FAST_WORKING_DIRECTORY if accessing objects in pack files is
192189
# generally faster on your platform than accessing the working directory.
193190
#
@@ -1345,10 +1342,6 @@ endif
13451342
ifdef NO_PREAD
13461343
COMPAT_CFLAGS += -DNO_PREAD
13471344
COMPAT_OBJS += compat/pread.o
1348-
NO_THREAD_SAFE_PREAD = YesPlease
1349-
endif
1350-
ifdef NO_THREAD_SAFE_PREAD
1351-
BASIC_CFLAGS += -DNO_THREAD_SAFE_PREAD
13521345
endif
13531346
ifdef NO_FAST_WORKING_DIRECTORY
13541347
BASIC_CFLAGS += -DNO_FAST_WORKING_DIRECTORY

builtin/index-pack.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
4843
struct 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;
9187
static unsigned deepest_delta;
9288
static git_SHA_CTX input_ctx;
9389
static 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
*/
135132
static 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

147151
static 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 = xpread(pack_fd, inbuf, n, from);
552+
n = xpread(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)
14901497
int 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;

config.mak.uname

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ ifeq ($(uname_O),Cygwin)
160160
NO_SYMLINK_HEAD = YesPlease
161161
NO_IPV6 = YesPlease
162162
OLD_ICONV = UnfortunatelyYes
163-
NO_THREAD_SAFE_PREAD = YesPlease
164163
# There are conflicting reports about this.
165164
# On some boxes NO_MMAP is needed, and not so elsewhere.
166165
# Try commenting this out if you suspect MMAP is more efficient

0 commit comments

Comments
 (0)