Skip to content

Commit 120aae3

Browse files
committed
Use std threads instead of godot's
Don't really understand why, but waiting for a godot thread to finish crashed this ,-,
1 parent da95616 commit 120aae3

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

src/dbus_client_node.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ void DBusClientNode::open() {
9696
}
9797

9898
void DBusClientNode::close() {
99-
if (_thread.is_started()) {
100-
_thread.wait_to_finish();
99+
if (_thread_running) {
100+
_thread.join();
101101
}
102102

103103
_client->close();
@@ -156,9 +156,10 @@ Error DBusClientNode::request(const Variant **p_args, GDExtensionInt p_arg_count
156156
}
157157
const Callable callback = *p_args[1];
158158

159-
if (_use_threads && _thread.is_alive()) {
160-
_thread.wait_to_finish();
159+
if (_thread_running) {
160+
_thread.join();
161161
}
162+
_thread_running = true;
162163

163164
Ref<DBusMessage> request_message = _client->create_request(_destination,
164165
_path,
@@ -170,9 +171,9 @@ Error DBusClientNode::request(const Variant **p_args, GDExtensionInt p_arg_count
170171
}
171172

172173
if (_use_threads) {
173-
Callable async_request = Callable(this, "_request");
174-
async_request = async_request.bind(request_message, callback);
175-
_thread.start(async_request);
174+
_thread = std::thread([this, request_message, callback]() {
175+
_request(request_message, callback);
176+
});
176177
} else {
177178
_request(request_message, callback);
178179
}

src/dbus_client_node.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

33
#include <godot_cpp/classes/node.hpp>
4-
#include <godot_cpp/classes/thread.hpp>
4+
5+
#include <thread>
56

67
#include "dbus_client.h"
78
#include "dbus_level.h"
@@ -28,7 +29,8 @@ class DBusClientNode : public Node {
2829
* @param callback
2930
*/
3031
void _request(const Ref<DBusMessage> &p_request, Callable callback);
31-
Thread _thread;
32+
std::thread _thread;
33+
bool _thread_running = false;
3234
bool _autostart = true;
3335
bool _use_threads = false;
3436

src/dbus_server_node.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ void DBusServerNode::_bind_methods() {
3131
ClassDB::bind_method(D_METHOD("stop"), &DBusServerNode::stop);
3232

3333
ClassDB::bind_method(D_METHOD("_server_thread_loop"), &DBusServerNode::_server_thread_loop);
34-
ClassDB::bind_method(D_METHOD("_process_bus"), &DBusServerNode::_process_bus);
3534

3635
ADD_PROPERTY(PropertyInfo(Variant::STRING, "object_path"), "set_object_path", "get_object_path");
3736
ADD_PROPERTY(PropertyInfo(Variant::STRING, "interface_name"), "set_interface_name", "get_interface_name");
3837
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "methods", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "DBusMethod")), "set_methods", "get_methods");
3938
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "get_autostart");
4039
ADD_PROPERTY(PropertyInfo(Variant::INT, "bus_level", PROPERTY_HINT_ENUM, "USER, SYSTEM"), "set_bus_level", "get_bus_level");
41-
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "running", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY), "", "is_running");
40+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "running"), "", "is_running");
4241
}
4342

4443
DBusServerNode::DBusServerNode() :
@@ -68,7 +67,7 @@ void DBusServerNode::_server_thread_loop() {
6867
const uint64_t BUS_WAIT_TIME = 50000; //0.5s
6968
while (is_running()) {
7069
//do processing on main thread, since need to wait for callback anyways and sometimes this outputs notifications which crashes outside of the main thread
71-
r = call_deferred("_process_bus");
70+
r = sd_bus_process(_bus, NULL);
7271
ERR_BUS_FAIL("Failed to process bus");
7372

7473
if (r > 0) //if processed try to process another one without waiting
@@ -87,10 +86,6 @@ void DBusServerNode::_server_thread_loop() {
8786
}
8887
}
8988

90-
int DBusServerNode::_process_bus() {
91-
return sd_bus_process(_bus, NULL);
92-
}
93-
9489
// Setter and Getter for _object_path
9590
void DBusServerNode::set_object_path(const String &p_name) {
9691
ERR_FAIL_COND(_running);
@@ -185,16 +180,14 @@ void DBusServerNode::start() {
185180

186181
r = sd_bus_request_name(_bus, _interface_name, 0);
187182
ERR_BUS_FAIL("Failed to acquire service name " + String(_interface_name));
188-
_thread.start(Callable(this, "_server_thread_loop"));
183+
_thread = std::thread(&DBusServerNode::_server_thread_loop, this);
189184
_running = true;
190185
}
191186

192187
void DBusServerNode::stop() {
193188
ERR_FAIL_COND_MSG(!_running, "Already stopped");
194189
_set_running(false);
195-
if (_thread.is_alive()) {
196-
_thread.wait_to_finish();
197-
}
190+
_thread.join();
198191
sd_bus_slot_unref(_slot);
199192
sd_bus_unref(_bus);
200193
_slot = nullptr;

src/dbus_server_node.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44
#include "dbus_method.h"
55
#include <godot_cpp/classes/mutex.hpp>
66
#include <godot_cpp/classes/node.hpp>
7-
#include <godot_cpp/classes/thread.hpp>
87
#include <godot_cpp/core/class_db.hpp>
98
#include <godot_cpp/templates/hash_map.hpp>
109

10+
#include <mutex>
11+
#include <thread>
12+
1113
using namespace godot;
1214

1315
class DBusServerNode : public Node {
1416
GDCLASS(DBusServerNode, Node);
1517

1618
private:
17-
Thread _thread;
19+
std::thread _thread;
1820
CharString _object_path;
1921
CharString _interface_name;
2022
Array _methods;
2123
bool _autostart;
2224
DBusLevel::Level _bus_level = DBusLevel::USER;
2325

24-
Mutex _lock;
26+
std::mutex _lock;
2527
bool _running;
2628

2729
HashMap<String, Ref<DBusMethod>> _method_map;
@@ -32,7 +34,6 @@ class DBusServerNode : public Node {
3234
int _v_table_size;
3335

3436
void _server_thread_loop();
35-
int _process_bus();
3637
void _set_running(const bool p_running);
3738

3839
protected:

0 commit comments

Comments
 (0)