summaryrefslogtreecommitdiff
path: root/concurrency.c
blob: 4af563c8eee06f67e50aa07d8589c2d653c1b101 (plain) (blame)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>
#include "apl9.h"

#define STACKSIZE (8*1024*1024) /* 8 MB */

typedef struct SpawnData SpawnData;
struct SpawnData
{
	Function func;
	Array *left;
	Array *right;
	Channel *setupdone;
};

static void newprocfn(void *);
static ThreadData *newthreaddata(void);

/* global data */
static Lock threadlock;
static int nthreads;
static ThreadData **threads;
int mainstacksize = STACKSIZE;

void
initthreads(void)
{
	ThreadData *td = newthreaddata();
	void **tdptr = threaddata();
	*tdptr = td;
}

int
spawnthread(Function f, Array *left, Array *right)
{
	/* lock the data structures */
	/* Spawn a new proc */
	/* Allocate a new mailbox */
	/* Send a message back to spawnthread that the setup is ready */
	/* In spawnthread: unlock datastructures, return thread id */
	/* in new proc:
		run runfunc
		lock datastructures
		delete mailbox
		unlock datastructures
		exit proc
	*/
	Channel *setupdone = chancreate(sizeof(int), 0);
	SpawnData *sp = malloc(sizeof(SpawnData));
	sp->func = f;
	sp->left = left ? duparray(left) : nil;
	sp->right = duparray(right);
	sp->setupdone = setupdone;
	int id = proccreate(newprocfn, sp, STACKSIZE);
	recv(setupdone, nil); /* wait for new proc to signal that the setup is done */
	chanfree(setupdone);
	return id;
}

ThreadData *
getthreaddata(void)
{
	void **tdptr = threaddata();
	return (ThreadData *)*tdptr;
}

void
messagesend(Array *a, int id)
{
	ThreadData *td = nil;
	lock(&threadlock);
	for(int i = 0; i < nthreads && td == nil; i++)
		if(threads[i]->id == id)
			td = threads[i];
	unlock(&threadlock);
	if(td != nil){
		qlock(&td->lock);
		Mail *newmail = malloc(sizeof(Mail));
		newmail->contents = fnSame(a);
		newmail->next = 0;
		if(td->lastmail != nil)
			td->lastmail->next = newmail;
		else
			td->mail = newmail;
		td->lastmail = newmail;
		rwakeup(&td->empty);
		qunlock(&td->lock);
	}
}

Array *
messagerecv(Function match, int timeout)
{
	USED(timeout);
	USED(match);
	ThreadData *td = getthreaddata();
	qlock(&td->lock);
	while(td->mail == nil)
		rsleep(&td->empty);
	Mail *m = td->mail;
	td->mail = m->next;
	if(td->mail == nil)
		td->lastmail = nil;
	qunlock(&td->lock);
	Array *a = m->contents;
	free(m);
	return a;
}


static void
newprocfn(void *data)
{
	SpawnData *sp = (SpawnData *)data;
	ThreadData *td = newthreaddata();
	void **tdptr = threaddata();
	*tdptr = td;
	int done = 1;
	send(sp->setupdone, &done);
	runfunc(sp->func, sp->left, sp->right);
	lock(&threadlock);
	/* TODO remove thread */
	unlock(&threadlock);
	freearray(sp->left);
	freearray(sp->right);
	free(sp);
	free(td);
}

static ThreadData *
newthreaddata(void)
{
	ThreadData *td = mallocz(sizeof(ThreadData), 1);
	td->id = threadid();
	td->currentdfn = nil;
	td->mail = 0;
	td->lastmail = 0;
	td->empty.l = &td->lock;

	lock(&threadlock);
	nthreads++;
	threads = realloc(threads, sizeof(ThreadData *) * nthreads);
	threads[nthreads-1] = td;
	unlock(&threadlock);
	return td;
}