id.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. snprint(buf, sizeof buf, "%s [%s]", argv0, name);
  66. n = strlen(buf)+1;
  67. s = strchr(buf, ' ');
  68. if(s)
  69. *s = '\0';
  70. write(fd, buf, n);
  71. close(fd);
  72. }
  73. }
  74. }
  75. char*
  76. threadgetname(void)
  77. {
  78. return _threadgetproc()->thread->cmdname;
  79. }
  80. void**
  81. threaddata(void)
  82. {
  83. return &_threadgetproc()->thread->udata[0];
  84. }
  85. void**
  86. procdata(void)
  87. {
  88. return &_threadgetproc()->udata;
  89. }
  90. static Lock privlock;
  91. static int privmask = 1;
  92. int
  93. tprivalloc(void)
  94. {
  95. int i;
  96. lock(&privlock);
  97. for(i=0; i<NPRIV; i++)
  98. if(!(privmask&(1<<i))){
  99. privmask |= 1<<i;
  100. unlock(&privlock);
  101. return i;
  102. }
  103. unlock(&privlock);
  104. return -1;
  105. }
  106. void
  107. tprivfree(int i)
  108. {
  109. if(i < 0 || i >= NPRIV)
  110. abort();
  111. lock(&privlock);
  112. privmask &= ~(1<<i);
  113. }
  114. void**
  115. tprivaddr(int i)
  116. {
  117. return &_threadgetproc()->thread->udata[i];
  118. }