netperf: 'times_up' mechanism based on pthread

This commit is contained in:
Christian Helmuth 2019-02-07 10:45:09 +01:00 committed by Norman Feske
parent 9e9614af13
commit afc95e36e1
1 changed files with 55 additions and 1 deletions

View File

@ -11,14 +11,68 @@
* under the terms of the GNU Affero General Public License version 3.
*/
#include <pthread.h>
#include <unistd.h>
#include <base/log.h>
/* 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);
}