2018-03-14 14:52:02 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-03-14 14:52:02 +11:00
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "event.hpp"
|
|
|
|
#include "../cast.hpp"
|
2018-05-12 14:28:23 +10:00
|
|
|
#include "../posix/except.hpp"
|
2018-03-14 14:52:02 +11:00
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <linux/futex.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <limits>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::thread::event;
|
2018-03-14 14:52:02 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
static long
|
|
|
|
sys_futex (void *addr1, int op, int val1, struct timespec *timeout, void *addr2, int val3)
|
|
|
|
{
|
|
|
|
return syscall (SYS_futex, addr1, op | FUTEX_PRIVATE_FLAG, val1, timeout, addr2, val3);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
event::event ():
|
|
|
|
value (0)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
event::wait (void)
|
|
|
|
{
|
|
|
|
for (auto observed = value.load (); observed == value.load (); ) {
|
|
|
|
auto res = sys_futex (&value, FUTEX_WAIT, observed, nullptr, nullptr, 0);
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN: return;
|
|
|
|
case EINTR: continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
posix::error::throw_code ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
2018-08-16 12:10:05 +10:00
|
|
|
event::notify_all (void)
|
2018-03-14 14:52:02 +11:00
|
|
|
{
|
|
|
|
return notify (std::numeric_limits<int>::max ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
event::notify (int count)
|
|
|
|
{
|
|
|
|
++value;
|
|
|
|
auto res = sys_futex (&value, FUTEX_WAKE, count, nullptr, nullptr, 0);
|
|
|
|
if (res < 0)
|
|
|
|
posix::error::throw_code ();
|
2018-08-05 14:42:02 +10:00
|
|
|
return cruft::cast::narrow<int> (res);
|
2018-03-14 14:52:02 +11:00
|
|
|
}
|