rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 1 | #include "threadimpl.h" |
| 2 | |
| 3 | int |
| 4 | threadid(void) |
| 5 | { |
| 6 | return _threadgetproc()->thread->id; |
| 7 | } |
| 8 | |
| 9 | int |
| 10 | threadpid(int id) |
| 11 | { |
| 12 | int pid; |
| 13 | Proc *p; |
| 14 | Thread *t; |
| 15 | |
| 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 | |
| 36 | int |
| 37 | threadsetgrp(int ng) |
| 38 | { |
| 39 | int og; |
| 40 | Thread *t; |
| 41 | |
| 42 | t = _threadgetproc()->thread; |
| 43 | og = t->grp; |
| 44 | t->grp = ng; |
| 45 | return og; |
| 46 | } |
| 47 | |
| 48 | int |
| 49 | threadgetgrp(void) |
| 50 | { |
| 51 | return _threadgetproc()->thread->grp; |
| 52 | } |
| 53 | |
| 54 | void |
rsc | be36ff6 | 2004-04-29 17:13:24 +0000 | [diff] [blame] | 55 | threadsetname(char *fmt, ...) |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 56 | { |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 57 | Proc *p; |
| 58 | Thread *t; |
rsc | be36ff6 | 2004-04-29 17:13:24 +0000 | [diff] [blame] | 59 | va_list arg; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 60 | |
| 61 | p = _threadgetproc(); |
| 62 | t = p->thread; |
| 63 | if (t->cmdname) |
| 64 | free(t->cmdname); |
rsc | be36ff6 | 2004-04-29 17:13:24 +0000 | [diff] [blame] | 65 | va_start(arg, fmt); |
| 66 | t->cmdname = vsmprint(fmt, arg); |
| 67 | va_end(fmt); |
| 68 | |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 69 | /* Plan 9 only |
| 70 | if(p->nthreads == 1){ |
| 71 | snprint(buf, sizeof buf, "#p/%d/args", getpid()); |
| 72 | if((fd = open(buf, OWRITE)) >= 0){ |
| 73 | snprint(buf, sizeof buf, "%s [%s]", argv0, name); |
| 74 | n = strlen(buf)+1; |
| 75 | s = strchr(buf, ' '); |
| 76 | if(s) |
| 77 | *s = '\0'; |
| 78 | write(fd, buf, n); |
| 79 | close(fd); |
| 80 | } |
| 81 | } |
| 82 | */ |
| 83 | } |
| 84 | |
| 85 | char* |
| 86 | threadgetname(void) |
| 87 | { |
| 88 | return _threadgetproc()->thread->cmdname; |
| 89 | } |
| 90 | |
| 91 | void** |
| 92 | threaddata(void) |
| 93 | { |
| 94 | return &_threadgetproc()->thread->udata[0]; |
| 95 | } |
| 96 | |
| 97 | void** |
| 98 | procdata(void) |
| 99 | { |
| 100 | return &_threadgetproc()->udata; |
| 101 | } |
| 102 | |
| 103 | static Lock privlock; |
| 104 | static int privmask = 1; |
| 105 | |
| 106 | int |
| 107 | tprivalloc(void) |
| 108 | { |
| 109 | int i; |
| 110 | |
| 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 | |
| 122 | void |
| 123 | tprivfree(int i) |
| 124 | { |
| 125 | if(i < 0 || i >= NPRIV) |
| 126 | abort(); |
| 127 | lock(&privlock); |
| 128 | privmask &= ~(1<<i); |
| 129 | } |
| 130 | |
| 131 | void** |
| 132 | tprivaddr(int i) |
| 133 | { |
| 134 | return &_threadgetproc()->thread->udata[i]; |
| 135 | } |