ObjFW
OFThread.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3  * Jonathan Schleifer <js@webkeks.org>
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 OF_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 {
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_PROPERTIES
81 # ifdef OF_HAVE_BLOCKS
82 @property (copy) of_thread_block_t threadBlock;
83 # endif
84 @property (copy) OFString *name;
85 @property float priority;
86 @property size_t stackSize;
87 # endif
88 
94 + (instancetype)thread;
95 
96 # ifdef OF_HAVE_BLOCKS
97 
103 + (instancetype)threadWithThreadBlock: (of_thread_block_t)threadBlock;
104 # endif
105 
111 + (OFThread*)currentThread;
112 
118 + (OFThread*)mainThread;
119 
126 + (OFMutableDictionary*)threadDictionary;
127 #endif
128 
135 + (void)sleepForTimeInterval: (of_time_interval_t)timeInterval;
136 
142 + (void)sleepUntilDate: (OFDate*)date;
143 
148 + (void)yield;
149 
150 #ifdef OF_HAVE_THREADS
151 
154 + (void)terminate OF_NO_RETURN;
155 
161 + (void)terminateWithObject: (nullable id)object OF_NO_RETURN;
162 
163 # ifdef OF_HAVE_BLOCKS
164 
170 - initWithThreadBlock: (of_thread_block_t)threadBlock;
171 # endif
172 
181 - (nullable id)main;
182 
189 - (void)handleTermination OF_REQUIRES_SUPER;
190 
194 - (void)start;
195 
201 - (id)join;
202 
208 - (OFRunLoop*)runLoop;
209 
215 - (nullable OFString*)name;
216 
222 - (void)setName: (nullable OFString*)name;
223 
229 - (float)priority;
230 
239 - (void)setPriority: (float)priority;
240 
246 - (size_t)stackSize;
247 
255 - (void)setStackSize: (size_t)stackSize;
256 #endif
257 @end
258 
259 OF_ASSUME_NONNULL_END
A class for storing, accessing and comparing dates.
Definition: OFDate.h:30
The root class for all other classes inside ObjFW.
Definition: OFObject.h:364
A class for handling strings.
Definition: OFString.h:91
A class which provides portable threads.
Definition: OFThread.h:53
id OF_NULLABLE(^ of_thread_block_t)(void)
A block to be executed in a new thread.
Definition: OFThread.h:37
An abstract class for storing and changing objects in a dictionary.
Definition: OFMutableDictionary.h:47
A protocol for the creation of copies.
Definition: OFObject.h:896
A class providing a run loop for the application and its processes.
Definition: OFRunLoop.h:40
double of_time_interval_t
A time interval in seconds.
Definition: OFObject.h:90