]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libthread/id.c
merge
[plan9front.git] / sys / src / libthread / id.c
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include "threadimpl.h"
5 #include <tos.h>
6
7 int
8 threadid(void)
9 {
10         return _threadgetproc()->thread->id;
11 }
12
13 int
14 threadpid(int id)
15 {
16         int pid;
17         Proc *p;
18         Thread *t;
19
20         if (id < 0)
21                 return -1;
22         if (id == 0)
23                 return _threadgetproc()->pid;
24         lock(&_threadpq.lock);
25         for (p = _threadpq.head; p; p = p->next){
26                 lock(&p->lock);
27                 for (t = p->threads.head; t; t = t->nextt)
28                         if (t->id == id){
29                                 pid = p->pid;
30                                 unlock(&p->lock);
31                                 unlock(&_threadpq.lock);
32                                 return pid;
33                         }
34                 unlock(&p->lock);
35         }
36         unlock(&_threadpq.lock);
37         return -1;
38 }
39
40 int
41 threadsetgrp(int ng)
42 {
43         int og;
44         Thread *t;
45
46         t = _threadgetproc()->thread;
47         og = t->grp;
48         t->grp = ng;
49         return og;
50 }
51
52 int
53 threadgetgrp(void)
54 {
55         return _threadgetproc()->thread->grp;
56 }
57
58 void
59 threadsetname(char *fmt, ...)
60 {
61         int fd;
62         char buf[32];
63         va_list arg;
64         Proc *p;
65         Thread *t;
66
67         p = _threadgetproc();
68         t = p->thread;
69         if (t->cmdname)
70                 free(t->cmdname);
71         va_start(arg, fmt);
72         t->cmdname = vsmprint(fmt, arg);
73         va_end(arg);
74         if(t->cmdname && p->nthreads == 1){
75                 snprint(buf, sizeof buf, "/proc/%lud/args", (ulong)getpid());
76                 if((fd = open(buf, OWRITE|OCEXEC)) >= 0){
77                         write(fd, t->cmdname, strlen(t->cmdname)+1);
78                         close(fd);
79                 }
80         }
81 }
82
83 char*
84 threadgetname(void)
85 {
86         return _threadgetproc()->thread->cmdname;
87 }
88
89 void**
90 threaddata(void)
91 {
92         return &_threadgetproc()->thread->udata;
93 }
94
95 void**
96 procdata(void)
97 {
98         return &_threadgetproc()->udata;
99 }