-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkiwiStdThreadImpl.hpp
More file actions
187 lines (158 loc) · 3.99 KB
/
Copy pathkiwiStdThreadImpl.hpp
File metadata and controls
187 lines (158 loc) · 3.99 KB
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
// Implementation header
#ifndef LIBKIWI_CORE_STD_THREAD_IMPL_HPP
#define LIBKIWI_CORE_STD_THREAD_IMPL_HPP
// Declaration header
#ifndef LIBKIWI_CORE_STD_THREAD_H
#include <libkiwi/core/kiwiStdThread.h>
#endif
#include <libkiwi/prim/kiwiBitCast.h>
namespace kiwi {
//! @addtogroup libkiwi_core
//! @{
/**
* @name Non-member function
*/
/**@{*/
/**
* @brief Constructor
*
* @param pFunc Static, no-parameter function
*/
template <typename TRet> StdThread::StdThread(TRet (*pFunc)()) {
K_ASSERT_PTR(pFunc);
SetFunction(pFunc);
Start();
}
/**
* @brief Constructor
*
* @param pFunc Static, single-parameter function
* @param pArg Function argument
*/
template <typename TRet>
StdThread::StdThread(TRet (*pFunc)(StdThread::Param), StdThread::Param pArg) {
K_ASSERT_PTR(pFunc);
SetFunction(pFunc);
SetGPR(3, BitCast<u32>(pArg));
Start();
}
/**@}*/
/**
* @name Member function (non-const)
*/
/**@{*/
/**
* @brief Constructor
*
* @param pFunc No-parameter member function
* @param rObj Class instance
*/
template <typename TRet, typename TClass>
StdThread::StdThread(TRet (TClass::*pFunc)(), TClass& rObj) {
K_ASSERT(pFunc);
SetMemberFunction(pFunc, rObj);
Start();
}
/**
* @brief Constructor
*
* @param pFunc Single-parameter member function
* @param rObj Class instance
* @param pArg Function argument
*/
template <typename TRet, typename TClass>
StdThread::StdThread(TRet (TClass::*pFunc)(StdThread::Param), TClass& rObj,
StdThread::Param pArg) {
K_ASSERT(pFunc);
SetMemberFunction(pFunc, rObj);
SetGPR(4, BitCast<u32>(pArg));
Start();
}
/**@}*/
/**
* @name Member function (const)
*/
/**@{*/
/**
* @brief Constructor
*
* @param pFunc No-parameter, const member function
* @param rObj Class instance
*/
template <typename TRet, typename TClass>
StdThread::StdThread(TRet (TClass::*pFunc)() const, const TClass& rObj) {
K_ASSERT(pFunc);
SetMemberFunction(pFunc, rObj);
Start();
}
/**
* @brief Constructor
*
* @param pFunc Single-parameter, const member function
* @param rObj Class instance
* @param pArg Function argument
*/
template <typename TRet, typename TClass>
StdThread::StdThread(TRet (TClass::*pFunc)(StdThread::Param) const,
const TClass& rObj, StdThread::Param pArg) {
K_ASSERT(pFunc);
SetMemberFunction(pFunc, rObj);
SetGPR(4, BitCast<u32>(pArg));
Start();
}
namespace detail {
//! @addtogroup libkiwi_core
//! @{
/**
* @brief Pointer-to-member-function (PTMF)
*/
struct MemberFunction {
s32 toff; //!< 'This' pointer offset for target object type
s32 voff; //!< Vtable offset into class
union {
s32 foff; //!< Function offset into vtable
void* pAddr; //!< Raw function address (if voff is -1)
};
};
/**
* @brief Sets a member function to run on this thread
*
* @param pFunc Function
* @param rObj Class instance
*/
template <typename TFunc, typename TClass>
K_DONT_INLINE void StdThreadImpl::SetMemberFunction(TFunc pFunc,
const TClass& rObj) {
K_STATIC_ASSERT_EX(sizeof(TFunc) == sizeof(MemberFunction),
"Not a member function");
register const MemberFunction* pPtmf;
register u32 self;
// clang-format off
asm volatile {
mr pPtmf, r4 // pFunc -> pPtmf
mr self, r5 // rObj -> self
}
K_ASSERT_PTR(pPtmf);
K_ASSERT(self != 0);
// clang-format on
K_ASSERT_PTR(mpOSThread);
K_ASSERT(mpOSThread->state == OS_THREAD_STATE_READY);
// Adjust this pointer
self += pPtmf->toff;
SetGPR(3, self);
// Non-virtual function?
if (pPtmf->voff == -1) {
SetFunction(pPtmf->pAddr);
return;
}
// Find virtual function table
const void** pVtbl = BitCast<const void**>(self + pPtmf->voff);
// Find virtual function address
K_ASSERT(pPtmf->foff >= 0);
SetFunction(pVtbl[pPtmf->foff / sizeof(void*)]);
}
//! @}
} // namespace detail
//! @}
} // namespace kiwi
#endif