genode/repos/base/src/test/timed_semaphore/main.cc
Norman Feske bf62d6b896 Move timer from os to base repository
Since the timer and timeout handling is part of the base library (the
dynamic linker), it belongs to the base repository.

Besides moving the timer and its related infrastructure (alarm, timeout
libs, tests) to the base repository, this patch also moves the timer
from the 'drivers' subdirectory directly to 'src' and disamibuates the
timer's build locations for the various kernels. Otherwise the different
timer implementations could interfere with each other when using one
build directory with multiple kernels.

Note that this patch changes the include paths for the former os/timer,
os/alarm.h, os/duration.h, and os/timed_semaphore.h to base/.

Issue #3101
2019-01-14 12:33:57 +01:00

79 lines
1.8 KiB
C++

/*
* \brief Test for the timed-semaphore
* \author Stefan Kalkowski
* \author Martin Stein
* \date 2010-03-05
*/
/*
* Copyright (C) 2010-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
/* Genode includes */
#include <timer_session/connection.h>
#include <base/timed_semaphore.h>
#include <base/thread.h>
#include <base/component.h>
using namespace Genode;
struct Test : Thread
{
struct Failed : Exception { };
unsigned id;
Timer::Connection wakeup_timer;
unsigned const wakeup_period;
Timed_semaphore sem { };
bool stop_wakeup { false };
Lock wakeup_stopped { Lock::LOCKED };
bool got_timeouts { false };
void entry()
{
do {
wakeup_timer.msleep(wakeup_period);
sem.up();
} while (!stop_wakeup);
wakeup_stopped.unlock();
}
Test(Env &env, bool timeouts, unsigned id, char const *brief)
: Thread(env, "wakeup", 1024 * sizeof(addr_t)), id(id), wakeup_timer(env),
wakeup_period(timeouts ? 1000 : 100)
{
log("\nTEST ", id, ": ", brief, "\n");
Thread::start();
try { for (int i = 0; i < 10; i++) { sem.down(timeouts ? 100 : 1000); } }
catch (Timeout_exception) { got_timeouts = true; }
if (timeouts != got_timeouts) {
throw Failed(); }
stop_wakeup = true;
wakeup_stopped.lock();
}
~Test() { log("\nTEST ", id, " finished\n"); }
};
struct Main
{
Constructible<Test> test { };
Main(Env &env)
{
log("--- Timed semaphore test ---");
test.construct(env, false, 1, "without timeouts"); test.destruct();
test.construct(env, true, 2, "with timeouts"); test.destruct();
log("--- Timed semaphore test finished ---");
}
};
void Component::construct(Genode::Env &env) { static Main main(env); }