File: thread_helpers.c

package info (click to toggle)
monodevelop 5.10.0.871-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 272,040 kB
  • ctags: 258,152
  • sloc: cs: 1,439,098; xml: 947,206; ansic: 148,611; java: 64,114; makefile: 4,256; lisp: 3,174; python: 2,072; sh: 2,058; objc: 302; sql: 111; php: 65
file content (44 lines) | stat: -rw-r--r-- 875 bytes parent folder | download | duplicates (3)
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
#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);
}