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 _exits(_threadexitsallstatus);
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(_threadsleep, _threadwakeup);
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;
52 p = _newproc(mainlauncher, a, mainstacksize, "threadmain", 0, 0);
53 _scheduler(p);
54 abort(); /* not reached */
55 return 0;
56 }
58 static void
59 mainlauncher(void *arg)
60 {
61 Mainarg *a;
63 a = arg;
64 threadmain(a->argc, a->argv);
65 threadexits("threadmain");
66 }
68 void
69 _threadsignal(void)
70 {
71 }
73 void
74 _threadsignalpasser(void)
75 {
76 }
78 int
79 _schedfork(Proc *p)
80 {
81 int pid;
82 lock(&p->lock);
83 pid = ffork(RFMEM|RFNOWAIT, _scheduler, p);
84 p->pid = pid;
85 unlock(&p->lock);
86 return pid;
88 }
90 void
91 _schedexit(Proc *p)
92 {
93 char ex[ERRMAX];
94 Proc **l;
96 lock(&_threadpq.lock);
97 for(l=&_threadpq.head; *l; l=&(*l)->next){
98 if(*l == p){
99 *l = p->next;
100 if(*l == nil)
101 _threadpq.tail = l;
102 break;
105 _threadprocs--;
106 unlock(&_threadpq.lock);
108 strncpy(ex, p->exitstr, sizeof ex);
109 ex[sizeof ex-1] = '\0';
110 free(p);
111 _exits(ex);
114 int
115 nrand(int n)
117 return random()%n;
120 void
121 _systhreadinit(void)
125 void
126 threadstats(void)
128 extern int _threadnrendez, _threadhighnrendez,
129 _threadnalt, _threadhighnentry;
130 fprint(2, "*** THREAD LIBRARY STATS ***\n");
131 fprint(2, "nrendez %d high simultaneous %d\n",
132 _threadnrendez, _threadhighnrendez);
133 fprint(2, "nalt %d high simultaneous entry %d\n",
134 _threadnalt, _threadhighnentry);