aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/id.c
blob: 727798d38510545277c3887f605bc03ce2a0f062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "threadimpl.h"

int
threadid(void)
{
	return _threadgetproc()->thread->id;
}

int
threadpid(int id)
{
	int pid;
	Proc *p;
	Thread *t;

	if (id < 0)
		return -1;
	if (id == 0)
		return _threadgetproc()->pid;
	lock(&_threadpq.lock);
	for (p = _threadpq.head; p->next; p = p->next){
		lock(&p->lock);
		for (t = p->threads.head; t; t = t->nextt)
			if (t->id == id){
				pid = p->pid;
				unlock(&p->lock);
				unlock(&_threadpq.lock);
				return pid;
			}
		unlock(&p->lock);
	}
	unlock(&_threadpq.lock);
	return -1;
}

int
threadsetgrp(int ng)
{
	int og;
	Thread *t;

	t = _threadgetproc()->thread;
	og = t->grp;
	t->grp = ng;
	return og;
}

int
threadgetgrp(void)
{
	return _threadgetproc()->thread->grp;
}

void
threadsetname(char *name)
{
/*
	int fd, n;
	char buf[128], *s;
*/
	Proc *p;
	Thread *t;

	p = _threadgetproc();
	t = p->thread;
	if (t->cmdname)
		free(t->cmdname);
	t->cmdname = strdup(name);
/* Plan 9 only 
	if(p->nthreads == 1){
		snprint(buf, sizeof buf, "#p/%d/args", getpid());
		if((fd = open(buf, OWRITE)) >= 0){
			snprint(buf, sizeof buf, "%s [%s]", argv0, name);
			n = strlen(buf)+1;
			s = strchr(buf, ' ');
			if(s)
				*s = '\0';
			write(fd, buf, n);
			close(fd);
		}
	}
*/
}

char*
threadgetname(void)
{
	return _threadgetproc()->thread->cmdname;
}

void**
threaddata(void)
{
	return &_threadgetproc()->thread->udata[0];
}

void**
procdata(void)
{
	return &_threadgetproc()->udata;
}

static Lock privlock;
static int privmask = 1;

int
tprivalloc(void)
{
	int i;

	lock(&privlock);
	for(i=0; i<NPRIV; i++)
		if(!(privmask&(1<<i))){
			privmask |= 1<<i;
			unlock(&privlock);
			return i;
		}
	unlock(&privlock);
	return -1;
}

void
tprivfree(int i)
{
	if(i < 0 || i >= NPRIV)
		abort();
	lock(&privlock);
	privmask &= ~(1<<i);
}

void**
tprivaddr(int i)
{
	return &_threadgetproc()->thread->udata[i];
}