Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_posix/sys/src/libc/port/lock.c – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include <u.h>
2
#include <libc.h>
3
 
4
void
5
lock(Lock *l)
6
{
7
	if(ainc(&l->key) == 1)
8
		return;	/* changed from 0 -> 1: we hold lock */
9
	/* otherwise wait in kernel */
10
	while(semacquire(&l->sem, 1) < 0){
11
		/* interrupted; try again */
12
	}
13
}
14
 
15
void
16
unlock(Lock *l)
17
{
18
	if(adec(&l->key) == 0)
19
		return;	/* changed from 1 -> 0: no contention */
20
	semrelease(&l->sem, 1);
21
}
22
 
23
int
24
canlock(Lock *l)
25
{
26
	if(ainc(&l->key) == 1)
27
		return 1;	/* changed from 0 -> 1: success */
28
	/* Undo increment (but don't miss wakeup) */
29
	if(adec(&l->key) == 0)
30
		return 0;	/* changed from 1 -> 0: no contention */
31
	semrelease(&l->sem, 1);
32
	return 0;
33
}