ObjFW
OFPlainThread.h
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 
31 #if defined(OF_HAVE_PTHREADS)
32 # include <pthread.h>
33 typedef pthread_t OFPlainThread;
34 #elif defined(OF_WINDOWS)
35 # include <windows.h>
36 typedef HANDLE OFPlainThread;
37 #elif defined(OF_AMIGAOS)
38 # include <exec/tasks.h>
39 # include <exec/semaphores.h>
40 typedef struct {
41  struct Task *task;
42  void (*function)(id);
43  id object;
44  struct SignalSemaphore semaphore;
45  struct Task *joinTask;
46  unsigned char joinSigBit;
47  bool detached, done;
48 } *OFPlainThread;
49 #endif
50 
51 typedef struct {
52  float priority;
53  size_t stackSize;
54 } OFPlainThreadAttributes;
55 
56 #if defined(OF_HAVE_PTHREADS)
57 static OF_INLINE OFPlainThread
58 OFCurrentPlainThread(void)
59 {
60  return pthread_self();
61 }
62 
63 static OF_INLINE bool
64 OFPlainThreadIsCurrent(OFPlainThread thread)
65 {
66  return pthread_equal(thread, pthread_self());
67 }
68 #elif defined(OF_WINDOWS)
69 static OF_INLINE OFPlainThread
70 OFCurrentPlainThread(void)
71 {
72  return GetCurrentThread();
73 }
74 
75 static OF_INLINE bool
76 OFPlainThreadIsCurrent(OFPlainThread thread)
77 {
78  return (thread == GetCurrentThread());
79 }
80 #elif defined(OF_AMIGAOS)
81 extern OFPlainThread OFCurrentPlainThread(void);
82 extern bool OFPlainThreadIsCurrent(OFPlainThread);
83 #endif
84 
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 extern int OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr);
89 extern int OFPlainThreadNew(OFPlainThread *thread, const char *name,
90  void (*function)(id), id object, const OFPlainThreadAttributes *attr);
91 extern void OFSetThreadName(const char *name);
92 extern int OFPlainThreadJoin(OFPlainThread thread);
93 extern int OFPlainThreadDetach(OFPlainThread thread);
94 #ifdef __cplusplus
95 }
96 #endif