aboutsummaryrefslogtreecommitdiff
path: root/src/lib9/atnotify.c
blob: 60e8ad0c036292abbaeced5b89fbddebe82af736 (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
#include <u.h>
#include <libc.h>

#define	NFN	33
static	int	(*onnot[NFN])(void*, char*);
static	Lock	onnotlock;

static
void
notifier(void *v, char *s)
{
	int i;

	for(i=0; i<NFN; i++)
		if(onnot[i] && ((*onnot[i])(v, s))){
			noted(NCONT);
			return;
		}
	noted(NDFLT);
}

int
atnotify(int (*f)(void*, char*), int in)
{
	int i, n, ret;
	static int init;

	if(!init){
		notify(notifier);
		init = 1;		/* assign = */
	}
	ret = 0;
	lock(&onnotlock);
	if(in){
		for(i=0; i<NFN; i++)
			if(onnot[i] == 0) {
				onnot[i] = f;
				ret = 1;
				break;
			}
	}else{
		n = 0;
		for(i=0; i<NFN; i++)
			if(onnot[i]){
				if(ret==0 && onnot[i]==f){
					onnot[i] = 0;
					ret = 1;
				}else
					n++;
			}
		if(n == 0){
			init = 0;
			notify(0);
		}
	}
	unlock(&onnotlock);
	return ret;
}