File: thread_task.c

package info (click to toggle)
libixp 0.6~20121202%2Bhg148-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 968 kB
  • ctags: 1,619
  • sloc: ansic: 4,907; sh: 142; perl: 121; makefile: 110
file content (191 lines) | stat: -rw-r--r-- 2,976 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
180
181
182
183
184
185
186
187
188
189
190
191
/* Written by Kris Maglione <maglione.k at Gmail> */
/* Public domain */
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <task.h>
#include "ixp_local.h"

static IxpThread ixp_task;

/**
 * Function: ixp_taskinit
 *
 * This function initializes libixp for use in multithreaded
 * programs using the libtask threading system. When using libixp in
 * such programs, this function must be called before any other
 * libixp functions.
 *
 * This function is part of libixp_task, which is part of the libixp
 * distribution, but is not built by default unless enabled in
 * config.mk.
 */
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 */
};