aboutsummaryrefslogtreecommitdiff
path: root/src/lib9/_p9proc.c
blob: 6e4010cda8392e47c004f156a8cb2801187e391d (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
#include <u.h>
#include <libc.h>
#include "9proc.h"

static Lock uproclock;
static Uproc *phash[PIDHASH];

Uproc*
_p9uproc(void)
{
	/* for now, assume getpid is fast or cached */
	int pid;
	Uproc *up;

	pid = getpid();
again:
if(0)print("find %d\n", pid);
	lock(&uproclock);
	for(up=phash[pid%PIDHASH]; up; up=up->next){
		if(up->pid == pid){
if(0)print("found %d\n", pid);
			unlock(&uproclock);
			return up;
		}
	}

	up = mallocz(sizeof(Uproc), 1);
	if(up == nil){
if(0)print("again %d\n", pid);
		unlock(&uproclock);
		sleep(1000);
		goto again;
	}

againpipe:
	if(pipe(up->pipe) < 0){
if(0)print("againpipe %d\n", pid);
		sleep(1000);
		goto againpipe;
	}

	up->pid = pid;
	up->next = phash[pid%PIDHASH];
	phash[pid%PIDHASH] = up;
if(0)print("link %d\n", pid);
	unlock(&uproclock);
	return up;
}

void
_p9uprocdie(void)
{
	Uproc **l, *up;
	int pid;

	pid = getpid();
if(0)print("die %d\n", pid);
	lock(&uproclock);
	for(l=&phash[pid%33]; *l; l=&(*l)->next){
		if((*l)->pid == pid){
			up = *l;
			*l = up->next;
if(0)print("died %d\n", pid);
			unlock(&uproclock);
			close(up->pipe[0]);
			close(up->pipe[1]);
			free(up);
			return;
		}
	}
if(0)print("not started %d\n", pid);
	unlock(&uproclock);
}