ObjFW
Loading...
Searching...
No Matches
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
27OF_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)
44typedef 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
119@property OF_NULLABLE_PROPERTY (copy) OFString *name;
120
121# ifdef OF_HAVE_BLOCKS
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
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
226+ (nullable OFDNSResolver *)DNSResolver;
227#endif
228
235+ (void)sleepForTimeInterval: (OFTimeInterval)timeInterval;
236
242+ (void)sleepUntilDate: (OFDate *)date;
243
248+ (void)yield;
249
250#ifdef OF_HAVE_THREADS
254+ (void)terminate OF_NO_RETURN;
255
262+ (void)terminateWithObject: (nullable id)object OF_NO_RETURN;
263
272+ (void)setName: (nullable OFString *)name;
273
279+ (nullable OFString *)name;
280
281# ifdef OF_HAVE_BLOCKS
288- (instancetype)initWithBlock: (OFThreadBlock)block;
289# endif
290
296- (nullable id)main;
297
304- (void)handleTermination OF_REQUIRES_SUPER;
305
312- (void)start;
313
320- (id)join;
321#else
322- (instancetype)init OF_UNAVAILABLE;
323#endif
324@end
325
326OF_ASSUME_NONNULL_END
double OFTimeInterval
A time interval in seconds.
Definition OFObject.h:154
id(^ OFThreadBlock)(void)
A block to be executed in a new thread.
Definition OFThread.h:44
A class for resolving DNS names.
Definition OFDNSResolver.h:132
A class for storing, accessing and comparing dates.
Definition OFDate.h:34
An abstract class for storing and changing objects in a dictionary.
Definition OFMutableDictionary.h:48
The root class for all other classes inside ObjFW.
Definition OFObject.h:692
A class providing a run loop for the application and its processes.
Definition OFRunLoop.h:66
A class for handling strings.
Definition OFString.h:139
A class which provides portable threads.
Definition OFThread.h:66
OFString * name
The name for the thread to use when starting it.
Definition OFThread.h:119
OFThreadBlock block
The block to execute in the thread.
Definition OFThread.h:125
A protocol for the creation of copies.
Definition OFObject.h:1350