aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/proctab.c
blob: b8dd2097466d2d37c48ca66d6968daf14696c7d8 (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
#include "threadimpl.h"

/* this will need work */
enum
{
	PTABHASH = 257,
};

static int multi;
static Proc *theproc;

void
_threadmultiproc(void)
{
	if(multi == 0){
		multi = 1;
		_threadsetproc(theproc);
	}
}

static Lock ptablock;
Proc *ptab[PTABHASH];

void
_threadsetproc(Proc *p)
{
	int h;

	if(!multi){
		theproc = p;
		return;
	}
	lock(&ptablock);
	h = ((unsigned)p->pid)%PTABHASH;
	p->link = ptab[h];
	unlock(&ptablock);
	ptab[h] = p;
}

static Proc*
__threadgetproc(int rm)
{
	Proc **l, *p;
	int h, pid;

	if(!multi)
		return theproc;

	pid = getpid();

	lock(&ptablock);
	h = ((unsigned)pid)%PTABHASH;
	for(l=&ptab[h]; p=*l; l=&p->link){
		if(p->pid == pid){
			if(rm)
				*l = p->link;
			unlock(&ptablock);
			return p;
		}
	}
	unlock(&ptablock);
	return nil;
}

Proc*
_threadgetproc(void)
{
	return __threadgetproc(0);
}

Proc*
_threaddelproc(void)
{
	return __threadgetproc(1);
}