ObjFW
Loading...
Searching...
No Matches
OFPlainThread.h
1/*
2 * Copyright (c) 2008-2024 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>
29typedef pthread_t OFPlainThread;
30#elif defined(OF_WINDOWS)
31# include <windows.h>
32typedef HANDLE OFPlainThread;
33#elif defined(OF_AMIGAOS)
34# include <exec/tasks.h>
35# include <exec/semaphores.h>
36typedef 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
47typedef struct {
48 float priority;
49 size_t stackSize;
50} OFPlainThreadAttributes;
51
52#if defined(OF_HAVE_PTHREADS)
53static OF_INLINE OFPlainThread
54OFCurrentPlainThread(void)
55{
56 return pthread_self();
57}
58
59static OF_INLINE bool
60OFPlainThreadIsCurrent(OFPlainThread thread)
61{
62 return pthread_equal(thread, pthread_self());
63}
64#elif defined(OF_WINDOWS)
65static OF_INLINE OFPlainThread
66OFCurrentPlainThread(void)
67{
68 return GetCurrentThread();
69}
70
71static OF_INLINE bool
72OFPlainThreadIsCurrent(OFPlainThread thread)
73{
74 return (thread == GetCurrentThread());
75}
76#elif defined(OF_AMIGAOS)
77extern OFPlainThread OFCurrentPlainThread(void);
78extern bool OFPlainThreadIsCurrent(OFPlainThread);
79#endif
80
81#ifdef __cplusplus
82extern "C" {
83#endif
84extern int OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr);
85extern int OFPlainThreadNew(OFPlainThread *thread, const char *name,
86 void (*function)(id), id object, const OFPlainThreadAttributes *attr);
87extern void OFSetThreadName(const char *name);
88extern int OFPlainThreadJoin(OFPlainThread thread);
89extern int OFPlainThreadDetach(OFPlainThread thread);
90#ifdef __cplusplus
91}
92#endif