2012-04-12 11:59:29 +02:00
|
|
|
/*
|
|
|
|
* \brief POSIX semaphore implementation
|
|
|
|
* \author Christian Prochaska
|
2020-02-05 15:47:09 +01:00
|
|
|
* \author Christian Helmuth
|
2012-04-12 11:59:29 +02:00
|
|
|
* \date 2012-03-12
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2020-02-05 15:47:09 +01:00
|
|
|
* Copyright (C) 2012-2020 Genode Labs GmbH
|
2012-04-12 11:59:29 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2012-04-12 11:59:29 +02:00
|
|
|
*/
|
|
|
|
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
/* Genode includes */
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
#include <base/log.h>
|
2017-04-19 21:25:03 +02:00
|
|
|
#include <base/semaphore.h>
|
2012-04-12 11:59:29 +02:00
|
|
|
#include <semaphore.h>
|
2020-02-05 15:47:09 +01:00
|
|
|
#include <libc/allocator.h>
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
|
|
|
|
/* libc includes */
|
2020-02-05 15:47:09 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
2012-04-12 11:59:29 +02:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
/* libc-internal includes */
|
2020-02-05 15:47:09 +01:00
|
|
|
#include <internal/monitor.h>
|
|
|
|
#include <internal/errno.h>
|
2019-09-19 20:37:17 +02:00
|
|
|
#include <internal/types.h>
|
2020-02-05 15:47:09 +01:00
|
|
|
#include <internal/time.h>
|
|
|
|
#include <internal/init.h>
|
2019-09-19 20:37:17 +02:00
|
|
|
|
|
|
|
using namespace Libc;
|
2012-04-12 11:59:29 +02:00
|
|
|
|
2019-05-23 12:30:18 +02:00
|
|
|
|
2020-02-05 15:47:09 +01:00
|
|
|
static Monitor *_monitor_ptr;
|
|
|
|
|
|
|
|
|
|
|
|
void Libc::init_semaphore_support(Monitor &monitor)
|
|
|
|
{
|
|
|
|
_monitor_ptr = &monitor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-12 11:59:29 +02:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This class is named 'struct sem' because the 'sem_t' type is
|
|
|
|
* defined as 'struct sem*' in 'semaphore.h'
|
|
|
|
*/
|
2020-02-05 15:47:09 +01:00
|
|
|
struct sem
|
2012-04-12 11:59:29 +02:00
|
|
|
{
|
2020-02-05 15:47:09 +01:00
|
|
|
int _count;
|
|
|
|
unsigned _applicants { 0 };
|
|
|
|
Lock _data_mutex;
|
|
|
|
Lock _monitor_mutex;
|
|
|
|
|
|
|
|
struct Missing_call_of_init_pthread_support : Exception { };
|
|
|
|
|
|
|
|
struct Applicant
|
|
|
|
{
|
|
|
|
sem &s;
|
|
|
|
|
|
|
|
Applicant(sem &s) : s(s)
|
|
|
|
{
|
|
|
|
Lock::Guard lock_guard(s._data_mutex);
|
|
|
|
++s._applicants;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Applicant()
|
|
|
|
{
|
|
|
|
Lock::Guard lock_guard(s._data_mutex);
|
|
|
|
--s._applicants;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Monitor & _monitor()
|
|
|
|
{
|
|
|
|
if (!_monitor_ptr)
|
|
|
|
throw Missing_call_of_init_pthread_support();
|
|
|
|
return *_monitor_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
sem(int value) : _count(value) { }
|
|
|
|
|
|
|
|
int trydown()
|
|
|
|
{
|
|
|
|
Lock::Guard lock_guard(_data_mutex);
|
|
|
|
|
|
|
|
if (_count > 0) {
|
|
|
|
_count--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
int down()
|
|
|
|
{
|
|
|
|
Lock::Guard monitor_guard(_monitor_mutex);
|
|
|
|
|
|
|
|
/* fast path without contention */
|
|
|
|
if (trydown() == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
Applicant guard { *this };
|
|
|
|
|
|
|
|
auto fn = [&] { return trydown() == 0; };
|
|
|
|
|
|
|
|
(void)_monitor().monitor(_monitor_mutex, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int down_timed(timespec const &abs_timeout)
|
|
|
|
{
|
|
|
|
Lock::Guard monitor_guard(_monitor_mutex);
|
|
|
|
|
|
|
|
/* fast path without wait - does not check abstimeout according to spec */
|
|
|
|
if (trydown() == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
timespec abs_now;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &abs_now);
|
|
|
|
|
|
|
|
uint64_t const timeout_ms = calculate_relative_timeout_ms(abs_now, abs_timeout);
|
|
|
|
if (!timeout_ms)
|
|
|
|
return ETIMEDOUT;
|
|
|
|
|
|
|
|
{
|
|
|
|
Applicant guard { *this };
|
|
|
|
|
|
|
|
auto fn = [&] { return trydown() == 0; };
|
|
|
|
|
|
|
|
if (_monitor().monitor(_monitor_mutex, fn, timeout_ms))
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int up()
|
|
|
|
{
|
|
|
|
Lock::Guard monitor_guard(_monitor_mutex);
|
|
|
|
Lock::Guard lock_guard(_data_mutex);
|
|
|
|
|
|
|
|
_count++;
|
|
|
|
|
|
|
|
if (_applicants)
|
|
|
|
_monitor().charge_monitors();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int count()
|
|
|
|
{
|
|
|
|
return _count;
|
|
|
|
}
|
2012-04-12 11:59:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int sem_close(sem_t *)
|
|
|
|
{
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
warning(__func__, " not implemented");
|
2020-02-05 15:47:09 +01:00
|
|
|
return Errno(ENOSYS);
|
2012-04-12 11:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sem_destroy(sem_t *sem)
|
|
|
|
{
|
2019-09-16 13:52:39 +02:00
|
|
|
Libc::Allocator alloc { };
|
|
|
|
destroy(alloc, *sem);
|
2012-04-12 11:59:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
|
|
|
|
{
|
2020-02-05 15:47:09 +01:00
|
|
|
*sval = (*sem)->count();
|
2012-04-12 11:59:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sem_init(sem_t *sem, int pshared, unsigned int value)
|
|
|
|
{
|
2019-09-16 13:52:39 +02:00
|
|
|
Libc::Allocator alloc { };
|
|
|
|
*sem = new (alloc) struct sem(value);
|
2012-04-12 11:59:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sem_t *sem_open(const char *, int, ...)
|
|
|
|
{
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
warning(__func__, " not implemented");
|
2012-04-12 11:59:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sem_post(sem_t *sem)
|
|
|
|
{
|
2020-02-05 15:47:09 +01:00
|
|
|
if (int res = (*sem)->up())
|
|
|
|
return Errno(res);
|
|
|
|
|
2012-04-12 11:59:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-05 15:47:09 +01:00
|
|
|
int sem_timedwait(sem_t * __restrict sem, const struct timespec * __restrict abstime)
|
2012-04-12 11:59:29 +02:00
|
|
|
{
|
2020-02-05 15:47:09 +01:00
|
|
|
/* abstime must be non-null according to the spec */
|
|
|
|
if (int res = (*sem)->down_timed(*abstime))
|
|
|
|
return Errno(res);
|
|
|
|
|
|
|
|
return 0;
|
2012-04-12 11:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-05 15:47:09 +01:00
|
|
|
int sem_trywait(sem_t *sem)
|
2012-04-12 11:59:29 +02:00
|
|
|
{
|
2020-02-05 15:47:09 +01:00
|
|
|
if (int res = (*sem)->trydown())
|
|
|
|
return Errno(res);
|
|
|
|
|
|
|
|
return 0;
|
2012-04-12 11:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sem_unlink(const char *)
|
|
|
|
{
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
warning(__func__, " not implemented");
|
2020-02-05 15:47:09 +01:00
|
|
|
return Errno(ENOSYS);
|
2012-04-12 11:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sem_wait(sem_t *sem)
|
|
|
|
{
|
2020-02-05 15:47:09 +01:00
|
|
|
if (int res = (*sem)->down())
|
|
|
|
return Errno(res);
|
|
|
|
|
2012-04-12 11:59:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|