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 int
36 main(int argc, char **argv)
37 {
38 Mainarg *a;
39 Proc *p;
41 //_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0;
42 _systhreadinit();
43 _qlockinit(_threadrendezvous);
44 _sysfatal = _threadsysfatal;
45 notify(_threadnote);
46 if(mainstacksize == 0)
47 mainstacksize = 32*1024;
49 a = _threadmalloc(sizeof *a, 1);
50 a->argc = argc;
51 a->argv = argv;
53 p = _newproc(mainlauncher, a, mainstacksize, "threadmain", 0, 0);
54 _schedinit(p);
55 abort(); /* not reached */
56 return 0;
57 }
59 static void
60 mainlauncher(void *arg)
61 {
62 Mainarg *a;
64 a = arg;
65 threadmain(a->argc, a->argv);
66 threadexits("threadmain");
67 }
69 void
70 _threadsignal(void)
71 {
72 }
74 void
75 _threadsignalpasser(void)
76 {
77 }
79 int
80 _schedfork(Proc *p)
81 {
82 int pid;
83 lock(&p->lock);
84 pid = ffork(RFMEM|RFNOWAIT, _schedinit, p);
85 p->pid = pid;
86 unlock(&p->lock);
87 return pid;
89 }
91 void
92 _schedexit(Proc *p)
93 {
94 char ex[ERRMAX];
95 Proc **l;
97 lock(&_threadpq.lock);
98 for(l=&_threadpq.head; *l; l=&(*l)->next){
99 if(*l == p){
100 *l = p->next;
101 if(*l == nil)
102 _threadpq.tail = l;
103 break;
106 _threadprocs--;
107 unlock(&_threadpq.lock);
109 strncpy(ex, p->exitstr, sizeof ex);
110 ex[sizeof ex-1] = '\0';
111 free(p);
112 _exit(ex[0]);
115 int
116 nrand(int n)
118 return random()%n;
121 void
122 _systhreadinit(void)
126 void
127 threadstats(void)
129 extern int _threadnrendez, _threadhighnrendez,
130 _threadnalt, _threadhighnentry;
131 fprint(2, "*** THREAD LIBRARY STATS ***\n");
132 fprint(2, "nrendez %d high simultaneous %d\n",
133 _threadnrendez, _threadhighnrendez);
134 fprint(2, "nalt %d high simultaneous entry %d\n",
135 _threadnalt, _threadhighnentry);