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 *name)
56 {
57 /*
58 int fd, n;
59 char buf[128], *s;
60 */
61 Proc *p;
62 Thread *t;
64 p = _threadgetproc();
65 t = p->thread;
66 if (t->cmdname)
67 free(t->cmdname);
68 t->cmdname = strdup(name);
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 }
85 char*
86 threadgetname(void)
87 {
88 return _threadgetproc()->thread->cmdname;
89 }
91 void**
92 threaddata(void)
93 {
94 return &_threadgetproc()->thread->udata[0];
95 }
97 void**
98 procdata(void)
99 {
100 return &_threadgetproc()->udata;
103 static Lock privlock;
104 static int privmask = 1;
106 int
107 tprivalloc(void)
109 int i;
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;
118 unlock(&privlock);
119 return -1;
122 void
123 tprivfree(int i)
125 if(i < 0 || i >= NPRIV)
126 abort();
127 lock(&privlock);
128 privmask &= ~(1<<i);
131 void**
132 tprivaddr(int i)
134 return &_threadgetproc()->thread->udata[i];