note.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. int _threadnopasser;
  14. #define NFN 33
  15. #define ERRLEN 48
  16. typedef struct Note Note;
  17. struct Note
  18. {
  19. Lock inuse;
  20. Proc *proc; /* recipient */
  21. char s[ERRMAX]; /* arg2 */
  22. };
  23. static Note notes[128];
  24. static Note *enotes = notes+nelem(notes);
  25. static int (*onnote[NFN])(void*, char*);
  26. static int onnotepid[NFN];
  27. static Lock onnotelock;
  28. int
  29. threadnotify(int (*f)(void*, char*), int in)
  30. {
  31. int i, topid;
  32. int (*from)(void*, char*), (*to)(void*, char*);
  33. if(in){
  34. from = nil;
  35. to = f;
  36. topid = _threadgetproc()->pid;
  37. }else{
  38. from = f;
  39. to = nil;
  40. topid = 0;
  41. }
  42. lock(&onnotelock);
  43. for(i=0; i<NFN; i++)
  44. if(onnote[i]==from){
  45. onnote[i] = to;
  46. onnotepid[i] = topid;
  47. break;
  48. }
  49. unlock(&onnotelock);
  50. return i<NFN;
  51. }
  52. static void
  53. delayednotes(Proc *p, void *v)
  54. {
  55. int i;
  56. Note *n;
  57. int (*fn)(void*, char*);
  58. if(!p->pending)
  59. return;
  60. p->pending = 0;
  61. for(n=notes; n<enotes; n++){
  62. if(n->proc == p){
  63. for(i=0; i<NFN; i++){
  64. if(onnotepid[i]!=p->pid || (fn = onnote[i])==nil)
  65. continue;
  66. if((*fn)(v, n->s))
  67. break;
  68. }
  69. if(i==NFN){
  70. _threaddebug(DBGNOTE, "Unhandled note %s, proc %p\n", n->s, p);
  71. if(v != nil)
  72. noted(NDFLT);
  73. else if(strncmp(n->s, "sys:", 4)==0)
  74. abort();
  75. threadexitsall(n->s);
  76. }
  77. n->proc = nil;
  78. unlock(&n->inuse);
  79. }
  80. }
  81. }
  82. void
  83. _threadnote(void *v, char *s)
  84. {
  85. Proc *p;
  86. Note *n;
  87. _threaddebug(DBGNOTE, "Got note %s", s);
  88. if(strncmp(s, "sys:", 4) == 0)
  89. noted(NDFLT);
  90. if(_threadexitsallstatus){
  91. _threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'\n", _threadexitsallstatus);
  92. _exits(_threadexitsallstatus);
  93. }
  94. if(strcmp(s, "threadint")==0)
  95. noted(NCONT);
  96. p = _threadgetproc();
  97. if(p == nil)
  98. noted(NDFLT);
  99. for(n=notes; n<enotes; n++)
  100. if(canlock(&n->inuse))
  101. break;
  102. if(n==enotes)
  103. sysfatal("libthread: too many delayed notes");
  104. utfecpy(n->s, n->s+ERRMAX, s);
  105. n->proc = p;
  106. p->pending = 1;
  107. if(!p->splhi)
  108. delayednotes(p, v);
  109. noted(NCONT);
  110. }
  111. int
  112. _procsplhi(void)
  113. {
  114. int s;
  115. Proc *p;
  116. p = _threadgetproc();
  117. s = p->splhi;
  118. p->splhi = 1;
  119. return s;
  120. }
  121. void
  122. _procsplx(int s)
  123. {
  124. Proc *p;
  125. p = _threadgetproc();
  126. p->splhi = s;
  127. if(s)
  128. return;
  129. if(p->pending)
  130. delayednotes(p, nil);
  131. }