rendez.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. Rgrp _threadrgrp;
  14. static int isdirty;
  15. static void*
  16. finish(Thread *t, void *val)
  17. {
  18. void *ret;
  19. ret = t->rendval;
  20. t->rendval = val;
  21. while(t->state == Running)
  22. sleep(0);
  23. lock(&t->proc->lock);
  24. if(t->state == Rendezvous){ /* not always true: might be Dead */
  25. t->state = Ready;
  26. _threadready(t);
  27. }
  28. unlock(&t->proc->lock);
  29. return ret;
  30. }
  31. void*
  32. _threadrendezvous(void *tag, void *val)
  33. {
  34. void *ret;
  35. Thread *t, **l;
  36. lock(&_threadrgrp.lock);
  37. l = &_threadrgrp.hash[((uintptr)tag)%nelem(_threadrgrp.hash)];
  38. for(t=*l; t; l=&t->rendhash, t=*l){
  39. if(t->rendtag==tag){
  40. _threaddebug(DBGREND, "Rendezvous with thread %d.%d", t->proc->pid, t->id);
  41. *l = t->rendhash;
  42. ret = finish(t, val);
  43. unlock(&_threadrgrp.lock);
  44. return ret;
  45. }
  46. }
  47. /* Going to sleep here. */
  48. t = _threadgetproc()->thread;
  49. t->rendbreak = 0;
  50. t->inrendez = 1;
  51. t->rendtag = tag;
  52. t->rendval = val;
  53. t->rendhash = *l;
  54. *l = t;
  55. t->nextstate = Rendezvous;
  56. _threaddebug(DBGREND, "Rendezvous for tag %p", t->rendtag);
  57. unlock(&_threadrgrp.lock);
  58. _sched();
  59. t->inrendez = 0;
  60. _threaddebug(DBGREND, "Woke after rendezvous; val is %p", t->rendval);
  61. return t->rendval;
  62. }
  63. /*
  64. * This is called while holding _threadpq.lock and p->lock,
  65. * so we can't lock _threadrgrp.lock. Instead our caller has
  66. * to call _threadbreakrendez after dropping those locks.
  67. */
  68. void
  69. _threadflagrendez(Thread *t)
  70. {
  71. t->rendbreak = 1;
  72. isdirty = 1;
  73. }
  74. void
  75. _threadbreakrendez(void)
  76. {
  77. int i;
  78. Thread *t, **l;
  79. if(isdirty == 0)
  80. return;
  81. lock(&_threadrgrp.lock);
  82. if(isdirty == 0){
  83. unlock(&_threadrgrp.lock);
  84. return;
  85. }
  86. isdirty = 0;
  87. for(i=0; i<nelem(_threadrgrp.hash); i++){
  88. l = &_threadrgrp.hash[i];
  89. for(t=*l; t; t=*l){
  90. if(t->rendbreak){
  91. *l = t->rendhash;
  92. finish(t, (void*)~0);
  93. }else
  94. l=&t->rendhash;
  95. }
  96. }
  97. unlock(&_threadrgrp.lock);
  98. }