ObjFW
OFThread.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3.0 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * version 3.0 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 3.0 along with this program. If not, see
17  * <https://www.gnu.org/licenses/>.
18  */
19 
20 #include <setjmp.h>
21 
22 #import "OFObject.h"
23 #ifdef OF_HAVE_THREADS
24 # import "OFPlainThread.h"
25 #endif
26 
27 OF_ASSUME_NONNULL_BEGIN
28 
31 @class OFDate;
32 #ifdef OF_HAVE_SOCKETS
33 @class OFDNSResolver;
34 #endif
35 @class OFRunLoop;
36 @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
37 
38 #if defined(OF_HAVE_THREADS) && defined(OF_HAVE_BLOCKS)
39 
44 typedef id _Nullable (^OFThreadBlock)(void);
45 #endif
46 
65 @interface OFThread: OFObject
66 #ifdef OF_HAVE_THREADS
67  <OFCopying>
68 {
69 @private
70  OFPlainThread _thread;
71  OFPlainThreadAttributes _attr;
72  enum OFThreadState {
73  OFThreadStateNotRunning,
74  OFThreadStateRunning,
75  OFThreadStateWaitingForJoin
76  } _running;
77 # ifndef OF_OBJFW_RUNTIME
78  void *_pool;
79 # endif
80 # ifdef OF_HAVE_BLOCKS
81  OFThreadBlock _Nullable _block;
82 # endif
83  jmp_buf _exitEnv;
84  id _returnValue;
85  bool _supportsSockets;
86  OFRunLoop *_Nullable _runLoop;
87  OFMutableDictionary *_threadDictionary;
88  OFString *_Nullable _name;
89 # ifdef OF_HAVE_SOCKETS
90  OFDNSResolver *_DNSResolver;
91 # endif
92  OF_RESERVE_IVARS(OFThread, 4)
93 }
94 #endif
95 
96 #ifdef OF_HAVE_CLASS_PROPERTIES
97 # ifdef OF_HAVE_THREADS
98 @property (class, readonly, nullable, nonatomic) OFThread *currentThread;
99 @property (class, readonly, nullable, nonatomic) OFThread *mainThread;
100 @property (class, readonly, nonatomic) bool isMainThread;
101 @property (class, readonly, nullable, nonatomic)
102  OFMutableDictionary *threadDictionary;
103 @property (class, nullable, copy, nonatomic) OFString *name;
104 # endif
105 # ifdef OF_HAVE_SOCKETS
106 @property (class, readonly, nonatomic) OFDNSResolver *DNSResolver;
107 # endif
108 #endif
109 
110 #ifdef OF_HAVE_THREADS
111 
119 @property OF_NULLABLE_PROPERTY (copy) OFString *name;
120 
121 # ifdef OF_HAVE_BLOCKS
122 
125 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFThreadBlock block;
126 # endif
127 
131 @property (readonly, nonatomic) OFRunLoop *runLoop;
132 
145 @property (nonatomic) float priority;
146 
155 @property (nonatomic) size_t stackSize;
156 
168 @property (nonatomic) bool supportsSockets;
169 
175 + (instancetype)thread;
176 
177 # ifdef OF_HAVE_BLOCKS
178 
184 + (instancetype)threadWithBlock: (OFThreadBlock)block;
185 # endif
186 
192 + (nullable OFThread *)currentThread;
193 
199 + (nullable OFThread *)mainThread;
200 
206 + (bool)isMainThread;
207 
214 + (nullable OFMutableDictionary *)threadDictionary;
215 #endif
216 
217 #ifdef OF_HAVE_SOCKETS
218 
226 + (nullable OFDNSResolver *)DNSResolver;
227 #endif
228 
235 + (void)sleepForTimeInterval: (OFTimeInterval)timeInterval;
236 
242 + (void)sleepUntilDate: (OFDate *)date;
243 
248 + (void)yield;
249 
250 #if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS) || \
251  defined(DOXYGEN)
252 
257 + (void)waitForVerticalBlank;
258 #endif
259 
260 #ifdef OF_HAVE_THREADS
261 
264 + (void)terminate OF_NO_RETURN;
265 
272 + (void)terminateWithObject: (nullable id)object OF_NO_RETURN;
273 
282 + (void)setName: (nullable OFString *)name;
283 
289 + (nullable OFString *)name;
290 
291 # ifdef OF_HAVE_BLOCKS
292 
298 - (instancetype)initWithBlock: (OFThreadBlock)block;
299 # endif
300 
306 - (nullable id)main;
307 
314 - (void)handleTermination OF_REQUIRES_SUPER;
315 
322 - (void)start;
323 
330 - (id)join;
331 #else
332 - (instancetype)init OF_UNAVAILABLE;
333 #endif
334 @end
335 
336 OF_ASSUME_NONNULL_END
A class for storing, accessing and comparing dates.
Definition: OFDate.h:33
The root class for all other classes inside ObjFW.
Definition: OFObject.h:690
A class for handling strings.
Definition: OFString.h:142
id(^ OFThreadBlock)(void)
A block to be executed in a new thread.
Definition: OFThread.h:44
A class which provides portable threads.
Definition: OFThread.h:65
A class for resolving DNS names.
Definition: OFDNSResolver.h:131
double OFTimeInterval
A time interval in seconds.
Definition: OFObject.h:154
An abstract class for storing and changing objects in a dictionary.
Definition: OFMutableDictionary.h:46
A protocol for the creation of copies.
Definition: OFObject.h:1350
A class providing a run loop for the application and its processes.
Definition: OFRunLoop.h:65