summaryrefslogtreecommitdiff
path: root/tests/threads/thread_helpers.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2014-04-10 22:31:01 -0700
committerRussell Belfer <rb@github.com>2014-04-17 14:56:41 -0700
commit7d4908724fd7d4d8e096b4faf2c652ba5b77644e (patch)
tree2fcd09e7e040607c124f6e658c31f33e1aa1868a /tests/threads/thread_helpers.c
parent1fa17b5c92cb92a2785fba403b87525169b205c0 (diff)
downloadlibgit2-7d4908724fd7d4d8e096b4faf2c652ba5b77644e.tar.gz
Attribute file cache refactor
This is a big refactoring of the attribute file cache to be a bit simpler which in turn makes it easier to enforce a lock around any updates to the cache so that it can be used in a threaded env. Tons of changes to the attributes and ignores code.
Diffstat (limited to 'tests/threads/thread_helpers.c')
-rw-r--r--tests/threads/thread_helpers.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/threads/thread_helpers.c b/tests/threads/thread_helpers.c
new file mode 100644
index 000000000..25370dddb
--- /dev/null
+++ b/tests/threads/thread_helpers.c
@@ -0,0 +1,44 @@
+#include "clar_libgit2.h"
+#include "thread_helpers.h"
+
+void run_in_parallel(
+ int repeats,
+ int threads,
+ void *(*func)(void *),
+ void (*before_test)(void),
+ void (*after_test)(void))
+{
+ int r, t, *id = git__calloc(threads, sizeof(int));
+#ifdef GIT_THREADS
+ git_thread *th = git__calloc(threads, sizeof(git_thread));
+ cl_assert(th != NULL);
+#else
+ void *th = NULL;
+#endif
+
+ cl_assert(id != NULL);
+
+ for (r = 0; r < repeats; ++r) {
+ if (before_test) before_test();
+
+ for (t = 0; t < threads; ++t) {
+ id[t] = t;
+#ifdef GIT_THREADS
+ cl_git_pass(git_thread_create(&th[t], NULL, func, &id[t]));
+#else
+ cl_assert(func(&id[t]) == &id[t]);
+#endif
+ }
+
+#ifdef GIT_THREADS
+ for (t = 0; t < threads; ++t)
+ cl_git_pass(git_thread_join(th[t], NULL));
+ memset(th, 0, threads * sizeof(git_thread));
+#endif
+
+ if (after_test) after_test();
+ }
+
+ git__free(id);
+ git__free(th);
+}