summaryrefslogtreecommitdiffstatsci
path: root/tests/multithreading/multithreaded.c
blob: 2b2afc9195097dd5b51f4bb4d04fea999cdfd5ea (plain)
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
#include "test.h"
#include "tinycthread.h"

#define THREAD_COUNT 8


static int finishedThreadCount = 0;
static mtx_t mutex;

void drawRandomRect (VkvgContext ctx, float s) {
	float w = (float)test_width;
	float h = (float)test_height;
	randomize_color(ctx);

	float x = truncf(w*rndf());
	float y = truncf(h*rndf());

	vkvg_rectangle(ctx, x, y, s, s);
}
void _before_submit (void* data) {
	mtx_lock((mtx_t*)data);
	printf("sumbit  0x%lx\n", thrd_current());
}
void _after_submit (void* data) {
	printf("release 0x%lx\n", thrd_current());
	mtx_unlock((mtx_t*)data);
}

int drawRectsThread () {
	VkvgSurface s = vkvg_surface_create(device, test_width, test_height);
	VkvgContext ctx = vkvg_create(s);
	for (uint32_t i=0; i<test_size; i++) {
		drawRandomRect(ctx, 14.0f);
		vkvg_fill (ctx);
	}
	vkvg_destroy(ctx);

	mtx_lock(&mutex);

	finishedThreadCount++;

	ctx = vkvg_create(surf);
		vkvg_set_source_surface (ctx, s, 0, 0);
		vkvg_paint(ctx);
	vkvg_destroy(ctx);

	mtx_unlock(&mutex);

	vkvg_surface_destroy (s);
	return 0;
}
void fixedSizeRects(){
	printf("==>main 0x%lx\n", thrd_current());
	mtx_t gQMutex;
	mtx_init (&gQMutex, mtx_plain);
	vkvg_device_set_queue_guards (device, _before_submit, _after_submit, &gQMutex);
	thrd_t threads[THREAD_COUNT];

	finishedThreadCount = 0;
	mtx_init (&mutex,mtx_plain);
	for (uint32_t i=0; i<THREAD_COUNT; i++) {
		thrd_create (&threads[i], drawRectsThread, NULL);
	}

	const struct timespec ts = {1,0};
	while (finishedThreadCount < THREAD_COUNT)
		thrd_sleep(&ts, NULL);

	mtx_destroy(&mutex);

	vkvg_device_set_queue_guards (device, NULL, NULL, NULL);
	mtx_destroy (&gQMutex);
	printf("------- 0x%lx\n", thrd_current());
}

int main(int argc, char *argv[]) {
	PERFORM_TEST (fixedSizeRects, argc, argv);
	return 0;
}