note.c 2.7 KB

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