Subject: Re: What's cooking in git.git (Apr 2012, #06; Sun, 15)

From: Nguyen Thai Ngoc Duy <[email-masked]>
Date: Sat, 21 Apr 2012 12:46:36 +0700

On Sat, Apr 21, 2012 at 10:00:18AM +0700, Nguyen Thai Ngoc Duy wrote:
> > I don't know our use-cases, but I'd be a lot happier if I could find a
> > safe way to have it not update the file-pointer. Just reading it and
> > setting it back again would be racy.
> 
> Replace pread() in index-pack to pread_weak() because we know we don't
> care about file offset in index-pack. Define pread_weak as pread
> normally. Windows port can provide its own pread_weak version, which
> can freely move file offset.

Or we could avoid sharing pack_fd file handle, making pread's
thread-safety irrelevant. This does not give any performance
improvements on linux (I guess linux vfs does not hold per-file
locks), so I'm not interested. If you guys want multithread index-pack
on Windows, go for it.

-- 8< --
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index c1c3c81..051325a 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -50,6 +50,7 @@ struct thread_local {
 #endif
 	struct base_data *base_cache;
 	size_t base_cache_used;
+	int pack_fd;
 };
 
 /*
@@ -89,7 +90,8 @@ static off_t consumed_bytes;
 static unsigned deepest_delta;
 static git_SHA_CTX input_ctx;
 static uint32_t input_crc32;
-static int input_fd, output_fd, pack_fd;
+static const char *curr_pack;
+static int input_fd, output_fd;
 
 #ifndef NO_PTHREADS
 
@@ -126,16 +128,23 @@ static inline void unlock_mutex(pthread_mutex_t *mutex)
  */
 static void init_thread(void)
 {
+	int i;
 	init_recursive_mutex(&read_mutex);
 	pthread_mutex_init(&counter_mutex, NULL);
 	pthread_mutex_init(&work_mutex, NULL);
 	pthread_key_create(&key, NULL);
 	thread_data = xcalloc(nr_threads, sizeof(*thread_data));
+	for (i = 0; i < nr_threads; i++) {
+		thread_data[i].pack_fd = open(curr_pack, O_RDONLY);
+		if (thread_data[i].pack_fd == -1)
+			die_errno("unable to open %s", curr_pack);
+	}
 	threads_active = 1;
 }
 
 static void cleanup_thread(void)
 {
+	int i;
 	if (!threads_active)
 		return;
 	threads_active = 0;
@@ -143,6 +152,8 @@ static void cleanup_thread(void)
 	pthread_mutex_destroy(&counter_mutex);
 	pthread_mutex_destroy(&work_mutex);
 	pthread_key_delete(key);
+	for (i = 0; i < nr_threads; i++)
+		close(thread_data[i].pack_fd);
 	free(thread_data);
 }
 
@@ -267,13 +278,13 @@ static const char *open_pack_file(const char *pack_name)
 			output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
 		if (output_fd < 0)
 			die_errno("unable to create '%s'", pack_name);
-		pack_fd = output_fd;
+		nothread_data.pack_fd = output_fd;
 	} else {
 		input_fd = open(pack_name, O_RDONLY);
 		if (input_fd < 0)
 			die_errno("cannot open packfile '%s'", pack_name);
 		output_fd = -1;
-		pack_fd = input_fd;
+		nothread_data.pack_fd = input_fd;
 	}
 	git_SHA1_Init(&input_ctx);
 	return pack_name;
@@ -479,7 +490,7 @@ static void *get_data_from_pack(struct object_entry *obj)
 
 	do {
 		ssize_t n = (len < 64*1024) ? len : 64*1024;
-		n = pread(pack_fd, inbuf, n, from);
+		n = pread(get_thread_data()->pack_fd, inbuf, n, from);
 		if (n < 0)
 			die_errno("cannot pread pack file");
 		if (!n)
@@ -1237,7 +1248,7 @@ static void show_pack_info(int stat_only)
 int cmd_index_pack(int argc, const char **argv, const char *prefix)
 {
 	int i, fix_thin_pack = 0, verify = 0, stat_only = 0, stat = 0;
-	const char *curr_pack, *curr_index;
+	const char *curr_index;
 	const char *index_name = NULL, *pack_name = NULL;
 	const char *keep_name = NULL, *keep_msg = NULL;
 	char *index_name_buf = NULL, *keep_name_buf = NULL;
-- 8< --
-- 
Duy