ObjFW
Loading...
Searching...
No Matches
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>
35typedef pthread_t OFPlainThread;
36#elif defined(OF_WINDOWS)
37# include <windows.h>
38typedef HANDLE OFPlainThread;
39#elif defined(OF_AMIGAOS)
40# include <exec/tasks.h>
41# include <exec/semaphores.h>
42typedef 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
53typedef struct {
54 float priority;
55 size_t stackSize;
56} OFPlainThreadAttributes;
57
58#if defined(OF_HAVE_PTHREADS) || defined(DOXYGEN)
64static OF_INLINE OFPlainThread
66{
67 return pthread_self();
68}
69
76static OF_INLINE bool
77OFPlainThreadIsCurrent(OFPlainThread thread)
78{
79 return pthread_equal(thread, pthread_self());
80}
81#elif defined(OF_WINDOWS)
82static OF_INLINE OFPlainThread
84{
85 return GetCurrentThread();
86}
87
88static OF_INLINE bool
89OFPlainThreadIsCurrent(OFPlainThread thread)
90{
91 return (thread == GetCurrentThread());
92}
93#elif defined(OF_AMIGAOS)
94extern OFPlainThread OFCurrentPlainThread(void);
95extern bool OFPlainThreadIsCurrent(OFPlainThread);
96#endif
97
98#ifdef __cplusplus
99extern "C" {
100#endif
107extern int OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr);
108
122extern int OFPlainThreadNew(OFPlainThread *thread, const char *name,
123 void (*function)(id), id object, const OFPlainThreadAttributes *attr);
124
130extern void OFSetThreadName(const char *name);
131
138extern int OFPlainThreadJoin(OFPlainThread thread);
139
146extern int OFPlainThreadDetach(OFPlainThread thread);
147#ifdef __cplusplus
148}
149#endif
void OFSetThreadName(const char *name)
Sets the name of the current thread.
int OFPlainThreadDetach(OFPlainThread thread)
Detaches the specified thread.
int OFPlainThreadJoin(OFPlainThread thread)
Joins the specified thread.
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.
static OF_INLINE OFPlainThread OFCurrentPlainThread(void)
Returns the current plain thread.
Definition OFPlainThread.h:65