aboutsummaryrefslogtreecommitdiff
path: root/src/lib9/rendez.c
blob: cf23a4e5ad63df9024dc09195b27dc7b8c0f56c1 (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
#include <u.h>
#include <libc.h>
#include "9proc.h"

static Lock rendlock;
static Uproc *rendhash[RENDHASH];

ulong
rendezvous(ulong tag, ulong val)
{
	char c;
	ulong ret;
	Uproc *t, *self, **l;

	self = _p9uproc();
	lock(&rendlock);
	l = &rendhash[tag%RENDHASH];
	for(t=*l; t; l=&t->rendhash, t=*l){
		if(t->rendtag==tag){
			*l = t->rendhash;
			ret = t->rendval;
			t->rendval = val;
			t->rendtag++;
			c = 0;
			unlock(&rendlock);
			write(t->pipe[1], &c, 1);
			return ret;
		}
	}

	/* Going to sleep here. */
	t = self;
	t->rendtag = tag;
	t->rendval = val;
	t->rendhash = *l;
	*l = t;
	unlock(&rendlock);
	do
		read(t->pipe[0], &c, 1);
	while(t->rendtag == tag);
	return t->rendval;
}