ObjFW
OFThread.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #import "OFObject.h"
18 
19 #ifdef OF_HAVE_THREADS
20 # import "threading.h"
21 #endif
22 
23 OF_ASSUME_NONNULL_BEGIN
24 
27 @class OFDate;
28 @class OFRunLoop;
29 @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
30 
31 #if defined(OF_HAVE_THREADS) && defined(OF_HAVE_BLOCKS)
32 
37 typedef id _Nullable (^of_thread_block_t)(void);
38 #endif
39 
53 @interface OFThread: OFObject
54 #ifdef OF_HAVE_THREADS
55  <OFCopying>
56 {
57 # ifdef OF_THREAD_M
58 @public
59 # else
60 @private
61 # endif
62  of_thread_t _thread;
63  of_thread_attr_t _attr;
64  enum of_thread_running {
65  OF_THREAD_NOT_RUNNING,
66  OF_THREAD_RUNNING,
67  OF_THREAD_WAITING_FOR_JOIN
68  } _running;
69  void *_pool;
70 # ifdef OF_HAVE_BLOCKS
71  of_thread_block_t _Nullable _threadBlock;
72 # endif
73  id _returnValue;
74  OFRunLoop *_Nullable _runLoop;
75  OFMutableDictionary *_threadDictionary;
76 @private
77  OFString *_Nullable _name;
78 }
79 
83 @property OF_NULLABLE_PROPERTY (copy, nonatomic) OFString *name;
84 
85 # ifdef OF_HAVE_BLOCKS
86 
89 @property OF_NULLABLE_PROPERTY (readonly, nonatomic)
90  of_thread_block_t threadBlock;
91 # endif
92 
98 + (instancetype)thread;
99 
100 # ifdef OF_HAVE_BLOCKS
101 
107 + (instancetype)threadWithThreadBlock: (of_thread_block_t)threadBlock;
108 # endif
109 
116 
122 + (OFThread *)mainThread;
123 
131 #endif
132 
139 + (void)sleepForTimeInterval: (of_time_interval_t)timeInterval;
140 
146 + (void)sleepUntilDate: (OFDate *)date;
147 
152 + (void)yield;
153 
154 #ifdef OF_HAVE_THREADS
155 
158 + (void)terminate OF_NO_RETURN;
159 
165 + (void)terminateWithObject: (nullable id)object OF_NO_RETURN;
166 
167 # ifdef OF_HAVE_BLOCKS
168 
174 - initWithThreadBlock: (of_thread_block_t)threadBlock;
175 # endif
176 
182 - (nullable id)main;
183 
190 - (void)handleTermination OF_REQUIRES_SUPER;
191 
195 - (void)start;
196 
202 - (id)join;
203 
209 - (OFRunLoop *)runLoop;
210 
220 - (float)priority;
221 
233 - (void)setPriority: (float)priority;
234 
240 - (size_t)stackSize;
241 
249 - (void)setStackSize: (size_t)stackSize;
250 #else
251 - init OF_UNAVAILABLE;
252 #endif
253 @end
254 
255 OF_ASSUME_NONNULL_END
OFRunLoop * runLoop()
Returns the run loop for the thread.
Definition: OFThread.m:377
void terminate()
Terminates the current thread, letting it return nil.
Definition: OFThread.m:250
id _Nullable(^ of_thread_block_t)(void)
A block to be executed in a new thread.
Definition: OFThread.h:37
id copy()
Returns the class.
Definition: OFObject.m:1133
A class for handling strings.
Definition: OFString.h:114
OFMutableDictionary * threadDictionary()
Returns a dictionary to store thread-specific data, meaning it returns a different dictionary for eve...
Definition: OFThread.m:175
void handleTermination()
This routine is executed when the thread&#39;s main method has finished executing or terminate has been c...
Definition: OFThread.m:331
OFThread * currentThread()
Returns the current thread.
Definition: OFThread.m:165
id init()
Initializes an already allocated object.
Definition: OFObject.m:488
An abstract class for storing and changing objects in a dictionary.
Definition: OFMutableDictionary.h:39
nullable id main()
The main routine of the thread. You need to reimplement this!
Definition: OFThread.m:324
OFThread * mainThread()
Returns the main thread.
Definition: OFThread.m:170
A protocol for the creation of copies.
Definition: OFObject.h:914
A class providing a run loop for the application and its processes.
Definition: OFRunLoop.h:41
double of_time_interval_t
A time interval in seconds.
Definition: OFObject.h:91
instancetype thread()
Creates a new thread.
Definition: OFThread.m:153
void start()
Starts the thread.
Definition: OFThread.m:341
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
A class for storing, accessing and comparing dates.
Definition: OFDate.h:30
id join()
Joins a thread.
Definition: OFThread.m:362
void yield()
Yields a processor voluntarily and moves the thread to the end of the queue for its priority...
Definition: OFThread.m:240
float priority()
Returns the priority of the thread.
Definition: OFThread.m:396
size_t stackSize()
Returns the stack size of the thread.
Definition: OFThread.m:410
A class which provides portable threads.
Definition: OFThread.h:53
readonly
Definition: OFThread.h:89