exit.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <thread.h>
  4. #include "threadimpl.h"
  5. char *_threadexitsallstatus;
  6. Channel *_threadwaitchan;
  7. void
  8. threadexits(char *exitstr)
  9. {
  10. Proc *p;
  11. Thread *t;
  12. p = _threadgetproc();
  13. t = p->thread;
  14. t->moribund = 1;
  15. if(exitstr==nil)
  16. exitstr="";
  17. utfecpy(p->exitstr, p->exitstr+ERRMAX, exitstr);
  18. _sched();
  19. }
  20. void
  21. threadexitsall(char *exitstr)
  22. {
  23. Proc *p;
  24. int *pid;
  25. int i, npid, mypid;
  26. if(exitstr == nil)
  27. exitstr = "";
  28. _threadexitsallstatus = exitstr;
  29. _threaddebug(DBGSCHED, "_threadexitsallstatus set to %p", _threadexitsallstatus);
  30. mypid = getpid();
  31. /*
  32. * signal others.
  33. * copying all the pids first avoids other threads
  34. * teardown procedures getting in the way.
  35. */
  36. lock(&_threadpq.lock);
  37. npid = 0;
  38. for(p=_threadpq.head; p; p=p->next)
  39. npid++;
  40. pid = _threadmalloc(npid*sizeof(pid[0]), 0);
  41. npid = 0;
  42. for(p = _threadpq.head; p; p=p->next)
  43. pid[npid++] = p->pid;
  44. unlock(&_threadpq.lock);
  45. for(i=0; i<npid; i++)
  46. if(pid[i] != mypid)
  47. postnote(PNPROC, pid[i], "threadint");
  48. /* leave */
  49. exits(exitstr);
  50. }
  51. Channel*
  52. threadwaitchan(void)
  53. {
  54. if(_threadwaitchan==nil)
  55. _threadwaitchan = chancreate(sizeof(Waitmsg*), 16);
  56. return _threadwaitchan;
  57. }