ObjFW
OFThread.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2021 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 
20 #ifdef OF_HAVE_THREADS
21 # import "thread.h"
22 #endif
23 
24 OF_ASSUME_NONNULL_BEGIN
25 
28 @class OFDate;
29 #ifdef OF_HAVE_SOCKETS
30 @class OFDNSResolver;
31 #endif
32 @class OFRunLoop;
33 @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
34 
35 #if defined(OF_HAVE_THREADS) && defined(OF_HAVE_BLOCKS)
41 typedef id _Nullable (^of_thread_block_t)(void);
42 #endif
43 
62 @interface OFThread: OFObject
63 #ifdef OF_HAVE_THREADS
64  <OFCopying>
65 {
66 @private
67  of_thread_t _thread;
68  of_thread_attr_t _attr;
69  enum of_thread_running {
70  OF_THREAD_NOT_RUNNING,
71  OF_THREAD_RUNNING,
72  OF_THREAD_WAITING_FOR_JOIN
73  } _running;
74 # ifndef OF_OBJFW_RUNTIME
75  void *_pool;
76 # endif
77 # ifdef OF_HAVE_BLOCKS
78  of_thread_block_t _Nullable _threadBlock;
79 # endif
80  jmp_buf _exitEnv;
81  id _returnValue;
82  bool _supportsSockets;
83  OFRunLoop *_Nullable _runLoop;
84  OFMutableDictionary *_threadDictionary;
85  OFString *_Nullable _name;
86 # ifdef OF_HAVE_SOCKETS
87  OFDNSResolver *_DNSResolver;
88 # endif
89  OF_RESERVE_IVARS(OFThread, 4)
90 }
91 #endif
92 
93 #ifdef OF_HAVE_CLASS_PROPERTIES
94 # ifdef OF_HAVE_THREADS
95 @property (class, readonly, nullable, nonatomic) OFThread *currentThread;
96 @property (class, readonly, nullable, nonatomic) OFThread *mainThread;
97 @property (class, readonly, nonatomic) bool isMainThread;
98 @property (class, readonly, nullable, nonatomic)
99  OFMutableDictionary *threadDictionary;
100 @property (class, nullable, copy, nonatomic) OFString *name;
101 # endif
102 # ifdef OF_HAVE_SOCKETS
103 @property (class, readonly, nonatomic) OFDNSResolver *DNSResolver;
104 # endif
105 #endif
106 
107 #ifdef OF_HAVE_THREADS
116 @property OF_NULLABLE_PROPERTY (copy) OFString *name;
117 
118 # ifdef OF_HAVE_BLOCKS
122 @property OF_NULLABLE_PROPERTY (readonly, nonatomic)
123  of_thread_block_t threadBlock;
124 # endif
125 
129 @property (readonly, nonatomic) OFRunLoop *runLoop;
130 
140 @property (nonatomic) float priority;
141 
147 @property (nonatomic) size_t stackSize;
148 
156 @property (nonatomic) bool supportsSockets;
157 
163 + (instancetype)thread;
164 
165 # ifdef OF_HAVE_BLOCKS
172 + (instancetype)threadWithThreadBlock: (of_thread_block_t)threadBlock;
173 # endif
174 
180 + (nullable OFThread *)currentThread;
181 
187 + (nullable OFThread *)mainThread;
188 
194 + (bool)isMainThread;
195 
202 + (nullable OFMutableDictionary *)threadDictionary;
203 #endif
204 
205 #ifdef OF_HAVE_SOCKETS
211 + (OFDNSResolver *)DNSResolver;
212 #endif
213 
220 + (void)sleepForTimeInterval: (of_time_interval_t)timeInterval;
221 
227 + (void)sleepUntilDate: (OFDate *)date;
228 
233 + (void)yield;
234 
235 #ifdef OF_HAVE_THREADS
239 + (void)terminate OF_NO_RETURN;
240 
246 + (void)terminateWithObject: (nullable id)object OF_NO_RETURN;
247 
256 + (void)setName: (nullable OFString *)name;
257 
263 + (nullable OFString *)name;
264 
265 # ifdef OF_HAVE_BLOCKS
272 - (instancetype)initWithThreadBlock: (of_thread_block_t)threadBlock;
273 # endif
274 
280 - (nullable id)main;
281 
288 - (void)handleTermination OF_REQUIRES_SUPER;
289 
293 - (void)start;
294 
300 - (id)join;
301 #else
302 - (instancetype)init OF_UNAVAILABLE;
303 #endif
304 @end
305 
306 OF_ASSUME_NONNULL_END
double of_time_interval_t
A time interval in seconds.
Definition: OFObject.h:138
id _Nullable(^ of_thread_block_t)(void)
A block to be executed in a new thread.
Definition: OFThread.h:41
struct objc_object * id
A pointer to any object.
Definition: ObjFWRT.h:90
A class for resolving DNS names.
Definition: OFDNSResolver.h:126
A class for storing, accessing and comparing dates.
Definition: OFDate.h:36
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:520
A class providing a run loop for the application and its processes.
Definition: OFRunLoop.h:62
A class for handling strings.
Definition: OFString.h:132
A class which provides portable threads.
Definition: OFThread.h:63
OFString * name
The name for the thread to use when starting it.
Definition: OFThread.h:116
A protocol for the creation of copies.
Definition: OFObject.h:1187