ObjFW  Check-in [0a55edad35]

Overview
Comment:Split OFThread.m into multiple files.

It was time to finally have one file per class.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0a55edad35d3f0287d519173deab50895b008ffc755d31b0fba308462a8944a4
User & Date: js on 2012-10-26 11:04:41
Other Links: manifest | tags
Context
2012-10-26
11:23
Add OFLocking protocol. check-in: 7c33158c22 user: js tags: trunk
11:04
Split OFThread.m into multiple files. check-in: 0a55edad35 user: js tags: trunk
10:16
Add OF_ prefix to a private method missing it. check-in: cbf1901c7f user: js tags: trunk
Changes

Modified configure.ac from [30254b5fb3] to [b6a1472c42].

436
437
438
439
440
441
442

443







444
445
446
447
448
449
450
436
437
438
439
440
441
442
443

444
445
446
447
448
449
450
451
452
453
454
455
456
457







+
-
+
+
+
+
+
+
+







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

	AC_DEFINE(OF_THREADS, 1, [Whether we have threads])
	AC_SUBST(THREADING_SOURCES, "	\
	AC_SUBST(OFTHREAD_M, "OFThread.m")
		OFThread.m		\
		OFThreadPool.m		\
		OFTLSKey.m		\
		OFMutex.m		\
		OFRecursiveMutex.m	\
		OFCondition.m		\
	")
	AC_SUBST(OFTHREADTESTS_M, "OFThreadTests.m")
	AC_SUBST(OFHTTPREQUESTTESTS_M, "OFHTTPRequestTests.m")
	AC_SUBST(THREADING_H, "threading.h")

	AC_MSG_CHECKING(whether __thread works)
	AC_TRY_LINK([
		/* It seems __thread is buggy with GCC 4.1 */

Modified extra.mk.in from [09b4d1b91a] to [5e032b6b91].

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

21
22
23
24
25
26
27

28
29
30
31
32
33
34
35
36
37
38
39
40







-












+
OBJC_SYNC_M = @OBJC_SYNC_M@
OFHTTPREQUESTTESTS_M = @OFHTTPREQUESTTESTS_M@
OFPLUGIN_M = @OFPLUGIN_M@
OFPLUGINTESTS_M = @OFPLUGINTESTS_M@
OFSTREAMOBSERVER_KQUEUE_M = @OFSTREAMOBSERVER_KQUEUE_M@
OFSTREAMOBSERVER_POLL_M = @OFSTREAMOBSERVER_POLL_M@
OFSTREAMOBSERVER_SELECT_M = @OFSTREAMOBSERVER_SELECT_M@
OFTHREAD_M = @OFTHREAD_M@
OFTHREADTESTS_M = @OFTHREADTESTS_M@
PROPERTIESTESTS_M = @PROPERTIESTESTS_M@
REEXPORT_LIBOBJC = @REEXPORT_LIBOBJC@
RUNTIME = @RUNTIME@
RUNTIME_A = @RUNTIME_A@
RUNTIME_RUNTIME_A = @RUNTIME_RUNTIME_A@
RUNTIME_RUNTIME_LIB_A = @RUNTIME_RUNTIME_LIB_A@
RUNTIME_LIB_A = @RUNTIME_LIB_A@
TESTPLUGIN = @TESTPLUGIN@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@
THREADING_H = @THREADING_H@
THREADING_SOURCES = @THREADING_SOURCES@

Modified src/Makefile from [2695ae7f64] to [7a8233cdb8].

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







-
-












+







       OFString+Hashing.m		\
       OFString+JSONValue.m		\
       OFString+Serialization.m		\
       OFString+URLEncoding.m		\
       OFString+XMLEscaping.m		\
       OFString+XMLUnescaping.m		\
       OFTCPSocket.m			\
       ${OFTHREAD_M}			\
       OFThreadPool.m			\
       OFTimer.m			\
       OFURL.m				\
       OFXMLAttribute.m			\
       OFXMLCDATA.m			\
       OFXMLCharacters.m		\
       OFXMLComment.m			\
       OFXMLElement.m			\
       OFXMLElement+Serialization.m	\
       OFXMLElementBuilder.m		\
       OFXMLNode.m			\
       OFXMLParser.m			\
       OFXMLProcessingInstructions.m	\
       ${THREADING_SOURCES}		\
       base64.m				\
       of_asprintf.m			\
       of_strptime.m			\
       unicode.m

INCLUDES := ${SRCS:.m=.h}		\
	    OFCollection.h		\

Added src/OFCondition.h version [1e8d6909b1].



















































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012
 *   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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFMutex.h"

/**
 * \brief A class implementing a condition variable for thread synchronization.
 */
@interface OFCondition: OFMutex
{
	of_condition_t condition;
	BOOL conditionInitialized;
}

/**
 * \brief Creates a new condition.
 *
 * \return A new, autoreleased OFCondition
 */
+ (instancetype)condition;

/**
 * \brief Blocks the current thread until another thread calls -[signal] or
 *	  -[broadcast].
 */
- (void)wait;

/**
 * \brief Signals the next waiting thread to continue.
 */
- (void)signal;

/**
 * \brief Signals all threads to continue.
 */
- (void)broadcast;
@end

Added src/OFCondition.m version [7c6f9d2d69].



















































































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012
 *   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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFCondition.h"

#import "OFConditionBroadcastFailedException.h"
#import "OFConditionSignalFailedException.h"
#import "OFConditionStillWaitingException.h"
#import "OFConditionWaitFailedException.h"
#import "OFInitializationFailedException.h"

@implementation OFCondition
+ (instancetype)condition
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	if (!of_condition_new(&condition)) {
		Class c = [self class];
		[self release];
		@throw [OFInitializationFailedException exceptionWithClass: c];
	}

	conditionInitialized = YES;

	return self;
}

- (void)wait
{
	if (!of_condition_wait(&condition, &mutex))
		@throw [OFConditionWaitFailedException
		    exceptionWithClass: [self class]
			     condition: self];
}

- (void)signal
{
	if (!of_condition_signal(&condition))
		@throw [OFConditionSignalFailedException
		    exceptionWithClass: [self class]
			     condition: self];
}

- (void)broadcast
{
	if (!of_condition_broadcast(&condition))
		@throw [OFConditionBroadcastFailedException
		    exceptionWithClass: [self class]
			     condition: self];
}

- (void)dealloc
{
	if (conditionInitialized)
		if (!of_condition_free(&condition))
			@throw [OFConditionStillWaitingException
			    exceptionWithClass: [self class]
				     condition: self];

	[super dealloc];
}
@end

Added src/OFMutex.h version [a9c6524320].
























































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012
 *   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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"

#import "threading.h"

/**
 * \brief A class for creating mutual exclusions.
 */
@interface OFMutex: OFObject
{
	of_mutex_t mutex;
	BOOL initialized;
}

/**
 * \brief Creates a new mutex.
 *
 * \return A new autoreleased mutex.
 */
+ (instancetype)mutex;

- OF_initWithoutCreatingMutex;

/**
 * \brief Locks the mutex.
 */
- (void)lock;

/**
 * \brief Tries to lock the mutex.
 *
 * \return A boolean whether the mutex could be acquired
 */
- (BOOL)tryLock;

/**
 * \brief Unlocks the mutex.
 */
- (void)unlock;
@end

Added src/OFMutex.m version [3c3018edf4].





















































































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012
 *   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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFMutex.h"

#import "OFInitializationFailedException.h"
#import "OFMutexLockFailedException.h"
#import "OFMutexStillLockedException.h"
#import "OFMutexUnlockFailedException.h"

@implementation OFMutex
+ (instancetype)mutex
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	if (!of_mutex_new(&mutex)) {
		Class c = [self class];
		[self release];
		@throw [OFInitializationFailedException exceptionWithClass: c];
	}

	initialized = YES;

	return self;
}

- OF_initWithoutCreatingMutex
{
	return [super init];
}

- (void)lock
{
	if (!of_mutex_lock(&mutex))
		@throw [OFMutexLockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (BOOL)tryLock
{
	return of_mutex_trylock(&mutex);
}

- (void)unlock
{
	if (!of_mutex_unlock(&mutex))
		@throw [OFMutexUnlockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (void)dealloc
{
	if (initialized)
		if (!of_mutex_free(&mutex))
			@throw [OFMutexStillLockedException
			    exceptionWithClass: [self class]
					 mutex: self];

	[super dealloc];
}
@end

Added src/OFRecursiveMutex.h version [dd0c304280].




























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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012
 *   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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFMutex.h"

/**
 * \brief A class for creating mutual exclusions which can be entered
 *	  recursively.
 */
@interface OFRecursiveMutex: OFMutex
{
	of_rmutex_t rmutex;
}
@end

Added src/OFRecursiveMutex.m version [ef20679bce].










































































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012
 *   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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFRecursiveMutex.h"

#import "OFInitializationFailedException.h"
#import "OFMutexLockFailedException.h"
#import "OFMutexStillLockedException.h"
#import "OFMutexUnlockFailedException.h"

@implementation OFRecursiveMutex
- init
{
	self = [super OF_initWithoutCreatingMutex];

	if (!of_rmutex_new(&rmutex)) {
		Class c = [self class];
		[self release];
		@throw [OFInitializationFailedException exceptionWithClass: c];
	}

	initialized = YES;

	return self;
}

- (void)lock
{
	if (!of_rmutex_lock(&rmutex))
		@throw [OFMutexLockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (BOOL)tryLock
{
	return of_rmutex_trylock(&rmutex);
}

- (void)unlock
{
	if (!of_rmutex_unlock(&rmutex))
		@throw [OFMutexUnlockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (void)dealloc
{
	if (initialized)
		if (!of_rmutex_free(&rmutex))
			@throw [OFMutexStillLockedException
			    exceptionWithClass: [self class]
					 mutex: self];

	[super dealloc];
}
@end

Modified src/OFStreamObserver.m from [44c71d7505] to [f18e106529].

27
28
29
30
31
32
33

34


35
36
37
38
39
40
41
27
28
29
30
31
32
33
34

35
36
37
38
39
40
41
42
43







+
-
+
+







#import "OFArray.h"
#import "OFDictionary.h"
#import "OFStream.h"
#import "OFDataArray.h"
#ifdef _WIN32
# import "OFTCPSocket.h"
#endif
#ifdef OF_THREADS
#import "OFThread.h"
# import "OFMutex.h"
#endif

#ifdef HAVE_KQUEUE
# import "OFStreamObserver_kqueue.h"
#endif
#ifdef HAVE_POLL_H
# import "OFStreamObserver_poll.h"
#endif

Added src/OFTLSKey.h version [3990e8ca34].





























































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012
 *   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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"
#import "OFList.h"

#import "threading.h"

/**
 * \brief A class for Thread Local Storage keys.
 */
@interface OFTLSKey: OFObject
{
@public
	of_tlskey_t key;
@protected
	void (*destructor)(id);
	of_list_object_t *listObject;
	BOOL initialized;
}

/**
 * \brief Creates a new Thread Local Storage key
 *
 * \return A new, autoreleased Thread Local Storage key
 */
+ (instancetype)TLSKey;

/**
 * \brief Creates a new Thread Local Storage key with the specified destructor.
 *
 * \param destructor A destructor that is called when the thread is terminated
 * \return A new autoreleased Thread Local Storage key
 */
+ (instancetype)TLSKeyWithDestructor: (void(*)(id))destructor;

+ (void)OF_callAllDestructors;

/**
 * \brief Initializes an already allocated Thread Local Storage Key with the
 *	  specified destructor.
 *
 * \param destructor A destructor that is called when the thread is terminated
 * \return An initialized Thread Local Storage key
 */
- initWithDestructor: (void(*)(id))destructor;
@end

Added src/OFTLSKey.m version [576ec30af6].










































































































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012
 *   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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFTLSKey.h"

#import "OFInitializationFailedException.h"

static OFList *TLSKeys;

@implementation OFTLSKey
+ (void)initialize
{
	if (self == [OFTLSKey class])
		TLSKeys = [[OFList alloc] init];
}

+ (instancetype)TLSKey
{
	return [[[self alloc] init] autorelease];
}

+ (instancetype)TLSKeyWithDestructor: (void(*)(id))destructor
{
	return [[[self alloc] initWithDestructor: destructor] autorelease];
}

+ (void)OF_callAllDestructors
{
	of_list_object_t *iter;

	@synchronized (TLSKeys) {
		for (iter = [TLSKeys firstListObject]; iter != NULL;
		    iter = iter->next) {
			OFTLSKey *key = (OFTLSKey*)iter->object;

			if (key->destructor != NULL)
				key->destructor(iter->object);
		}
	}
}

- init
{
	self = [super init];

	@try {
		if (!of_tlskey_new(&key))
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];

		initialized = YES;

		@synchronized (TLSKeys) {
			listObject = [TLSKeys appendObject: self];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithDestructor: (void(*)(id))destructor_
{
	self = [self init];

	destructor = destructor_;

	return self;
}

- (void)dealloc
{
	if (destructor != NULL)
		destructor(self);

	if (initialized)
		of_tlskey_free(key);

	/* In case we called [self release] in init */
	if (listObject != NULL) {
		@synchronized (TLSKeys) {
			[TLSKeys removeListObject: listObject];
		}
	}

	[super dealloc];
}
@end

Modified src/OFThread.h from [ac4e14b1b1] to [384da59e46].

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







-
+











-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"
#import "OFList.h"
#import "OFTLSKey.h"

#import "threading.h"

@class OFDate;
@class OFSortedList;
@class OFRunLoop;

#ifdef OF_HAVE_BLOCKS
typedef id (^of_thread_block_t)(id object);
#endif

/**
 * \brief A class for Thread Local Storage keys.
 */
@interface OFTLSKey: OFObject
{
@public
	of_tlskey_t key;
/* Work around a bug in gcc 4.4.4 (possibly only on Haiku) */
#if !defined(__GNUC__) || __GNUC__ != 4 || __GNUC_MINOR__ != 4 || \
    __GNUC_PATCHLEVEL__ != 4
@protected
#endif
	void (*destructor)(id);
	of_list_object_t *listObject;
	BOOL initialized;
}

/**
 * \brief Creates a new Thread Local Storage key
 *
 * \return A new, autoreleased Thread Local Storage key
 */
+ (instancetype)TLSKey;

/**
 * \brief Creates a new Thread Local Storage key with the specified destructor.
 *
 * \param destructor A destructor that is called when the thread is terminated
 * \return A new autoreleased Thread Local Storage key
 */
+ (instancetype)TLSKeyWithDestructor: (void(*)(id))destructor;

+ (void)OF_callAllDestructors;

/**
 * \brief Initializes an already allocated Thread Local Storage Key with the
 *	  specified destructor.
 *
 * \param destructor A destructor that is called when the thread is terminated
 * \return An initialized Thread Local Storage key
 */
- initWithDestructor: (void(*)(id))destructor;
@end

/**
 * \brief A class which provides portable threads.
 *
 * To use it, you should create a new class derived from it and reimplement
 * main.
 *
 * \warning Even though the OFCopying protocol is implemented, it does
256
257
258
259
260
261
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
212
213
214
215
216
217
218




















































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/**
 * \brief Returns the run loop for the thread.
 *
 * \return The run loop for the thread
 */
- (OFRunLoop*)runLoop;
@end

/**
 * \brief A class for creating mutual exclusions.
 */
@interface OFMutex: OFObject
{
	of_mutex_t mutex;
	BOOL initialized;
}

/**
 * \brief Creates a new mutex.
 *
 * \return A new autoreleased mutex.
 */
+ (instancetype)mutex;

/**
 * \brief Locks the mutex.
 */
- (void)lock;

/**
 * \brief Tries to lock the mutex.
 *
 * \return A boolean whether the mutex could be acquired
 */
- (BOOL)tryLock;

/**
 * \brief Unlocks the mutex.
 */
- (void)unlock;
@end

/**
 * \brief A class for creating mutual exclusions which can be entered
 *	  recursively.
 */
@interface OFRecursiveMutex: OFMutex
{
	of_rmutex_t rmutex;
}
@end

/**
 * \brief A class implementing a condition variable for thread synchronization.
 */
@interface OFCondition: OFMutex
{
	of_condition_t condition;
	BOOL conditionInitialized;
}

/**
 * \brief Creates a new condition.
 *
 * \return A new, autoreleased OFCondition
 */
+ (instancetype)condition;

/**
 * \brief Blocks the current thread until another thread calls -[signal] or
 *	  -[broadcast].
 */
- (void)wait;

/**
 * \brief Signals the next waiting thread to continue.
 */
- (void)signal;

/**
 * \brief Signals all threads to continue.
 */
- (void)broadcast;
@end

Modified src/OFThread.m from [dca7dfad37] to [94dbbf6fa0].

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







-
-
-
-


-
-
-










-







#import "OFRunLoop.h"
#import "OFAutoreleasePool.h"

#ifdef _WIN32
# include <windows.h>
#endif

#import "OFConditionBroadcastFailedException.h"
#import "OFConditionSignalFailedException.h"
#import "OFConditionStillWaitingException.h"
#import "OFConditionWaitFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFMutexLockFailedException.h"
#import "OFMutexStillLockedException.h"
#import "OFMutexUnlockFailedException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfRangeException.h"
#import "OFThreadJoinFailedException.h"
#import "OFThreadStartFailedException.h"
#import "OFThreadStillRunningException.h"

#import "atomic.h"
#import "autorelease.h"
#import "threading.h"

static OFList *TLSKeys;
static of_tlskey_t threadSelfKey;
static OFThread *mainThread;

static id
call_main(id object)
{
	OFThread *thread = (OFThread*)object;
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
349
350
351
352
353
354
355
356































































































































































































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
	[super dealloc];
}

- copy
{
	return [self retain];
}
@end

@implementation OFTLSKey
+ (void)initialize
{
	if (self == [OFTLSKey class])
		TLSKeys = [[OFList alloc] init];
}

+ (instancetype)TLSKey
{
	return [[[self alloc] init] autorelease];
}

+ (instancetype)TLSKeyWithDestructor: (void(*)(id))destructor
{
	return [[[self alloc] initWithDestructor: destructor] autorelease];
}

+ (void)OF_callAllDestructors
{
	of_list_object_t *iter;

	@synchronized (TLSKeys) {
		for (iter = [TLSKeys firstListObject]; iter != NULL;
		    iter = iter->next) {
			OFTLSKey *key = (OFTLSKey*)iter->object;

			if (key->destructor != NULL)
				key->destructor(iter->object);
		}
	}
}

- init
{
	self = [super init];

	@try {
		if (!of_tlskey_new(&key))
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];

		initialized = YES;

		@synchronized (TLSKeys) {
			listObject = [TLSKeys appendObject: self];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithDestructor: (void(*)(id))destructor_
{
	self = [self init];

	destructor = destructor_;

	return self;
}

- (void)dealloc
{
	if (destructor != NULL)
		destructor(self);

	if (initialized)
		of_tlskey_free(key);

	/* In case we called [self release] in init */
	if (listObject != NULL) {
		@synchronized (TLSKeys) {
			[TLSKeys removeListObject: listObject];
		}
	}

	[super dealloc];
}
@end

@implementation OFMutex
+ (instancetype)mutex
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	if (!of_mutex_new(&mutex)) {
		Class c = [self class];
		[self release];
		@throw [OFInitializationFailedException exceptionWithClass: c];
	}

	initialized = YES;

	return self;
}

- OF_initWithoutCreatingMutex
{
	return [super init];
}

- (void)lock
{
	if (!of_mutex_lock(&mutex))
		@throw [OFMutexLockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (BOOL)tryLock
{
	return of_mutex_trylock(&mutex);
}

- (void)unlock
{
	if (!of_mutex_unlock(&mutex))
		@throw [OFMutexUnlockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (void)dealloc
{
	if (initialized)
		if (!of_mutex_free(&mutex))
			@throw [OFMutexStillLockedException
			    exceptionWithClass: [self class]
					 mutex: self];

	[super dealloc];
}
@end

@implementation OFRecursiveMutex
- init
{
	self = [super OF_initWithoutCreatingMutex];

	if (!of_rmutex_new(&rmutex)) {
		Class c = [self class];
		[self release];
		@throw [OFInitializationFailedException exceptionWithClass: c];
	}

	initialized = YES;

	return self;
}

- (void)lock
{
	if (!of_rmutex_lock(&rmutex))
		@throw [OFMutexLockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (BOOL)tryLock
{
	return of_rmutex_trylock(&rmutex);
}

- (void)unlock
{
	if (!of_rmutex_unlock(&rmutex))
		@throw [OFMutexUnlockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (void)dealloc
{
	if (initialized)
		if (!of_rmutex_free(&rmutex))
			@throw [OFMutexStillLockedException
			    exceptionWithClass: [self class]
					 mutex: self];

	[super dealloc];
}
@end

@implementation OFCondition
+ (instancetype)condition
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	if (!of_condition_new(&condition)) {
		Class c = [self class];
		[self release];
		@throw [OFInitializationFailedException exceptionWithClass: c];
	}

	conditionInitialized = YES;

	return self;
}

- (void)wait
{
	if (!of_condition_wait(&condition, &mutex))
		@throw [OFConditionWaitFailedException
		    exceptionWithClass: [self class]
			     condition: self];
}

- (void)signal
{
	if (!of_condition_signal(&condition))
		@throw [OFConditionSignalFailedException
		    exceptionWithClass: [self class]
			     condition: self];
}

- (void)broadcast
{
	if (!of_condition_broadcast(&condition))
		@throw [OFConditionBroadcastFailedException
		    exceptionWithClass: [self class]
			     condition: self];
}

- (void)dealloc
{
	if (conditionInitialized)
		if (!of_condition_free(&condition))
			@throw [OFConditionStillWaitingException
			    exceptionWithClass: [self class]
				     condition: self];

	[super dealloc];
}
@end

Modified src/OFThreadPool.m from [ba662533a2] to [5b323e884d].

16
17
18
19
20
21
22

23
24
25
26
27
28
29
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30







+








#include "config.h"

#import "OFThreadPool.h"
#import "OFArray.h"
#import "OFList.h"
#import "OFThread.h"
#import "OFCondition.h"

#import "autorelease.h"

@interface OFThreadPoolJob: OFObject
{
	id target;
	SEL selector;

Modified src/OFTimer.m from [614e54faf3] to [ccda29e08b].

15
16
17
18
19
20
21
22

23
24
25
26
27
28
29
15
16
17
18
19
20
21

22
23
24
25
26
27
28
29







-
+







 */

#include "config.h"

#import "OFTimer.h"
#import "OFDate.h"
#import "OFRunLoop.h"
#import "OFThread.h"
#import "OFCondition.h"

#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"

#import "autorelease.h"
#import "macros.h"

Modified src/ObjFW.h from [2fc0f8109d] to [2df4cec6a1].

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







+
-
-
-
-
+
+
+
+
+


















+
-
-
-
+
+
+
+











+
-
-
-
+
+
+
+
















+


-
+
+
+
+







#import "OFAcceptFailedException.h"
#import "OFAddressTranslationFailedException.h"
#import "OFAlreadyConnectedException.h"
#import "OFBindFailedException.h"
#import "OFChangeDirectoryFailedException.h"
#import "OFChangeFileModeFailedException.h"
#import "OFChangeFileOwnerFailedException.h"
#ifdef OF_THREADS
#import "OFConditionBroadcastFailedException.h"
#import "OFConditionSignalFailedException.h"
#import "OFConditionStillWaitingException.h"
#import "OFConditionWaitFailedException.h"
# import "OFConditionBroadcastFailedException.h"
# import "OFConditionSignalFailedException.h"
# import "OFConditionStillWaitingException.h"
# import "OFConditionWaitFailedException.h"
#endif
#import "OFConnectionFailedException.h"
#import "OFCopyFileFailedException.h"
#import "OFCreateDirectoryFailedException.h"
#import "OFDeleteDirectoryFailedException.h"
#import "OFDeleteFileFailedException.h"
#import "OFEnumerationMutationException.h"
#import "OFHashAlreadyCalculatedException.h"
#import "OFHTTPRequestFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFInvalidJSONException.h"
#import "OFInvalidServerReplyException.h"
#import "OFLinkFailedException.h"
#import "OFListenFailedException.h"
#import "OFMalformedXMLException.h"
#import "OFMemoryNotPartOfObjectException.h"
#ifdef OF_THREADS
#import "OFMutexLockFailedException.h"
#import "OFMutexStillLockedException.h"
#import "OFMutexUnlockFailedException.h"
# import "OFMutexLockFailedException.h"
# import "OFMutexStillLockedException.h"
# import "OFMutexUnlockFailedException.h"
#endif
#import "OFNotConnectedException.h"
#import "OFNotImplementedException.h"
#import "OFOpenFileFailedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFReadOrWriteFailedException.h"
#import "OFRenameFileFailedException.h"
#import "OFSeekFailedException.h"
#import "OFSetOptionFailedException.h"
#import "OFSymlinkFailedException.h"
#ifdef OF_THREADS
#import "OFThreadJoinFailedException.h"
#import "OFThreadStartFailedException.h"
#import "OFThreadStillRunningException.h"
# import "OFThreadJoinFailedException.h"
# import "OFThreadStartFailedException.h"
# import "OFThreadStillRunningException.h"
#endif
#import "OFTruncatedDataException.h"
#import "OFUnboundNamespaceException.h"
#import "OFUnsupportedProtocolException.h"
#import "OFWriteFailedException.h"

#import "macros.h"

#ifdef OF_PLUGINS
# import "OFPlugin.h"
#endif

#ifdef OF_ATOMIC_OPS
# import "atomic.h"
#endif

#ifdef OF_THREADS
# import "threading.h"
# import "OFThread.h"
# import "OFThreadPool.h"
# import "threading.h"
# import "OFTLSKey.h"
# import "OFMutex.h"
# import "OFRecursiveMutex.h"
# import "OFCondition.h"
#endif

#import "autorelease.h"
#import "asprintf.h"
#import "base64.h"
#import "of_asprintf.h"
#import "of_strptime.h"

Modified src/exceptions/OFConditionBroadcastFailedException.m from [faca705211] to [3559216b34].

14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28







-
+







 * file.
 */

#include "config.h"

#import "OFConditionBroadcastFailedException.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFCondition.h"

#import "OFNotImplementedException.h"

@implementation OFConditionBroadcastFailedException
+ (instancetype)exceptionWithClass: (Class)class_
			 condition: (OFCondition*)condition
{

Modified src/exceptions/OFConditionSignalFailedException.m from [eac6850397] to [d23e0ba837].

14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28







-
+







 * file.
 */

#include "config.h"

#import "OFConditionSignalFailedException.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFCondition.h"

#import "OFNotImplementedException.h"

@implementation OFConditionSignalFailedException
+ (instancetype)exceptionWithClass: (Class)class_
			 condition: (OFCondition*)condition
{

Modified src/exceptions/OFConditionStillWaitingException.m from [36d7085370] to [bdcb857f81].

14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28







-
+







 * file.
 */

#include "config.h"

#import "OFConditionStillWaitingException.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFCondition.h"

#import "OFNotImplementedException.h"

@implementation OFConditionStillWaitingException
+ (instancetype)exceptionWithClass: (Class)class_
			 condition: (OFCondition*)condition
{

Modified src/exceptions/OFConditionWaitFailedException.m from [e651da53f2] to [f57be4081a].

14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28







-
+







 * file.
 */

#include "config.h"

#import "OFConditionWaitFailedException.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFCondition.h"

#import "OFNotImplementedException.h"

@implementation OFConditionWaitFailedException
+ (instancetype)exceptionWithClass: (Class)class_
			 condition: (OFCondition*)condition
{

Modified src/exceptions/OFMutexLockFailedException.m from [addab7c263] to [f08d944c22].

14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28







-
+







 * file.
 */

#include "config.h"

#import "OFMutexLockFailedException.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFMutex.h"

#import "OFNotImplementedException.h"

@implementation OFMutexLockFailedException
+ (instancetype)exceptionWithClass: (Class)class_
			     mutex: (OFMutex*)mutex
{

Modified src/exceptions/OFMutexStillLockedException.m from [e4ecfab482] to [b67597220c].

14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28







-
+







 * file.
 */

#include "config.h"

#import "OFMutexStillLockedException.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFMutex.h"

#import "OFNotImplementedException.h"

@implementation OFMutexStillLockedException
+ (instancetype)exceptionWithClass: (Class)class_
			     mutex: (OFMutex*)mutex
{

Modified src/exceptions/OFMutexUnlockFailedException.m from [ada6abbbc1] to [b30cc8b0ab].

14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28







-
+







 * file.
 */

#include "config.h"

#import "OFMutexUnlockFailedException.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFMutex.h"

#import "OFNotImplementedException.h"

@implementation OFMutexUnlockFailedException
+ (instancetype)exceptionWithClass: (Class)class_
			     mutex: (OFMutex*)mutex
{

Modified tests/OFHTTPRequestTests.m from [dca423d859] to [9265df0702].

22
23
24
25
26
27
28

29
30
31
32
33
34
35
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36







+








#include <assert.h>

#import "OFHTTPRequest.h"
#import "OFString.h"
#import "OFTCPSocket.h"
#import "OFThread.h"
#import "OFCondition.h"
#import "OFURL.h"
#import "OFDictionary.h"
#import "OFAutoreleasePool.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFHTTPRequest";