summaryrefslogtreecommitdiff
path: root/main2.c
blob: 67e5b3a51039937f2d22cf2c2bdcf2a56177819f (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <u.h>
#include <libc.h>
#include <thread.h>

typedef struct Ttask Ttask;
typedef struct Taskq Taskq;
typedef struct Tpool Tpool;

struct Ttask
{
	void (*fn)(void*);
	void *arg;
	Ttask *next;
};

struct Taskq
{
	Ttask *hd;
	Ttask *tl;
};

struct Tpool
{
	QLock;
	Rendez empty;
	ulong nprocs;
	Ref nworking;

	Taskq subq;	/* task submission queue */
	Channel *done;	/* task completion signal */
};

void
taskqput(Tpool *tp, Ttask *t)
{
	qlock(tp);
	if(tp->subq.tl == nil){
		tp->subq.hd = tp->subq.tl = t;
		rwakeup(&tp->empty);
		qunlock(tp);
		return;
	}

	tp->subq.tl->next = t;
	tp->subq.tl = t;
	rwakeup(&tp->empty);
	qunlock(tp);
}

Ttask *
taskqget(Tpool *tp)
{
	Ttask *t;

	qlock(tp);
	while(tp->subq.hd == nil)
		rsleep(&tp->empty);

	t = tp->subq.hd;
	tp->subq.hd = t->next;
	t->next = nil;
	if(tp->subq.hd == nil)
		tp->subq.tl = nil;
	qunlock(tp);
	return t;
}

void
threadloop(void *arg)
{
	Tpool *pool;
	Ttask *task;

	pool = arg;

	for(;;){
		task = taskqget(pool);
		if(task == nil)
			continue;
		incref(&pool->nworking);
		task->fn(task->arg);
		decref(&pool->nworking);
		nbsend(pool->done, nil);
	}
}

Tpool *
mkthreadpool(ulong nprocs)
{
	Tpool *tp;

	tp = malloc(sizeof *tp);
	memset(tp, 0, sizeof *tp);
	tp->empty.l = &tp->QLock;
	tp->nprocs = nprocs;
	tp->done = chancreate(sizeof(void*), 0);
	while(nprocs--)
		proccreate(threadloop, tp, mainstacksize);
	return tp;
}

void
threadpoolexec(Tpool *tp, void (*fn)(void*), void *arg)
{
	Ttask *t;

	t = malloc(sizeof *t);
	memset(t, 0, sizeof *t);
	t->fn = fn;
	t->arg = arg;
	taskqput(tp, t);
}

typedef struct Tsum Tsum;
struct Tsum
{
	int a;
	int b;
};
void
sum(void *arg)
{
	Tsum *sum;
	int cnt;

	sum = arg;
	cnt = 100;
	while(cnt--) sum->a = sum->a+sum->b;
}

void
usage(void)
{
	fprint(2, "usage: %s [-t] [-n nprocs]\n", argv0);
	exits(nil);
}

void
threadmain(int argc, char *argv[])
{
	static int W = 1000, H = 1000;
	Tpool *pool;
	Tsum *t;
	int i, j;
	int threaded;
	int nprocs;

	threaded = 0;
	nprocs = 8;
	ARGBEGIN{
	case 't': threaded++; break;
	case 'n': nprocs = strtoul(EARGF(usage()), nil, 0); break;
	default: usage();
	}ARGEND;
	if(argc != 0)
		usage();

	t = malloc(W*H*sizeof(*t));
	if(threaded){
		pool = mkthreadpool(nprocs);

		for(i = 0; i < H; i++)
		for(j = 0; j < W; j++){
			t[i*W+j] = (Tsum){i, j};
			threadpoolexec(pool, sum, &t[i*W+j]);
		}

		while(pool->nworking.ref > 0)
			recvp(pool->done);

		threadexitsall(nil);
	}

	for(i = 0; i < H; i++)
	for(j = 0; j < W; j++){
		t[i*W+j] = (Tsum){i, j};
		sum(&t[i*W+j]);
	}
	exits(nil);
}