xref: /plan9-contrib/sys/src/libc/port/atnotify.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 
4 #define	NFN	33
5 static	int	(*onnot[NFN])(void*, char*);
6 
7 static
8 void
9 notifier(void *v, char *s)
10 {
11 	int i;
12 
13 	for(i=0; i<NFN; i++)
14 		if(onnot[i] && ((*onnot[i])(v, s))){
15 			noted(NCONT);
16 			return;
17 		}
18 	noted(NDFLT);
19 }
20 
21 int
22 atnotify(int (*f)(void*, char*), int in)
23 {
24 	int i, n, ret;
25 	static int init;
26 
27 	if(!init){
28 		notify(notifier);
29 		init = 1;		/* assign = */
30 	}
31 	ret = 0;
32 	if(in){
33 		for(i=0; i<NFN; i++)
34 			if(onnot[i] == 0) {
35 				onnot[i] = f;
36 				ret = 1;
37 				break;
38 			}
39 	}else{
40 		n = 0;
41 		for(i=0; i<NFN; i++)
42 			if(onnot[i]){
43 				if(ret==0 && onnot[i]==f){
44 					onnot[i] = 0;
45 					ret = 1;
46 				}else
47 					n++;
48 			}
49 		if(n == 0){
50 			init = 0;
51 			notify(0);
52 		}
53 	}
54 	return ret;
55 }
56