Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/apple/thread_apple.h
9973 views
1
/**************************************************************************/
2
/* thread_apple.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/templates/safe_refcount.h"
34
#include "core/typedefs.h"
35
36
#include <pthread.h>
37
#include <new> // For hardware interference size
38
39
class String;
40
41
class Thread {
42
public:
43
typedef void (*Callback)(void *p_userdata);
44
45
typedef uint64_t ID;
46
47
enum : ID {
48
UNASSIGNED_ID = 0,
49
MAIN_ID = 1
50
};
51
52
enum Priority {
53
PRIORITY_LOW,
54
PRIORITY_NORMAL,
55
PRIORITY_HIGH
56
};
57
58
struct Settings {
59
Priority priority;
60
/// Override the default stack size (0 means default)
61
uint64_t stack_size = 0;
62
Settings() { priority = PRIORITY_NORMAL; }
63
};
64
65
#if defined(__cpp_lib_hardware_interference_size)
66
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Winterference-size")
67
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
68
GODOT_GCC_WARNING_POP
69
#else
70
// At a negligible memory cost, we use a conservatively high value.
71
static constexpr size_t CACHE_LINE_BYTES = 128;
72
#endif
73
74
private:
75
friend class Main;
76
77
ID id = UNASSIGNED_ID;
78
pthread_t pthread;
79
80
static SafeNumeric<uint64_t> id_counter;
81
static thread_local ID caller_id;
82
83
static void *thread_callback(void *p_data);
84
85
static void make_main_thread() { caller_id = MAIN_ID; }
86
static void release_main_thread() { caller_id = id_counter.increment(); }
87
88
public:
89
_FORCE_INLINE_ static void yield() { pthread_yield_np(); }
90
91
_FORCE_INLINE_ ID get_id() const { return id; }
92
// get the ID of the caller thread
93
_FORCE_INLINE_ static ID get_caller_id() {
94
return caller_id;
95
}
96
// get the ID of the main thread
97
_FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; }
98
99
_FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; }
100
101
static Error set_name(const String &p_name);
102
103
ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings());
104
bool is_started() const { return id != UNASSIGNED_ID; }
105
/// Waits until thread is finished, and deallocates it.
106
void wait_to_finish();
107
108
Thread() = default;
109
~Thread();
110
};
111
112