From afc95e36e1fce4021f8dcc18e04068bbed023eae Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Thu, 7 Feb 2019 10:45:09 +0100 Subject: [PATCH] netperf: 'times_up' mechanism based on pthread --- repos/ports/src/app/netperf/timer.cc | 56 +++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/repos/ports/src/app/netperf/timer.cc b/repos/ports/src/app/netperf/timer.cc index 1b0986035..6121ceea2 100644 --- a/repos/ports/src/app/netperf/timer.cc +++ b/repos/ports/src/app/netperf/timer.cc @@ -11,14 +11,68 @@ * under the terms of the GNU Affero General Public License version 3. */ +#include +#include #include /* defined in "ports/contrib/netperf/src/netlib.c" */ extern "C" int times_up; +namespace { + + struct Timer_thread + { + pthread_t _pthread { }; + pthread_attr_t _attr { }; + pthread_mutex_t _mutex { PTHREAD_MUTEX_INITIALIZER }; + + int _seconds_left = 0; + + void _entry() + { + for (;;) { + sleep(1); + + pthread_mutex_lock(&_mutex); + if (_seconds_left) { + --_seconds_left; + if (_seconds_left == 0) { + times_up = true; + } + } + pthread_mutex_unlock(&_mutex); + } + } + + static void *_entry(void *arg) + { + Timer_thread &myself = *(Timer_thread *)arg; + myself._entry(); + + return nullptr; + } + + Timer_thread() + { + pthread_mutex_init(&_mutex, nullptr); + pthread_create(&_pthread, &_attr, _entry, this); + } + + void schedule_timeout(int seconds) + { + pthread_mutex_lock(&_mutex); + times_up = false; + _seconds_left = seconds; + pthread_mutex_unlock(&_mutex); + } + }; +} + extern "C" void start_timer(int time) { - Genode::warning(__func__, " not implemented, 'times_up' is never updated"); + static Timer_thread timer_thread { }; + + timer_thread.schedule_timeout(time); }