Library: verify thread self-recognition

...this function was also ported to the new wrapper,
and can be verified now in a much more succinct way.

''This completes porting of the thread-wrapper''
This commit is contained in:
Fischlurch 2023-09-29 23:59:26 +02:00
parent 1d625a01e0
commit d79e33f797
4 changed files with 46 additions and 39 deletions

View file

@ -196,7 +196,7 @@ namespace lib {
return *value_;
}
template<typename TY>
template<typename TY =RES>
TY
get() const
{

View file

@ -160,7 +160,7 @@ namespace lib {
, threadImpl_{forward<ARGS> (args)... }
{ }
/** determine if the currently executing code runs within this thread */
/** detect if the currently executing code runs within this thread */
bool invokedWithinThread() const;
void markThreadStart();
@ -294,6 +294,9 @@ namespace lib {
return Policy::isLive();
}
/** @return does this call happen from within this thread? */
using Policy::invokedWithinThread;
/** Create a new thread to execute the given operation.
* The new thread starts up synchronously, can't be cancelled and it can't be joined.

View file

@ -26,10 +26,7 @@
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/thread.hpp"
#include "lib/error.hpp"
using test::Test;
@ -38,35 +35,12 @@ using test::Test;
namespace lib {
namespace test {
namespace {
class TestThread
: Thread
{
public:
TestThread()
: Thread{"test Thread self recognition"
,[&]()
{
CHECK (invocation_happens_within_this_thread());
}}
{ }
bool
invocation_happens_within_this_thread()
{
return invokedWithinThread();
}
};
}
/******************************************************//**
* @test verify the ability of a thread to detect code
* executing within the thread itself.
*
* @see Thread::invokedWithinThread()
* @see thread::ThreadWrapper::invokedWithinThread()
* @see steam::control::DispatcherLoop::stateIsSynched()
*/
class ThreadWrapperSelfRecognitionTest_test : public Test
@ -75,11 +49,13 @@ namespace test {
virtual void
run (Arg)
{
TestThread testThread;
ThreadJoinable<bool> testThread{"Thread self detection"
,[&]{
return testThread.invokedWithinThread();
}};
CHECK (not testThread.invocation_happens_within_this_thread());
usleep(10); // give the thread a chance to run before shutdown
CHECK (not testThread.invokedWithinThread());
CHECK (testThread.join().get<bool>());
}
};

View file

@ -79016,7 +79016,7 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1695313755664" HGAP="-12" ID="ID_1075854169" MODIFIED="1695313767920" TEXT="Umstellung der Codebasis" VSHIFT="21">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#f0d5c5" COLOR="#990033" CREATED="1695313774171" ID="ID_1529952681" MODIFIED="1695340435812" TEXT="spezielle Abh&#xe4;ngigkeiten">
<node COLOR="#338800" CREATED="1695313774171" FOLDED="true" ID="ID_1529952681" MODIFIED="1696025089421" TEXT="spezielle Abh&#xe4;ngigkeiten">
<icon BUILTIN="help"/>
<node CREATED="1695313807671" ID="ID_1031643072" MODIFIED="1695313818061" TEXT="vault::Thread vs vault::ThreadJoinable">
<node CREATED="1695335467170" ID="ID_920953501" MODIFIED="1695335514560" TEXT="das erweist sich als praktische Abk&#xfc;rzung"/>
@ -79268,7 +79268,7 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1695394188133" ID="ID_1229328590" MODIFIED="1696015383325" TEXT="Umbau">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1695597011139" ID="ID_125138411" MODIFIED="1695859127749" TEXT="Kopie des bestehenden Thread-Wrappers umschreiben">
<node COLOR="#338800" CREATED="1695597011139" FOLDED="true" ID="ID_125138411" MODIFIED="1695859127749" TEXT="Kopie des bestehenden Thread-Wrappers umschreiben">
<linktarget COLOR="#6ebe5a" DESTINATION="ID_125138411" ENDARROW="Default" ENDINCLINATION="43;-18;" ID="Arrow_ID_195305087" SOURCE="ID_215388471" STARTARROW="None" STARTINCLINATION="-113;6;"/>
<icon BUILTIN="button_ok"/>
<node COLOR="#435e98" CREATED="1695597146529" ID="ID_938038908" MODIFIED="1695948178166" TEXT="std::thread liefert fast alle Funktionalit&#xe4;t">
@ -79954,7 +79954,7 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1695394205547" ID="ID_1719816317" MODIFIED="1696015377362" TEXT="Tests umstellen und modernisieren">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1695394270093" ID="ID_585437655" MODIFIED="1696007768225" TEXT="ThreadWrapper_test">
<node COLOR="#338800" CREATED="1695394270093" FOLDED="true" ID="ID_585437655" MODIFIED="1696007768225" TEXT="ThreadWrapper_test">
<icon BUILTIN="button_ok"/>
<node COLOR="#338800" CREATED="1695911786871" ID="ID_1371513124" MODIFIED="1695911794232" TEXT="l&#xe4;uft nach Umstellung">
<icon BUILTIN="button_ok"/>
@ -80020,7 +80020,7 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<icon BUILTIN="button_ok"/>
</node>
</node>
<node COLOR="#338800" CREATED="1695394665822" ID="ID_881973960" MODIFIED="1696015353416" TEXT="ThreadWrapperJoin_test">
<node COLOR="#338800" CREATED="1695394665822" FOLDED="true" ID="ID_881973960" MODIFIED="1696015353416" TEXT="ThreadWrapperJoin_test">
<icon BUILTIN="button_ok"/>
<node COLOR="#338800" CREATED="1695911786871" ID="ID_1004221223" MODIFIED="1695911801372" TEXT="l&#xe4;uft nach Umstellung">
<icon BUILTIN="button_ok"/>
@ -80040,8 +80040,8 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<node COLOR="#435e98" CREATED="1696010036948" ID="ID_440877573" MODIFIED="1696015360375" TEXT="Fehlerbehandlung"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1695394744028" ID="ID_1755648327" MODIFIED="1695394746744" TEXT="ThreadWrapperSelfRecognitionTest_test">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1695394744028" FOLDED="true" ID="ID_1755648327" MODIFIED="1696025182064" TEXT="ThreadWrapperSelfRecognition_test">
<icon BUILTIN="button_ok"/>
<node BACKGROUND_COLOR="#e0ceaa" COLOR="#690f14" CREATED="1695911804614" ID="ID_174546781" MODIFIED="1695911929623" TEXT="Scheitert mit Fehler">
<richcontent TYPE="NOTE"><html>
<head>
@ -80060,6 +80060,34 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</body>
</html></richcontent>
<icon BUILTIN="broken-line"/>
<icon BUILTIN="button_cancel"/>
<node CREATED="1696023930450" ID="ID_1910034301" MODIFIED="1696023935701" TEXT="nicht mehr reproduzierbar"/>
<node CREATED="1696023936220" ID="ID_1057418547" MODIFIED="1696023963855" TEXT="(inzwischen diverse Fixes zum Binding)"/>
</node>
<node COLOR="#77b3ba" CREATED="1696025158721" ID="ID_299445816" MODIFIED="1696025179668" TEXT="auch diesen Test kann man jetzt viel knapper schreiben">
<icon BUILTIN="smiley-oh"/>
</node>
<node COLOR="#435e98" CREATED="1696023965128" ID="ID_300657180" MODIFIED="1696025025547" TEXT="besser explizit den bool-Wert in den Main-Thread bringen">
<icon BUILTIN="idea"/>
<node CREATED="1696023991959" ID="ID_1226872902" MODIFIED="1696024013956" TEXT="jetzt wo wir das so ganz locker k&#xf6;nnen">
<icon BUILTIN="ksmiletris"/>
</node>
<node CREATED="1696024808008" ID="ID_609747317" MODIFIED="1696025021431" TEXT="zirkul&#xe4;rer Bezug &#x27f9; template parameter deduction unm&#xf6;glich">
<richcontent TYPE="NOTE"><html>
<head>
</head>
<body>
<p>
...das ist <i>interessant,</i>&#160;aber nicht kritisch; und zwar weil ich den Test jetzt umgeschrieben habe auf ein Lambda, und gar keine eigenst&#228;ndige Klasse mehr verwende &#8212; viel spannender ist, da&#223; der C++ - Compiler &#252;berhaupt schafft, solchen Code zu &#8222;knacken&#8220;
</p>
</body>
</html>
</richcontent>
</node>
<node COLOR="#338800" CREATED="1696025030399" ID="ID_838223706" MODIFIED="1696025042055" TEXT="auch gepr&#xfc;ft da&#xdf; es funktioniert (mal die Logik umgedreht)">
<icon BUILTIN="button_ok"/>
</node>
</node>
</node>
<node COLOR="#338800" CREATED="1695394753483" FOLDED="true" ID="ID_1338410455" MODIFIED="1695584550744" TEXT="SyncBarrier_test">