MCPcopy Create free account
hub / github.com/DISTORTEC/distortos / phase3

Function phase3

test/Thread/ThreadOperationsTestCase.cpp:151–265  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

149 */
150
151bool phase3()
152{
153#ifdef DISTORTOS_THREAD_DETACH_ENABLE
154
155 const auto allocatedMemory = mallinfo().uordblks;
156 const auto lambda =
157 [](int& sharedRet)
158 {
159 sharedRet = ThisThread::detach();
160 };
161
162 // attempt to detach static thread must fail with ENOTSUP
163 {
164 int sharedRet {};
165 auto staticThread = makeAndStartStaticThread<testThreadStackSize>(1, lambda, std::ref(sharedRet));
166 bool result {true};
167 if (staticThread.detach() != ENOTSUP) // static thread cannot be detached
168 result = false;
169 if (staticThread.getState() != ThreadState::runnable)
170 result = false;
171 if (staticThread.getIdentifier() == ThreadIdentifier{})
172 result = false;
173 if (staticThread.join() != 0 || result == false || sharedRet != ENOTSUP) // self-detach must fail too
174 return false;
175 }
176
177 // detaching dynamic thread that is not yet started must succeed, but the thread is just deleted and does not run
178 {
179 int sharedRet {0x2e981e48};
180 auto dynamicThread = makeDynamicThread({testThreadStackSize, 1}, lambda, std::ref(sharedRet));
181 bool result {true};
182 if (dynamicThread.detach() != 0)
183 result = false;
184 if (dynamicThread.start() != EINVAL) // detached thread cannot be started
185 result = false;
186 if (dynamicThread.getState() != ThreadState::detached)
187 result = false;
188 if (dynamicThread.getIdentifier() != ThreadIdentifier{})
189 result = false;
190 if (dynamicThread.join() != EINVAL || result == false || sharedRet != 0x2e981e48 ||
191 dynamicThread.detach() != EINVAL) // detached thread cannot be joined, self-detach must fail
192 return false;
193 }
194
195 if (mallinfo().uordblks != allocatedMemory) // dynamic memory must be deallocated after each test phase
196 return false;
197
198 // detaching dynamic thread that is started, but not yet terminated, must succeed
199 {
200 int sharedRet {0x0f0dad58};
201 auto dynamicThread = makeAndStartDynamicThread({testThreadStackSize, 1}, lambda, std::ref(sharedRet));
202 bool result {true};
203 if (dynamicThread.detach() != 0)
204 result = false;
205 if (dynamicThread.getState() != ThreadState::detached)
206 result = false;
207 if (dynamicThread.getIdentifier() != ThreadIdentifier{})
208 result = false;

Callers

nothing calls this directly

Calls 9

detachFunction · 0.85
makeDynamicThreadFunction · 0.85
waitForNextTickFunction · 0.85
detachMethod · 0.45
getStateMethod · 0.45
getIdentifierMethod · 0.45
joinMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected