id.c 1.9 KB

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