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 fprint(2, "unhandled note %s, pid %d\n", n->s, p->pid);
69 if(v != nil)
70 noted(NDFLT);
71 else if(strncmp(n->s, "sys:", 4)==0)
72 abort();
73 threadexitsall(n->s);
74 }
75 n->proc = nil;
76 unlock(&n->inuse);
77 }
78 }
79 }
81 void
82 _threadnote(void *v, char *s)
83 {
84 Proc *p;
85 Note *n;
87 _threaddebug(DBGNOTE, "Got note %s", s);
88 if(strncmp(s, "sys:", 4) == 0 && strcmp(s, "sys: write on closed pipe") != 0)
89 noted(NDFLT);
91 // if(_threadexitsallstatus){
92 // _threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'\n", _threadexitsallstatus);
93 // _exits(_threadexitsallstatus);
94 // }
96 if(strcmp(s, "threadint")==0 || strcmp(s, "interrupt")==0)
97 noted(NCONT);
99 p = _threadgetproc();
100 if(p == nil)
101 noted(NDFLT);
103 for(n=notes; n<enotes; n++)
104 if(canlock(&n->inuse))
105 break;
106 if(n==enotes)
107 sysfatal("libthread: too many delayed notes");
108 utfecpy(n->s, n->s+ERRMAX, s);
109 n->proc = p;
110 p->pending = 1;
111 if(!p->splhi)
112 delayednotes(p, v);
113 noted(NCONT);
116 int
117 _procsplhi(void)
119 int s;
120 Proc *p;
122 p = _threadgetproc();
123 s = p->splhi;
124 p->splhi = 1;
125 return s;
128 void
129 _procsplx(int s)
131 Proc *p;
133 p = _threadgetproc();
134 p->splhi = s;
135 if(s)
136 return;
137 /*
138 if(p->pending)
139 delayednotes(p, nil);
140 */