summaryrefslogtreecommitdiff
path: root/src/fiber_win32.cpp
blob: 61718188ce688baba59b2ff07ebd89dd50d01db7 (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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* $Id$ */

/** @file fiber_win32.cpp Win32 implementation of Fiber. */

#include "stdafx.h"
#include "fiber.hpp"
#include <stdlib.h>
#include <windows.h>
#include <process.h>

class Fiber_Win32 : public Fiber {
private:
	LPVOID m_fiber;
	FiberFunc m_proc;
	void *m_param;
	bool m_attached;

	static Fiber_Win32 *s_main;

public:
	/**
	 * Create a win32 fiber and start it, calling proc(param).
	 */
	Fiber_Win32(FiberFunc proc, void *param) :
		m_fiber(NULL),
		m_proc(proc),
		m_param(param),
		m_attached(false)
	{
		CreateFiber();
	}

	/**
	 * Create a win32 fiber and attach current thread to it.
	 */
	Fiber_Win32(void *param) :
		m_fiber(NULL),
		m_proc(NULL),
		m_param(param),
		m_attached(true)
	{
		ConvertThreadToFiber();
		if (s_main == NULL) s_main = this;
	}

	/* virtual */ ~Fiber_Win32()
	{
		if (this->m_fiber != NULL) {
			if (this->m_attached) {
				this->ConvertFiberToThread();
			} else {
				this->DeleteFiber();
			}
		}
	}

	/* virtual */ void SwitchToFiber()
	{
		typedef VOID (WINAPI *FnSwitchToFiber)(LPVOID fiber);

		static FnSwitchToFiber fnSwitchToFiber = (FnSwitchToFiber)stGetProcAddr("SwitchToFiber");
		assert(fnSwitchToFiber != NULL);

		fnSwitchToFiber(this->m_fiber);
	}

	/* virtual */ void Exit()
	{
		/* Simply switch back to the main fiber, we kill the fiber sooner or later */
		s_main->SwitchToFiber();
	}

	/* virtual */ bool IsRunning()
	{
		return this->m_fiber != NULL;
	}

	/* virtual */ void *GetFiberData()
	{
		return this->m_param;
	}

	/**
	 * Win95 doesn't have Fiber support. So check if we have Fiber support,
	 *  and else fall back on Fiber_Thread.
	 */
	static bool IsSupported()
	{
		static bool first_run = true;
		static bool is_supported = false;

		if (first_run) {
			first_run = false;
			static const char *names[] = {
				"ConvertThreadToFiber",
				"CreateFiber",
				"DeleteFiber",
				"ConvertFiberToThread",
				"SwitchToFiber"};
			for (size_t i = 0; i < lengthof(names); i++) {
				if (stGetProcAddr(names[i]) == NULL) return false;
			}
			is_supported = true;
		}
		return is_supported;
	}

private:
	/**
	 * Get a function from kernel32.dll.
	 * @param name Function to get.
	 * @return Proc to the function, or NULL when not found.
	 */
	static FARPROC stGetProcAddr(const char *name)
	{
		static HMODULE hKernel = LoadLibraryA("kernel32.dll");
		return GetProcAddress(hKernel, name);
	}

	/**
	 * First function which is called within the fiber.
	 */
	static VOID CALLBACK stFiberProc(LPVOID fiber)
	{
		Fiber_Win32 *cur = (Fiber_Win32 *)fiber;
		cur->m_proc(cur->m_param);
	}

	/**
	 * Delete a fiber.
	 */
	void DeleteFiber()
	{
		typedef VOID (WINAPI *FnDeleteFiber)(LPVOID lpFiber);

		static FnDeleteFiber fnDeleteFiber = (FnDeleteFiber)stGetProcAddr("DeleteFiber");
		assert(fnDeleteFiber != NULL);

		fnDeleteFiber(this->m_fiber);
		this->m_fiber = NULL;
	}

	/**
	 * Convert a current thread to a fiber.
	 */
	void ConvertThreadToFiber()
	{
		typedef LPVOID (WINAPI *FnConvertThreadToFiber)(LPVOID lpParameter);

		static FnConvertThreadToFiber fnConvertThreadToFiber = (FnConvertThreadToFiber)stGetProcAddr("ConvertThreadToFiber");
		assert(fnConvertThreadToFiber != NULL);

		this->m_fiber = fnConvertThreadToFiber(this);
	}

	/**
	 * Create a new fiber.
	 */
	void CreateFiber()
	{
		typedef LPVOID (WINAPI *FnCreateFiber)(SIZE_T dwStackSize, LPFIBER_START_ROUTINE lpStartAddress, LPVOID lpParameter);

		static FnCreateFiber fnCreateFiber = (FnCreateFiber)stGetProcAddr("CreateFiber");
		assert(fnCreateFiber != NULL);

		this->m_fiber = fnCreateFiber(0, &stFiberProc, this);
	}

	/**
	 * Convert a fiber back to a thread.
	 */
	void ConvertFiberToThread()
	{
		typedef BOOL (WINAPI *FnConvertFiberToThread)();

		static FnConvertFiberToThread fnConvertFiberToThread = (FnConvertFiberToThread)stGetProcAddr("ConvertFiberToThread");
		assert(fnConvertFiberToThread != NULL);

		fnConvertFiberToThread();
		this->m_fiber = NULL;
	}
};

/* Initialize the static member of Fiber_Win32 */
/* static */ Fiber_Win32 *Fiber_Win32::s_main = NULL;

/* Include Fiber_Thread, as Win95 needs it */
#include "fiber_thread.cpp"

/* static */ Fiber *Fiber::New(FiberFunc proc, void *param)
{
	if (Fiber_Win32::IsSupported()) return new Fiber_Win32(proc, param);
	return new Fiber_Thread(proc, param);
}

/* static */ Fiber *Fiber::AttachCurrent(void *param)
{
	if (Fiber_Win32::IsSupported()) return new Fiber_Win32(param);
	return new Fiber_Thread(param);
}

/* static */ void *Fiber::GetCurrentFiberData()
{
	if (Fiber_Win32::IsSupported()) return ((Fiber *)::GetFiberData())->GetFiberData();
	return Fiber_Thread::GetCurrentFiber()->GetFiberData();
}