ObjFW  Check-in [b5e6a49dca]

Overview
Comment:Implement conditions for win32.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b5e6a49dca4a59d5f92d9b9b1996902ec78915a92e14cf8109b07332a03fb8c3
User & Date: js on 2011-03-11 12:15:18
Other Links: manifest | tags
Context
2011-03-11
12:29
Create an autorelease pool before calling blocks. check-in: 44f8152a03 user: js tags: trunk
12:15
Implement conditions for win32. check-in: b5e6a49dca user: js tags: trunk
2011-03-10
20:34
Improve namespace handling. check-in: a95ca84f7e user: js tags: trunk
Changes

Modified src/threading.h from [0b981f818f] to [33ef5dc30b].

29
30
31
32
33
34
35




36
37
38
39
40
41
42
typedef pthread_mutex_t of_mutex_t;
typedef pthread_cond_t of_condition_t;
#elif defined(_WIN32)
# include <windows.h>
typedef HANDLE of_thread_t;
typedef DWORD of_tlskey_t;
typedef CRITICAL_SECTION of_mutex_t;




#endif

#if defined(OF_ATOMIC_OPS)
# import "atomic.h"
typedef volatile int of_spinlock_t;
# define OF_SPINCOUNT 10
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)







>
>
>
>







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
typedef pthread_mutex_t of_mutex_t;
typedef pthread_cond_t of_condition_t;
#elif defined(_WIN32)
# include <windows.h>
typedef HANDLE of_thread_t;
typedef DWORD of_tlskey_t;
typedef CRITICAL_SECTION of_mutex_t;
typedef struct {
	HANDLE event;
	int count;
} of_condition_t;
#endif

#if defined(OF_ATOMIC_OPS)
# import "atomic.h"
typedef volatile int of_spinlock_t;
# define OF_SPINCOUNT 10
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
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
196
197
198
199


200

201
202
203
204
205
206
207

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

	// XXX




#endif
}

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


	// XXX













#endif
}

static OF_INLINE BOOL
of_condition_signal(of_condition_t *condition)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_cond_signal(condition) ? NO : YES);
#elif defined(_WIN32)
	// XXX
#endif
}

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

	// XXX





#endif
}

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


	// XXX

#endif
}

static OF_INLINE BOOL
of_tlskey_new(of_tlskey_t *key)
{
#if defined(OF_HAVE_PTHREADS)







>
|
>
>
>
>









>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>









|









>
|
>
>
>
>
>









>
>
|
>







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

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

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

	return YES;
#endif
}

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

	of_atomic_inc_int(&condition->count);

	if (WaitForSingleObject(condition->event, INFINITE) != WAIT_OBJECT_0) {
		of_mutex_lock(mutex);
		return NO;
	}

	of_atomic_dec_int(&condition->count);

	if (!of_mutex_lock(mutex))
		return NO;

	return YES;
#endif
}

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

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

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

	return YES;
#endif
}

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

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

static OF_INLINE BOOL
of_tlskey_new(of_tlskey_t *key)
{
#if defined(OF_HAVE_PTHREADS)