Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_posix/sys/src/games/music/playlistfs/boilerplate.c – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include <u.h>
2
#include <libc.h>
3
#include <thread.h>
4
#include <fcall.h>
5
#include "playlist.h"
6
 
7
static Channel *reqs;
8
 
9
Req*
10
reqalloc(void)
11
{
12
	Req *r;
13
 
14
	if(reqs == nil)
15
		reqs = chancreate(sizeof(Req*), 256);
16
	if(r = nbrecvp(reqs))
17
		return r;
18
	r = malloc(sizeof(Req));
19
	return r;
20
}
21
 
22
void
23
reqfree(Req *r)
24
{
25
	if(!nbsendp(reqs, r))
26
		free(r);
27
}
28
 
29
Wmsg
30
waitmsg(Worker *w, Channel *q)
31
{
32
	Wmsg m;
33
 
34
	sendp(q, w);
35
	recv(w->eventc, &m);
36
	return m;
37
}
38
 
39
int
40
sendmsg(Channel *q, Wmsg *m)
41
{
42
	Worker *w;
43
 
44
	while(w = nbrecvp(q)){
45
		/* Test for markerdom (see bcastmsg) */
46
		if(w->eventc){
47
			send(w->eventc, m);
48
			return 1;
49
		}
50
		sendp(q, w);	/* put back */
51
	}
52
	return 0;
53
}
54
 
55
void
56
bcastmsg(Channel *q, Wmsg *m)
57
{
58
	Worker *w, marker;
59
	void *a;
60
 
61
	a = m->arg;
62
	/*
63
	 * Use a marker to mark the end of the queue.
64
	 * This prevents workers from getting the
65
	 * broadcast and putting themselves back on the
66
	 * queue before the broadcast has finished
67
	 */
68
	marker.eventc = nil;	/* Only markers have eventc == nil */
69
	sendp(q, &marker);
70
	while((w = recvp(q)) != &marker){
71
		if(w->eventc == nil){
72
			/* somebody else's marker, put it back */
73
			sendp(q, w);
74
		}else{
75
			if(a) m->arg = strdup(a);
76
			send(w->eventc, m);
77
		}
78
	}
79
	free(a);
80
	m->arg = nil;
81
}
82
 
83
void
84
readbuf(Req *r, void *s, long n)
85
{
86
	r->ofcall.count = r->ifcall.count;
87
	if(r->ifcall.offset >= n){
88
		r->ofcall.count = 0;
89
		return;
90
	}
91
	if(r->ifcall.offset+r->ofcall.count > n)
92
		r->ofcall.count = n - r->ifcall.offset;
93
	memmove(r->ofcall.data, (char*)s+r->ifcall.offset, r->ofcall.count);
94
}
95
 
96
void
97
readstr(Req *r, char *s)
98
{
99
	readbuf(r, s, strlen(s));
100
}