File: thread_task.c

package info (click to toggle)
libixp 0.5-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 608 kB
  • sloc: ansic: 4,857; sh: 96; makefile: 75
file content (179 lines) | stat: -rw-r--r-- 2,575 bytes parent folder | download | duplicates (2)
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
/* Written by Kris Maglione <fbsdaemon@gmail.com> */
/* Public domain */
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <task.h>
#include "ixp_local.h"

static IxpThread ixp_task;

int
ixp_taskinit() {
	ixp_thread = &ixp_task;
	return 0;
}

static char*
errbuf(void) {
	void **p;

	p = taskdata();
	if(*p == nil)
		*p = emallocz(IXP_ERRMAX);
	return *p;
}

/* Mutex */
static int
initmutex(IxpMutex *m) {
	m->aux = emallocz(sizeof(QLock));
	return 0;
}

static void
mdestroy(IxpMutex *m) {
	free(m->aux);
	m->aux = nil;
}

static void
mlock(IxpMutex *m) {
	qlock(m->aux);
}

static int
mcanlock(IxpMutex *m) {
	return canqlock(m->aux);
}

static void
munlock(IxpMutex *m) {
	qunlock(m->aux);
}

/* RWLock */
static int
initrwlock(IxpRWLock *rw) {
	rw->aux = emallocz(sizeof(RWLock));
	return 0;
}

static void
rwdestroy(IxpRWLock *rw) {
	free(rw->aux);
	rw->aux = nil;
}

static void
_rlock(IxpRWLock *rw) {
	rlock(rw->aux);
}

static int
_canrlock(IxpRWLock *rw) {
	return canrlock(rw->aux);
}

static void
_wlock(IxpRWLock *rw) {
	wlock(rw->aux);
}

static int
_canwlock(IxpRWLock *rw) {
	return canwlock(rw->aux);
}

static void
_runlock(IxpRWLock *rw) {
	runlock(rw->aux);
}

static void
_wunlock(IxpRWLock *rw) {
	wunlock(rw->aux);
}

/* Rendez */
static int
initrendez(IxpRendez *r) {
	r->aux = emallocz(sizeof(Rendez));
	return 0;
}

static void
rdestroy(IxpRendez *r) {
	free(r->aux);
	r->aux = nil;
}

static void
rsleep(IxpRendez *r) {
	Rendez *rz;

	rz = r->aux;
	rz->l = r->mutex->aux;
	tasksleep(rz);
}

static int
rwake(IxpRendez *r) {
	Rendez *rz;

	rz = r->aux;
	rz->l = r->mutex->aux;
	return taskwakeup(rz);
}

static int
rwakeall(IxpRendez *r) {
	Rendez *rz;

	rz = r->aux;
	rz->l = r->mutex->aux;
	return taskwakeupall(rz);
}

/* Yielding IO */
static ssize_t
_read(int fd, void *buf, size_t size) {
	fdnoblock(fd);
	return fdread(fd, buf, size);
}

static ssize_t
_write(int fd, const void *buf, size_t size) {
	fdnoblock(fd);
	return fdwrite(fd, (void*)buf, size);
}

static IxpThread ixp_task = {
	/* Mutex */
	.initmutex = initmutex,
	.lock = mlock,
	.canlock = mcanlock,
	.unlock = munlock,
	.mdestroy = mdestroy,
	/* RWLock */
	.initrwlock = initrwlock,
	.rlock = _rlock,
	.canrlock = _canrlock,
	.wlock = _wlock,
	.canwlock = _canwlock,
	.runlock = _runlock,
	.wunlock = _wunlock,
	.rwdestroy = rwdestroy,
	/* Rendez */
	.initrendez = initrendez,
	.sleep = rsleep,
	.wake = rwake,
	.wakeall = rwakeall,
	.rdestroy = rdestroy,
	/* Other */
	.errbuf = errbuf,
	.read = _read,
	.write = _write,
	.select = select, /* wrong */
};