Blob


1 #include "threadimpl.h"
3 int
4 threadid(void)
5 {
6 return _threadgetproc()->thread->id;
7 }
9 int
10 threadpid(int id)
11 {
12 int pid;
13 Proc *p;
14 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 }
36 int
37 threadsetgrp(int ng)
38 {
39 int og;
40 Thread *t;
42 t = _threadgetproc()->thread;
43 og = t->grp;
44 t->grp = ng;
45 return og;
46 }
48 int
49 threadgetgrp(void)
50 {
51 return _threadgetproc()->thread->grp;
52 }
54 void
55 threadsetname(char *fmt, ...)
56 {
57 Proc *p;
58 Thread *t;
59 va_list arg;
61 p = _threadgetproc();
62 t = p->thread;
63 if(t->name)
64 free(t->name);
65 va_start(arg, fmt);
66 t->name = vsmprint(fmt, arg);
67 va_end(arg);
69 _threaddebug(DBGSCHED, "set name %s", t->name);
70 /* Plan 9 only
71 if(p->nthreads == 1){
72 snprint(buf, sizeof buf, "#p/%d/args", getpid());
73 if((fd = open(buf, OWRITE)) >= 0){
74 snprint(buf, sizeof buf, "%s [%s]", argv0, name);
75 n = strlen(buf)+1;
76 s = strchr(buf, ' ');
77 if(s)
78 *s = '\0';
79 write(fd, buf, n);
80 close(fd);
81 }
82 }
83 */
84 }
86 char*
87 threadgetname(void)
88 {
89 return _threadgetproc()->thread->name;
90 }
92 void**
93 threaddata(void)
94 {
95 return &_threadgetproc()->thread->udata[0];
96 }
98 void**
99 procdata(void)
101 return &_threadgetproc()->udata;
104 static Lock privlock;
105 static int privmask = 1;
107 int
108 tprivalloc(void)
110 int i;
112 lock(&privlock);
113 for(i=0; i<NPRIV; i++)
114 if(!(privmask&(1<<i))){
115 privmask |= 1<<i;
116 unlock(&privlock);
117 return i;
119 unlock(&privlock);
120 return -1;
123 void
124 tprivfree(int i)
126 if(i < 0 || i >= NPRIV)
127 abort();
128 lock(&privlock);
129 privmask &= ~(1<<i);
132 void**
133 tprivaddr(int i)
135 return &_threadgetproc()->thread->udata[i];