author | truebrain |
Fri, 18 Jul 2008 10:15:16 +0000 | |
branch | noai |
changeset 11168 | 3842648184cd |
parent 11095 | a0da321a39b3 |
permissions | -rw-r--r-- |
9859
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
1 |
/* $Id$ */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
2 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
3 |
/** @file fiber_thread.cpp ThreadObject implementation of Fiber. */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
4 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
5 |
#include "stdafx.h" |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
6 |
#include "fiber.hpp" |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
7 |
#include "thread.h" |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
8 |
#include <stdlib.h> |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
9 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
10 |
class Fiber_Thread : public Fiber { |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
11 |
private: |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
12 |
ThreadObject *m_thread; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
13 |
FiberFunc m_proc; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
14 |
void *m_param; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
15 |
bool m_attached; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
16 |
ThreadSemaphore *m_sem; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
17 |
bool m_kill; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
18 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
19 |
static Fiber_Thread *s_current; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
20 |
static Fiber_Thread *s_main; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
21 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
22 |
public: |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
23 |
/** |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
24 |
* Create a ThreadObject fiber and start it, calling proc(param). |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
25 |
*/ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
26 |
Fiber_Thread(FiberFunc proc, void *param) : |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
27 |
m_thread(NULL), |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
28 |
m_proc(proc), |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
29 |
m_param(param), |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
30 |
m_attached(false), |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
31 |
m_kill(false) |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
32 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
33 |
this->m_sem = ThreadSemaphore::New(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
34 |
/* Create a thread and start stFiberProc */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
35 |
this->m_thread = ThreadObject::New(&stFiberProc, this); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
36 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
37 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
38 |
/** |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
39 |
* Create a ThreadObject fiber and attach current thread to it. |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
40 |
*/ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
41 |
Fiber_Thread(void *param) : |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
42 |
m_thread(NULL), |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
43 |
m_proc(NULL), |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
44 |
m_param(param), |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
45 |
m_attached(true), |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
46 |
m_kill(false) |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
47 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
48 |
this->m_sem = ThreadSemaphore::New(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
49 |
/* Attach the current thread to this Fiber */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
50 |
this->m_thread = ThreadObject::AttachCurrent(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
51 |
/* We are the current thread */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
52 |
if (s_current == NULL) s_current = this; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
53 |
if (s_main == NULL) s_main = this; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
54 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
55 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
56 |
~Fiber_Thread() |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
57 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
58 |
/* Remove the thread if needed */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
59 |
if (this->m_thread != NULL) { |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
60 |
assert(this->m_attached || !this->m_thread->IsRunning()); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
61 |
delete this->m_thread; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
62 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
63 |
/* Remove the semaphore */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
64 |
delete this->m_sem; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
65 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
66 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
67 |
/* virtual */ void SwitchToFiber() |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
68 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
69 |
/* You can't switch to yourself */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
70 |
assert(s_current != this); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
71 |
Fiber_Thread *cur = s_current; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
72 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
73 |
/* Continue the execution of 'this' Fiber */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
74 |
this->m_sem->Set(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
75 |
/* Hold the execution of the current Fiber */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
76 |
cur->m_sem->Wait(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
77 |
if (this->m_kill) { |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
78 |
/* If the thread we switched too was killed, join it so it can finish quiting */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
79 |
this->m_thread->Join(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
80 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
81 |
/* If we continue, we are the current thread */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
82 |
s_current = cur; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
83 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
84 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
85 |
/* virtual */ void Exit() |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
86 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
87 |
/* Kill off our thread */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
88 |
this->m_kill = true; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
89 |
this->m_thread->Exit(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
90 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
91 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
92 |
/* virtual */ bool IsRunning() |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
93 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
94 |
if (this->m_thread == NULL) return false; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
95 |
return this->m_thread->IsRunning(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
96 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
97 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
98 |
/* virtual */ void *GetFiberData() |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
99 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
100 |
return this->m_param; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
101 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
102 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
103 |
static Fiber_Thread *GetCurrentFiber() |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
104 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
105 |
return s_current; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
106 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
107 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
108 |
private: |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
109 |
/** |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
110 |
* First function which is called within the fiber. |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
111 |
*/ |
10867
5de2923d6e59
(svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents:
9859
diff
changeset
|
112 |
static void stFiberProc(void *fiber) |
9859
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
113 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
114 |
Fiber_Thread *cur = (Fiber_Thread *)fiber; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
115 |
/* Now suspend the thread until we get SwitchToFiber() for the first time */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
116 |
cur->m_sem->Wait(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
117 |
/* If we continue, we are the current thread */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
118 |
s_current = cur; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
119 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
120 |
try { |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
121 |
cur->m_proc(cur->m_param); |
11095
a0da321a39b3
(svn r13652) [NoAI] -Fix: throw thread-exit-signal with a custom class, not with the value '0', which might be captured by a lot of other layers
truebrain
parents:
10867
diff
changeset
|
122 |
} catch (OTTDThreadExitSignal e) { |
9859
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
123 |
/* Unlock the main thread */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
124 |
s_main->m_sem->Set(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
125 |
throw; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
126 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
127 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
128 |
}; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
129 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
130 |
/* Initialize the static member of Fiber_Thread */ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
131 |
/* static */ Fiber_Thread *Fiber_Thread::s_current = NULL; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
132 |
/* static */ Fiber_Thread *Fiber_Thread::s_main = NULL; |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
133 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
134 |
#ifndef WIN32 |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
135 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
136 |
/* static */ Fiber *Fiber::New(FiberFunc proc, void *param) |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
137 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
138 |
return new Fiber_Thread(proc, param); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
139 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
140 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
141 |
/* static */ Fiber *Fiber::AttachCurrent(void *param) |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
142 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
143 |
return new Fiber_Thread(param); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
144 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
145 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
146 |
/* static */ void *Fiber::GetCurrentFiberData() |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
147 |
{ |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
148 |
return Fiber_Thread::GetCurrentFiber()->GetFiberData(); |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
149 |
} |
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
150 |
|
81621c6ba0e9
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents:
diff
changeset
|
151 |
#endif /* WIN32 */ |