File: thread_pthread.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 (195 lines) | stat: -rw-r--r-- 3,258 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
192
193
194
195
/* Written by Kris Maglione <maglione.k at Gmail> */
/* Public domain */
#define _XOPEN_SOURCE 600
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include "ixp_local.h"

static IxpThread ixp_pthread;
static pthread_key_t errstr_k;

/**
 * Function: ixp_pthread_init
 *
 * This function initializes libixp for use in multithreaded
 * programs using the POSIX thread system. When using libixp in such
 * programs, this function must be called before any other libixp
 * functions. This function is part of libixp_pthread, which you
 * must explicitly link against.
 */
int
ixp_pthread_init() {
	int ret;

	IXP_ASSERT_VERSION;

	ret = pthread_key_create(&errstr_k, free);
	if(ret) {
		werrstr("can't create TLS value: %s", ixp_errbuf());
		return 1;
	}

	ixp_thread = &ixp_pthread;
	return 0;
}

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

	ret = pthread_getspecific(errstr_k);
	if(ret == nil) {
		ret = emallocz(IXP_ERRMAX);
		pthread_setspecific(errstr_k, (void*)ret);
	}
	return ret;
}

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

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

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

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

static int
initmutex(IxpMutex *m) {
	pthread_mutex_t *mutex;

	mutex = emalloc(sizeof *mutex);
	if(pthread_mutex_init(mutex, nil)) {
		free(mutex);
		return 1;
	}

	m->aux = mutex;
	return 0;
}

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

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

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

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

static void
rwunlock(IxpRWLock *rw) {
	pthread_rwlock_unlock(rw->aux);
}

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

static int
initrwlock(IxpRWLock *rw) {
	pthread_rwlock_t *rwlock;

	rwlock = emalloc(sizeof *rwlock);
	if(pthread_rwlock_init(rwlock, nil)) {
		free(rwlock);
		return 1;
	}

	rw->aux = rwlock;
	return 0;
}

static void
rsleep(IxpRendez *r) {
	pthread_cond_wait(r->aux, r->mutex->aux);
}

static int
rwake(IxpRendez *r) {
	pthread_cond_signal(r->aux);
	return 0;
}

static int
rwakeall(IxpRendez *r) {
	pthread_cond_broadcast(r->aux);
	return 0;
}

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

static int
initrendez(IxpRendez *r) {
	pthread_cond_t *cond;

	cond = emalloc(sizeof *cond);
	if(pthread_cond_init(cond, nil)) {
		free(cond);
		return 1;
	}

	r->aux = cond;
	return 0;
}

static IxpThread ixp_pthread = {
	/* Mutex */
	.initmutex = initmutex,
	.lock = mlock,
	.canlock = mcanlock,
	.unlock = munlock,
	.mdestroy = mdestroy,
	/* RWLock */
	.initrwlock = initrwlock,
	.rlock = rlock,
	.canrlock = canrlock,
	.wlock = wlock,
	.canwlock = canwlock,
	.runlock = rwunlock,
	.wunlock = rwunlock,
	.rwdestroy = rwdestroy,
	/* Rendez */
	.initrendez = initrendez,
	.sleep = rsleep,
	.wake = rwake,
	.wakeall = rwakeall,
	.rdestroy = rdestroy,
	/* Other */
	.errbuf = errbuf,
	.read = read,
	.write = write,
	.select = select,
};