main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <thread.h>
  12. #include "threadimpl.h"
  13. typedef struct Mainarg Mainarg;
  14. struct Mainarg
  15. {
  16. int argc;
  17. char **argv;
  18. };
  19. int mainstacksize;
  20. int _threadnotefd;
  21. int _threadpasserpid;
  22. static jmp_buf _mainjmp;
  23. static void mainlauncher(void*);
  24. extern void (*_sysfatal)(char*, ...);
  25. extern void (*__assert)(char*);
  26. extern int (*_dial)(char*, char*, char*, int*);
  27. extern int _threaddial(char*, char*, char*, int*);
  28. static Proc **mainp;
  29. void
  30. main(int argc, char **argv)
  31. {
  32. Mainarg *a;
  33. Proc *p;
  34. rfork(RFREND);
  35. mainp = &p;
  36. if(setjmp(_mainjmp))
  37. _schedinit(p);
  38. //_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0;
  39. _systhreadinit();
  40. _qlockinit(_threadrendezvous);
  41. _sysfatal = _threadsysfatal;
  42. _dial = _threaddial;
  43. __assert = _threadassert;
  44. notify(_threadnote);
  45. if(mainstacksize == 0)
  46. mainstacksize = 8*1024;
  47. a = _threadmalloc(sizeof *a, 1);
  48. a->argc = argc;
  49. a->argv = argv;
  50. p = _newproc(mainlauncher, a, mainstacksize, "threadmain", 0, 0);
  51. _schedinit(p);
  52. abort(); /* not reached */
  53. }
  54. static void
  55. mainlauncher(void *arg)
  56. {
  57. Mainarg *a;
  58. a = arg;
  59. threadmain(a->argc, a->argv);
  60. threadexits("threadmain");
  61. }
  62. static void
  63. efork(Execargs *e)
  64. {
  65. char buf[ERRMAX];
  66. _threaddebug(DBGEXEC, "_schedexec %s", e->prog);
  67. close(e->fd[0]);
  68. exec(e->prog, e->args);
  69. _threaddebug(DBGEXEC, "_schedexec failed: %r");
  70. rerrstr(buf, sizeof buf);
  71. if(buf[0]=='\0')
  72. strcpy(buf, "exec failed");
  73. write(e->fd[1], buf, strlen(buf));
  74. close(e->fd[1]);
  75. _exits(buf);
  76. }
  77. int
  78. _schedexec(Execargs *e)
  79. {
  80. int pid;
  81. switch(pid = rfork(RFREND|RFNOTEG|RFFDG|RFMEM|RFPROC)){
  82. case 0:
  83. efork(e);
  84. default:
  85. return pid;
  86. }
  87. }
  88. int
  89. _schedfork(Proc *p)
  90. {
  91. int pid;
  92. switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT|p->rforkflag)){
  93. case 0:
  94. *mainp = p; /* write to stack, so local to proc */
  95. longjmp(_mainjmp, 1);
  96. default:
  97. return pid;
  98. }
  99. }
  100. void
  101. _schedexit(Proc *p)
  102. {
  103. char ex[ERRMAX];
  104. Proc **l;
  105. lock(&_threadpq.lock);
  106. for(l=&_threadpq.head; *l; l=&(*l)->next){
  107. if(*l == p){
  108. *l = p->next;
  109. if(*l == nil)
  110. _threadpq.tail = l;
  111. break;
  112. }
  113. }
  114. unlock(&_threadpq.lock);
  115. utfecpy(ex, ex+sizeof ex, p->exitstr);
  116. free(p);
  117. _exits(ex);
  118. }
  119. void
  120. _schedexecwait(void)
  121. {
  122. int pid;
  123. Channel *c;
  124. Proc *p;
  125. Thread *t;
  126. Waitmsg *w;
  127. p = _threadgetproc();
  128. t = p->thread;
  129. pid = t->ret;
  130. _threaddebug(DBGEXEC, "_schedexecwait %d", t->ret);
  131. rfork(RFCFDG);
  132. for(;;){
  133. w = wait();
  134. if(w == nil)
  135. break;
  136. if(w->pid == pid)
  137. break;
  138. free(w);
  139. }
  140. if(w != nil){
  141. if((c = _threadwaitchan) != nil)
  142. sendp(c, w);
  143. else
  144. free(w);
  145. }
  146. threadexits("procexec");
  147. }
  148. static Proc **procp;
  149. void
  150. _systhreadinit(void)
  151. {
  152. procp = (Proc **)privalloc();
  153. }
  154. Proc*
  155. _threadgetproc(void)
  156. {
  157. return *procp;
  158. }
  159. void
  160. _threadsetproc(Proc *p)
  161. {
  162. *procp = p;
  163. }