ObjFW  Diff

Differences From Artifact [ad9f8119e0]:

To Artifact [b5ecf36784]:


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
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







-
-
+
+




-
+












-
+







# define of_thread_current() GetCurrentThread()
#endif

static OF_INLINE BOOL
of_thread_new(of_thread_t *thread, id (*main)(id), id data)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_create(thread, NULL, (void*(*)(void*))main,
	    (void*)data) ? NO : YES);
	return !pthread_create(thread, NULL, (void*(*)(void*))main,
	    (void*)data);
#elif defined(_WIN32)
	*thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)main,
	    (void*)data, 0, NULL);

	return (thread == NULL ? NO : YES);
	return (thread != NULL);
#endif
}

static OF_INLINE BOOL
of_thread_join(of_thread_t thread)
{
#if defined(OF_HAVE_PTHREADS)
	void *ret;

	if (pthread_join(thread, &ret))
		return NO;

	return (ret != PTHREAD_CANCELED ? YES : NO);
	return (ret != PTHREAD_CANCELED);
#elif defined(_WIN32)
	if (WaitForSingleObject(thread, INFINITE))
		return NO;

	CloseHandle(thread);

	return YES;
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
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







-
+










-
+










-
+










-
+

-
+







-
+










-
+










+

+
+
+



-
+







#endif
}

static OF_INLINE BOOL
of_mutex_new(of_mutex_t *mutex)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_mutex_init(mutex, NULL) ? NO : YES);
	return !pthread_mutex_init(mutex, NULL);
#elif defined(_WIN32)
	InitializeCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_mutex_free(of_mutex_t *mutex)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_mutex_destroy(mutex) ? NO : YES);
	return !pthread_mutex_destroy(mutex);
#elif defined(_WIN32)
	DeleteCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_mutex_lock(of_mutex_t *mutex)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_mutex_lock(mutex) ? NO : YES);
	return !pthread_mutex_lock(mutex);
#elif defined(_WIN32)
	EnterCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_mutex_trylock(of_mutex_t *mutex)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_mutex_trylock(mutex) ? NO : YES);
	return !pthread_mutex_trylock(mutex);
#elif defined(_WIN32)
	return (TryEnterCriticalSection(mutex) ? YES : NO);
	return TryEnterCriticalSection(mutex);
#endif
}

static OF_INLINE BOOL
of_mutex_unlock(of_mutex_t *mutex)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_mutex_unlock(mutex) ? NO : YES);
	return !pthread_mutex_unlock(mutex);
#elif defined(_WIN32)
	LeaveCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_condition_new(of_condition_t *condition)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_cond_init(condition, NULL) ? NO : YES);
	return !pthread_cond_init(condition, NULL);
#elif defined(_WIN32)
	condition->count = 0;

	if ((condition->event = CreateEvent(NULL, FALSE, 0, NULL)) == NULL)
		return NO;

	return YES;
#endif
}

#if defined(OF_HAVE_PTHREADS)
static OF_INLINE BOOL
#elif defined(_WIN32)
static BOOL
#endif
of_condition_wait(of_condition_t *condition, of_mutex_t *mutex)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_cond_wait(condition, mutex) ? NO : YES);
	return !pthread_cond_wait(condition, mutex);
#elif defined(_WIN32)
	if (!of_mutex_unlock(mutex))
		return NO;

	of_atomic_inc_int(&condition->count);

	if (WaitForSingleObject(condition->event, INFINITE) != WAIT_OBJECT_0) {
195
196
197
198
199
200
201
202

203
204

205
206
207

208



209
210
211
212

213
214
215
216
217
218
219
220
221
222
223

224



225
226
227
228

229
230
231
232
233

234
235
236
237
238
239
240
241

242
243

244
245
246
247
248
249
250
199
200
201
202
203
204
205

206
207

208
209
210
211
212
213
214
215
216
217
218
219

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

240
241
242
243
244

245
246
247
248
249
250
251
252

253
254

255
256
257
258
259
260
261
262







-
+

-
+



+

+
+
+



-
+











+

+
+
+



-
+




-
+







-
+

-
+







#endif
}

static OF_INLINE BOOL
of_condition_signal(of_condition_t *condition)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_cond_signal(condition) ? NO : YES);
	return !pthread_cond_signal(condition);
#elif defined(_WIN32)
	return (SetEvent(condition->event) ? YES : NO);
	return SetEvent(condition->event);
#endif
}

#if defined(OF_HAVE_PTHREADS)
static OF_INLINE BOOL
#elif defined(_WIN32)
static BOOL
#endif
of_condition_broadcast(of_condition_t *condition)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_cond_broadcast(condition) ? NO : YES);
	return !pthread_cond_broadcast(condition);
#elif defined(_WIN32)
	size_t i;

	for (i = 0; i < condition->count; i++)
		if (!SetEvent(condition->event))
			return NO;

	return YES;
#endif
}

#if defined(OF_HAVE_PTHREADS)
static OF_INLINE BOOL
#elif defined(_WIN32)
static BOOL
#endif
of_condition_free(of_condition_t *condition)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_cond_destroy(condition) ? NO : YES);
	return !pthread_cond_destroy(condition);
#elif defined(_WIN32)
	if (condition->count)
		return NO;

	return (CloseHandle(condition->event) ? YES : NO);
	return CloseHandle(condition->event);
#endif
}

static OF_INLINE BOOL
of_tlskey_new(of_tlskey_t *key)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_key_create(key, NULL) ? NO : YES);
	return !pthread_key_create(key, NULL);
#elif defined(_WIN32)
	return ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES ? NO : YES);
	return ((*key = TlsAlloc()) != TLS_OUT_OF_INDEXES);
#endif
}

static OF_INLINE id
of_tlskey_get(of_tlskey_t key)
{
#if defined(OF_HAVE_PTHREADS)
262
263
264
265
266
267
268
269

270
271

272
273
274
275
276
277
278
279

280
281

282
283
284
285
286
287
288
289
290
291
292

293
294
295
296
297
298
299
300
301
302

303
304

305
306
307
308
309
310
311
274
275
276
277
278
279
280

281
282

283
284
285
286
287
288
289
290

291
292

293
294
295
296
297
298
299
300
301
302
303

304
305
306
307
308
309
310
311
312
313

314
315

316
317
318
319
320
321
322
323







-
+

-
+







-
+

-
+










-
+









-
+

-
+








static OF_INLINE BOOL
of_tlskey_set(of_tlskey_t key, id obj)
{
	void *p = (obj != nil ? (void*)obj : NULL);

#if defined(OF_HAVE_PTHREADS)
	return (pthread_setspecific(key, p) ? NO : YES);
	return !pthread_setspecific(key, p);
#elif defined(_WIN32)
	return (TlsSetValue(key, p) ? YES : NO);
	return TlsSetValue(key, p);
#endif
}

static OF_INLINE BOOL
of_tlskey_free(of_tlskey_t key)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_key_delete(key) ? NO : YES);
	return !pthread_key_delete(key);
#elif defined(_WIN32)
	return (TlsFree(key) ? YES : NO);
	return TlsFree(key);
#endif
}

static OF_INLINE BOOL
of_spinlock_new(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	*s = 0;
	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_init(s, 0) ? NO : YES);
	return !pthread_spin_init(s, 0);
#else
	return of_mutex_new(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_trylock(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	return (of_atomic_cmpswap_int(s, 0, 1) ? YES : NO);
	return of_atomic_cmpswap_int(s, 0, 1);
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_trylock(s) ? NO : YES);
	return !pthread_spin_trylock(s);
#else
	return of_mutex_trylock(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_lock(of_spinlock_t *s)
326
327
328
329
330
331
332
333

334
335
336
337
338
339
340
341
342
343
344
345
346

347
348
349
350
351
352
353
354
355
356
357
358

359
360
361
362
338
339
340
341
342
343
344

345
346
347
348
349
350
351
352
353
354
355
356
357

358
359
360
361
362
363
364
365
366
367
368
369

370
371
372
373
374







-
+












-
+











-
+




#  endif
# else
	while (!of_spinlock_trylock(s));
# endif

	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_lock(s) ? NO : YES);
	return !pthread_spin_lock(s);
#else
	return of_mutex_lock(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_unlock(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	*s = 0;
	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_unlock(s) ? NO : YES);
	return !pthread_spin_unlock(s);
#else
	return of_mutex_unlock(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_free(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_destroy(s) ? NO : YES);
	return !pthread_spin_destroy(s);
#else
	return of_mutex_free(s);
#endif
}