Add git__compare_and_swap and use it This removes the lock from the repository object and changes the internals to use the new atomic git__compare_and_swap to update the _odb, _config, _index, and _refdb variables in a threadsafe manner.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
diff --git a/src/global.h b/src/global.h
index f0ad1df..badbc08 100644
--- a/src/global.h
+++ b/src/global.h
@@ -10,14 +10,6 @@
#include "mwindow.h"
#include "hash.h"
-#if defined(GIT_THREADS) && defined(_MSC_VER)
-# define GIT_MEMORY_BARRIER MemoryBarrier()
-#elif defined(GIT_THREADS)
-# define GIT_MEMORY_BARRIER __sync_synchronize()
-#else
-# define GIT_MEMORY_BARRIER /* noop */
-#endif
-
typedef struct {
git_error *last_error;
git_error error_t;
diff --git a/src/repository.c b/src/repository.c
index 8744b91..59479dc 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -32,43 +32,57 @@
#define GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
-#define repo_swap_ptr(repo,item_ptr,new_value) \
- git__swap(&repo->lock, (void **)item_ptr, new_value)
-
-static void drop_odb(git_repository *repo)
+static void set_odb(git_repository *repo, git_odb *odb)
{
- git_odb *dropme = repo_swap_ptr(repo, &repo->_odb, NULL);
- if (dropme != NULL) {
- GIT_REFCOUNT_OWN(dropme, NULL);
- git_odb_free(dropme);
+ if (odb) {
+ GIT_REFCOUNT_OWN(odb, repo);
+ GIT_REFCOUNT_INC(odb);
+ }
+
+ if ((odb = git__swap(repo->_odb, odb)) != NULL) {
+ GIT_REFCOUNT_OWN(odb, NULL);
+ git_odb_free(odb);
}
}
-static void drop_refdb(git_repository *repo)
+static void set_refdb(git_repository *repo, git_refdb *refdb)
{
- git_refdb *dropme = repo_swap_ptr(repo, &repo->_refdb, NULL);
- if (dropme != NULL) {
- GIT_REFCOUNT_OWN(dropme, NULL);
- git_refdb_free(dropme);
+ if (refdb) {
+ GIT_REFCOUNT_OWN(refdb, repo);
+ GIT_REFCOUNT_INC(refdb);
+ }
+
+ if ((refdb = git__swap(repo->_refdb, refdb)) != NULL) {
+ GIT_REFCOUNT_OWN(refdb, NULL);
+ git_refdb_free(refdb);
}
}
-static void drop_config(git_repository *repo)
+static void set_config(git_repository *repo, git_config *config)
{
- git_config *dropme = repo_swap_ptr(repo, &repo->_config, NULL);
- if (dropme != NULL) {
- GIT_REFCOUNT_OWN(dropme, NULL);
- git_config_free(dropme);
- git_repository__cvar_cache_clear(repo);
+ if (config) {
+ GIT_REFCOUNT_OWN(config, repo);
+ GIT_REFCOUNT_INC(config);
+ }
+
+ if ((config = git__swap(repo->_config, config)) != NULL) {
+ GIT_REFCOUNT_OWN(config, NULL);
+ git_config_free(config);
}
+
+ git_repository__cvar_cache_clear(repo);
}
-static void drop_index(git_repository *repo)
+static void set_index(git_repository *repo, git_index *index)
{
- git_index *dropme = repo_swap_ptr(repo, &repo->_index, NULL);
- if (dropme != NULL) {
- GIT_REFCOUNT_OWN(dropme, NULL);
- git_index_free(dropme);
+ if (index) {
+ GIT_REFCOUNT_OWN(index, repo);
+ GIT_REFCOUNT_INC(index);
+ }
+
+ if ((index = git__swap(repo->_index, index)) != NULL) {
+ GIT_REFCOUNT_OWN(index, NULL);
+ git_index_free(index);
}
}
@@ -81,15 +95,14 @@ void git_repository_free(git_repository *repo)
git_attr_cache_flush(repo);
git_submodule_config_free(repo);
- drop_config(repo);
- drop_index(repo);
- drop_odb(repo);
- drop_refdb(repo);
+ set_config(repo, NULL);
+ set_index(repo, NULL);
+ set_odb(repo, NULL);
+ set_refdb(repo, NULL);
git__free(repo->path_repository);
git__free(repo->workdir);
- git_mutex_free(&repo->lock);
git__free(repo);
}
@@ -122,8 +135,6 @@ static git_repository *repository_alloc(void)
memset(repo, 0x0, sizeof(git_repository));
- git_mutex_init(&repo->lock);
-
if (git_cache_init(&repo->objects) < 0) {
git__free(repo);
return NULL;
@@ -581,7 +592,7 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo)
if (!error) {
GIT_REFCOUNT_OWN(config, repo);
- config = repo_swap_ptr(repo, &repo->_config, config);
+ config = git__compare_and_swap(&repo->_config, NULL, config);
if (config != NULL) {
GIT_REFCOUNT_OWN(config, NULL);
git_config_free(config);
@@ -609,17 +620,7 @@ int git_repository_config(git_config **out, git_repository *repo)
void git_repository_set_config(git_repository *repo, git_config *config)
{
assert(repo && config);
-
- GIT_REFCOUNT_OWN(config, repo);
- GIT_REFCOUNT_INC(config);
-
- config = repo_swap_ptr(repo, &repo->_config, config);
- if (config != NULL) {
- GIT_REFCOUNT_OWN(config, NULL);
- git_config_free(config);
- }
-
- git_repository__cvar_cache_clear(repo);
+ set_config(repo, config);
}
int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
@@ -638,7 +639,7 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
if (!error) {
GIT_REFCOUNT_OWN(odb, repo);
- odb = repo_swap_ptr(repo, &repo->_odb, odb);
+ odb = git__compare_and_swap(&repo->_odb, NULL, odb);
if (odb != NULL) {
GIT_REFCOUNT_OWN(odb, NULL);
git_odb_free(odb);
@@ -664,15 +665,7 @@ int git_repository_odb(git_odb **out, git_repository *repo)
void git_repository_set_odb(git_repository *repo, git_odb *odb)
{
assert(repo && odb);
-
- GIT_REFCOUNT_OWN(odb, repo);
- GIT_REFCOUNT_INC(odb);
-
- odb = repo_swap_ptr(repo, &repo->_odb, odb);
- if (odb != NULL) {
- GIT_REFCOUNT_OWN(odb, NULL);
- git_odb_free(odb);
- }
+ set_odb(repo, odb);
}
int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
@@ -688,7 +681,7 @@ int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
if (!error) {
GIT_REFCOUNT_OWN(refdb, repo);
- refdb = repo_swap_ptr(repo, &repo->_refdb, refdb);
+ refdb = git__compare_and_swap(&repo->_refdb, NULL, refdb);
if (refdb != NULL) {
GIT_REFCOUNT_OWN(refdb, NULL);
git_refdb_free(refdb);
@@ -712,15 +705,7 @@ int git_repository_refdb(git_refdb **out, git_repository *repo)
void git_repository_set_refdb(git_repository *repo, git_refdb *refdb)
{
assert(repo && refdb);
-
- GIT_REFCOUNT_OWN(refdb, repo);
- GIT_REFCOUNT_INC(refdb);
-
- refdb = repo_swap_ptr(repo, &repo->_refdb, refdb);
- if (refdb != NULL) {
- GIT_REFCOUNT_OWN(refdb, NULL);
- git_refdb_free(refdb);
- }
+ set_refdb(repo, refdb);
}
int git_repository_index__weakptr(git_index **out, git_repository *repo)
@@ -739,7 +724,7 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo)
if (!error) {
GIT_REFCOUNT_OWN(index, repo);
- index = repo_swap_ptr(repo, &repo->_index, index);
+ index = git__compare_and_swap(&repo->_index, NULL, index);
if (index != NULL) {
GIT_REFCOUNT_OWN(index, NULL);
git_index_free(index);
@@ -767,15 +752,7 @@ int git_repository_index(git_index **out, git_repository *repo)
void git_repository_set_index(git_repository *repo, git_index *index)
{
assert(repo && index);
-
- GIT_REFCOUNT_OWN(index, repo);
- GIT_REFCOUNT_INC(index);
-
- index = repo_swap_ptr(repo, &repo->_index, index);
- if (index != NULL) {
- GIT_REFCOUNT_OWN(index, NULL);
- git_index_free(index);
- }
+ set_index(repo, index);
}
static int check_repositoryformatversion(git_config *config)
@@ -1465,14 +1442,13 @@ static int at_least_one_cb(const char *refname, void *payload)
static int repo_contains_no_reference(git_repository *repo)
{
- int error;
-
- error = git_reference_foreach(repo, GIT_REF_LISTALL, at_least_one_cb, NULL);
+ int error = git_reference_foreach(repo, GIT_REF_LISTALL, at_least_one_cb, NULL);
if (error == GIT_EUSER)
return 0;
-
- return error == 0 ? 1 : error;
+ if (!error)
+ return 1;
+ return error;
}
int git_repository_is_empty(git_repository *repo)
diff --git a/src/repository.h b/src/repository.h
index 873498d..cc2f8c2 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -83,7 +83,6 @@ struct git_repository {
git_refdb *_refdb;
git_config *_config;
git_index *_index;
- git_mutex lock;
git_cache objects;
git_attr_cache attrcache;
diff --git a/src/thread-utils.h b/src/thread-utils.h
index 2ca290a..7b66318 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -68,6 +68,21 @@ GIT_INLINE(int) git_atomic_dec(git_atomic *a)
#endif
}
+GIT_INLINE(void *) git___compare_and_swap(
+ volatile void **ptr, void *oldval, void *newval)
+{
+ bool swapped;
+#if defined(GIT_WIN32)
+ swapped = ((LONGLONG)oldval == InterlockedCompareExchange64(
+ (LONGLONG volatile *)ptr, (LONGLONG)newval, (LONGLONG)oldval));
+#elif defined(__GNUC__)
+ swapped = (__sync_val_compare_and_swap(ptr, oldval, newval) == oldval);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+ return swapped ? oldval : newval;
+}
+
#else
#define git_thread unsigned int
@@ -101,8 +116,34 @@ GIT_INLINE(int) git_atomic_dec(git_atomic *a)
return --a->val;
}
+GIT_INLINE(void *) git___compare_and_swap(
+ volatile void **ptr, void *oldval, void *newval)
+{
+ if (*ptr == oldval)
+ *ptr = newval;
+ else
+ oldval = newval;
+ return oldval;
+}
+
#endif
+/* 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((volatile void **)P, O, N)
+
+#define git__swap(ptr, val) git__compare_and_swap(&ptr, ptr, val)
+
extern int git_online_cpus(void);
+#if defined(GIT_THREADS) && defined(GIT_WIN32)
+# define GIT_MEMORY_BARRIER MemoryBarrier()
+#elif defined(GIT_THREADS)
+# define GIT_MEMORY_BARRIER __sync_synchronize()
+#else
+# define GIT_MEMORY_BARRIER /* noop */
+#endif
+
#endif /* INCLUDE_thread_utils_h__ */
diff --git a/src/util.h b/src/util.h
index a2233a7..82435ae 100644
--- a/src/util.h
+++ b/src/util.h
@@ -313,23 +313,4 @@ int git__date_parse(git_time_t *out, const char *date);
*/
extern size_t git__unescape(char *str);
-/*
- * Swap a pointer with thread safety, returning old value.
- */
-GIT_INLINE(void *) git__swap(git_mutex *lock, void **ptr_ptr, void *new_ptr)
-{
- void *old_ptr;
-
- if (*ptr_ptr == new_ptr)
- return NULL;
- if (git_mutex_lock(lock) < 0)
- return new_ptr;
-
- old_ptr = *ptr_ptr;
- *ptr_ptr = new_ptr;
-
- git_mutex_unlock(lock);
- return old_ptr;
-}
-
#endif /* INCLUDE_util_h__ */