Overview
Comment: | Make sure +[OFThread sleep*] is always available.
This means that even if --disable-threads has been specified, it is |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
71d45a29d1d06423237a8c1fbfa70df4 |
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 | OFHTTPServer.m \ OFStreamObserver.m \ OFStreamSocket.m \ OFTCPSocket.m SRCS_THREADS = OFCondition.m \ OFMutex.m \ OFRecursiveMutex.m \ | < | 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 | #include <assert.h> #import "OFRunLoop.h" #import "OFDictionary.h" #ifdef OF_HAVE_SOCKETS # import "OFStreamObserver.h" #endif #ifdef OF_HAVE_THREADS | > < | 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 | * 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" | > | | > | | > > | | | | | | | | | | | | 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 | * 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; enum { OF_THREAD_NOT_RUNNING, OF_THREAD_RUNNING, OF_THREAD_WAITING_FOR_JOIN } _running; # ifdef OF_HAVE_BLOCKS of_thread_block_t _block; # endif id _returnValue; OFRunLoop *_runLoop; OFString *_name; } # ifdef OF_HAVE_PROPERTIES # ifdef OF_HAVE_BLOCKS @property (copy) of_thread_block_t block; # endif @property (copy) OFString *name; # endif /*! * @brief Creates a new thread. * * @return A new, autoreleased thread */ + (instancetype)thread; # ifdef OF_HAVE_BLOCKS /*! * @brief Creates a new thread with the specified block. * * @param block A block which is executed by the thread * @return A new, autoreleased thread */ + (instancetype)threadWithBlock: (of_thread_block_t)block; # endif /*! * @brief Sets the Thread Local Storage for the specified key. * * The specified object is first retained and then the object stored before is * released. You can specify nil as object if you want the old object to be * released and don't want any new object for the TLS key. |
︙ | ︙ | |||
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | /*! * @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; /*! | > | > | | | 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 | /*! * @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. * * @param object The object which the terminated thread will return */ + (void)terminateWithObject: (id)object; + (void)OF_createMainThread; # ifdef OF_HAVE_BLOCKS /*! * @brief Initializes an already allocated thread with the specified block. * * @param block A block which is executed by the thread * @return An initialized OFThread. */ - initWithBlock: (of_thread_block_t)block; # endif /*! * @brief The main routine of the thread. You need to reimplement this! * * It can access the object passed to the threadWithObject or initWithObject * method using the instance variable named 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 | #include "config.h" #define OF_THREAD_M #define __NO_EXT_QNX #include <math.h> #ifndef _WIN32 # include <unistd.h> # include <sched.h> #endif | > > > > | > > | | | > > > | | | | | | | > > | | | | | 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 | #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) { OFThread *thread = (OFThread*)object; if (!of_tlskey_set(threadSelfKey, thread)) @throw [OFInitializationFailedException exceptionWithClass: [thread class]]; objc_autoreleasePoolPush(); /* * Nasty workaround for thread implementations which can't return a * value on join. */ # ifdef OF_HAVE_BLOCKS if (thread->_block != NULL) thread->_returnValue = [thread->_block() retain]; else # endif thread->_returnValue = [[thread main] retain]; [thread handleTermination]; thread->_running = OF_THREAD_WAITING_FOR_JOIN; [OFTLSKey OF_callAllDestructors]; # ifdef OF_OBJFW_RUNTIME /* * As the values returned by objc_autoreleasePoolPush() in the ObjFW * runtime are not actually pointers, but sequential numbers, 0 means * we pop everything. */ objc_autoreleasePoolPop(0); # endif [thread release]; return 0; } static void set_thread_name(OFThread *thread) { # ifdef __HAIKU__ OFString *name = thread->_name; 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]) return; if (!of_tlskey_new(&threadSelfKey)) @throw [OFInitializationFailedException exceptionWithClass: self]; } + (instancetype)thread { return [[[self alloc] init] autorelease]; } # ifdef OF_HAVE_BLOCKS + (instancetype)threadWithBlock: (of_thread_block_t)block { return [[[self alloc] initWithBlock: block] autorelease]; } # endif + (void)setObject: (id)object forTLSKey: (OFTLSKey*)key { id oldObject = of_tlskey_get(key->_key); if (!of_tlskey_set(key->_key, [object retain])) |
︙ | ︙ | |||
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 223 224 225 226 227 228 229 230 231 232 233 | #ifdef OF_HAVE_SCHED_YIELD sched_yield(); #else [self sleepForTimeInterval: 0]; #endif } + (void)terminate { [self terminateWithObject: nil]; } + (void)terminateWithObject: (id)object { OFThread *thread = of_tlskey_get(threadSelfKey); if (thread != nil) { thread->_returnValue = [object retain]; [thread handleTermination]; thread->_running = OF_THREAD_WAITING_FOR_JOIN; } [OFTLSKey OF_callAllDestructors]; | > | | | | | 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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | #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 { OFThread *thread = of_tlskey_get(threadSelfKey); if (thread != nil) { thread->_returnValue = [object retain]; [thread handleTermination]; thread->_running = OF_THREAD_WAITING_FOR_JOIN; } [OFTLSKey OF_callAllDestructors]; # ifdef OF_OBJFW_RUNTIME /* * As the values returned by objc_autoreleasePoolPush() in the ObjFW * runtime are not actually pointers, but sequential numbers, 0 means * we pop everything. */ objc_autoreleasePoolPop(0); # endif [thread release]; of_thread_exit(); } + (void)OF_createMainThread { mainThread = [[OFThread alloc] init]; mainThread->_thread = of_thread_current(); if (!of_tlskey_set(threadSelfKey, mainThread)) @throw [OFInitializationFailedException exceptionWithClass: self]; } # ifdef OF_HAVE_BLOCKS - initWithBlock: (of_thread_block_t)block { self = [super init]; @try { _block = [block copy]; } @catch (id e) { [self release]; @throw e; } return self; } # endif - (id)main { [[OFRunLoop currentRunLoop] run]; return nil; } |
︙ | ︙ | |||
326 327 328 329 330 331 332 | - copy { return [self retain]; } - (OFRunLoop*)runLoop { | | | | | 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | - copy { return [self retain]; } - (OFRunLoop*)runLoop { # ifdef OF_HAVE_ATOMIC_OPS if (_runLoop == nil) { OFRunLoop *tmp = [[OFRunLoop alloc] init]; if (!of_atomic_cmpswap_ptr((void**)&_runLoop, nil, tmp)) [tmp release]; } # else @synchronized (self) { if (_runLoop == nil) _runLoop = [[OFRunLoop alloc] init]; } # endif return [[_runLoop retain] autorelease]; } - (OFString*)name { OF_GETTER(_name, true) |
︙ | ︙ | |||
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 | #endif #ifdef OF_HAVE_ATOMIC_OPS # import "atomic.h" #endif #import "OFLocking.h" #ifdef OF_HAVE_THREADS # import "threading.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" |