id.c 1.8 KB

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