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 _threadBlock;
72 # endif
73  id _returnValue;
74  OFRunLoop *_runLoop;
75  OFMutableDictionary *_threadDictionary;
76 @private
77  OFString *_name;
78 }
79 
80 #ifdef OF_HAVE_BLOCKS
81 
84 @property (readonly, nonatomic) of_thread_block_t threadBlock;
85 #endif
86 
92 + (instancetype)thread;
93 
94 # ifdef OF_HAVE_BLOCKS
95 
101 + (instancetype)threadWithThreadBlock: (of_thread_block_t)threadBlock;
102 # endif
103 
110 
116 + (OFThread *)mainThread;
117 
125 #endif
126 
133 + (void)sleepForTimeInterval: (of_time_interval_t)timeInterval;
134 
140 + (void)sleepUntilDate: (OFDate *)date;
141 
146 + (void)yield;
147 
148 #ifdef OF_HAVE_THREADS
149 
152 + (void)terminate OF_NO_RETURN;
153 
159 + (void)terminateWithObject: (nullable id)object OF_NO_RETURN;
160 
161 # ifdef OF_HAVE_BLOCKS
162 
168 - initWithThreadBlock: (of_thread_block_t)threadBlock;
169 # endif
170 
176 - (nullable id)main;
177 
184 - (void)handleTermination OF_REQUIRES_SUPER;
185 
189 - (void)start;
190 
196 - (id)join;
197 
203 - (OFRunLoop *)runLoop;
204 
210 - (nullable OFString *)name;
211 
217 - (void)setName: (nullable OFString *)name;
218 
228 - (float)priority;
229 
241 - (void)setPriority: (float)priority;
242 
248 - (size_t)stackSize;
249 
257 - (void)setStackSize: (size_t)stackSize;
258 #else
259 - init OF_UNAVAILABLE;
260 #endif
261 @end
262 
263 OF_ASSUME_NONNULL_END
OFRunLoop * runLoop()
Returns the run loop for the thread.
Definition: OFThread.m:374
void terminate()
Terminates the current thread, letting it return nil.
Definition: OFThread.m:242
id _Nullable(^ of_thread_block_t)(void)
A block to be executed in a new thread.
Definition: OFThread.h:37
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:167
void handleTermination()
This routine is executed when the thread&#39;s main method has finished executing or terminate has been c...
Definition: OFThread.m:323
OFThread * currentThread()
Returns the current thread.
Definition: OFThread.m:157
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:316
OFThread * mainThread()
Returns the main thread.
Definition: OFThread.m:162
A protocol for the creation of copies.
Definition: OFObject.h:912
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:145
nullable OFString * name()
Returns the name of the thread or nil if none has been set.
Definition: OFThread.m:393
void start()
Starts the thread.
Definition: OFThread.m:333
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:359
of_thread_block_t threadBlock
Definition: OFThread.h:84
void yield()
Yields a processor voluntarily and moves the thread to the end of the queue for its priority...
Definition: OFThread.m:232
float priority()
Returns the priority of the thread.
Definition: OFThread.m:409
size_t stackSize()
Returns the stack size of the thread.
Definition: OFThread.m:423
A class which provides portable threads.
Definition: OFThread.h:53