summaryrefslogtreecommitdiffstats
path: root/core/os/threaded_array_processor.h
blob: e584fbb193abee61111f0eb0bbaf0f7f21f7aa69 (plain)
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
71
72
73
74
75
76
77
78
79
80
#ifndef THREADED_ARRAY_PROCESSOR_H
#define THREADED_ARRAY_PROCESSOR_H

#include "os/mutex.h"
#include "os/os.h"
#include "os/thread.h"
#include "safe_refcount.h"
#include "thread_safe.h"

template <class C, class U>
struct ThreadArrayProcessData {
	uint32_t elements;
	uint32_t index;
	C *instance;
	U userdata;
	void (C::*method)(uint32_t, U);

	void process(uint32_t p_index) {
		(instance->*method)(p_index, userdata);
	}
};

#ifndef NO_THREADS

template <class T>
void process_array_thread(void *ud) {

	T &data = *(T *)ud;
	while (true) {
		uint32_t index = atomic_increment(&data.index);
		if (index >= data.elements)
			break;
		data.process(index);
	}
}

template <class C, class M, class U>
void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {

	ThreadArrayProcessData<C, U> data;
	data.method = p_method;
	data.instance = p_instance;
	data.userdata = p_userdata;
	data.index = 0;
	data.elements = p_elements;
	data.process(data.index); //process first, let threads increment for next

	Vector<Thread *> threads;

	threads.resize(OS::get_singleton()->get_processor_count());

	for (int i = 0; i < threads.size(); i++) {
		threads[i] = Thread::create(process_array_thread<ThreadArrayProcessData<C, U> >, &data);
	}

	for (int i = 0; i < threads.size(); i++) {
		Thread::wait_to_finish(threads[i]);
		memdelete(threads[i]);
	}
}

#else

template <class C, class M, class U>
void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {

	ThreadArrayProcessData<C, U> data;
	data.method = p_method;
	data.instance = p_instance;
	data.userdata = p_userdata;
	data.index = 0;
	data.elements = p_elements;
	for (uint32_t i = 0; i < p_elements; i++) {
		data.process(i);
	}
}

#endif

#endif // THREADED_ARRAY_PROCESSOR_H