aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/devdraw/cocoa-thread.c
blob: 4b2443cebee3a3a214ba3a99ca3935817bfcb11f (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
#include <u.h>
#include <libc.h>
#include "cocoa-thread.h"

static pthread_mutex_t initlock = PTHREAD_MUTEX_INITIALIZER;

void
qlock(QLock *q)
{
	if(q->init == 0){
		pthread_mutex_lock(&initlock);
		if(q->init == 0){
			pthread_mutex_init(&q->m, nil);
			q->init = 1;
		}
		pthread_mutex_unlock(&initlock);
	}
	pthread_mutex_lock(&q->m);
}

void
qunlock(QLock *q)
{
	pthread_mutex_unlock(&q->m);
}

static void
rinit(Rendez *r)
{
	pthread_mutex_lock(&initlock);
	if(r->init == 0){
		pthread_cond_init(&r->c, nil);
		r->init = 1;
	}
	pthread_mutex_unlock(&initlock);
}

void
rsleep(Rendez *r)
{
	if(r->init == 0)
		rinit(r);
	pthread_cond_wait(&r->c, &r->l->m);
}

int
rwakeup(Rendez *r)
{
	if(r->init == 0)
		rinit(r);
	pthread_cond_signal(&r->c);

	return 0;
}