ObjFW
OFPlainThread.h
1 /*
2  * Copyright (c) 2008-2023 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 "objfw-defs.h"
17 
18 #include "platform.h"
19 
20 #if !defined(OF_HAVE_THREADS) || \
21  (!defined(OF_HAVE_PTHREADS) && !defined(OF_WINDOWS) && !defined(OF_AMIGAOS))
22 # error No threads available!
23 #endif
24 
25 #import "OFObject.h"
26 
27 #if defined(OF_HAVE_PTHREADS)
28 # include <pthread.h>
29 typedef pthread_t OFPlainThread;
30 #elif defined(OF_WINDOWS)
31 # include <windows.h>
32 typedef HANDLE OFPlainThread;
33 #elif defined(OF_AMIGAOS)
34 # include <exec/tasks.h>
35 # include <exec/semaphores.h>
36 typedef struct {
37  struct Task *task;
38  void (*function)(id);
39  id object;
40  struct SignalSemaphore semaphore;
41  struct Task *joinTask;
42  unsigned char joinSigBit;
43  bool detached, done;
44 } *OFPlainThread;
45 #endif
46 
47 typedef struct {
48  float priority;
49  size_t stackSize;
50 } OFPlainThreadAttributes;
51 
52 #if defined(OF_HAVE_PTHREADS)
53 static OF_INLINE OFPlainThread
54 OFCurrentPlainThread(void)
55 {
56  return pthread_self();
57 }
58 
59 static OF_INLINE bool
60 OFPlainThreadIsCurrent(OFPlainThread thread)
61 {
62  return pthread_equal(thread, pthread_self());
63 }
64 #elif defined(OF_WINDOWS)
65 static OF_INLINE OFPlainThread
66 OFCurrentPlainThread(void)
67 {
68  return GetCurrentThread();
69 }
70 
71 static OF_INLINE bool
72 OFPlainThreadIsCurrent(OFPlainThread thread)
73 {
74  return (thread == GetCurrentThread());
75 }
76 #elif defined(OF_AMIGAOS)
77 extern OFPlainThread OFCurrentPlainThread(void);
78 extern bool OFPlainThreadIsCurrent(OFPlainThread);
79 #endif
80 
81 #ifdef __cplusplus
82 extern "C" {
83 #endif
84 extern int OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr);
85 extern int OFPlainThreadNew(OFPlainThread *thread, const char *name,
86  void (*function)(id), id object, const OFPlainThreadAttributes *attr);
87 extern void OFSetThreadName(const char *name);
88 extern int OFPlainThreadJoin(OFPlainThread thread);
89 extern int OFPlainThreadDetach(OFPlainThread thread);
90 #ifdef __cplusplus
91 }
92 #endif