-
Notifications
You must be signed in to change notification settings - Fork 1
/
ythreader.h
70 lines (48 loc) · 1.43 KB
/
ythreader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Created by Daniel on 2024-05-01.
//
#ifndef YTHREADER_H
#define YTHREADER_H
#include "scene/main/node.h"
#include "core/os/thread.h"
class YThreader : public Node {
GDCLASS(YThreader,Node);
protected:
void _notification(int p_what);
static void _worker_thread(void *p_threader);
static void _bind_methods();
public:
void queue_job(const Callable &p_callable);
int thread_count = 1;
int get_thread_count() const {return thread_count;}
void set_thread_count(int p_tcount) {thread_count = p_tcount;}
void lock_mutex();
void unlock_mutex();
static bool is_main_thread();
int _use_thread_count = 0;
Vector<Callable> thread_queue;
// A mutex to synchronize access to the task queue by different threads.
Semaphore available;
// A mutex used to notify worker that a new task has become available.
Mutex run_mutex;
// Queue mutex to prevent manipulate with task queue on same time.
Mutex queue_mutex;
Vector<Thread*> workers;
// Custom mutex
Mutex custom_mutex;
// An atomic (in mind) variable indicating to the workers to keep running.
bool _running = true;
YThreader() {
}
};
class YThreaderInterrupter : public RefCounted {
GDCLASS(YThreaderInterrupter, RefCounted);
protected:
bool cancelled = false;
Mutex mutex;
static void _bind_methods();
public:
void cancel();
bool is_cancelled();
};
#endif //YTHREADER_H