ObjFW
thread.h
1 /*
2  * Copyright (c) 2008-2021 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 "macros.h"
26 
27 #if defined(OF_HAVE_PTHREADS)
28 # include <pthread.h>
29 typedef pthread_t of_thread_t;
30 #elif defined(OF_WINDOWS)
31 # include <windows.h>
32 typedef HANDLE of_thread_t;
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 } *of_thread_t;
45 #endif
46 
47 typedef struct of_thread_attr_t {
48  float priority;
49  size_t stackSize;
50 } of_thread_attr_t;
51 
52 #if defined(OF_HAVE_PTHREADS)
53 # define of_thread_is_current(t) pthread_equal(t, pthread_self())
54 # define of_thread_current() pthread_self()
55 #elif defined(OF_WINDOWS)
56 # define of_thread_is_current(t) (t == GetCurrentThread())
57 # define of_thread_current() GetCurrentThread()
58 #elif defined(OF_AMIGAOS)
59 # define of_thread_is_current(t) (t->thread == FindTask(NULL))
60 extern of_thread_t of_thread_current(void);
61 #endif
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 extern int of_thread_attr_init(of_thread_attr_t *attr);
67 extern int of_thread_new(of_thread_t *thread, const char *name,
68  void (*function)(id), id object, const of_thread_attr_t *attr);
69 extern void of_thread_set_name(const char *name);
70 extern int of_thread_join(of_thread_t thread);
71 extern int of_thread_detach(of_thread_t thread);
72 #ifdef __cplusplus
73 }
74 #endif
struct objc_object * id
A pointer to any object.
Definition: ObjFWRT.h:90