git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Nguyen Thai Ngoc Duy <pclouds@gmail.com>
Date
Apr 21, 2012, 05:46 UTC
Message-ID
<20120421054636.GA6849@do>
In-Reply-To
<20120421030018.GA32687@do>
On Sat, Apr 21, 2012 at 10:00:18AM +0700, Nguyen Thai Ngoc Duy wrote:
Show 8 quoted lines
> > 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
Previous: Nguyen Thai Ngoc Duy
Message 19 of 19 in “What's cooking in git.git (Apr 2012, #06; Sun, 15)”
  1. Junio C HamanoApr 16, 2012
  2. Nelson Benitez LeonApr 16, 2012
  3. Junio C HamanoApr 16, 2012
  4. Nelson Benitez LeonApr 16, 2012
  5. Junio C HamanoApr 16, 2012
  6. Johannes SixtApr 18, 2012
  7. Ramsay JonesApr 18, 2012
  8. Johannes SixtApr 19, 2012
  9. Johannes SixtApr 19, 2012
  10. Nguyen Thai Ngoc DuyApr 19, 2012
  11. Nguyen Thai Ngoc DuyApr 19, 2012
  12. Erik Faye-LundApr 19, 2012
  13. Nguyen Thai Ngoc DuyApr 19, 2012
  14. Erik Faye-LundApr 19, 2012
  15. Nguyen Thai Ngoc DuyApr 19, 2012
  16. Johannes SixtApr 19, 2012
  17. Erik Faye-LundApr 19, 2012
  18. Nguyen Thai Ngoc DuyApr 21, 2012
  19. Nguyen Thai Ngoc DuyApr 21, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.