ObjFW  Check-in [71d45a29d1]

Overview
Comment:Make sure +[OFThread sleep*] is always available.

This means that even if --disable-threads has been specified, it is
possible to use +[OFThread sleep*] to sleep the current (and only)
thread.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 71d45a29d1d06423237a8c1fbfa70df4ae60c82eb5dbeca240b1f3d6d3d17488
User & Date: js on 2013-04-26 20:40:43
Other Links: manifest | tags
Context
2013-04-26
21:10
Use nanosleep() if available. check-in: db70226153 user: js tags: trunk
20:40
Make sure +[OFThread sleep*] is always available. check-in: 71d45a29d1 user: js tags: trunk
20:18
Only build OFProcess if available. check-in: 9965b1550e user: js tags: trunk
Changes

Modified src/Makefile from [fe3d6ea0fc] to [7ad86b5b2c].

44
45
46
47
48
49
50

51
52
53
54
55
56
57
       OFString+Hashing.m		\
       OFString+JSONValue.m		\
       OFString+Serialization.m		\
       OFString+URLEncoding.m		\
       OFString+XMLEscaping.m		\
       OFString+XMLUnescaping.m		\
       OFSystemInfo.m			\

       OFTimer.m			\
       OFURL.m				\
       OFXMLAttribute.m			\
       OFXMLCDATA.m			\
       OFXMLCharacters.m		\
       OFXMLComment.m			\
       OFXMLElement.m			\







>







44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
       OFString+Hashing.m		\
       OFString+JSONValue.m		\
       OFString+Serialization.m		\
       OFString+URLEncoding.m		\
       OFString+XMLEscaping.m		\
       OFString+XMLUnescaping.m		\
       OFSystemInfo.m			\
       OFThread.m			\
       OFTimer.m			\
       OFURL.m				\
       OFXMLAttribute.m			\
       OFXMLCDATA.m			\
       OFXMLCharacters.m		\
       OFXMLComment.m			\
       OFXMLElement.m			\
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
	       OFHTTPServer.m		\
	       OFStreamObserver.m	\
	       OFStreamSocket.m		\
	       OFTCPSocket.m
SRCS_THREADS = OFCondition.m		\
	       OFMutex.m		\
	       OFRecursiveMutex.m	\
	       OFThread.m		\
	       OFThreadPool.m		\
	       OFTLSKey.m

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







<







75
76
77
78
79
80
81

82
83
84
85
86
87
88
	       OFHTTPServer.m		\
	       OFStreamObserver.m	\
	       OFStreamSocket.m		\
	       OFTCPSocket.m
SRCS_THREADS = OFCondition.m		\
	       OFMutex.m		\
	       OFRecursiveMutex.m	\

	       OFThreadPool.m		\
	       OFTLSKey.m

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

Modified src/OFRunLoop.m from [e463b5f0e9] to [76c781112c].

19
20
21
22
23
24
25

26
27
28
29
30
31
32
33
34
#include <assert.h>

#import "OFRunLoop.h"
#import "OFDictionary.h"
#ifdef OF_HAVE_SOCKETS
# import "OFStreamObserver.h"
#endif

#ifdef OF_HAVE_THREADS
# import "OFThread.h"
# import "OFMutex.h"
#endif
#import "OFSortedList.h"
#import "OFTimer.h"
#import "OFDate.h"

#import "autorelease.h"







>

<







19
20
21
22
23
24
25
26
27

28
29
30
31
32
33
34
#include <assert.h>

#import "OFRunLoop.h"
#import "OFDictionary.h"
#ifdef OF_HAVE_SOCKETS
# import "OFStreamObserver.h"
#endif
#import "OFThread.h"
#ifdef OF_HAVE_THREADS

# import "OFMutex.h"
#endif
#import "OFSortedList.h"
#import "OFTimer.h"
#import "OFDate.h"

#import "autorelease.h"

Modified src/OFThread.h from [b48c7d5a0b] to [b4c8773e10].

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
 * 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 "OFTLSKey.h"

#import "threading.h"


/* Haiku used to define this for some unknown reason which causes trouble */
#ifdef protected
# undef protected
#endif

/*! @file */

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

#ifdef OF_HAVE_BLOCKS
/*!
 * @brief A block to be executed in a new thread.
 *
 * @return The object which should be returned when the thread is joined
 */
typedef id (^of_thread_block_t)(void);
#endif

/*!
 * @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 *not*
 *	    return an independent copy of the thread, but instead retains it.
 *	    This is so that the thread can be used as a key for a dictionary,
 *	    so context can be associated with a thread.
 */
@interface OFThread: OFObject <OFCopying>


{
#ifdef OF_THREAD_M
@public
#else
@private
#endif
	of_thread_t _thread;







>



>












|



















|
>
>







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
 * 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"
#ifdef OF_HAVE_THREADS
#import "OFTLSKey.h"

#import "threading.h"
#endif

/* Haiku used to define this for some unknown reason which causes trouble */
#ifdef protected
# undef protected
#endif

/*! @file */

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

#if defined(OF_HAVE_THREADS) && defined(OF_HAVE_BLOCKS)
/*!
 * @brief A block to be executed in a new thread.
 *
 * @return The object which should be returned when the thread is joined
 */
typedef id (^of_thread_block_t)(void);
#endif

/*!
 * @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 *not*
 *	    return an independent copy of the thread, but instead retains it.
 *	    This is so that the thread can be used as a key for a dictionary,
 *	    so context can be associated with a thread.
 */
@interface OFThread: OFObject
#ifdef OF_HAVE_THREADS
    <OFCopying>
{
#ifdef OF_THREAD_M
@public
#else
@private
#endif
	of_thread_t _thread;
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

/*!
 * @brief Returns the main thread.
 *
 * @return The main thread
 */
+ (OFThread*)mainThread;


/*!
 * @brief Suspends execution of the current thread for the specified time
 *	  interval.
 *
 * @param seconds The number of seconds to sleep
 */
+ (void)sleepForTimeInterval: (double)seconds;

/*!
 * @brief Suspends execution of the current thread until the specified date.
 *
 * @param date The date to wait for
 */
+ (void)sleepUntilDate: (OFDate*)date;

/*!
 * @brief Yields a processor voluntarily and moves the thread at the end of the
 *	  queue for its priority.
 */
+ (void)yield;


/*!
 * @brief Terminates the current thread, letting it return nil.
 */
+ (void)terminate;

/*!
 * @brief Terminates the current thread, letting it return the specified object.







>

















|




>







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

/*!
 * @brief Returns the main thread.
 *
 * @return The main thread
 */
+ (OFThread*)mainThread;
#endif

/*!
 * @brief Suspends execution of the current thread for the specified time
 *	  interval.
 *
 * @param seconds The number of seconds to sleep
 */
+ (void)sleepForTimeInterval: (double)seconds;

/*!
 * @brief Suspends execution of the current thread until the specified date.
 *
 * @param date The date to wait for
 */
+ (void)sleepUntilDate: (OFDate*)date;

/*!
 * @brief Yields a processor voluntarily and moves the thread to the end of the
 *	  queue for its priority.
 */
+ (void)yield;

#ifdef OF_HAVE_THREADS
/*!
 * @brief Terminates the current thread, letting it return nil.
 */
+ (void)terminate;

/*!
 * @brief Terminates the current thread, letting it return the specified object.
223
224
225
226
227
228
229

230

/*!
 * @brief Sets the name for the thread.
 *
 * @param name The name for the thread
 */
- (void)setName: (OFString*)name;

@end







>

229
230
231
232
233
234
235
236
237

/*!
 * @brief Sets the name for the thread.
 *
 * @param name The name for the thread
 */
- (void)setName: (OFString*)name;
#endif
@end

Modified src/OFThread.m from [ffbd3cc3a9] to [426c027f93].

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

#include "config.h"

#define OF_THREAD_M

#define __NO_EXT_QNX


#include <math.h>

#ifndef _WIN32
# include <unistd.h>



# include <sched.h>
#endif

#ifdef __HAIKU__
# include <kernel/OS.h>
#endif

#import "OFRunLoop.h"
#import "OFThread.h"
#import "OFList.h"
#import "OFDate.h"
#import "OFSortedList.h"
#import "OFAutoreleasePool.h"

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

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"

#import "OFOutOfRangeException.h"

#import "OFThreadJoinFailedException.h"
#import "OFThreadStartFailedException.h"
#import "OFThreadStillRunningException.h"


#ifdef OF_HAVE_ATOMIC_OPS
# import "atomic.h"
#endif
#import "autorelease.h"


#import "threading.h"

static of_tlskey_t threadSelfKey;
static OFThread *mainThread;

static id
call_main(id object)







>




>
>
>



|
















>

>



>





>
>







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

#include "config.h"

#define OF_THREAD_M

#define __NO_EXT_QNX

#include <stdlib.h>
#include <math.h>

#ifndef _WIN32
# include <unistd.h>
#endif

#ifdef OF_HAVE_SCHED_YIELD
# include <sched.h>
#endif

#if defined(OF_HAVE_THREADS) && defined(__HAIKU__)
# include <kernel/OS.h>
#endif

#import "OFRunLoop.h"
#import "OFThread.h"
#import "OFList.h"
#import "OFDate.h"
#import "OFSortedList.h"
#import "OFAutoreleasePool.h"

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

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfRangeException.h"
#ifdef OF_HAVE_THREADS
#import "OFThreadJoinFailedException.h"
#import "OFThreadStartFailedException.h"
#import "OFThreadStillRunningException.h"
#endif

#ifdef OF_HAVE_ATOMIC_OPS
# import "atomic.h"
#endif
#import "autorelease.h"

#ifdef OF_HAVE_THREADS
#import "threading.h"

static of_tlskey_t threadSelfKey;
static OFThread *mainThread;

static id
call_main(id object)
108
109
110
111
112
113
114

115
116

117
118
119
120
121
122
123
	if (name == nil)
		name = [thread className];

	rename_thread(get_pthread_thread_id(thread->_thread),
	    [name UTF8String]);
#endif
}


@implementation OFThread

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
@synthesize block = _block;
#endif

+ (void)initialize
{
	if (self != [OFThread class])







>


>







117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	if (name == nil)
		name = [thread className];

	rename_thread(get_pthread_thread_id(thread->_thread),
	    [name UTF8String]);
#endif
}
#endif

@implementation OFThread
#ifdef OF_HAVE_THREADS
#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
@synthesize block = _block;
#endif

+ (void)initialize
{
	if (self != [OFThread class])
162
163
164
165
166
167
168

169
170
171
172
173
174
175
	return [[(id)of_tlskey_get(threadSelfKey) retain] autorelease];
}

+ (OFThread*)mainThread
{
	return mainThread;
}


+ (void)sleepForTimeInterval: (double)seconds
{
	if (seconds < 0)
		@throw [OFOutOfRangeException exceptionWithClass: self];

#ifndef _WIN32







>







173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
	return [[(id)of_tlskey_get(threadSelfKey) retain] autorelease];
}

+ (OFThread*)mainThread
{
	return mainThread;
}
#endif

+ (void)sleepForTimeInterval: (double)seconds
{
	if (seconds < 0)
		@throw [OFOutOfRangeException exceptionWithClass: self];

#ifndef _WIN32
209
210
211
212
213
214
215

216
217
218
219
220
221
222
#ifdef OF_HAVE_SCHED_YIELD
	sched_yield();
#else
	[self sleepForTimeInterval: 0];
#endif
}


+ (void)terminate
{
	[self terminateWithObject: nil];
}

+ (void)terminateWithObject: (id)object
{







>







221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifdef OF_HAVE_SCHED_YIELD
	sched_yield();
#else
	[self sleepForTimeInterval: 0];
#endif
}

#ifdef OF_HAVE_THREADS
+ (void)terminate
{
	[self terminateWithObject: nil];
}

+ (void)terminateWithObject: (id)object
{
375
376
377
378
379
380
381













382
		of_thread_detach(_thread);

	[_returnValue release];
	[_runLoop release];

	[super dealloc];
}













@end







>
>
>
>
>
>
>
>
>
>
>
>
>

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
		of_thread_detach(_thread);

	[_returnValue release];
	[_runLoop release];

	[super dealloc];
}
#else
- init
{
	@try {
		[self doesNotRecognizeSelector: _cmd];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	abort();
}
#endif
@end

Modified src/ObjFW.h from [459089c732] to [58cac27b30].

158
159
160
161
162
163
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#endif

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

#import "OFLocking.h"

#ifdef OF_HAVE_THREADS
# import "threading.h"
# import "OFThread.h"
# import "OFThreadPool.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"







>


<












158
159
160
161
162
163
164
165
166
167

168
169
170
171
172
173
174
175
176
177
178
179
#endif

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

#import "OFLocking.h"
#import "OFThread.h"
#ifdef OF_HAVE_THREADS
# import "threading.h"

# import "OFThreadPool.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"