File: queue.c

package info (click to toggle)
tra 20020816-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 1,696 kB
  • ctags: 2,623
  • sloc: ansic: 22,519; makefile: 406; asm: 269
file content (131 lines) | stat: -rw-r--r-- 1,638 bytes parent folder | download
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
#include "tra.h"

static void
qin(void *a)
{
	Queue *q;
	Syncpath *s;

	q = a;
	for(;;){
		s = recvp(q->in);
		if(q->s==nil)
			q->es = &q->s;
		s->nextq = nil;
		*q->es = s;
		q->es = &s->nextq;
		q->n++;
		if(q->n > q->m)
			q->m = q->n;
		if(q->waiter){
			threadready(q->waiter);
			q->waiter = nil;
		}
	}
}

static void
qout(void *a)
{
	Queue *q;
	Syncpath *s;

	q = a;
	for(;;){
		while(q->s == nil){
			q->waiter = curthread;
			threadsleep();
		}
		s = q->s;
		q->s = s->nextq;
		q->n--;
		sendp(q->out, s);
	}
}

static void
kin(void *a)
{
	Queue *q;
	Syncpath *s;

	q = a;
	for(;;){
		s = recvp(q->in);
		q->n++;
		if(q->n > q->m)
			q->m = q->n;
		s->nextq = q->s;
		q->s = s;
		if(q->waiter){
			threadready(q->waiter);
			q->waiter = nil;
		}
	}
}

static void
kout(void *a)
{
	Queue *q;
	Syncpath *s;

	q = a;
	for(;;){
		while(q->s == nil){
			q->waiter = curthread;
			threadsleep();
		}
		s = q->s;
		q->s = s->nextq;
		q->n--;
		sendp(q->out, s);
	}
}

Queue*
mkstack(void)
{
	Queue *q;

	q = emalloc(sizeof(Queue));
	q->in = chan(Syncpath*);
	q->out = chan(Syncpath*);
	threadcreate(kin, q);
	threadcreate(kout, q);
	return q;
}

Queue*
mkqueue(void)
{
	Queue *q;

	q = emalloc(sizeof(Queue));
	q->in = chan(Syncpath*);
	q->out = chan(Syncpath*);
	threadcreate(qin, q);
	threadcreate(qout, q);
	return q;
}

Syncpath*
qrecv(Queue *q)
{
	Syncpath *s;

	s = recvp(q->out);
	if(q->printrecv)
		q->printrecv(s);
//fprint(2, "deq %p returns %p (%P)\n", q, s, s->p);
	return s;
}

void
qsend(Queue *q, Syncpath *s)
{
//fprint(2, "enq %p gets %p (%P)\n", q, s, s->p);
	if(q->printsend)
		q->printsend(s);
	sendp(q->in, s);
}