plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

thread.c (460B)


      1 #include "a.h"
      2 
      3 typedef struct New New;
      4 struct New
      5 {
      6 	void (*fn)(void*);
      7 	void *arg;
      8 };
      9 
     10 Channel *mailthreadchan;
     11 
     12 void
     13 mailthread(void (*fn)(void*), void *arg)
     14 {
     15 	New n;
     16 
     17 	n.fn = fn;
     18 	n.arg = arg;
     19 	send(mailthreadchan, &n);
     20 }
     21 
     22 void
     23 mailproc(void *v)
     24 {
     25 	New n;
     26 
     27 	USED(v);
     28 	while(recv(mailthreadchan, &n) == 1)
     29 		threadcreate(n.fn, n.arg, STACK);
     30 }
     31 
     32 void
     33 mailthreadinit(void)
     34 {
     35 	mailthreadchan = chancreate(sizeof(New), 0);
     36 	proccreate(mailproc, nil, STACK);
     37 }