summaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-04-15 00:09:03 -0700
committerVicent Marti <tanoku@gmail.com>2013-04-22 16:52:07 +0200
commit536078688549ac3d50483eecdec5a8169d921927 (patch)
tree3ceb9965aba3f946b89bbbd99a8071707351d50f /src/util.h
parent116bbdf0446cd5335b73e691c3352f368eac9b8f (diff)
downloadlibgit2-536078688549ac3d50483eecdec5a8169d921927.tar.gz
Further threading fixes
This builds on the earlier thread safety work to make it so that setting the odb, index, refdb, or config for a repository is done in a threadsafe manner with minimized locking time. This is done by adding a lock to the repository object and using it to guard the assignment of the above listed pointers. The lock is only held to assign the pointer value. This also contains some minor fixes to the other work with pack files to reduce the time that locks are being held to and fix an apparently memory leak.
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/util.h b/src/util.h
index af3ef0b46..a2233a7e8 100644
--- a/src/util.h
+++ b/src/util.h
@@ -306,11 +306,30 @@ int git__date_parse(git_time_t *out, const char *date);
/*
* Unescapes a string in-place.
- *
+ *
* Edge cases behavior:
* - "jackie\" -> "jacky\"
* - "chan\\" -> "chan\"
*/
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__ */