id.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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->next; 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 *name)
  52. {
  53. int fd, n;
  54. char buf[128], *s;
  55. Proc *p;
  56. Thread *t;
  57. p = _threadgetproc();
  58. t = p->thread;
  59. if (t->cmdname)
  60. free(t->cmdname);
  61. t->cmdname = strdup(name);
  62. if(p->nthreads == 1){
  63. snprint(buf, sizeof buf, "#p/%d/args", getpid());
  64. if((fd = open(buf, OWRITE)) >= 0){
  65. n = strlen(name)+1;
  66. write(fd, name, n);
  67. close(fd);
  68. }
  69. }
  70. }
  71. char*
  72. threadgetname(void)
  73. {
  74. return _threadgetproc()->thread->cmdname;
  75. }
  76. void**
  77. threaddata(void)
  78. {
  79. return &_threadgetproc()->thread->udata[0];
  80. }
  81. void**
  82. procdata(void)
  83. {
  84. return &_threadgetproc()->udata;
  85. }
  86. static Lock privlock;
  87. static int privmask = 1;
  88. int
  89. tprivalloc(void)
  90. {
  91. int i;
  92. lock(&privlock);
  93. for(i=0; i<NPRIV; i++)
  94. if(!(privmask&(1<<i))){
  95. privmask |= 1<<i;
  96. unlock(&privlock);
  97. return i;
  98. }
  99. unlock(&privlock);
  100. return -1;
  101. }
  102. void
  103. tprivfree(int i)
  104. {
  105. if(i < 0 || i >= NPRIV)
  106. abort();
  107. lock(&privlock);
  108. privmask &= ~(1<<i);
  109. }
  110. void**
  111. tprivaddr(int i)
  112. {
  113. return &_threadgetproc()->thread->udata[i];
  114. }