id.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include <tos.h>
  14. int
  15. threadid(void)
  16. {
  17. return _threadgetproc()->thread->id;
  18. }
  19. int
  20. threadpid(int id)
  21. {
  22. int pid;
  23. Proc *p;
  24. Thread *t;
  25. if (id < 0)
  26. return -1;
  27. if (id == 0)
  28. return _threadgetproc()->pid;
  29. lock(&_threadpq.lock);
  30. for (p = _threadpq.head; p; p = p->next){
  31. lock(&p->lock);
  32. for (t = p->threads.head; t; t = t->nextt)
  33. if (t->id == id){
  34. pid = p->pid;
  35. unlock(&p->lock);
  36. unlock(&_threadpq.lock);
  37. return pid;
  38. }
  39. unlock(&p->lock);
  40. }
  41. unlock(&_threadpq.lock);
  42. return -1;
  43. }
  44. int
  45. threadsetgrp(int ng)
  46. {
  47. int og;
  48. Thread *t;
  49. t = _threadgetproc()->thread;
  50. og = t->grp;
  51. t->grp = ng;
  52. return og;
  53. }
  54. int
  55. threadgetgrp(void)
  56. {
  57. return _threadgetproc()->thread->grp;
  58. }
  59. void
  60. threadsetname(char *fmt, ...)
  61. {
  62. int fd;
  63. char buf[128];
  64. va_list arg;
  65. Proc *p;
  66. Thread *t;
  67. p = _threadgetproc();
  68. t = p->thread;
  69. if (t->cmdname)
  70. free(t->cmdname);
  71. va_start(arg, fmt);
  72. t->cmdname = vsmprint(fmt, arg);
  73. va_end(arg);
  74. if(t->cmdname && p->nthreads == 1){
  75. snprint(buf, sizeof buf, "#p/%lu/args", _tos->pid); //getpid());
  76. if((fd = open(buf, OWRITE)) >= 0){
  77. write(fd, t->cmdname, strlen(t->cmdname)+1);
  78. close(fd);
  79. }
  80. }
  81. }
  82. char*
  83. threadgetname(void)
  84. {
  85. Proc *p;
  86. if((p = _threadgetproc()) && p->thread)
  87. return p->thread->cmdname;
  88. return nil;
  89. }
  90. void**
  91. threaddata(void)
  92. {
  93. return &_threadgetproc()->thread->udata[0];
  94. }
  95. void**
  96. _workerdata(void)
  97. {
  98. return &_threadgetproc()->wdata;
  99. }
  100. void**
  101. procdata(void)
  102. {
  103. return &_threadgetproc()->udata;
  104. }
  105. static Lock privlock;
  106. static int privmask = 1;
  107. int
  108. tprivalloc(void)
  109. {
  110. int i;
  111. lock(&privlock);
  112. for(i=0; i<NPRIV; i++)
  113. if(!(privmask&(1<<i))){
  114. privmask |= 1<<i;
  115. unlock(&privlock);
  116. return i;
  117. }
  118. unlock(&privlock);
  119. return -1;
  120. }
  121. void
  122. tprivfree(int i)
  123. {
  124. if(i < 0 || i >= NPRIV)
  125. abort();
  126. lock(&privlock);
  127. privmask &= ~(1<<i);
  128. }
  129. void**
  130. tprivaddr(int i)
  131. {
  132. return &_threadgetproc()->thread->udata[i];
  133. }