2009-01-18 16:59:24 +01:00
|
|
|
/*
|
|
|
|
|
ThreadWrapperJoin(Test) - wait blocking on termination of a thread
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-01-18 16:59:24 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-01-18 16:59:24 +01:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2009-01-18 16:59:24 +01:00
|
|
|
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.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-01-18 16:59:24 +01:00
|
|
|
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.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-01-18 16:59:24 +01:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2010-01-24 14:05:32 +01:00
|
|
|
#include "lib/test/test-helper.hpp"
|
2009-01-18 16:59:24 +01:00
|
|
|
|
2009-09-24 23:02:40 +02:00
|
|
|
#include "lib/symbol.hpp"
|
2009-01-18 16:59:24 +01:00
|
|
|
#include "backend/thread-wrapper.hpp"
|
|
|
|
|
#include "lib/error.hpp"
|
|
|
|
|
#include "lib/sync.hpp"
|
|
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
#include <functional>
|
2009-01-18 16:59:24 +01:00
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
using std::bind;
|
2009-01-18 16:59:24 +01:00
|
|
|
using test::Test;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-02-01 01:01:44 +01:00
|
|
|
namespace backend {
|
2010-01-24 14:05:32 +01:00
|
|
|
namespace test {
|
2009-01-18 16:59:24 +01:00
|
|
|
|
2010-01-24 14:05:32 +01:00
|
|
|
using lumiera::error::LUMIERA_ERROR_LOGIC;
|
|
|
|
|
namespace {
|
2009-01-18 16:59:24 +01:00
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
const int DESTRUCTION_CODE = 23;
|
2009-01-18 16:59:24 +01:00
|
|
|
|
2010-01-24 14:05:32 +01:00
|
|
|
LUMIERA_ERROR_DEFINE(SPECIAL, "grandiose exception");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
/***********************************************************************//**
|
2010-01-24 14:05:32 +01:00
|
|
|
* @test use the Lumiera backend to create some new threads, additionally
|
|
|
|
|
* synchronising with these child threads and waiting for termination.
|
|
|
|
|
*
|
|
|
|
|
* @see backend::Thread
|
|
|
|
|
* @see threads.h
|
|
|
|
|
*/
|
|
|
|
|
class ThreadWrapperJoin_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
run (Arg)
|
|
|
|
|
{
|
|
|
|
|
simpleUse ();
|
|
|
|
|
wrongUse ();
|
|
|
|
|
getError ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
volatile int aValue_; ///< state to be modified by the other thread
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
theAction (int secretValue) ///< to be run in a new thread...
|
|
|
|
|
{
|
|
|
|
|
usleep (100000); // pause 100ms prior to modifying
|
|
|
|
|
|
|
|
|
|
if (DESTRUCTION_CODE == secretValue)
|
2010-01-30 07:10:07 +01:00
|
|
|
LUMIERA_ERROR_SET(test, SPECIAL, 0);
|
2010-01-24 14:05:32 +01:00
|
|
|
else
|
|
|
|
|
aValue_ = secretValue+42;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
simpleUse ()
|
|
|
|
|
{
|
|
|
|
|
aValue_=0;
|
|
|
|
|
int mySecret = (rand() % 1000) - 500;
|
|
|
|
|
|
|
|
|
|
ThreadJoinable newThread("test Thread joining-1"
|
|
|
|
|
, bind (&ThreadWrapperJoin_test::theAction, this, mySecret)
|
|
|
|
|
);
|
|
|
|
|
newThread.join(); // blocks until theAction() is done
|
|
|
|
|
|
|
|
|
|
CHECK (aValue_ == mySecret+42);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
wrongUse ()
|
|
|
|
|
{
|
|
|
|
|
ThreadJoinable newThread("test Thread joining-2"
|
|
|
|
|
, bind (&ThreadWrapperJoin_test::theAction, this, 1234)
|
|
|
|
|
);
|
|
|
|
|
newThread.join(); // blocks until theAction() is done
|
|
|
|
|
|
|
|
|
|
VERIFY_ERROR(LOGIC, newThread.join() );
|
|
|
|
|
VERIFY_ERROR(LOGIC, newThread.join() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
getError()
|
|
|
|
|
{
|
2010-02-14 23:39:15 +01:00
|
|
|
ThreadJoinable thread1("test Thread joining-3"
|
|
|
|
|
, bind (&ThreadWrapperJoin_test::theAction, this, DESTRUCTION_CODE)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
VERIFY_ERROR(SPECIAL, thread1.join().maybeThrow() );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ThreadJoinable thread2("test Thread joining-4"
|
|
|
|
|
, bind (&ThreadWrapperJoin_test::theAction, this, DESTRUCTION_CODE)
|
|
|
|
|
);
|
2010-01-24 14:05:32 +01:00
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (!thread2.join().isValid() ); // can check success without throwing
|
2010-01-24 14:05:32 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (ThreadWrapperJoin_test, "function common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace backend::test
|