feat: modules moved and engine moved to submodule
This commit is contained in:
parent
dfb5e645cd
commit
c33d2130cc
5136 changed files with 225275 additions and 64485 deletions
|
|
@ -5,3 +5,4 @@ Import("env")
|
|||
|
||||
# Driver source files
|
||||
env.add_source_files(env.drivers_sources, "*.mm")
|
||||
env.add_source_files(env.drivers_sources, "*.cpp")
|
||||
|
|
|
|||
|
|
@ -28,7 +28,10 @@
|
|||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/input/input.h"
|
||||
#include "core/input/input_enums.h"
|
||||
|
||||
#define Key _QKey
|
||||
#import <GameController/GameController.h>
|
||||
|
|
@ -43,6 +46,11 @@ struct GameController {
|
|||
RumbleContext *rumble_context API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) = nil;
|
||||
NSInteger ff_effect_timestamp = 0;
|
||||
bool force_feedback = false;
|
||||
bool double_nintendo_joycon_layout = false;
|
||||
bool single_nintendo_joycon_layout = false;
|
||||
|
||||
bool axis_changed[(int)JoyAxis::MAX];
|
||||
double axis_value[(int)JoyAxis::MAX];
|
||||
|
||||
GameController(int p_joy_id, GCController *p_controller);
|
||||
~GameController();
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
#import "joypad_apple.h"
|
||||
|
||||
#include <CoreHaptics/CoreHaptics.h>
|
||||
#import <CoreHaptics/CoreHaptics.h>
|
||||
#import <os/log.h>
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
|
|
@ -134,6 +134,11 @@ public:
|
|||
GameController::GameController(int p_joy_id, GCController *p_controller) :
|
||||
joy_id(p_joy_id), controller(p_controller) {
|
||||
force_feedback = NO;
|
||||
|
||||
for (int i = 0; i < (int)JoyAxis::MAX; i++) {
|
||||
axis_changed[i] = false;
|
||||
axis_value[i] = 0.0;
|
||||
}
|
||||
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
|
||||
if (controller.haptics != nil) {
|
||||
// Create a rumble context for the controller.
|
||||
|
|
@ -144,6 +149,16 @@ GameController::GameController(int p_joy_id, GCController *p_controller) :
|
|||
}
|
||||
}
|
||||
|
||||
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
|
||||
if ([controller.productCategory isEqualToString:@"Switch Pro Controller"] || [controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L/R)"]) {
|
||||
double_nintendo_joycon_layout = true;
|
||||
}
|
||||
|
||||
if ([controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L)"] || [controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (R)"]) {
|
||||
single_nintendo_joycon_layout = true;
|
||||
}
|
||||
}
|
||||
|
||||
int l_joy_id = joy_id;
|
||||
|
||||
auto BUTTON = [l_joy_id](JoyButton p_button) {
|
||||
|
|
@ -152,13 +167,199 @@ GameController::GameController(int p_joy_id, GCController *p_controller) :
|
|||
};
|
||||
};
|
||||
|
||||
if (controller.extendedGamepad != nil) {
|
||||
auto JOYSTICK_LEFT = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) {
|
||||
if (axis_value[(int)JoyAxis::LEFT_X] != xValue) {
|
||||
axis_changed[(int)JoyAxis::LEFT_X] = true;
|
||||
axis_value[(int)JoyAxis::LEFT_X] = xValue;
|
||||
}
|
||||
if (axis_value[(int)JoyAxis::LEFT_Y] != -yValue) {
|
||||
axis_changed[(int)JoyAxis::LEFT_Y] = true;
|
||||
axis_value[(int)JoyAxis::LEFT_Y] = -yValue;
|
||||
}
|
||||
};
|
||||
|
||||
auto JOYSTICK_RIGHT = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) {
|
||||
if (axis_value[(int)JoyAxis::RIGHT_X] != xValue) {
|
||||
axis_changed[(int)JoyAxis::RIGHT_X] = true;
|
||||
axis_value[(int)JoyAxis::RIGHT_X] = xValue;
|
||||
}
|
||||
if (axis_value[(int)JoyAxis::RIGHT_Y] != -yValue) {
|
||||
axis_changed[(int)JoyAxis::RIGHT_Y] = true;
|
||||
axis_value[(int)JoyAxis::RIGHT_Y] = -yValue;
|
||||
}
|
||||
};
|
||||
|
||||
auto TRIGGER_LEFT = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
|
||||
if (axis_value[(int)JoyAxis::TRIGGER_LEFT] != value) {
|
||||
axis_changed[(int)JoyAxis::TRIGGER_LEFT] = true;
|
||||
axis_value[(int)JoyAxis::TRIGGER_LEFT] = value;
|
||||
}
|
||||
};
|
||||
|
||||
auto TRIGGER_RIGHT = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
|
||||
if (axis_value[(int)JoyAxis::TRIGGER_RIGHT] != value) {
|
||||
axis_changed[(int)JoyAxis::TRIGGER_RIGHT] = true;
|
||||
axis_value[(int)JoyAxis::TRIGGER_RIGHT] = value;
|
||||
}
|
||||
};
|
||||
|
||||
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
|
||||
if (controller.physicalInputProfile != nil) {
|
||||
GCPhysicalInputProfile *profile = controller.physicalInputProfile;
|
||||
|
||||
GCControllerButtonInput *buttonA = profile.buttons[GCInputButtonA];
|
||||
GCControllerButtonInput *buttonB = profile.buttons[GCInputButtonB];
|
||||
GCControllerButtonInput *buttonX = profile.buttons[GCInputButtonX];
|
||||
GCControllerButtonInput *buttonY = profile.buttons[GCInputButtonY];
|
||||
if (double_nintendo_joycon_layout) {
|
||||
if (buttonA) {
|
||||
buttonA.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
}
|
||||
if (buttonB) {
|
||||
buttonB.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
}
|
||||
if (buttonX) {
|
||||
buttonX.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
}
|
||||
if (buttonY) {
|
||||
buttonY.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
}
|
||||
} else if (single_nintendo_joycon_layout) {
|
||||
if (buttonA) {
|
||||
buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
}
|
||||
if (buttonB) {
|
||||
buttonB.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
}
|
||||
if (buttonX) {
|
||||
buttonX.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
}
|
||||
if (buttonY) {
|
||||
buttonY.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
}
|
||||
} else {
|
||||
if (buttonA) {
|
||||
buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
}
|
||||
if (buttonB) {
|
||||
buttonB.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
}
|
||||
if (buttonX) {
|
||||
buttonX.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
}
|
||||
if (buttonY) {
|
||||
buttonY.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
}
|
||||
}
|
||||
|
||||
GCControllerButtonInput *leftThumbstickButton = profile.buttons[GCInputLeftThumbstickButton];
|
||||
GCControllerButtonInput *rightThumbstickButton = profile.buttons[GCInputRightThumbstickButton];
|
||||
if (leftThumbstickButton) {
|
||||
leftThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::LEFT_STICK);
|
||||
}
|
||||
if (rightThumbstickButton) {
|
||||
rightThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::RIGHT_STICK);
|
||||
}
|
||||
|
||||
GCControllerButtonInput *leftShoulder = profile.buttons[GCInputLeftShoulder];
|
||||
GCControllerButtonInput *rightShoulder = profile.buttons[GCInputRightShoulder];
|
||||
if (leftShoulder) {
|
||||
leftShoulder.pressedChangedHandler = BUTTON(JoyButton::LEFT_SHOULDER);
|
||||
}
|
||||
if (rightShoulder) {
|
||||
rightShoulder.pressedChangedHandler = BUTTON(JoyButton::RIGHT_SHOULDER);
|
||||
}
|
||||
|
||||
GCControllerButtonInput *leftTrigger = profile.buttons[GCInputLeftTrigger];
|
||||
GCControllerButtonInput *rightTrigger = profile.buttons[GCInputRightTrigger];
|
||||
if (leftTrigger) {
|
||||
leftTrigger.valueChangedHandler = TRIGGER_LEFT;
|
||||
}
|
||||
if (rightTrigger) {
|
||||
rightTrigger.valueChangedHandler = TRIGGER_RIGHT;
|
||||
}
|
||||
|
||||
GCControllerButtonInput *buttonMenu = profile.buttons[GCInputButtonMenu];
|
||||
GCControllerButtonInput *buttonHome = profile.buttons[GCInputButtonHome];
|
||||
GCControllerButtonInput *buttonOptions = profile.buttons[GCInputButtonOptions];
|
||||
if (buttonMenu) {
|
||||
buttonMenu.pressedChangedHandler = BUTTON(JoyButton::START);
|
||||
}
|
||||
if (buttonHome) {
|
||||
buttonHome.pressedChangedHandler = BUTTON(JoyButton::GUIDE);
|
||||
}
|
||||
if (buttonOptions) {
|
||||
buttonOptions.pressedChangedHandler = BUTTON(JoyButton::BACK);
|
||||
}
|
||||
|
||||
// Xbox controller buttons.
|
||||
if (@available(macOS 12.0, iOS 15.0, tvOS 15.0, *)) {
|
||||
GCControllerButtonInput *buttonShare = profile.buttons[GCInputButtonShare];
|
||||
if (buttonShare) {
|
||||
buttonShare.pressedChangedHandler = BUTTON(JoyButton::MISC1);
|
||||
}
|
||||
}
|
||||
|
||||
GCControllerButtonInput *paddleButton1 = profile.buttons[GCInputXboxPaddleOne];
|
||||
GCControllerButtonInput *paddleButton2 = profile.buttons[GCInputXboxPaddleTwo];
|
||||
GCControllerButtonInput *paddleButton3 = profile.buttons[GCInputXboxPaddleThree];
|
||||
GCControllerButtonInput *paddleButton4 = profile.buttons[GCInputXboxPaddleFour];
|
||||
if (paddleButton1) {
|
||||
paddleButton1.pressedChangedHandler = BUTTON(JoyButton::PADDLE1);
|
||||
}
|
||||
if (paddleButton2) {
|
||||
paddleButton2.pressedChangedHandler = BUTTON(JoyButton::PADDLE2);
|
||||
}
|
||||
if (paddleButton3) {
|
||||
paddleButton3.pressedChangedHandler = BUTTON(JoyButton::PADDLE3);
|
||||
}
|
||||
if (paddleButton4) {
|
||||
paddleButton4.pressedChangedHandler = BUTTON(JoyButton::PADDLE4);
|
||||
}
|
||||
|
||||
GCControllerDirectionPad *leftThumbstick = profile.dpads[GCInputLeftThumbstick];
|
||||
if (leftThumbstick) {
|
||||
leftThumbstick.valueChangedHandler = JOYSTICK_LEFT;
|
||||
}
|
||||
|
||||
GCControllerDirectionPad *rightThumbstick = profile.dpads[GCInputRightThumbstick];
|
||||
if (rightThumbstick) {
|
||||
rightThumbstick.valueChangedHandler = JOYSTICK_RIGHT;
|
||||
}
|
||||
|
||||
GCControllerDirectionPad *dpad = nil;
|
||||
if (controller.extendedGamepad != nil) {
|
||||
dpad = controller.extendedGamepad.dpad;
|
||||
} else if (controller.microGamepad != nil) {
|
||||
dpad = controller.microGamepad.dpad;
|
||||
}
|
||||
if (dpad) {
|
||||
dpad.up.pressedChangedHandler = BUTTON(JoyButton::DPAD_UP);
|
||||
dpad.down.pressedChangedHandler = BUTTON(JoyButton::DPAD_DOWN);
|
||||
dpad.left.pressedChangedHandler = BUTTON(JoyButton::DPAD_LEFT);
|
||||
dpad.right.pressedChangedHandler = BUTTON(JoyButton::DPAD_RIGHT);
|
||||
}
|
||||
}
|
||||
} else if (controller.extendedGamepad != nil) {
|
||||
GCExtendedGamepad *gamepad = controller.extendedGamepad;
|
||||
|
||||
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
if (double_nintendo_joycon_layout) {
|
||||
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
} else if (single_nintendo_joycon_layout) {
|
||||
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
} else {
|
||||
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
}
|
||||
|
||||
gamepad.leftShoulder.pressedChangedHandler = BUTTON(JoyButton::LEFT_SHOULDER);
|
||||
gamepad.rightShoulder.pressedChangedHandler = BUTTON(JoyButton::RIGHT_SHOULDER);
|
||||
gamepad.dpad.up.pressedChangedHandler = BUTTON(JoyButton::DPAD_UP);
|
||||
|
|
@ -166,21 +367,10 @@ GameController::GameController(int p_joy_id, GCController *p_controller) :
|
|||
gamepad.dpad.left.pressedChangedHandler = BUTTON(JoyButton::DPAD_LEFT);
|
||||
gamepad.dpad.right.pressedChangedHandler = BUTTON(JoyButton::DPAD_RIGHT);
|
||||
|
||||
gamepad.leftThumbstick.valueChangedHandler = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) {
|
||||
Input::get_singleton()->joy_axis(l_joy_id, JoyAxis::LEFT_X, xValue);
|
||||
Input::get_singleton()->joy_axis(l_joy_id, JoyAxis::LEFT_Y, -yValue);
|
||||
};
|
||||
|
||||
gamepad.rightThumbstick.valueChangedHandler = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) {
|
||||
Input::get_singleton()->joy_axis(l_joy_id, JoyAxis::RIGHT_X, xValue);
|
||||
Input::get_singleton()->joy_axis(l_joy_id, JoyAxis::RIGHT_Y, -yValue);
|
||||
};
|
||||
gamepad.leftTrigger.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
|
||||
Input::get_singleton()->joy_axis(l_joy_id, JoyAxis::TRIGGER_LEFT, value);
|
||||
};
|
||||
gamepad.rightTrigger.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
|
||||
Input::get_singleton()->joy_axis(l_joy_id, JoyAxis::TRIGGER_RIGHT, value);
|
||||
};
|
||||
gamepad.leftThumbstick.valueChangedHandler = JOYSTICK_LEFT;
|
||||
gamepad.rightThumbstick.valueChangedHandler = JOYSTICK_RIGHT;
|
||||
gamepad.leftTrigger.valueChangedHandler = TRIGGER_LEFT;
|
||||
gamepad.rightTrigger.valueChangedHandler = TRIGGER_RIGHT;
|
||||
|
||||
if (@available(macOS 10.14.1, iOS 12.1, tvOS 12.1, *)) {
|
||||
gamepad.leftThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::LEFT_STICK);
|
||||
|
|
@ -313,7 +503,13 @@ void JoypadApple::add_joypad(GCController *p_controller) {
|
|||
}
|
||||
|
||||
// Tell Godot about our new controller.
|
||||
Input::get_singleton()->joy_connection_changed(joy_id, true, String::utf8(p_controller.vendorName.UTF8String));
|
||||
char const *device_name;
|
||||
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
|
||||
device_name = p_controller.productCategory.UTF8String;
|
||||
} else {
|
||||
device_name = p_controller.vendorName.UTF8String;
|
||||
}
|
||||
Input::get_singleton()->joy_connection_changed(joy_id, true, String::utf8(device_name));
|
||||
|
||||
// Assign our player index.
|
||||
joypads.insert(joy_id, memnew(GameController(joy_id, p_controller)));
|
||||
|
|
@ -404,6 +600,13 @@ void JoypadApple::process_joypads() {
|
|||
int id = E.key;
|
||||
GameController &joypad = *E.value;
|
||||
|
||||
for (int i = 0; i < (int)JoyAxis::MAX; i++) {
|
||||
if (joypad.axis_changed[i]) {
|
||||
joypad.axis_changed[i] = false;
|
||||
Input::get_singleton()->joy_axis(id, (JoyAxis)i, joypad.axis_value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (joypad.force_feedback) {
|
||||
Input *input = Input::get_singleton();
|
||||
uint64_t timestamp = input->get_joy_vibration_timestamp(id);
|
||||
|
|
|
|||
129
engine/drivers/apple/thread_apple.cpp
Normal file
129
engine/drivers/apple/thread_apple.cpp
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
/**************************************************************************/
|
||||
/* thread_apple.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "thread_apple.h"
|
||||
|
||||
#include "core/error/error_macros.h"
|
||||
#include "core/object/script_language.h"
|
||||
#include "core/string/ustring.h"
|
||||
|
||||
SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.
|
||||
thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment();
|
||||
|
||||
struct ThreadData {
|
||||
Thread::Callback callback;
|
||||
void *userdata;
|
||||
Thread::ID caller_id;
|
||||
};
|
||||
|
||||
void *Thread::thread_callback(void *p_data) {
|
||||
ThreadData *thread_data = static_cast<ThreadData *>(p_data);
|
||||
|
||||
// Set the caller ID for this thread
|
||||
caller_id = thread_data->caller_id;
|
||||
|
||||
ScriptServer::thread_enter(); // Scripts may need to attach a stack.
|
||||
|
||||
// Call the actual callback
|
||||
thread_data->callback(thread_data->userdata);
|
||||
|
||||
ScriptServer::thread_exit();
|
||||
|
||||
// Clean up
|
||||
memdelete(thread_data);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Error Thread::set_name(const String &p_name) {
|
||||
int err = pthread_setname_np(p_name.utf8().get_data());
|
||||
return err == 0 ? OK : ERR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
|
||||
ERR_FAIL_COND_V_MSG(id != UNASSIGNED_ID, UNASSIGNED_ID, "A Thread object has been re-started without wait_to_finish() having been called on it.");
|
||||
id = id_counter.increment();
|
||||
|
||||
ThreadData *thread_data = memnew(ThreadData);
|
||||
thread_data->callback = p_callback;
|
||||
thread_data->userdata = p_user;
|
||||
thread_data->caller_id = id;
|
||||
|
||||
// Create the thread
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
switch (p_settings.priority) {
|
||||
case PRIORITY_LOW:
|
||||
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_UTILITY, 0);
|
||||
break;
|
||||
case PRIORITY_NORMAL:
|
||||
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INITIATED, 0);
|
||||
break;
|
||||
case PRIORITY_HIGH:
|
||||
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
if (p_settings.stack_size > 0) {
|
||||
pthread_attr_setstacksize(&attr, p_settings.stack_size);
|
||||
}
|
||||
|
||||
// Create the thread
|
||||
pthread_create(&pthread, &attr, thread_callback, thread_data);
|
||||
|
||||
// Clean up attributes
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void Thread::wait_to_finish() {
|
||||
ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started.");
|
||||
ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait.");
|
||||
|
||||
int err = pthread_join(pthread, nullptr);
|
||||
if (err != 0) {
|
||||
ERR_FAIL_MSG("Thread::wait_to_finish() failed to join thread.");
|
||||
}
|
||||
pthread = pthread_t();
|
||||
id = UNASSIGNED_ID;
|
||||
}
|
||||
|
||||
Thread::~Thread() {
|
||||
if (id != UNASSIGNED_ID) {
|
||||
#ifdef DEBUG_ENABLED
|
||||
WARN_PRINT(
|
||||
"A Thread object is being destroyed without its completion having been realized.\n"
|
||||
"Please call wait_to_finish() on it to ensure correct cleanup.");
|
||||
#endif
|
||||
pthread_detach(pthread);
|
||||
}
|
||||
}
|
||||
110
engine/drivers/apple/thread_apple.h
Normal file
110
engine/drivers/apple/thread_apple.h
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/**************************************************************************/
|
||||
/* thread_apple.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/templates/safe_refcount.h"
|
||||
#include "core/typedefs.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <new> // For hardware interference size
|
||||
|
||||
class String;
|
||||
|
||||
class Thread {
|
||||
public:
|
||||
typedef void (*Callback)(void *p_userdata);
|
||||
|
||||
typedef uint64_t ID;
|
||||
|
||||
enum : ID {
|
||||
UNASSIGNED_ID = 0,
|
||||
MAIN_ID = 1
|
||||
};
|
||||
|
||||
enum Priority {
|
||||
PRIORITY_LOW,
|
||||
PRIORITY_NORMAL,
|
||||
PRIORITY_HIGH
|
||||
};
|
||||
|
||||
struct Settings {
|
||||
Priority priority;
|
||||
/// Override the default stack size (0 means default)
|
||||
uint64_t stack_size = 0;
|
||||
Settings() { priority = PRIORITY_NORMAL; }
|
||||
};
|
||||
|
||||
#if defined(__cpp_lib_hardware_interference_size)
|
||||
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Winterference-size")
|
||||
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
|
||||
GODOT_GCC_WARNING_POP
|
||||
#else
|
||||
// At a negligible memory cost, we use a conservatively high value.
|
||||
static constexpr size_t CACHE_LINE_BYTES = 128;
|
||||
#endif
|
||||
|
||||
private:
|
||||
friend class Main;
|
||||
|
||||
ID id = UNASSIGNED_ID;
|
||||
pthread_t pthread;
|
||||
|
||||
static SafeNumeric<uint64_t> id_counter;
|
||||
static thread_local ID caller_id;
|
||||
|
||||
static void *thread_callback(void *p_data);
|
||||
|
||||
static void make_main_thread() { caller_id = MAIN_ID; }
|
||||
static void release_main_thread() { caller_id = id_counter.increment(); }
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ static void yield() { pthread_yield_np(); }
|
||||
|
||||
_FORCE_INLINE_ ID get_id() const { return id; }
|
||||
// get the ID of the caller thread
|
||||
_FORCE_INLINE_ static ID get_caller_id() {
|
||||
return caller_id;
|
||||
}
|
||||
// get the ID of the main thread
|
||||
_FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; }
|
||||
|
||||
_FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; }
|
||||
|
||||
static Error set_name(const String &p_name);
|
||||
|
||||
ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings());
|
||||
bool is_started() const { return id != UNASSIGNED_ID; }
|
||||
/// Waits until thread is finished, and deallocates it.
|
||||
void wait_to_finish();
|
||||
|
||||
Thread() = default;
|
||||
~Thread();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue