2018-03-14 18:12:34 +11:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "semaphore.hpp"
|
|
|
|
|
|
|
|
#include "../cast.hpp"
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <linux/futex.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <cruft/util/posix/except.hpp>
|
|
|
|
#include <limits>
|
|
|
|
|
2018-03-23 14:10:20 +11:00
|
|
|
using util::thread::semaphore;
|
2018-03-14 18:12:34 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
semaphore::semaphore ():
|
|
|
|
semaphore (1)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
semaphore::semaphore (int initial):
|
|
|
|
m_value (initial)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
2018-03-22 13:37:28 +11:00
|
|
|
semaphore::acquire (int count)
|
2018-03-14 18:12:34 +11:00
|
|
|
{
|
2018-03-22 13:37:28 +11:00
|
|
|
CHECK_GE (count, 0);
|
|
|
|
|
2018-03-14 18:12:34 +11:00
|
|
|
do {
|
|
|
|
int now = m_value;
|
|
|
|
|
|
|
|
// if our value is positive then attempt to decrement it and return,
|
|
|
|
// else retry because someone interfered with us.
|
2018-03-22 13:37:28 +11:00
|
|
|
if (now - count >= 0) {
|
|
|
|
if (m_value.compare_exchange_weak (now, now - count))
|
|
|
|
return now - count;
|
2018-03-14 18:12:34 +11:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the count doesn't appear to allow us to acquire. sleep until
|
|
|
|
// there's been a modification and retry.
|
|
|
|
if (-1 == sys_futex (&m_value, FUTEX_WAIT, now, nullptr, nullptr, 0)) {
|
|
|
|
switch (errno) {
|
2018-03-22 13:37:28 +11:00
|
|
|
case EAGAIN: break;
|
|
|
|
case EINTR: break;
|
|
|
|
default:
|
|
|
|
posix::error::throw_code ();
|
2018-03-14 18:12:34 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 13:37:28 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
semaphore::acquire (void)
|
|
|
|
{
|
|
|
|
return acquire (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-14 18:12:34 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
2018-03-22 13:39:16 +11:00
|
|
|
semaphore::release (int count)
|
2018-03-14 18:12:34 +11:00
|
|
|
{
|
2018-03-22 13:39:16 +11:00
|
|
|
auto res = m_value += count;
|
|
|
|
if (sys_futex (&m_value, FUTEX_WAKE, count, nullptr, nullptr, 0) < 0)
|
2018-03-14 18:12:34 +11:00
|
|
|
posix::error::throw_code ();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 13:39:16 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
semaphore::release (void)
|
|
|
|
{
|
|
|
|
return release (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-14 18:12:34 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
semaphore::value (void) const
|
|
|
|
{
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
semaphore::operator++ (void)
|
|
|
|
{
|
|
|
|
return release ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
semaphore::operator-- (void)
|
|
|
|
{
|
|
|
|
// we don't need to wake anyone because this will only serve to delay
|
|
|
|
// their wakeup.
|
|
|
|
return --m_value;
|
|
|
|
}
|