xref: /plan9/sys/src/libthread/id.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include "threadimpl.h"
5 
6 int
7 threadid(void)
8 {
9 	return _threadgetproc()->thread->id;
10 }
11 
12 int
13 threadpid(int id)
14 {
15 	int pid;
16 	Proc *p;
17 	Thread *t;
18 
19 	if (id < 0)
20 		return -1;
21 	if (id == 0)
22 		return _threadgetproc()->pid;
23 	lock(&_threadpq.lock);
24 	for (p = _threadpq.head; p->next; p = p->next){
25 		lock(&p->lock);
26 		for (t = p->threads.head; t; t = t->nextt)
27 			if (t->id == id){
28 				pid = p->pid;
29 				unlock(&p->lock);
30 				unlock(&_threadpq.lock);
31 				return pid;
32 			}
33 		unlock(&p->lock);
34 	}
35 	unlock(&_threadpq.lock);
36 	return -1;
37 }
38 
39 int
40 threadsetgrp(int ng)
41 {
42 	int og;
43 	Thread *t;
44 
45 	t = _threadgetproc()->thread;
46 	og = t->grp;
47 	t->grp = ng;
48 	return og;
49 }
50 
51 int
52 threadgetgrp(void)
53 {
54 	return _threadgetproc()->thread->grp;
55 }
56 
57 void
58 threadsetname(char *name)
59 {
60 	Thread *t;
61 
62 	t = _threadgetproc()->thread;
63 	if (t->cmdname)
64 		free(t->cmdname);
65 	t->cmdname = strdup(name);
66 }
67 
68 char*
69 threadgetname(void)
70 {
71 	return _threadgetproc()->thread->cmdname;
72 }
73 
74 void**
75 threaddata(void)
76 {
77 	return &_threadgetproc()->thread->udata;
78 }
79 
80 void**
81 procdata(void)
82 {
83 	return &_threadgetproc()->udata;
84 }
85 
86