Blob


1 #include <u.h>
2 #include <signal.h>
3 #include "threadimpl.h"
5 typedef struct Mainarg Mainarg;
6 struct Mainarg
7 {
8 int argc;
9 char **argv;
10 };
12 int mainstacksize;
13 int _threadnotefd;
14 int _threadpasserpid;
15 static void mainlauncher(void*);
16 extern void (*_sysfatal)(char*, va_list);
18 void
19 _threadstatus(int x)
20 {
21 USED(x);
22 threadstatus();
23 }
25 void
26 _threaddie(int x)
27 {
28 extern char *_threadexitsallstatus;
29 USED(x);
31 if(_threadexitsallstatus)
32 exit(_threadexitsallstatus[0] ? 1 : 0);
33 }
35 static void
36 _nop(int x)
37 {
38 USED(x);
39 }
41 int
42 main(int argc, char **argv)
43 {
44 Mainarg *a;
45 Proc *p;
47 signal(SIGTERM, _threaddie);
48 signal(SIGCHLD, _nop);
49 signal(SIGALRM, _nop);
50 // signal(SIGINFO, _threadstatus);
51 // rfork(RFREND);
53 //_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0;
54 _systhreadinit();
55 _qlockinit(_threadrendezvous);
56 _sysfatal = _threadsysfatal;
57 notify(_threadnote);
58 if(mainstacksize == 0)
59 mainstacksize = 32*1024;
61 a = _threadmalloc(sizeof *a, 1);
62 a->argc = argc;
63 a->argv = argv;
65 p = _newproc(mainlauncher, a, mainstacksize, "threadmain", 0, 0);
66 _schedinit(p);
67 abort(); /* not reached */
68 return 0;
69 }
71 static void
72 mainlauncher(void *arg)
73 {
74 Mainarg *a;
76 a = arg;
77 threadmain(a->argc, a->argv);
78 threadexits("threadmain");
79 }
81 void
82 _threadsignal(void)
83 {
84 }
86 void
87 _threadsignalpasser(void)
88 {
89 }
91 int
92 _schedfork(Proc *p)
93 {
94 int pid;
95 lock(&p->lock);
96 pid = ffork(RFMEM|RFNOWAIT, _schedinit, p);
97 p->pid = pid;
98 unlock(&p->lock);
99 return pid;
103 void
104 _schedexit(Proc *p)
106 char ex[ERRMAX];
107 Proc **l;
109 lock(&_threadpq.lock);
110 for(l=&_threadpq.head; *l; l=&(*l)->next){
111 if(*l == p){
112 *l = p->next;
113 if(*l == nil)
114 _threadpq.tail = l;
115 break;
118 _threadprocs--;
119 unlock(&_threadpq.lock);
121 strncpy(ex, p->exitstr, sizeof ex);
122 ex[sizeof ex-1] = '\0';
123 free(p);
124 _exit(ex[0]);
127 int
128 nrand(int n)
130 return random()%n;
133 void
134 _systhreadinit(void)
138 void
139 threadstats(void)
141 extern int _threadnrendez, _threadhighnrendez,
142 _threadnalt, _threadhighnentry;
143 fprint(2, "*** THREAD LIBRARY STATS ***\n");
144 fprint(2, "nrendez %d high simultaneous %d\n",
145 _threadnrendez, _threadhighnrendez);
146 fprint(2, "nalt %d high simultaneous entry %d\n",
147 _threadnalt, _threadhighnentry);