ObjFW  Check-in [4b03ccfe1f]

Overview
Comment:It's far more likely the lock we search is at the end.
Additionally, add tests for objc_sync_*.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4b03ccfe1fb6adbe92d027937ffaed9d10875c6568b6100082a10a86e9d54191
User & Date: js on 2009-06-01 01:44:23
Other Links: manifest | tags
Context
2009-06-01
01:46
Copy the object for an OFThread so it's thread-safe. check-in: 9b3d408f0d user: js tags: trunk
01:44
It's far more likely the lock we search is at the end.
Additionally, add tests for objc_sync_*.
check-in: 4b03ccfe1f user: js tags: trunk
01:31
Fix recursive locking. check-in: 5bd604a8f8 user: js tags: trunk
Changes

Modified TODO from [dd0b2c72a6] to [7841473511].

1
2
3
4
5
6
7
8
9
10
11
Test if autorelease pool releases everything correctly when thread is ended

Proper UTF-8 support!
Tests for OFArray.
Tests for OFFile.
Tests for OFNumber.
Tests for readLine:.

Serialization

OFQueue



<







1
2
3

4
5
6
7
8
9
10
Test if autorelease pool releases everything correctly when thread is ended

Proper UTF-8 support!

Tests for OFFile.
Tests for OFNumber.
Tests for readLine:.

Serialization

OFQueue

Modified configure.ac from [aedaf8e108] to [b75ed815e1].

156
157
158
159
160
161
162

163
164
165
166
167
168
169
	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")
	AC_DEFINE(NEED_OBJC_SYNC_INIT, 1,
		[Whether objc_sync_init needs to be called])])

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







>







156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
	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, "objc_sync")
	AC_SUBST(OBJC_SYNC_M, "objc_sync.m")
	AC_DEFINE(NEED_OBJC_SYNC_INIT, 1,
		[Whether objc_sync_init needs to be called])])

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

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

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@

>






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

Modified src/objc_sync.m from [469f559547] to [0a8dcabb44].

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
	return (mutex_new(&mutex) ? YES : NO);
}

int
objc_sync_enter(id obj)
{
	size_t i;

	if (obj == nil)
		return 0;

	if (!mutex_lock(&mutex))
		return 1;

	for (i = 0; i < num_locks; i++) {
		if (locks[i].obj == obj) {
			if (thread_is_current(locks[i].thread))
				locks[i].recursion++;
			else {
				/* Make sure objc_sync_exit doesn't free it */
				locks[i].count++;








|







|







127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
	return (mutex_new(&mutex) ? YES : NO);
}

int
objc_sync_enter(id obj)
{
	int i;

	if (obj == nil)
		return 0;

	if (!mutex_lock(&mutex))
		return 1;

	for (i = num_locks - 1; i >= 0; i--) {
		if (locks[i].obj == obj) {
			if (thread_is_current(locks[i].thread))
				locks[i].recursion++;
			else {
				/* Make sure objc_sync_exit doesn't free it */
				locks[i].count++;

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

	return 0;
}

int
objc_sync_exit(id obj)
{
	size_t i;

	if (obj == nil)
		return 0;

	if (!mutex_lock(&mutex))
		return 1;

	for (i = 0; i < num_locks; i++) {
		if (locks[i].obj == obj) {
			if (locks[i].recursion > 0 &&
			    thread_is_current(locks[i].thread)) {
				locks[i].recursion--;

				if (!mutex_unlock(&mutex))
					return 1;







|







|







211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

	return 0;
}

int
objc_sync_exit(id obj)
{
	int i;

	if (obj == nil)
		return 0;

	if (!mutex_lock(&mutex))
		return 1;

	for (i = num_locks - 1; i >= 0; i--) {
		if (locks[i].obj == obj) {
			if (locks[i].recursion > 0 &&
			    thread_is_current(locks[i].thread)) {
				locks[i].recursion--;

				if (!mutex_unlock(&mutex))
					return 1;

Modified tests/Makefile from [d73d2d7d1e] to [d93b2a7df0].

1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
include ../extra.mk

SUBDIRS = OFObject		\
	  OFAutoreleasePool	\
	  OFDataArray		\
	  OFArray		\
	  OFDictionary		\
	  OFHashes		\
	  ${OFPLUGIN}		\
	  OFString		\
	  OFTCPSocket		\
	  OFThread		\
	  OFList		\
	  OFXMLFactory


include ../buildsys.mk













|
>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
include ../extra.mk

SUBDIRS = OFObject		\
	  OFAutoreleasePool	\
	  OFDataArray		\
	  OFArray		\
	  OFDictionary		\
	  OFHashes		\
	  ${OFPLUGIN}		\
	  OFString		\
	  OFTCPSocket		\
	  OFThread		\
	  OFList		\
	  OFXMLFactory		\
	  ${OBJC_SYNC}

include ../buildsys.mk

Added tests/objc_sync/Makefile version [71149fcbb2].



















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
PROG_NOINST = objc_sync${PROG_SUFFIX}
SRCS = objc_sync.m

include ../../buildsys.mk
include ../../extra.mk

CPPFLAGS += -I../../src -I../..
LIBS := -L../../src -lobjfw ${LIBS}

.PHONY: run

all: run
run: ${PROG_NOINST}
	rm -f libobjfw.so.0 libobjfw.so.0.1 libobjfw.dll libobjfw.dylib
	ln -s ../../src/libobjfw.so libobjfw.so.0
	ln -s ../../src/libobjfw.so libobjfw.so.0.1
	if test -f ../../src/libobjfw.dll; then \
		ln ../../src/libobjfw.dll libobjfw.dll; \
	fi
	ln -s ../../src/libobjfw.dylib libobjfw.dylib
	LD_LIBRARY_PATH=.$${LD_LIBRARY_PATH+:}$$LD_LIBRARY_PATH \
	DYLD_LIBRARY_PATH=.$${DYLD_LIBRARY_PATH+:}$$DYLD_LIBRARY_PATH \
	${TEST_LAUNCHER} ./${PROG_NOINST}; EXIT=$$?; \
	rm -f libobjfw.so.0 libobjfw.so.0.1 libobjfw.dll libobjfw.dylib; \
	exit $$EXIT

Added tests/objc_sync/objc_sync.m version [d8413d91c1].

















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#include <stdio.h>

#import "OFString.h"
#import "OFThread.h"

OFObject *lock;

@interface MyThread: OFThread
- main;
@end

@implementation MyThread
- main
{
	printf("[%s] Entering #1\n", [object cString]);
	@synchronized (lock) {
		printf("[%s] Entering #2\n", [object cString]);
		@synchronized (lock) {
			printf("[%s] Hello!\n", [object cString]);
		}
		printf("[%s] Left #2\n", [object cString]);
	}
	printf("[%s] Left #1\n", [object cString]);

	return nil;
}
@end

int
main()
{
	lock = [[OFObject alloc] init];
	MyThread *t1 = [MyThread threadWithObject: @"A"];
	MyThread *t2 = [MyThread threadWithObject: @"B"];

	[t1 join];
	[t2 join];

	return 0;
}