Commit ab7729747c4b55a93b8973e94429c747fe274529

Edward Thomson 2020-12-05T15:49:30

threads: give atomic functions the git_atomic prefix

diff --git a/src/attrcache.c b/src/attrcache.c
index 47fb675..2485b05 100644
--- a/src/attrcache.c
+++ b/src/attrcache.c
@@ -108,7 +108,7 @@ static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file)
 	 * Replace the existing value if another thread has
 	 * created it in the meantime.
 	 */
-	old = git__swap(entry->file[file->source], file);
+	old = git_atomic_swap(entry->file[file->source], file);
 
 	if (old) {
 		GIT_REFCOUNT_OWN(old, NULL);
@@ -132,7 +132,7 @@ static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
 		return error;
 
 	if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL)
-		old = git__compare_and_swap(&entry->file[file->source], file, NULL);
+		old = git_atomic_compare_and_swap(&entry->file[file->source], file, NULL);
 
 	attr_cache_unlock(cache);
 
@@ -321,7 +321,7 @@ static void attr_cache__free(git_attr_cache *cache)
 
 		git_strmap_foreach_value(cache->files, entry, {
 			for (i = 0; i < GIT_ATTR_FILE_NUM_SOURCES; ++i) {
-				if ((file = git__swap(entry->file[i], NULL)) != NULL) {
+				if ((file = git_atomic_swap(entry->file[i], NULL)) != NULL) {
 					GIT_REFCOUNT_OWN(file, NULL);
 					git_attr_file__free(file);
 				}
@@ -395,7 +395,7 @@ int git_attr_cache__init(git_repository *repo)
 	    (ret = git_pool_init(&cache->pool, 1)) < 0)
 		goto cancel;
 
-	cache = git__compare_and_swap(&repo->attrcache, NULL, cache);
+	cache = git_atomic_compare_and_swap(&repo->attrcache, NULL, cache);
 	if (cache)
 		goto cancel; /* raced with another thread, free this but no error */
 
@@ -417,7 +417,7 @@ int git_attr_cache_flush(git_repository *repo)
 	/* this could be done less expensively, but for now, we'll just free
 	 * the entire attrcache and let the next use reinitialize it...
 	 */
-	if (repo && (cache = git__swap(repo->attrcache, NULL)) != NULL)
+	if (repo && (cache = git_atomic_swap(repo->attrcache, NULL)) != NULL)
 		attr_cache__free(cache);
 
 	return 0;
diff --git a/src/config_cache.c b/src/config_cache.c
index 1a28ba9..2f0455a 100644
--- a/src/config_cache.c
+++ b/src/config_cache.c
@@ -111,7 +111,7 @@ int git_config__configmap_lookup(int *out, git_config *config, git_configmap_ite
 
 int git_repository__configmap_lookup(int *out, git_repository *repo, git_configmap_item item)
 {
-	*out = (int)(intptr_t)git__load(repo->configmap_cache[(int)item]);
+	*out = (int)(intptr_t)git_atomic_load(repo->configmap_cache[(int)item]);
 
 	if (*out == GIT_CONFIGMAP_NOT_CACHED) {
 		int error;
@@ -122,7 +122,7 @@ int git_repository__configmap_lookup(int *out, git_repository *repo, git_configm
 			(error = git_config__configmap_lookup(out, config, item)) < 0)
 			return error;
 
-		git__compare_and_swap(&repo->configmap_cache[(int)item], &oldval, out);
+		git_atomic_compare_and_swap(&repo->configmap_cache[(int)item], &oldval, out);
 	}
 
 	return 0;
diff --git a/src/diff_driver.c b/src/diff_driver.c
index 606c391..6b19012 100644
--- a/src/diff_driver.c
+++ b/src/diff_driver.c
@@ -144,7 +144,7 @@ static git_diff_driver_registry *git_repository_driver_registry(
 {
 	if (!repo->diff_drivers) {
 		git_diff_driver_registry *reg = git_diff_driver_registry_new();
-		reg = git__compare_and_swap(&repo->diff_drivers, NULL, reg);
+		reg = git_atomic_compare_and_swap(&repo->diff_drivers, NULL, reg);
 
 		if (reg != NULL) /* if we race, free losing allocation */
 			git_diff_driver_registry_free(reg);
diff --git a/src/index.c b/src/index.c
index 942fcca..7ebe075 100644
--- a/src/index.c
+++ b/src/index.c
@@ -495,7 +495,7 @@ static void index_free_deleted(git_index *index)
 		return;
 
 	for (i = 0; i < index->deleted.length; ++i) {
-		git_index_entry *ie = git__swap(index->deleted.contents[i], NULL);
+		git_index_entry *ie = git_atomic_swap(index->deleted.contents[i], NULL);
 		index_entry_free(ie);
 	}
 
@@ -2295,7 +2295,7 @@ int git_index_reuc_clear(git_index *index)
 	GIT_ASSERT_ARG(index);
 
 	for (i = 0; i < index->reuc.length; ++i)
-		index_entry_reuc_free(git__swap(index->reuc.contents[i], NULL));
+		index_entry_reuc_free(git_atomic_swap(index->reuc.contents[i], NULL));
 
 	git_vector_clear(&index->reuc);
 
@@ -3197,7 +3197,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
 		/* well, this isn't good */;
 	} else {
 		git_vector_swap(&entries, &index->entries);
-		entries_map = git__swap(index->entries_map, entries_map);
+		entries_map = git_atomic_swap(index->entries_map, entries_map);
 	}
 
 	index->dirty = 1;
@@ -3331,7 +3331,7 @@ static int git_index_read_iterator(
 	    goto done;
 
 	git_vector_swap(&new_entries, &index->entries);
-	new_entries_map = git__swap(index->entries_map, new_entries_map);
+	new_entries_map = git_atomic_swap(index->entries_map, new_entries_map);
 
 	git_vector_foreach(&remove_entries, i, entry) {
 		if (index->tree)
diff --git a/src/repository.c b/src/repository.c
index bef84ca..3b6bcbe 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -93,7 +93,7 @@ static void set_odb(git_repository *repo, git_odb *odb)
 		GIT_REFCOUNT_INC(odb);
 	}
 
-	if ((odb = git__swap(repo->_odb, odb)) != NULL) {
+	if ((odb = git_atomic_swap(repo->_odb, odb)) != NULL) {
 		GIT_REFCOUNT_OWN(odb, NULL);
 		git_odb_free(odb);
 	}
@@ -106,7 +106,7 @@ static void set_refdb(git_repository *repo, git_refdb *refdb)
 		GIT_REFCOUNT_INC(refdb);
 	}
 
-	if ((refdb = git__swap(repo->_refdb, refdb)) != NULL) {
+	if ((refdb = git_atomic_swap(repo->_refdb, refdb)) != NULL) {
 		GIT_REFCOUNT_OWN(refdb, NULL);
 		git_refdb_free(refdb);
 	}
@@ -119,7 +119,7 @@ static void set_config(git_repository *repo, git_config *config)
 		GIT_REFCOUNT_INC(config);
 	}
 
-	if ((config = git__swap(repo->_config, config)) != NULL) {
+	if ((config = git_atomic_swap(repo->_config, config)) != NULL) {
 		GIT_REFCOUNT_OWN(config, NULL);
 		git_config_free(config);
 	}
@@ -134,7 +134,7 @@ static void set_index(git_repository *repo, git_index *index)
 		GIT_REFCOUNT_INC(index);
 	}
 
-	if ((index = git__swap(repo->_index, index)) != NULL) {
+	if ((index = git_atomic_swap(repo->_index, index)) != NULL) {
 		GIT_REFCOUNT_OWN(index, NULL);
 		git_index_free(index);
 	}
@@ -1054,7 +1054,7 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo)
 		if (!error) {
 			GIT_REFCOUNT_OWN(config, repo);
 
-			config = git__compare_and_swap(&repo->_config, NULL, config);
+			config = git_atomic_compare_and_swap(&repo->_config, NULL, config);
 			if (config != NULL) {
 				GIT_REFCOUNT_OWN(config, NULL);
 				git_config_free(config);
@@ -1107,7 +1107,7 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
 	GIT_ASSERT_ARG(repo);
 	GIT_ASSERT_ARG(out);
 
-	*out = git__load(repo->_odb);
+	*out = git_atomic_load(repo->_odb);
 	if (*out == NULL) {
 		git_buf odb_path = GIT_BUF_INIT;
 		git_odb *odb;
@@ -1125,14 +1125,14 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
 			return error;
 		}
 
-		odb = git__compare_and_swap(&repo->_odb, NULL, odb);
+		odb = git_atomic_compare_and_swap(&repo->_odb, NULL, odb);
 		if (odb != NULL) {
 			GIT_REFCOUNT_OWN(odb, NULL);
 			git_odb_free(odb);
 		}
 
 		git_buf_dispose(&odb_path);
-		*out = git__load(repo->_odb);
+		*out = git_atomic_load(repo->_odb);
 	}
 
 	return error;
@@ -1170,7 +1170,7 @@ int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
 		if (!error) {
 			GIT_REFCOUNT_OWN(refdb, repo);
 
-			refdb = git__compare_and_swap(&repo->_refdb, NULL, refdb);
+			refdb = git_atomic_compare_and_swap(&repo->_refdb, NULL, refdb);
 			if (refdb != NULL) {
 				GIT_REFCOUNT_OWN(refdb, NULL);
 				git_refdb_free(refdb);
@@ -1218,7 +1218,7 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo)
 		if (!error) {
 			GIT_REFCOUNT_OWN(index, repo);
 
-			index = git__compare_and_swap(&repo->_index, NULL, index);
+			index = git_atomic_compare_and_swap(&repo->_index, NULL, index);
 			if (index != NULL) {
 				GIT_REFCOUNT_OWN(index, NULL);
 				git_index_free(index);
@@ -3044,8 +3044,8 @@ int git_repository_set_ident(git_repository *repo, const char *name, const char 
 		GIT_ERROR_CHECK_ALLOC(tmp_email);
 	}
 
-	tmp_name = git__swap(repo->ident_name, tmp_name);
-	tmp_email = git__swap(repo->ident_email, tmp_email);
+	tmp_name = git_atomic_swap(repo->ident_name, tmp_name);
+	tmp_email = git_atomic_swap(repo->ident_email, tmp_email);
 
 	git__free(tmp_name);
 	git__free(tmp_email);
diff --git a/src/runtime.c b/src/runtime.c
index 3fd0649..b61c3c4 100644
--- a/src/runtime.c
+++ b/src/runtime.c
@@ -37,7 +37,7 @@ static void shutdown_common(void)
 	for (pos = git_atomic32_get(&shutdown_callback_count);
 	     pos > 0;
 	     pos = git_atomic32_dec(&shutdown_callback_count)) {
-		cb = git__swap(shutdown_callback[pos - 1], NULL);
+		cb = git_atomic_swap(shutdown_callback[pos - 1], NULL);
 
 		if (cb != NULL)
 			cb();
diff --git a/src/thread-utils.h b/src/thread-utils.h
index 31a4c09..9b511b6 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -139,7 +139,7 @@ GIT_INLINE(int) git_atomic32_get(git_atomic32 *a)
 #endif
 }
 
-GIT_INLINE(void *) git___compare_and_swap(
+GIT_INLINE(void *) git_atomic__compare_and_swap(
 	void * volatile *ptr, void *oldval, void *newval)
 {
 #if defined(GIT_WIN32)
@@ -158,7 +158,7 @@ GIT_INLINE(void *) git___compare_and_swap(
 #endif
 }
 
-GIT_INLINE(volatile void *) git___swap(
+GIT_INLINE(volatile void *) git_atomic__swap(
 	void * volatile *ptr, void *newval)
 {
 #if defined(GIT_WIN32)
@@ -174,7 +174,7 @@ GIT_INLINE(volatile void *) git___swap(
 #endif
 }
 
-GIT_INLINE(volatile void *) git___load(void * volatile *ptr)
+GIT_INLINE(volatile void *) git_atomic__load(void * volatile *ptr)
 {
 #if defined(GIT_WIN32)
 	void *newval = NULL, *oldval = NULL;
@@ -294,7 +294,7 @@ GIT_INLINE(int) git_atomic32_get(git_atomic32 *a)
 	return (int)a->val;
 }
 
-GIT_INLINE(void *) git___compare_and_swap(
+GIT_INLINE(void *) git_atomic__compare_and_swap(
 	void * volatile *ptr, void *oldval, void *newval)
 {
 	if (*ptr == oldval)
@@ -304,7 +304,7 @@ GIT_INLINE(void *) git___compare_and_swap(
 	return oldval;
 }
 
-GIT_INLINE(volatile void *) git___swap(
+GIT_INLINE(volatile void *) git_atomic__swap(
 	void * volatile *ptr, void *newval)
 {
 	volatile void *old = *ptr;
@@ -312,7 +312,7 @@ GIT_INLINE(volatile void *) git___swap(
 	return old;
 }
 
-GIT_INLINE(volatile void *) git___load(void * volatile *ptr)
+GIT_INLINE(volatile void *) git_atomic__load(void * volatile *ptr)
 {
 	return *ptr;
 }
@@ -342,12 +342,14 @@ GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
 /* Atomically replace oldval with newval
  * @return oldval if it was replaced or newval if it was not
  */
-#define git__compare_and_swap(P,O,N) \
-	git___compare_and_swap((void * volatile *)P, O, N)
+#define git_atomic_compare_and_swap(P,O,N) \
+	git_atomic__compare_and_swap((void * volatile *)P, O, N)
 
-#define git__swap(ptr, val) (void *)git___swap((void * volatile *)&ptr, val)
+#define git_atomic_swap(ptr, val) \
+	(void *)git_atomic__swap((void * volatile *)&ptr, val)
 
-#define git__load(ptr) (void *)git___load((void * volatile *)&ptr)
+#define git_atomic_load(ptr) \
+	(void *)git_atomic__load((void * volatile *)&ptr)
 
 #if defined(GIT_THREADS)
 
diff --git a/src/util.h b/src/util.h
index 6c7c4c2..b8ea68c 100644
--- a/src/util.h
+++ b/src/util.h
@@ -186,10 +186,10 @@ typedef void (*git_refcount_freeptr)(void *r);
 }
 
 #define GIT_REFCOUNT_OWN(r, o) { \
-	(void)git__swap((r)->rc.owner, o); \
+	(void)git_atomic_swap((r)->rc.owner, o); \
 }
 
-#define GIT_REFCOUNT_OWNER(r) git__load((r)->rc.owner)
+#define GIT_REFCOUNT_OWNER(r) git_atomic_load((r)->rc.owner)
 
 #define GIT_REFCOUNT_VAL(r) git_atomic32_get((r)->rc.refcount)