Blob
1 #include "threadimpl.h"3 int _threadnopasser;5 #ifdef NOTDEF6 #define NFN 337 #define ERRLEN 488 typedef struct Note Note;9 struct Note10 {11 Lock inuse;12 Proc *proc; /* recipient */13 char s[ERRMAX]; /* arg2 */14 };16 static Note notes[128];17 static Note *enotes = notes+nelem(notes);18 static int (*onnote[NFN])(void*, char*);19 static int onnotepid[NFN];20 static Lock onnotelock;22 int23 threadnotify(int (*f)(void*, char*), int in)24 {25 int i, topid;26 int (*from)(void*, char*), (*to)(void*, char*);28 if(in){29 from = nil;30 to = f;31 topid = _threadgetproc()->pid;32 }else{33 from = f;34 to = nil;35 topid = 0;36 }37 lock(&onnotelock);38 for(i=0; i<NFN; i++)39 if(onnote[i]==from){40 onnote[i] = to;41 onnotepid[i] = topid;42 break;43 }44 unlock(&onnotelock);45 return i<NFN;46 }48 static void49 delayednotes(Proc *p, void *v)50 {51 int i;52 Note *n;53 int (*fn)(void*, char*);55 if(!p->pending)56 return;58 p->pending = 0;59 for(n=notes; n<enotes; n++){60 if(n->proc == p){61 for(i=0; i<NFN; i++){62 if(onnotepid[i]!=p->pid || (fn = onnote[i])==nil)63 continue;64 if((*fn)(v, n->s))65 break;66 }67 if(i==NFN){68 _threaddebug(DBGNOTE, "Unhandled note %s, proc %p\n", n->s, p);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 void82 _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)89 noted(NDFLT);91 // if(_threadexitsallstatus){92 // _threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'\n", _threadexitsallstatus);93 // _exits(_threadexitsallstatus);94 // }96 if(strcmp(s, "threadint")==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);114 }115 #endif117 int118 _procsplhi(void)119 {120 int s;121 Proc *p;123 p = _threadgetproc();124 s = p->splhi;125 p->splhi = 1;126 return s;127 }129 void130 _procsplx(int s)131 {132 Proc *p;134 p = _threadgetproc();135 p->splhi = s;136 if(s)137 return;138 /*139 if(p->pending)140 delayednotes(p, nil);141 */142 }