2010-11-16 14:47:38 -05:00
|
|
|
/*
|
2019-08-02 23:10:55 -04:00
|
|
|
* Copyright (C) 2010-2014 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2010-11-16 14:47:38 -05:00
|
|
|
|
|
|
|
#include "pbd/semutils.h"
|
|
|
|
#include "pbd/failed_constructor.h"
|
|
|
|
|
2022-05-11 17:42:33 -04:00
|
|
|
#ifdef USE_FUTEX_SEMAPHORE
|
|
|
|
#include <errno.h>
|
|
|
|
#include <linux/futex.h>
|
2022-05-11 22:49:11 -04:00
|
|
|
#include <sched.h>
|
2022-05-11 17:42:33 -04:00
|
|
|
#include <syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 14:47:38 -05:00
|
|
|
using namespace PBD;
|
|
|
|
|
2015-12-02 14:18:48 -05:00
|
|
|
Semaphore::Semaphore (const char* name, int val)
|
2010-11-16 14:47:38 -05:00
|
|
|
{
|
2015-12-02 07:38:59 -05:00
|
|
|
#ifdef WINDOWS_SEMAPHORE
|
2021-05-27 16:13:40 -04:00
|
|
|
(void) name; /* stop warning */
|
|
|
|
if ((_sem = CreateSemaphore(NULL, val, 32767, NULL)) == NULL) {
|
2013-07-11 12:46:05 -04:00
|
|
|
throw failed_constructor ();
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif __APPLE__
|
2010-11-16 14:47:38 -05:00
|
|
|
if ((_sem = sem_open (name, O_CREAT, 0600, val)) == (sem_t*) SEM_FAILED) {
|
|
|
|
throw failed_constructor ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this semaphore does not exist for IPC */
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2010-11-16 14:47:38 -05:00
|
|
|
if (sem_unlink (name)) {
|
|
|
|
throw failed_constructor ();
|
|
|
|
}
|
|
|
|
|
2022-05-11 17:42:33 -04:00
|
|
|
#elif defined USE_FUTEX_SEMAPHORE
|
|
|
|
(void)name; /* stop warning */
|
|
|
|
_value = val;
|
2010-11-16 14:47:38 -05:00
|
|
|
#else
|
2013-11-04 21:32:41 -05:00
|
|
|
(void) name; /* stop gcc warning on !Apple systems */
|
|
|
|
|
2010-11-16 15:01:01 -05:00
|
|
|
if (sem_init (&_sem, 0, val)) {
|
2010-11-16 14:47:38 -05:00
|
|
|
throw failed_constructor ();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-12-02 14:18:48 -05:00
|
|
|
Semaphore::~Semaphore ()
|
2010-11-16 14:47:38 -05:00
|
|
|
{
|
2015-12-02 07:38:59 -05:00
|
|
|
#ifdef WINDOWS_SEMAPHORE
|
2013-07-11 12:46:05 -04:00
|
|
|
CloseHandle(_sem);
|
|
|
|
#elif __APPLE__
|
2010-11-16 14:47:38 -05:00
|
|
|
sem_close (ptr_to_sem());
|
|
|
|
#endif
|
|
|
|
}
|
2013-07-11 12:46:05 -04:00
|
|
|
|
2015-12-02 07:38:59 -05:00
|
|
|
#ifdef WINDOWS_SEMAPHORE
|
2013-07-11 12:46:05 -04:00
|
|
|
|
|
|
|
int
|
2015-12-02 14:18:48 -05:00
|
|
|
Semaphore::signal ()
|
2013-07-11 12:46:05 -04:00
|
|
|
{
|
|
|
|
// non-zero on success, opposite to posix
|
|
|
|
return !ReleaseSemaphore(_sem, 1, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-12-02 14:18:48 -05:00
|
|
|
Semaphore::wait ()
|
2013-07-11 12:46:05 -04:00
|
|
|
{
|
|
|
|
DWORD result = 0;
|
|
|
|
result = WaitForSingleObject(_sem, INFINITE);
|
2021-05-27 16:17:15 -04:00
|
|
|
return (result == WAIT_OBJECT_0 ? 0 : -1);
|
2013-07-11 12:46:05 -04:00
|
|
|
}
|
|
|
|
|
2017-04-22 17:00:53 -04:00
|
|
|
int
|
|
|
|
Semaphore::reset ()
|
|
|
|
{
|
|
|
|
int rv = -1;
|
|
|
|
DWORD result;
|
|
|
|
do {
|
|
|
|
++rv;
|
|
|
|
result = WaitForSingleObject(_sem, 0);
|
|
|
|
} while (result == WAIT_OBJECT_0);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2022-05-11 17:42:33 -04:00
|
|
|
#elif defined USE_FUTEX_SEMAPHORE
|
|
|
|
|
|
|
|
int
|
|
|
|
Semaphore::signal ()
|
|
|
|
{
|
2022-05-11 22:49:11 -04:00
|
|
|
if (std::atomic_fetch_add_explicit (&_value, 1, std::memory_order_relaxed) < 0) {
|
2022-05-30 15:56:49 -04:00
|
|
|
while (syscall (SYS_futex, &_futex, FUTEX_WAKE_PRIVATE, 1, NULL, NULL, 0) < 1) {
|
2022-05-11 22:49:11 -04:00
|
|
|
sched_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2022-05-11 17:42:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Semaphore::wait ()
|
|
|
|
{
|
2022-05-11 22:49:11 -04:00
|
|
|
if (std::atomic_fetch_sub_explicit (&_value, 1, std::memory_order_relaxed) <= 0) {
|
2022-05-30 15:56:49 -04:00
|
|
|
syscall(SYS_futex, &_futex, FUTEX_WAIT_PRIVATE, _futex, NULL, 0, 0);
|
2022-05-11 17:42:33 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Semaphore::reset ()
|
|
|
|
{
|
2022-05-11 22:49:11 -04:00
|
|
|
int value = _value;
|
|
|
|
_value = 0;
|
|
|
|
return value;
|
2022-05-11 17:42:33 -04:00
|
|
|
}
|
|
|
|
|
2013-07-11 12:46:05 -04:00
|
|
|
#endif
|