Blob


1 #include "threadimpl.h"
3 int _threadnopasser;
5 #define NFN 33
6 #define ERRLEN 48
7 typedef struct Note Note;
8 struct Note
9 {
10 Lock inuse;
11 Proc *proc; /* recipient */
12 char s[ERRMAX]; /* arg2 */
13 };
15 static Note notes[128];
16 static Note *enotes = notes+nelem(notes);
17 static int (*onnote[NFN])(void*, char*);
18 static int onnotepid[NFN];
19 static Lock onnotelock;
21 int
22 threadnotify(int (*f)(void*, char*), int in)
23 {
24 int i, topid;
25 int (*from)(void*, char*), (*to)(void*, char*);
27 if(in){
28 from = 0;
29 to = f;
30 topid = _threadgetproc()->pid;
31 }else{
32 from = f;
33 to = 0;
34 topid = 0;
35 }
36 lock(&onnotelock);
37 for(i=0; i<NFN; i++)
38 if(onnote[i]==from){
39 onnote[i] = to;
40 onnotepid[i] = topid;
41 break;
42 }
43 unlock(&onnotelock);
44 return i<NFN;
45 }
47 static void
48 delayednotes(Proc *p, void *v)
49 {
50 int i;
51 Note *n;
52 int (*fn)(void*, char*);
54 if(!p->pending)
55 return;
57 p->pending = 0;
58 for(n=notes; n<enotes; n++){
59 if(n->proc == p){
60 for(i=0; i<NFN; i++){
61 if(onnotepid[i]!=p->pid || (fn = onnote[i])==nil)
62 continue;
63 if((*fn)(v, n->s))
64 break;
65 }
66 if(i==NFN){
67 _threaddebug(DBGNOTE, "Unhandled note %s, proc %p\n", n->s, p);
68 if(v != nil)
69 noted(NDFLT);
70 else if(strncmp(n->s, "sys:", 4)==0)
71 abort();
72 threadexitsall(n->s);
73 }
74 n->proc = nil;
75 unlock(&n->inuse);
76 }
77 }
78 }
80 void
81 _threadnote(void *v, char *s)
82 {
83 Proc *p;
84 Note *n;
86 _threaddebug(DBGNOTE, "Got note %s", s);
87 if(strncmp(s, "sys:", 4) == 0 && strcmp(s, "sys: write on closed pipe") != 0)
88 noted(NDFLT);
90 // if(_threadexitsallstatus){
91 // _threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'\n", _threadexitsallstatus);
92 // _exits(_threadexitsallstatus);
93 // }
95 if(strcmp(s, "threadint")==0 || strcmp(s, "interrupt")==0)
96 noted(NCONT);
98 p = _threadgetproc();
99 if(p == nil)
100 noted(NDFLT);
102 for(n=notes; n<enotes; n++)
103 if(canlock(&n->inuse))
104 break;
105 if(n==enotes)
106 sysfatal("libthread: too many delayed notes");
107 utfecpy(n->s, n->s+ERRMAX, s);
108 n->proc = p;
109 p->pending = 1;
110 if(!p->splhi)
111 delayednotes(p, v);
112 noted(NCONT);
115 int
116 _procsplhi(void)
118 int s;
119 Proc *p;
121 p = _threadgetproc();
122 s = p->splhi;
123 p->splhi = 1;
124 return s;
127 void
128 _procsplx(int s)
130 Proc *p;
132 p = _threadgetproc();
133 p->splhi = s;
134 if(s)
135 return;
136 /*
137 if(p->pending)
138 delayednotes(p, nil);
139 */