ObjFW  Check-in [de63989ad8]

Overview
Comment:Implement objc_sync_enter and objc_sync_exit if they're missing.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: de63989ad863014f378504ca340815be4a1b7561121ccf0d9580fa5eb77c2504
User & Date: js on 2009-05-24 19:31:13
Other Links: manifest | tags
Context
2009-05-24
21:09
Fall back to gethostbyname + locking if getaddrinfo is missing. check-in: 488da685e4 user: js tags: trunk
19:31
Implement objc_sync_enter and objc_sync_exit if they're missing. check-in: de63989ad8 user: js tags: trunk
18:04
Always use #include "config.h" instead of #import "config.h". check-in: 94ea6848a8 user: js tags: trunk
Changes

Modified configure.ac from [1c1ce1109b] to [a880605d64].

135
136
137
138
139
140
141


142
143
144
145
146
147
148
AX_CHECK_COMPILER_FLAGS(-fno-constant-cfstrings, [
	NO_CONST_CFSTRINGS="-fno-constant-cfstrings"
	OBJCFLAGS="$OBJCFLAGS -fno-constant-cfstrings"])
AC_SUBST(NO_CONST_CFSTRINGS)

AC_CHECK_HEADER(objc/runtime.h,
	[AC_DEFINE(HAVE_OBJC_RUNTIME_H, 1, [Whether we have objc/runtime.h])])



test x"$GCC" = x"yes" && CFLAGS="$CFLAGS -Werror"
if test x"$GOBJC" = x"yes"; then
	old_OBJCFLAGS="$OBJCFLAGS"
	OBJCFLAGS="$OBJCFLAGS -Werror"

	AC_MSG_CHECKING(whether gcc has bug objc/27438)







>
>







135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
AX_CHECK_COMPILER_FLAGS(-fno-constant-cfstrings, [
	NO_CONST_CFSTRINGS="-fno-constant-cfstrings"
	OBJCFLAGS="$OBJCFLAGS -fno-constant-cfstrings"])
AC_SUBST(NO_CONST_CFSTRINGS)

AC_CHECK_HEADER(objc/runtime.h,
	[AC_DEFINE(HAVE_OBJC_RUNTIME_H, 1, [Whether we have objc/runtime.h])])

AC_CHECK_FUNC(objc_sync_enter,, AC_SUBST(OBJC_SYNC_M, "objc_sync.m"))

test x"$GCC" = x"yes" && CFLAGS="$CFLAGS -Werror"
if test x"$GOBJC" = x"yes"; then
	old_OBJCFLAGS="$OBJCFLAGS"
	OBJCFLAGS="$OBJCFLAGS -Werror"

	AC_MSG_CHECKING(whether gcc has bug objc/27438)

Modified extra.mk.in from [cf1b4ce92e] to [1d1319fdb9].

1

2
3
4
5
6
ASPRINTF_C = @ASPRINTF_C@

OFPLUGIN = @OFPLUGIN@
OFPLUGIN_M = @OFPLUGIN_M@
WS2_LIBS = @WS2_LIBS@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@

>





1
2
3
4
5
6
7
ASPRINTF_C = @ASPRINTF_C@
OBJC_SYNC_M = @OBJC_SYNC_M@
OFPLUGIN = @OFPLUGIN@
OFPLUGIN_M = @OFPLUGIN_M@
WS2_LIBS = @WS2_LIBS@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@

Modified src/Makefile from [df14000352] to [27fadc8012].

25
26
27
28
29
30
31
32
33
34


35
36
37
38
39
40
41
42
       OFString.m		\
       OFTCPSocket.m		\
       OFThread.m		\
       OFURLEncoding.m		\
       OFXMLFactory.m		\
       ${ASPRINTF_C}

INCLUDESTMP = ${SRCS:.c=.h}
INCLUDES = ${INCLUDESTMP:.m=.h}	\
	   OFMacros.h



include ../buildsys.mk

CPPFLAGS += -I..
CFLAGS += ${LIB_CFLAGS}
OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}
LDFLAGS += ${LIB_LDFLAGS}







|
|

>
>








25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
       OFString.m		\
       OFTCPSocket.m		\
       OFThread.m		\
       OFURLEncoding.m		\
       OFXMLFactory.m		\
       ${ASPRINTF_C}

INCLUDESTMP := ${SRCS:.c=.h}
INCLUDES := ${INCLUDESTMP:.m=.h}	\
	   OFMacros.h

SRCS += ${OBJC_SYNC_M}

include ../buildsys.mk

CPPFLAGS += -I..
CFLAGS += ${LIB_CFLAGS}
OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}
LDFLAGS += ${LIB_LDFLAGS}

Added src/objc_sync.m version [531ff2d312].



























































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. 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.
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>

#ifndef _WIN32
#include <pthread.h>
#else
#include <windows.h>
#endif

struct locks_s {
	id		obj;
	size_t		count;
	size_t		recursion;
#ifndef _WIN32
	pthread_t	thread;
	pthread_mutex_t	mutex;
#else
	HANDLE		thread;
	HANDLE		mutex;
#endif
};

static int initialized = 0;
#ifndef _WIN32
static pthread_mutex_t mutex;
#else
static HANDLE mutex;
#endif
static struct locks_s *locks = NULL;
static size_t num_locks = 0;

int
objc_sync_enter(id obj)
{
	size_t i;

	/*
	 * FIXME:
	 * Theoretically, it's possible to encounter a race condition during
	 * initialization.
	 */
	if (!initialized) {
#ifndef _WIN32
		if (pthread_mutex_init(&mutex, NULL))
#else
		if ((mutex = CreateMutex(NULL, FALSE, NULL)) == NULL)
#endif
			return 1;

		initialized = 1;
	}

#ifndef _WIN32
	if (pthread_mutex_lock(&mutex))
#else
	if (WaitForSingleObject(mutex, INFINITE) != WAIT_OBJECT_0)
		return 1;
#endif

	for (i = 0; i < num_locks; i++) {
		if (locks[i].obj == obj) {
#ifndef _WIN32
			if (pthread_equal(pthread_self(), locks[i].thread))
#else
			if (locks[i].thread == GetCurrentThread())
#endif
				locks[i].recursion++;
			else
#ifndef _WIN32
				if (pthread_mutex_lock(&locks[i].mutex)) {
					pthread_mutex_unlock(&mutex);
					return 1;
				}
#else
				if (WaitForSingleObject(locks[i].mutex,
				    INFINITE) != WAIT_OBJECT_0) {
					ReleaseMutex(mutex);
					return 1;
				}
#endif

			locks[i].count++;

#ifndef _WIN32
			if (pthread_mutex_unlock(&locks[i].mutex))
#else
			if (!ReleaseMutex(mutex))
#endif
				return 1;

			return 0;
		}
	}

	if (locks == NULL) {
		if ((locks = malloc(sizeof(struct locks_s))) == NULL) {
#ifndef _WIN32
			pthread_mutex_unlock(&mutex);
#else
			ReleaseMutex(mutex);
#endif
			return 1;
		}
	} else {
		struct locks_s *new_locks;

		if ((new_locks = realloc(locks, (num_locks + 1) *
		    sizeof(struct locks_s))) == NULL) {
#ifndef _WIN32
			pthread_mutex_unlock(&mutex);
#else
			ReleaseMutex(mutex);
#endif
			return 1;
		}

		locks = new_locks;
	}

	locks[num_locks].obj = obj;
	locks[num_locks].count = 1;
	locks[num_locks].recursion = 0;

#ifndef _WIN32
	locks[num_locks].thread = pthread_self();

	if (pthread_mutex_init(&locks[num_locks].mutex, NULL)) {
		pthread_mutex_unlock(&mutex);
		return 1;
	}

	if (pthread_mutex_lock(&locks[num_locks].mutex)) {
		pthread_mutex_unlock(&mutex);
		return 1;
	}
#else
	locks[num_locks].thread = GetCurrentThread();

	if ((locks[num_locks].mutex = CreateMutex(NULL, TRUE, NULL)) == NULL) {
		ReleaseMutex(mutex);
		return 1;
	}
#endif

	num_locks++;

#ifndef _WIN32
	if (pthread_mutex_unlock(&mutex))
#else
	if (!ReleaseMutex(mutex))
#endif
		return 1;

	return 0;
}

int
objc_sync_exit(id obj)
{
	size_t i;

#ifndef _WIN32
	if (pthread_mutex_lock(&mutex))
#else
	if (WaitForSingleObject(mutex, INFINITE) != WAIT_OBJECT_0)
		return 1;
#endif

	for (i = 0; i < num_locks; i++) {
		if (locks[i].obj == obj) {
#ifndef _WIN32
			if (pthread_equal(pthread_self(), locks[i].thread) &&
			    locks[i].recursion) {
				locks[i].recursion--;

				if (pthread_mutex_unlock(&mutex))
					return 1;

				return 0;
			}
#else
			if (locks[i].thread == GetCurrentThread() &&
			    locks[i].recursion) {
				locks[i].recursion--;

				if (!ReleaseMutex(mutex))
					return 1;

				return 0;
			}
#endif

#ifndef _WIN32
			if (pthread_mutex_unlock(&locks[i].mutex)) {
				pthread_mutex_unlock(&mutex);
				return 1;
			}
#else
			if (!ReleaseMutex(locks[i].mutex)) {
				ReleaseMutex(mutex);
				return 1;
			}
#endif

			locks[i].count--;

			if (locks[i].count == 0) {
				struct locks_s *new_locks;

#ifndef _WIN32
				if (pthread_mutex_destroy(&locks[i].mutex)) {
					pthread_mutex_unlock(&mutex);
					return 1;
				}
#else
				if (!CloseHandle(locks[i].mutex)) {
					ReleaseMutex(mutex);
					return 1;
				}
#endif

				num_locks--;
				locks[i] = locks[num_locks];

				if ((new_locks = realloc(locks, (num_locks) *
				    sizeof(struct locks_s))) == NULL) {
#ifndef _WIN32
					pthread_mutex_unlock(&mutex);
#else
					ReleaseMutex(mutex);
#endif
					return 1;
				}

				locks = new_locks;
			}

#ifndef _WIN32
			if (pthread_mutex_unlock(&mutex))
#else
			if (!ReleaseMutex(mutex))
#endif
				return 1;

			return 0;
		}
	}

#ifndef _WIN32
	pthread_mutex_unlock(&mutex);
#else
	ReleaseMutex(mutex);
#endif

	return 1;
}