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 file is part of ObjFW. It may be distributed under the terms of the
7  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
8  * the packaging of this file.
9  *
10  * Alternatively, it may be distributed under the terms of the GNU General
11  * Public License, either version 2 or 3, which can be found in the file
12  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
13  * file.
14  */
15 
16 #include <setjmp.h>
17 
18 #import "OFObject.h"
19 #ifdef OF_HAVE_THREADS
20 # import "OFPlainThread.h"
21 #endif
22 
23 OF_ASSUME_NONNULL_BEGIN
24 
27 @class OFDate;
28 #ifdef OF_HAVE_SOCKETS
29 @class OFDNSResolver;
30 #endif
31 @class OFRunLoop;
32 @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
33 
34 #if defined(OF_HAVE_THREADS) && defined(OF_HAVE_BLOCKS)
40 typedef id _Nullable (^OFThreadBlock)(void);
41 #endif
42 
61 @interface OFThread: OFObject
62 #ifdef OF_HAVE_THREADS
63  <OFCopying>
64 {
65 @private
66  OFPlainThread _thread;
67  OFPlainThreadAttributes _attr;
68  enum OFThreadState {
69  OFThreadStateNotRunning,
70  OFThreadStateRunning,
71  OFThreadStateWaitingForJoin
72  } _running;
73 # ifndef OF_OBJFW_RUNTIME
74  void *_pool;
75 # endif
76 # ifdef OF_HAVE_BLOCKS
77  OFThreadBlock _Nullable _block;
78 # endif
79  jmp_buf _exitEnv;
80  id _returnValue;
81  bool _supportsSockets;
82  OFRunLoop *_Nullable _runLoop;
83  OFMutableDictionary *_threadDictionary;
84  OFString *_Nullable _name;
85 # ifdef OF_HAVE_SOCKETS
86  OFDNSResolver *_DNSResolver;
87 # endif
88  OF_RESERVE_IVARS(OFThread, 4)
89 }
90 #endif
91 
92 #ifdef OF_HAVE_CLASS_PROPERTIES
93 # ifdef OF_HAVE_THREADS
94 @property (class, readonly, nullable, nonatomic) OFThread *currentThread;
95 @property (class, readonly, nullable, nonatomic) OFThread *mainThread;
96 @property (class, readonly, nonatomic) bool isMainThread;
97 @property (class, readonly, nullable, nonatomic)
98  OFMutableDictionary *threadDictionary;
99 @property (class, nullable, copy, nonatomic) OFString *name;
100 # endif
101 # ifdef OF_HAVE_SOCKETS
102 @property (class, readonly, nonatomic) OFDNSResolver *DNSResolver;
103 # endif
104 #endif
105 
106 #ifdef OF_HAVE_THREADS
115 @property OF_NULLABLE_PROPERTY (copy) OFString *name;
116 
117 # ifdef OF_HAVE_BLOCKS
121 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFThreadBlock block;
122 # endif
123 
127 @property (readonly, nonatomic) OFRunLoop *runLoop;
128 
141 @property (nonatomic) float priority;
142 
151 @property (nonatomic) size_t stackSize;
152 
164 @property (nonatomic) bool supportsSockets;
165 
171 + (instancetype)thread;
172 
173 # ifdef OF_HAVE_BLOCKS
180 + (instancetype)threadWithBlock: (OFThreadBlock)block;
181 # endif
182 
188 + (nullable OFThread *)currentThread;
189 
195 + (nullable OFThread *)mainThread;
196 
202 + (bool)isMainThread;
203 
210 + (nullable OFMutableDictionary *)threadDictionary;
211 #endif
212 
213 #ifdef OF_HAVE_SOCKETS
222 + (nullable OFDNSResolver *)DNSResolver;
223 #endif
224 
231 + (void)sleepForTimeInterval: (OFTimeInterval)timeInterval;
232 
238 + (void)sleepUntilDate: (OFDate *)date;
239 
244 + (void)yield;
245 
246 #ifdef OF_HAVE_THREADS
250 + (void)terminate OF_NO_RETURN;
251 
258 + (void)terminateWithObject: (nullable id)object OF_NO_RETURN;
259 
268 + (void)setName: (nullable OFString *)name;
269 
275 + (nullable OFString *)name;
276 
277 # ifdef OF_HAVE_BLOCKS
284 - (instancetype)initWithBlock: (OFThreadBlock)block;
285 # endif
286 
292 - (nullable id)main;
293 
300 - (void)handleTermination OF_REQUIRES_SUPER;
301 
308 - (void)start;
309 
316 - (id)join;
317 #else
318 - (instancetype)init OF_UNAVAILABLE;
319 #endif
320 @end
321 
322 OF_ASSUME_NONNULL_END
double OFTimeInterval
A time interval in seconds.
Definition: OFObject.h:150
id(^ OFThreadBlock)(void)
A block to be executed in a new thread.
Definition: OFThread.h:40
A class for resolving DNS names.
Definition: OFDNSResolver.h:126
A class for storing, accessing and comparing dates.
Definition: OFDate.h:30
An abstract class for storing and changing objects in a dictionary.
Definition: OFMutableDictionary.h:44
The root class for all other classes inside ObjFW.
Definition: OFObject.h:688
A class providing a run loop for the application and its processes.
Definition: OFRunLoop.h:62
A class for handling strings.
Definition: OFString.h:135
A class which provides portable threads.
Definition: OFThread.h:62
OFString * name
The name for the thread to use when starting it.
Definition: OFThread.h:115
OFThreadBlock block
The block to execute in the thread.
Definition: OFThread.h:121
A protocol for the creation of copies.
Definition: OFObject.h:1346