From d74f1447f37c686e3fbcb60ab176bb681e25d01e Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 6 Jan 2017 23:26:33 +0100 Subject: [PATCH] Library: thread self recognition feature defined (#1054) --- src/backend/thread-wrapper.hpp | 9 +- tests/backend/thread-wrapper-join-test.cpp | 4 +- .../thread-wrapper-self-recognition-test.cpp | 90 +++++++++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 tests/backend/thread-wrapper-self-recognition-test.cpp diff --git a/src/backend/thread-wrapper.hpp b/src/backend/thread-wrapper.hpp index 835321ccc..3156e9b5c 100644 --- a/src/backend/thread-wrapper.hpp +++ b/src/backend/thread-wrapper.hpp @@ -80,7 +80,7 @@ namespace backend { * # failures in the thread function * The operation started in the new thread is protected by a top-level catch block. * Error states or caught exceptions can be propagated through the lumiera_error - * state flag, when using the \c join() facility. By invoking \join().maybeThrow() + * state flag, when using ThreadJoinable::join(). By invoking `join().maybeThrow()` * on a join-able thread, exceptions can be propagated. * @note any errorstate or caught exception detected on termination of a standard * async Thread is considered a violation of policy and will result in emergency @@ -221,6 +221,13 @@ namespace backend { ////////////////////////////////////////////////////////TICKET #1054 : consider to call safeguard here, to ensure this is called from within the thread lumiera_thread_sync (); } + + protected: + bool + invokedWithinThread() + { + UNIMPLEMENTED ("thread self recognition"); + } }; diff --git a/tests/backend/thread-wrapper-join-test.cpp b/tests/backend/thread-wrapper-join-test.cpp index 36ba34851..dbfffa286 100644 --- a/tests/backend/thread-wrapper-join-test.cpp +++ b/tests/backend/thread-wrapper-join-test.cpp @@ -24,10 +24,8 @@ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp" -#include "lib/symbol.hpp" #include "backend/thread-wrapper.hpp" #include "lib/error.hpp" -#include "lib/sync.hpp" #include @@ -126,7 +124,7 @@ namespace test { , bind (&ThreadWrapperJoin_test::theAction, this, DESTRUCTION_CODE) ); - CHECK (!thread2.join().isValid() ); // can check success without throwing + CHECK (not thread2.join().isValid()); // can check success without throwing } }; diff --git a/tests/backend/thread-wrapper-self-recognition-test.cpp b/tests/backend/thread-wrapper-self-recognition-test.cpp new file mode 100644 index 000000000..f247c412a --- /dev/null +++ b/tests/backend/thread-wrapper-self-recognition-test.cpp @@ -0,0 +1,90 @@ +/* + ThreadWrapperSelfRecognitionTest(Test) - detect when code is running within a thread + + Copyright (C) Lumiera.org + 2017, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +* *****************************************************/ + + +#include "lib/test/run.hpp" +#include "lib/test/test-helper.hpp" + +#include "backend/thread-wrapper.hpp" +#include "lib/error.hpp" + +using test::Test; + + + +namespace backend { +namespace test { + + using lumiera::error::LUMIERA_ERROR_LOGIC; + namespace { + + class TestThread + : Thread + { + void + doIt() + { + CHECK (invocation_happens_within_this_thread()); + } + + public: + TestThread() + : Thread{"test Thread self recognition", [&]() { doIt(); }} + { } + + 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 proc::control::DispatcherLoop::stateIsSynched() + */ + class ThreadWrapperSelfRecognitionTest_test : public Test + { + + virtual void + run (Arg) + { + TestThread testThread; + + CHECK (not testThread.invocation_happens_within_this_thread()); + } + }; + + + + /** Register this test class... */ + LAUNCHER (ThreadWrapperSelfRecognitionTest_test, "function common"); + + + +}} // namespace backend::test