aboutsummaryrefslogtreecommitdiff
path: root/Src/replicant/nu/win/ThreadLoop.cpp
blob: 3ff89e5905da4f4d5c69d01cc799233477357ef2 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "ThreadLoop.h"
#include <limits.h>

lifo_t ThreadLoop::procedure_cache = {0,};
lifo_t ThreadLoop::cache_bases= {0,};

#define PROCEDURE_CACHE_SEED 64
ThreadLoop::ThreadLoop()
{
	mpscq_init(&procedure_queue);
	procedure_notification = CreateSemaphoreW(0, 0, LONG_MAX, 0);
	kill_switch = CreateEvent(0, TRUE, FALSE, 0);
}

ThreadLoop::~ThreadLoop()
{
	CloseHandle(procedure_notification);
	CloseHandle(kill_switch);
}

void ThreadLoop::RefillCache()
{
	threadloop_node_t *cache_seed = (threadloop_node_t *)malloc(PROCEDURE_CACHE_SEED*sizeof(threadloop_node_t));
	
	if (cache_seed)
	{
		int i=PROCEDURE_CACHE_SEED;
		while (--i)
		{
			lifo_push(&procedure_cache, (queue_node_t *)&cache_seed[i]);
		}
		lifo_push(&cache_bases, (queue_node_t *)cache_seed);
	}
	else
	{
		Sleep(0); // yield and hope that someone else pops something off soon		
	}
}

void ThreadLoop::Run()
{
	HANDLE events[] = {kill_switch, procedure_notification};
	while (WaitForMultipleObjects(2, events, FALSE, INFINITE) == WAIT_OBJECT_0 + 1)
	{
		for (;;)
		{
			threadloop_node_t *apc = (threadloop_node_t *)mpscq_pop(&procedure_queue);
			if (apc == (threadloop_node_t *)1) /* special return value that indicates a busy list */
			{
				Sleep(0); // yield so that the thread that got pre-empted during push can finish
			}
			else
			{
				if (apc)
				{
					apc->func(apc->param1, apc->param2, apc->real_value);
					lifo_push(&procedure_cache, apc);
				}
				else
				{
					break;
				}
			}
		}
	}
}

void ThreadLoop::Step(unsigned int milliseconds)
{
	HANDLE events[] = {kill_switch, procedure_notification};
	if (WaitForMultipleObjects(2, events, FALSE, milliseconds) == WAIT_OBJECT_0 + 1)
	{
		for (;;)
		{
			threadloop_node_t *apc = (threadloop_node_t *)mpscq_pop(&procedure_queue);
			if (apc == (threadloop_node_t *)1) /* special return value that indicates a busy list */
			{
				Sleep(0); // yield so that the thread that got pre-empted during push can finish
			}
			else
			{
				if (apc)
				{
					apc->func(apc->param1, apc->param2, apc->real_value);
					lifo_push(&procedure_cache, apc);
				}
				else
				{
					break;
				}
			}
		}
	}
}

void ThreadLoop::Step()
{
	HANDLE events[] = {kill_switch, procedure_notification};
	if (WaitForMultipleObjects(2, events, FALSE, INFINITE) == WAIT_OBJECT_0 + 1)
	{
		for (;;)
		{
			threadloop_node_t *apc = (threadloop_node_t *)mpscq_pop(&procedure_queue);
			if (apc == (threadloop_node_t *)1) /* special return value that indicates a busy list */
			{
				Sleep(0); // yield so that the thread that got pre-empted during push can finish
			}
			else
			{
				if (apc)
				{
					apc->func(apc->param1, apc->param2, apc->real_value);
					lifo_push(&procedure_cache, apc);
				}
				else
				{
					break;
				}
			}
		}
	}
}

threadloop_node_t *ThreadLoop::GetAPC()
{
	threadloop_node_t *apc = 0;

	do 
	{
		apc = (threadloop_node_t *)lifo_pop(&procedure_cache);
		if (!apc)
			RefillCache();
	} while (!apc);
	return apc;
}

void ThreadLoop::Schedule(threadloop_node_t *apc)
{
	if (mpscq_push(&procedure_queue, apc) == 0)
		ReleaseSemaphore(procedure_notification, 1, 0);
}

void ThreadLoop::Kill()
{
	SetEvent(kill_switch);
}