Commit ab772974 by Edward Thomson

threads: give atomic functions the git_atomic prefix

parent 37763d38
...@@ -108,7 +108,7 @@ static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file) ...@@ -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 * Replace the existing value if another thread has
* created it in the meantime. * created it in the meantime.
*/ */
old = git__swap(entry->file[file->source], file); old = git_atomic_swap(entry->file[file->source], file);
if (old) { if (old) {
GIT_REFCOUNT_OWN(old, NULL); GIT_REFCOUNT_OWN(old, NULL);
...@@ -132,7 +132,7 @@ static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file) ...@@ -132,7 +132,7 @@ static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
return error; return error;
if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL) 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); attr_cache_unlock(cache);
...@@ -321,7 +321,7 @@ static void attr_cache__free(git_attr_cache *cache) ...@@ -321,7 +321,7 @@ static void attr_cache__free(git_attr_cache *cache)
git_strmap_foreach_value(cache->files, entry, { git_strmap_foreach_value(cache->files, entry, {
for (i = 0; i < GIT_ATTR_FILE_NUM_SOURCES; ++i) { 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_REFCOUNT_OWN(file, NULL);
git_attr_file__free(file); git_attr_file__free(file);
} }
...@@ -395,7 +395,7 @@ int git_attr_cache__init(git_repository *repo) ...@@ -395,7 +395,7 @@ int git_attr_cache__init(git_repository *repo)
(ret = git_pool_init(&cache->pool, 1)) < 0) (ret = git_pool_init(&cache->pool, 1)) < 0)
goto cancel; goto cancel;
cache = git__compare_and_swap(&repo->attrcache, NULL, cache); cache = git_atomic_compare_and_swap(&repo->attrcache, NULL, cache);
if (cache) if (cache)
goto cancel; /* raced with another thread, free this but no error */ goto cancel; /* raced with another thread, free this but no error */
...@@ -417,7 +417,7 @@ int git_attr_cache_flush(git_repository *repo) ...@@ -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 /* this could be done less expensively, but for now, we'll just free
* the entire attrcache and let the next use reinitialize it... * 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); attr_cache__free(cache);
return 0; return 0;
......
...@@ -111,7 +111,7 @@ int git_config__configmap_lookup(int *out, git_config *config, git_configmap_ite ...@@ -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) 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) { if (*out == GIT_CONFIGMAP_NOT_CACHED) {
int error; int error;
...@@ -122,7 +122,7 @@ int git_repository__configmap_lookup(int *out, git_repository *repo, git_configm ...@@ -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) (error = git_config__configmap_lookup(out, config, item)) < 0)
return error; 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; return 0;
......
...@@ -144,7 +144,7 @@ static git_diff_driver_registry *git_repository_driver_registry( ...@@ -144,7 +144,7 @@ static git_diff_driver_registry *git_repository_driver_registry(
{ {
if (!repo->diff_drivers) { if (!repo->diff_drivers) {
git_diff_driver_registry *reg = git_diff_driver_registry_new(); 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 */ if (reg != NULL) /* if we race, free losing allocation */
git_diff_driver_registry_free(reg); git_diff_driver_registry_free(reg);
......
...@@ -495,7 +495,7 @@ static void index_free_deleted(git_index *index) ...@@ -495,7 +495,7 @@ static void index_free_deleted(git_index *index)
return; return;
for (i = 0; i < index->deleted.length; ++i) { 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); index_entry_free(ie);
} }
...@@ -2295,7 +2295,7 @@ int git_index_reuc_clear(git_index *index) ...@@ -2295,7 +2295,7 @@ int git_index_reuc_clear(git_index *index)
GIT_ASSERT_ARG(index); GIT_ASSERT_ARG(index);
for (i = 0; i < index->reuc.length; ++i) 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); git_vector_clear(&index->reuc);
...@@ -3197,7 +3197,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree) ...@@ -3197,7 +3197,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
/* well, this isn't good */; /* well, this isn't good */;
} else { } else {
git_vector_swap(&entries, &index->entries); 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; index->dirty = 1;
...@@ -3331,7 +3331,7 @@ static int git_index_read_iterator( ...@@ -3331,7 +3331,7 @@ static int git_index_read_iterator(
goto done; goto done;
git_vector_swap(&new_entries, &index->entries); 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) { git_vector_foreach(&remove_entries, i, entry) {
if (index->tree) if (index->tree)
......
...@@ -93,7 +93,7 @@ static void set_odb(git_repository *repo, git_odb *odb) ...@@ -93,7 +93,7 @@ static void set_odb(git_repository *repo, git_odb *odb)
GIT_REFCOUNT_INC(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_REFCOUNT_OWN(odb, NULL);
git_odb_free(odb); git_odb_free(odb);
} }
...@@ -106,7 +106,7 @@ static void set_refdb(git_repository *repo, git_refdb *refdb) ...@@ -106,7 +106,7 @@ static void set_refdb(git_repository *repo, git_refdb *refdb)
GIT_REFCOUNT_INC(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_REFCOUNT_OWN(refdb, NULL);
git_refdb_free(refdb); git_refdb_free(refdb);
} }
...@@ -119,7 +119,7 @@ static void set_config(git_repository *repo, git_config *config) ...@@ -119,7 +119,7 @@ static void set_config(git_repository *repo, git_config *config)
GIT_REFCOUNT_INC(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_REFCOUNT_OWN(config, NULL);
git_config_free(config); git_config_free(config);
} }
...@@ -134,7 +134,7 @@ static void set_index(git_repository *repo, git_index *index) ...@@ -134,7 +134,7 @@ static void set_index(git_repository *repo, git_index *index)
GIT_REFCOUNT_INC(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_REFCOUNT_OWN(index, NULL);
git_index_free(index); git_index_free(index);
} }
...@@ -1054,7 +1054,7 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo) ...@@ -1054,7 +1054,7 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo)
if (!error) { if (!error) {
GIT_REFCOUNT_OWN(config, repo); 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) { if (config != NULL) {
GIT_REFCOUNT_OWN(config, NULL); GIT_REFCOUNT_OWN(config, NULL);
git_config_free(config); git_config_free(config);
...@@ -1107,7 +1107,7 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo) ...@@ -1107,7 +1107,7 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
GIT_ASSERT_ARG(repo); GIT_ASSERT_ARG(repo);
GIT_ASSERT_ARG(out); GIT_ASSERT_ARG(out);
*out = git__load(repo->_odb); *out = git_atomic_load(repo->_odb);
if (*out == NULL) { if (*out == NULL) {
git_buf odb_path = GIT_BUF_INIT; git_buf odb_path = GIT_BUF_INIT;
git_odb *odb; git_odb *odb;
...@@ -1125,14 +1125,14 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo) ...@@ -1125,14 +1125,14 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
return error; return error;
} }
odb = git__compare_and_swap(&repo->_odb, NULL, odb); odb = git_atomic_compare_and_swap(&repo->_odb, NULL, odb);
if (odb != NULL) { if (odb != NULL) {
GIT_REFCOUNT_OWN(odb, NULL); GIT_REFCOUNT_OWN(odb, NULL);
git_odb_free(odb); git_odb_free(odb);
} }
git_buf_dispose(&odb_path); git_buf_dispose(&odb_path);
*out = git__load(repo->_odb); *out = git_atomic_load(repo->_odb);
} }
return error; return error;
...@@ -1170,7 +1170,7 @@ int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo) ...@@ -1170,7 +1170,7 @@ int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
if (!error) { if (!error) {
GIT_REFCOUNT_OWN(refdb, repo); 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) { if (refdb != NULL) {
GIT_REFCOUNT_OWN(refdb, NULL); GIT_REFCOUNT_OWN(refdb, NULL);
git_refdb_free(refdb); git_refdb_free(refdb);
...@@ -1218,7 +1218,7 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo) ...@@ -1218,7 +1218,7 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo)
if (!error) { if (!error) {
GIT_REFCOUNT_OWN(index, repo); 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) { if (index != NULL) {
GIT_REFCOUNT_OWN(index, NULL); GIT_REFCOUNT_OWN(index, NULL);
git_index_free(index); git_index_free(index);
...@@ -3044,8 +3044,8 @@ int git_repository_set_ident(git_repository *repo, const char *name, const char ...@@ -3044,8 +3044,8 @@ int git_repository_set_ident(git_repository *repo, const char *name, const char
GIT_ERROR_CHECK_ALLOC(tmp_email); GIT_ERROR_CHECK_ALLOC(tmp_email);
} }
tmp_name = git__swap(repo->ident_name, tmp_name); tmp_name = git_atomic_swap(repo->ident_name, tmp_name);
tmp_email = git__swap(repo->ident_email, tmp_email); tmp_email = git_atomic_swap(repo->ident_email, tmp_email);
git__free(tmp_name); git__free(tmp_name);
git__free(tmp_email); git__free(tmp_email);
......
...@@ -37,7 +37,7 @@ static void shutdown_common(void) ...@@ -37,7 +37,7 @@ static void shutdown_common(void)
for (pos = git_atomic32_get(&shutdown_callback_count); for (pos = git_atomic32_get(&shutdown_callback_count);
pos > 0; pos > 0;
pos = git_atomic32_dec(&shutdown_callback_count)) { 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) if (cb != NULL)
cb(); cb();
......
...@@ -139,7 +139,7 @@ GIT_INLINE(int) git_atomic32_get(git_atomic32 *a) ...@@ -139,7 +139,7 @@ GIT_INLINE(int) git_atomic32_get(git_atomic32 *a)
#endif #endif
} }
GIT_INLINE(void *) git___compare_and_swap( GIT_INLINE(void *) git_atomic__compare_and_swap(
void * volatile *ptr, void *oldval, void *newval) void * volatile *ptr, void *oldval, void *newval)
{ {
#if defined(GIT_WIN32) #if defined(GIT_WIN32)
...@@ -158,7 +158,7 @@ GIT_INLINE(void *) git___compare_and_swap( ...@@ -158,7 +158,7 @@ GIT_INLINE(void *) git___compare_and_swap(
#endif #endif
} }
GIT_INLINE(volatile void *) git___swap( GIT_INLINE(volatile void *) git_atomic__swap(
void * volatile *ptr, void *newval) void * volatile *ptr, void *newval)
{ {
#if defined(GIT_WIN32) #if defined(GIT_WIN32)
...@@ -174,7 +174,7 @@ GIT_INLINE(volatile void *) git___swap( ...@@ -174,7 +174,7 @@ GIT_INLINE(volatile void *) git___swap(
#endif #endif
} }
GIT_INLINE(volatile void *) git___load(void * volatile *ptr) GIT_INLINE(volatile void *) git_atomic__load(void * volatile *ptr)
{ {
#if defined(GIT_WIN32) #if defined(GIT_WIN32)
void *newval = NULL, *oldval = NULL; void *newval = NULL, *oldval = NULL;
...@@ -294,7 +294,7 @@ GIT_INLINE(int) git_atomic32_get(git_atomic32 *a) ...@@ -294,7 +294,7 @@ GIT_INLINE(int) git_atomic32_get(git_atomic32 *a)
return (int)a->val; 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) void * volatile *ptr, void *oldval, void *newval)
{ {
if (*ptr == oldval) if (*ptr == oldval)
...@@ -304,7 +304,7 @@ GIT_INLINE(void *) git___compare_and_swap( ...@@ -304,7 +304,7 @@ GIT_INLINE(void *) git___compare_and_swap(
return oldval; return oldval;
} }
GIT_INLINE(volatile void *) git___swap( GIT_INLINE(volatile void *) git_atomic__swap(
void * volatile *ptr, void *newval) void * volatile *ptr, void *newval)
{ {
volatile void *old = *ptr; volatile void *old = *ptr;
...@@ -312,7 +312,7 @@ GIT_INLINE(volatile void *) git___swap( ...@@ -312,7 +312,7 @@ GIT_INLINE(volatile void *) git___swap(
return old; return old;
} }
GIT_INLINE(volatile void *) git___load(void * volatile *ptr) GIT_INLINE(volatile void *) git_atomic__load(void * volatile *ptr)
{ {
return *ptr; return *ptr;
} }
...@@ -342,12 +342,14 @@ GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a) ...@@ -342,12 +342,14 @@ GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
/* Atomically replace oldval with newval /* Atomically replace oldval with newval
* @return oldval if it was replaced or newval if it was not * @return oldval if it was replaced or newval if it was not
*/ */
#define git__compare_and_swap(P,O,N) \ #define git_atomic_compare_and_swap(P,O,N) \
git___compare_and_swap((void * volatile *)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) #if defined(GIT_THREADS)
......
...@@ -186,10 +186,10 @@ typedef void (*git_refcount_freeptr)(void *r); ...@@ -186,10 +186,10 @@ typedef void (*git_refcount_freeptr)(void *r);
} }
#define GIT_REFCOUNT_OWN(r, o) { \ #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) #define GIT_REFCOUNT_VAL(r) git_atomic32_get((r)->rc.refcount)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment