ObjFW  Check-in [d08376bc1f]

Overview
Comment:Introduce OF_HAVE_PTHREADS define instead of #ifndef _WIN32.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d08376bc1fe56d4bf9ba10cc8e683005e73b77c4092d5cc4b722698c2561d537
User & Date: js on 2010-01-29 19:29:29
Other Links: manifest | tags
Context
2010-01-29
19:31
Add OF_ATOMIC_OPS define to work around missing atomic ops if needed. check-in: 953895c116 user: js tags: trunk
19:29
Introduce OF_HAVE_PTHREADS define instead of #ifndef _WIN32. check-in: d08376bc1f user: js tags: trunk
15:22
Add more atomic ops. check-in: 78537d6ff3 user: js tags: trunk
Changes

Modified configure.ac from [1f580f5aef] to [5314e34ab4].

155
156
157
158
159
160
161


162
163
164
165
166
167
168
			AC_MSG_CHECKING(for threads)
			AC_MSG_RESULT(win32)
			;;
		*)
			ACX_PTHREAD([
				CPPLAGS="$CPPFLAGS $PTHREAD_CFLAGS"
				LIBS="$LIBS $PTHREAD_LIBS"


				], [
				AC_MSG_ERROR(No supported threads found!)])
			;;
	esac

	AC_DEFINE(OF_THREADS, 1, [Whether we have threads])
	AC_SUBST(OFTHREAD_M, "OFThread.m")







>
>







155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
			AC_MSG_CHECKING(for threads)
			AC_MSG_RESULT(win32)
			;;
		*)
			ACX_PTHREAD([
				CPPLAGS="$CPPFLAGS $PTHREAD_CFLAGS"
				LIBS="$LIBS $PTHREAD_LIBS"
				AC_DEFINE(OF_HAVE_PTHREADS, 1,
					[Whether we have pthreads])
				], [
				AC_MSG_ERROR(No supported threads found!)])
			;;
	esac

	AC_DEFINE(OF_THREADS, 1, [Whether we have threads])
	AC_SUBST(OFTHREAD_M, "OFThread.m")

Modified src/objfw-defs.h.in from [77d9244302] to [0ee2aeff28].

1
2
3
4
5
6

7
8
9
#undef OF_APPLE_RUNTIME
#undef OF_BIG_ENDIAN
#undef OF_GNU_RUNTIME
#undef OF_HAVE_ASPRINTF
#undef OF_HAVE_GCC_ATOMIC_OPS
#undef OF_HAVE_LIBKERN_OSATOMIC_H

#undef OF_PLUGINS
#undef OF_THREADS
#undef SIZE_MAX






>



1
2
3
4
5
6
7
8
9
10
#undef OF_APPLE_RUNTIME
#undef OF_BIG_ENDIAN
#undef OF_GNU_RUNTIME
#undef OF_HAVE_ASPRINTF
#undef OF_HAVE_GCC_ATOMIC_OPS
#undef OF_HAVE_LIBKERN_OSATOMIC_H
#undef OF_HAVE_PTHREADS
#undef OF_PLUGINS
#undef OF_THREADS
#undef SIZE_MAX

Modified src/threading.h from [0ccf73f4cd] to [d9ecd3df92].

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
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFMacros.h"




#ifndef _WIN32

#include <pthread.h>
typedef pthread_t of_thread_t;
typedef pthread_mutex_t of_mutex_t;
typedef pthread_key_t of_tlskey_t;
#else
#include <windows.h>
typedef HANDLE of_thread_t;
typedef CRITICAL_SECTION of_mutex_t;
typedef DWORD of_tlskey_t;
#endif

#ifndef _WIN32
# define of_thread_is_current(t) pthread_equal(t, pthread_self())
# define of_thread_current() pthread_self()
#else
# define of_thread_is_current(t) (t == GetCurrentThread())
# define of_thread_current() GetCurrentThread()
#endif

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

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

static OF_INLINE BOOL
of_thread_join(of_thread_t thread)
{
#ifndef _WIN32
	void *ret;

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

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

	CloseHandle(thread);

	return YES;
#endif
}

static OF_INLINE BOOL
of_thread_cancel(of_thread_t thread)
{
#ifndef _WIN32
	return (pthread_cancel(thread) ? NO : YES);
#else
	if (thread != INVALID_HANDLE_VALUE) {
		TerminateThread(thread, 1);
		CloseHandle(thread);
	}

	return YES;
#endif
}

static OF_INLINE BOOL
of_mutex_new(of_mutex_t *mutex)
{
#ifndef _WIN32
	return (pthread_mutex_init(mutex, NULL) ? NO : YES);
#else
	InitializeCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_mutex_free(of_mutex_t *mutex)
{
#ifndef _WIN32
	return (pthread_mutex_destroy(mutex) ? NO : YES);
#else
	DeleteCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_mutex_lock(of_mutex_t *mutex)
{
#ifndef _WIN32
	return (pthread_mutex_lock(mutex) ? NO : YES);
#else
	EnterCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_mutex_unlock(of_mutex_t *mutex)
{
#ifndef _WIN32
	return (pthread_mutex_unlock(mutex) ? NO : YES);
#else
	LeaveCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_tlskey_new(of_tlskey_t *key)
{
#ifndef _WIN32
	return (pthread_key_create(key, NULL) ? NO : YES);
#else
	return ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES ? NO : YES);
#endif
}

static OF_INLINE id
of_tlskey_get(of_tlskey_t key)
{
#ifndef _WIN32
	void *ret = pthread_getspecific(key);
#else
	void *ret = TlsGetValue(key);
#endif

	/* NULL and nil might be different! */
	if (ret == NULL)
		return nil;

	return (id)ret;
}

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

#ifndef _WIN32
	return (pthread_setspecific(key, p) ? NO : YES);
#else
	return (TlsSetValue(key, p) ? YES : NO);
#endif
}

static OF_INLINE BOOL
of_tlskey_free(of_tlskey_t key)
{
#ifndef _WIN32
	return (pthread_key_delete(key) ? NO : YES);
#else
	return (TlsFree(key) ? YES : NO);
#endif
}













>
>
>
|
>




|






|


|







|


|










|






|












|

|












|

|








|

|








|

|








|

|








|

|







|

|















|

|







|

|



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
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFMacros.h"

#if !defined(OF_THREADS) || (!defined(OF_HAVE_PTHREADS) && defined(_WIN32))
# error No threads available!
#endif

#if defined(OF_HAVE_PTHREADS)
#include <pthread.h>
typedef pthread_t of_thread_t;
typedef pthread_mutex_t of_mutex_t;
typedef pthread_key_t of_tlskey_t;
#elif defined(_WIN32)
#include <windows.h>
typedef HANDLE of_thread_t;
typedef CRITICAL_SECTION of_mutex_t;
typedef DWORD of_tlskey_t;
#endif

#if defined(OF_HAVE_PTHREADS)
# define of_thread_is_current(t) pthread_equal(t, pthread_self())
# define of_thread_current() pthread_self()
#elif defined(_WIN32)
# define of_thread_is_current(t) (t == GetCurrentThread())
# 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);
#elif defined(_WIN32)
	*thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)main,
	    (void*)data, 0, NULL);

	return (thread == NULL ? NO : YES);
#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);
#elif defined(_WIN32)
	if (WaitForSingleObject(thread, INFINITE))
		return NO;

	CloseHandle(thread);

	return YES;
#endif
}

static OF_INLINE BOOL
of_thread_cancel(of_thread_t thread)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_cancel(thread) ? NO : YES);
#elif defined(_WIN32)
	if (thread != INVALID_HANDLE_VALUE) {
		TerminateThread(thread, 1);
		CloseHandle(thread);
	}

	return YES;
#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);
#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);
#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);
#elif defined(_WIN32)
	EnterCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_mutex_unlock(of_mutex_t *mutex)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_mutex_unlock(mutex) ? NO : YES);
#elif defined(_WIN32)
	LeaveCriticalSection(mutex);
	return YES;
#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);
#elif defined(_WIN32)
	return ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES ? NO : YES);
#endif
}

static OF_INLINE id
of_tlskey_get(of_tlskey_t key)
{
#if defined(OF_HAVE_PTHREADS)
	void *ret = pthread_getspecific(key);
#elif defined(_WIN32)
	void *ret = TlsGetValue(key);
#endif

	/* NULL and nil might be different! */
	if (ret == NULL)
		return nil;

	return (id)ret;
}

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);
#elif defined(_WIN32)
	return (TlsSetValue(key, p) ? YES : NO);
#endif
}

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