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