plan9-thread.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 <oventi.h>
  12. enum
  13. {
  14. QueuingW, /* queuing for write lock */
  15. QueuingR, /* queuing for read lock */
  16. };
  17. typedef struct Thread Thread;
  18. struct Thread {
  19. int pid;
  20. int ref;
  21. char *error;
  22. int state;
  23. Thread *next;
  24. };
  25. struct VtLock {
  26. Lock lk;
  27. Thread *writer; /* thread writering write lock */
  28. int readers; /* number writering read lock */
  29. Thread *qfirst;
  30. Thread *qlast;
  31. uintptr pc;
  32. };
  33. struct VtRendez {
  34. VtLock *lk;
  35. Thread *wfirst;
  36. Thread *wlast;
  37. };
  38. enum {
  39. ERROR = 0,
  40. };
  41. static Thread **vtRock;
  42. static void vtThreadInit(void);
  43. static void threadSleep(Thread*);
  44. static void threadWakeup(Thread*);
  45. int
  46. vtThread(void (*f)(void*), void *rock)
  47. {
  48. int tid;
  49. tid = rfork(RFNOWAIT|RFMEM|RFPROC);
  50. switch(tid){
  51. case -1:
  52. vtOSError();
  53. return -1;
  54. case 0:
  55. break;
  56. default:
  57. return tid;
  58. }
  59. vtAttach();
  60. (*f)(rock);
  61. vtDetach();
  62. _exits(0);
  63. return 0;
  64. }
  65. static Thread *
  66. threadLookup(void)
  67. {
  68. return *vtRock;
  69. }
  70. void
  71. vtAttach(void)
  72. {
  73. int pid;
  74. Thread *p;
  75. static int init;
  76. static Lock lk;
  77. lock(&lk);
  78. if(!init) {
  79. rfork(RFREND);
  80. vtThreadInit();
  81. init = 1;
  82. }
  83. unlock(&lk);
  84. pid = getpid();
  85. p = *vtRock;
  86. if(p != nil && p->pid == pid) {
  87. p->ref++;
  88. return;
  89. }
  90. p = vtMemAllocZ(sizeof(Thread));
  91. p->ref = 1;
  92. p->pid = pid;
  93. *vtRock = p;
  94. }
  95. void
  96. vtDetach(void)
  97. {
  98. Thread *p;
  99. p = *vtRock;
  100. assert(p != nil);
  101. p->ref--;
  102. if(p->ref == 0) {
  103. vtMemFree(p->error);
  104. vtMemFree(p);
  105. *vtRock = nil;
  106. }
  107. }
  108. char *
  109. vtGetError(void)
  110. {
  111. char *s;
  112. if(ERROR)
  113. fprint(2, "vtGetError: %s\n", threadLookup()->error);
  114. s = threadLookup()->error;
  115. if(s == nil)
  116. return "unknown error";
  117. return s;
  118. }
  119. char*
  120. vtSetError(char* fmt, ...)
  121. {
  122. Thread *p;
  123. char *s;
  124. va_list args;
  125. p = threadLookup();
  126. va_start(args, fmt);
  127. s = vsmprint(fmt, args);
  128. vtMemFree(p->error);
  129. p->error = s;
  130. va_end(args);
  131. if(ERROR)
  132. fprint(2, "vtSetError: %s\n", p->error);
  133. werrstr("%s", p->error);
  134. return p->error;
  135. }
  136. static void
  137. vtThreadInit(void)
  138. {
  139. static Lock lk;
  140. lock(&lk);
  141. if(vtRock != nil) {
  142. unlock(&lk);
  143. return;
  144. }
  145. vtRock = (Thread **)privalloc();
  146. if(vtRock == nil)
  147. vtFatal("can't allocate thread-private storage");
  148. unlock(&lk);
  149. }
  150. VtLock*
  151. vtLockAlloc(void)
  152. {
  153. return vtMemAllocZ(sizeof(VtLock));
  154. }
  155. /*
  156. * RSC: I think the test is backward. Let's see who uses it.
  157. *
  158. void
  159. vtLockInit(VtLock **p)
  160. {
  161. static Lock lk;
  162. lock(&lk);
  163. if(*p != nil)
  164. *p = vtLockAlloc();
  165. unlock(&lk);
  166. }
  167. */
  168. void
  169. vtLockFree(VtLock *p)
  170. {
  171. if(p == nil)
  172. return;
  173. assert(p->writer == nil);
  174. assert(p->readers == 0);
  175. assert(p->qfirst == nil);
  176. vtMemFree(p);
  177. }
  178. VtRendez*
  179. vtRendezAlloc(VtLock *p)
  180. {
  181. VtRendez *q;
  182. q = vtMemAllocZ(sizeof(VtRendez));
  183. q->lk = p;
  184. setmalloctag(q, getcallerpc());
  185. return q;
  186. }
  187. void
  188. vtRendezFree(VtRendez *q)
  189. {
  190. if(q == nil)
  191. return;
  192. assert(q->wfirst == nil);
  193. vtMemFree(q);
  194. }
  195. int
  196. vtCanLock(VtLock *p)
  197. {
  198. Thread *t;
  199. lock(&p->lk);
  200. t = *vtRock;
  201. if(p->writer == nil && p->readers == 0) {
  202. p->writer = t;
  203. unlock(&p->lk);
  204. return 1;
  205. }
  206. unlock(&p->lk);
  207. return 0;
  208. }
  209. void
  210. vtLock(VtLock *p)
  211. {
  212. Thread *t;
  213. lock(&p->lk);
  214. p->pc = getcallerpc();
  215. t = *vtRock;
  216. if(p->writer == nil && p->readers == 0) {
  217. p->writer = t;
  218. unlock(&p->lk);
  219. return;
  220. }
  221. /*
  222. * venti currently contains code that assume locks can be passed between threads :-(
  223. * assert(p->writer != t);
  224. */
  225. if(p->qfirst == nil)
  226. p->qfirst = t;
  227. else
  228. p->qlast->next = t;
  229. p->qlast = t;
  230. t->next = nil;
  231. t->state = QueuingW;
  232. unlock(&p->lk);
  233. threadSleep(t);
  234. assert(p->writer == t && p->readers == 0);
  235. }
  236. int
  237. vtCanRLock(VtLock *p)
  238. {
  239. lock(&p->lk);
  240. if(p->writer == nil && p->qfirst == nil) {
  241. p->readers++;
  242. unlock(&p->lk);
  243. return 1;
  244. }
  245. unlock(&p->lk);
  246. return 0;
  247. }
  248. void
  249. vtRLock(VtLock *p)
  250. {
  251. Thread *t;
  252. lock(&p->lk);
  253. t = *vtRock;
  254. if(p->writer == nil && p->qfirst == nil) {
  255. p->readers++;
  256. unlock(&p->lk);
  257. return;
  258. }
  259. /*
  260. * venti currently contains code that assumes locks can be passed between threads
  261. * assert(p->writer != t);
  262. */
  263. if(p->qfirst == nil)
  264. p->qfirst = t;
  265. else
  266. p->qlast->next = t;
  267. p->qlast = t;
  268. t->next = nil;
  269. t->state = QueuingR;
  270. unlock(&p->lk);
  271. threadSleep(t);
  272. assert(p->writer == nil && p->readers > 0);
  273. }
  274. void
  275. vtUnlock(VtLock *p)
  276. {
  277. Thread *t, *tt;
  278. lock(&p->lk);
  279. /*
  280. * venti currently has code that assumes lock can be passed between threads :-)
  281. * assert(p->writer == *vtRock);
  282. */
  283. assert(p->writer != nil);
  284. assert(p->readers == 0);
  285. t = p->qfirst;
  286. if(t == nil) {
  287. p->writer = nil;
  288. unlock(&p->lk);
  289. return;
  290. }
  291. if(t->state == QueuingW) {
  292. p->qfirst = t->next;
  293. p->writer = t;
  294. unlock(&p->lk);
  295. threadWakeup(t);
  296. return;
  297. }
  298. p->writer = nil;
  299. while(t != nil && t->state == QueuingR) {
  300. tt = t;
  301. t = t->next;
  302. p->readers++;
  303. threadWakeup(tt);
  304. }
  305. p->qfirst = t;
  306. unlock(&p->lk);
  307. }
  308. void
  309. vtRUnlock(VtLock *p)
  310. {
  311. Thread *t;
  312. lock(&p->lk);
  313. assert(p->writer == nil && p->readers > 0);
  314. p->readers--;
  315. t = p->qfirst;
  316. if(p->readers > 0 || t == nil) {
  317. unlock(&p->lk);
  318. return;
  319. }
  320. assert(t->state == QueuingW);
  321. p->qfirst = t->next;
  322. p->writer = t;
  323. unlock(&p->lk);
  324. threadWakeup(t);
  325. }
  326. int
  327. vtSleep(VtRendez *q)
  328. {
  329. Thread *s, *t, *tt;
  330. VtLock *p;
  331. p = q->lk;
  332. lock(&p->lk);
  333. s = *vtRock;
  334. /*
  335. * venti currently contains code that assume locks can be passed between threads :-(
  336. * assert(p->writer != s);
  337. */
  338. assert(p->writer != nil);
  339. assert(p->readers == 0);
  340. t = p->qfirst;
  341. if(t == nil) {
  342. p->writer = nil;
  343. } else if(t->state == QueuingW) {
  344. p->qfirst = t->next;
  345. p->writer = t;
  346. threadWakeup(t);
  347. } else {
  348. p->writer = nil;
  349. while(t != nil && t->state == QueuingR) {
  350. tt = t;
  351. t = t->next;
  352. p->readers++;
  353. threadWakeup(tt);
  354. }
  355. }
  356. if(q->wfirst == nil)
  357. q->wfirst = s;
  358. else
  359. q->wlast->next = s;
  360. q->wlast = s;
  361. s->next = nil;
  362. unlock(&p->lk);
  363. threadSleep(s);
  364. assert(p->writer == s);
  365. return 1;
  366. }
  367. int
  368. vtWakeup(VtRendez *q)
  369. {
  370. Thread *t;
  371. VtLock *p;
  372. /*
  373. * take off wait and put on front of queue
  374. * put on front so guys that have been waiting will not get starved
  375. */
  376. p = q->lk;
  377. lock(&p->lk);
  378. /*
  379. * venti currently has code that assumes lock can be passed between threads :-)
  380. * assert(p->writer == *vtRock);
  381. */
  382. assert(p->writer != nil);
  383. t = q->wfirst;
  384. if(t == nil) {
  385. unlock(&p->lk);
  386. return 0;
  387. }
  388. q->wfirst = t->next;
  389. if(p->qfirst == nil)
  390. p->qlast = t;
  391. t->next = p->qfirst;
  392. p->qfirst = t;
  393. t->state = QueuingW;
  394. unlock(&p->lk);
  395. return 1;
  396. }
  397. int
  398. vtWakeupAll(VtRendez *q)
  399. {
  400. int i;
  401. for(i=0; vtWakeup(q); i++)
  402. ;
  403. return i;
  404. }
  405. static void
  406. threadSleep(Thread *t)
  407. {
  408. if(rendezvous(t, (void*)0x22bbdfd6) != (void*)0x44391f14)
  409. sysfatal("threadSleep: rendezvous failed: %r");
  410. }
  411. static void
  412. threadWakeup(Thread *t)
  413. {
  414. if(rendezvous(t, (void*)0x44391f14) != (void*)0x22bbdfd6)
  415. sysfatal("threadWakeup: rendezvous failed: %r");
  416. }