ObjFW
OFPlainThread.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 "objfw-defs.h"
21 
22 #include "platform.h"
23 
24 #if !defined(OF_HAVE_THREADS) || \
25  (!defined(OF_HAVE_PTHREADS) && !defined(OF_WINDOWS) && !defined(OF_AMIGAOS))
26 # error No threads available!
27 #endif
28 
29 #import "OFObject.h"
30 
33 #if defined(OF_HAVE_PTHREADS)
34 # include <pthread.h>
35 typedef pthread_t OFPlainThread;
36 #elif defined(OF_WINDOWS)
37 # include <windows.h>
38 typedef HANDLE OFPlainThread;
39 #elif defined(OF_AMIGAOS)
40 # include <exec/tasks.h>
41 # include <exec/semaphores.h>
42 typedef struct {
43  struct Task *task;
44  void (*function)(id);
45  id object;
46  struct SignalSemaphore semaphore;
47  struct Task *joinTask;
48  unsigned char joinSigBit;
49  bool detached, done;
50 } *OFPlainThread;
51 #endif
52 
53 typedef struct {
54  float priority;
55  size_t stackSize;
56 } OFPlainThreadAttributes;
57 
58 #if defined(OF_HAVE_PTHREADS) || defined(DOXYGEN)
59 
64 static OF_INLINE OFPlainThread
66 {
67  return pthread_self();
68 }
69 
76 static OF_INLINE bool
77 OFPlainThreadIsCurrent(OFPlainThread thread)
78 {
79  return pthread_equal(thread, pthread_self());
80 }
81 #elif defined(OF_WINDOWS)
82 static OF_INLINE OFPlainThread
84 {
85  return GetCurrentThread();
86 }
87 
88 static OF_INLINE bool
89 OFPlainThreadIsCurrent(OFPlainThread thread)
90 {
91  return (thread == GetCurrentThread());
92 }
93 #elif defined(OF_AMIGAOS)
94 extern OFPlainThread OFCurrentPlainThread(void);
95 extern bool OFPlainThreadIsCurrent(OFPlainThread);
96 #endif
97 
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101 
107 extern int OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr);
108 
122 extern int OFPlainThreadNew(OFPlainThread *thread, const char *name,
123  void (*function)(id), id object, const OFPlainThreadAttributes *attr);
124 
130 extern void OFSetThreadName(const char *name);
131 
138 extern int OFPlainThreadJoin(OFPlainThread thread);
139 
146 extern int OFPlainThreadDetach(OFPlainThread thread);
147 #ifdef __cplusplus
148 }
149 #endif
static OF_INLINE bool OFPlainThreadIsCurrent(OFPlainThread thread)
Returns whether the specified plain thread is the current thread.
Definition: OFPlainThread.h:77
int OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr)
Initializes the specified thread attributes.
int OFPlainThreadNew(OFPlainThread *thread, const char *name, void(*function)(id), id object, const OFPlainThreadAttributes *attr)
Creates a new plain thread.
void OFSetThreadName(const char *name)
Sets the name of the current thread.
int OFPlainThreadJoin(OFPlainThread thread)
Joins the specified thread.
int OFPlainThreadDetach(OFPlainThread thread)
Detaches the specified thread.
static OF_INLINE OFPlainThread OFCurrentPlainThread(void)
Returns the current plain thread.
Definition: OFPlainThread.h:65