1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/include/ipc/semaphore.h Thu Mar 11 00:00:52 2021 +0100
1.3 @@ -0,0 +1,31 @@
1.4 +/*
1.5 + * Semaphore utilities.
1.6 + *
1.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#pragma once
1.26 +
1.27 +#include <l4/sys/types.h>
1.28 +
1.29 +EXTERN_C_BEGIN
1.30 +
1.31 +long ipc_semaphore_down(l4_cap_idx_t cap);
1.32 +long ipc_semaphore_up(l4_cap_idx_t cap);
1.33 +
1.34 +EXTERN_C_END
2.1 --- a/lib/src/Makefile Mon Mar 08 01:03:49 2021 +0100
2.2 +++ b/lib/src/Makefile Thu Mar 11 00:00:52 2021 +0100
2.3 @@ -27,7 +27,7 @@
2.4
2.5 PLAIN_SRC_C = \
2.6 cap_alloc.c direct.c irq.c mem_ipc.c message.c \
2.7 - server.c sys_memory.c thread.c util_ipc.c \
2.8 + semaphore.c server.c sys_memory.c thread.c util_ipc.c \
2.9
2.10 # Normal definitions.
2.11
3.1 --- a/lib/src/cap_alloc.c Mon Mar 08 01:03:49 2021 +0100
3.2 +++ b/lib/src/cap_alloc.c Thu Mar 11 00:00:52 2021 +0100
3.3 @@ -34,13 +34,12 @@
3.4 #include <l4/re/env.h>
3.5 #include <l4/sys/consts.h>
3.6 #include <l4/sys/factory.h>
3.7 -#include <l4/sys/semaphore.h>
3.8 #include <l4/sys/task.h>
3.9 -#include <l4/sys/types.h>
3.10
3.11 #include <string.h> /* memset */
3.12
3.13 #include "cap_alloc.h"
3.14 +#include "semaphore.h"
3.15
3.16
3.17
3.18 @@ -131,16 +130,17 @@
3.19 l4_cap_idx_t ipc_cap_alloc()
3.20 {
3.21 l4_cap_idx_t cap;
3.22 + long err;
3.23
3.24 /* Allocate a capability in the critical section, then return. */
3.25
3.26 - if (l4_is_valid_cap(ipc_cap_alloc_semaphore))
3.27 - l4_semaphore_down(ipc_cap_alloc_semaphore, L4_IPC_NEVER);
3.28 + err = ipc_semaphore_down(ipc_cap_alloc_semaphore);
3.29 + if (err)
3.30 + return L4_INVALID_CAP;
3.31
3.32 cap = _ipc_cap_alloc();
3.33
3.34 - if (l4_is_valid_cap(ipc_cap_alloc_semaphore))
3.35 - l4_semaphore_up(ipc_cap_alloc_semaphore);
3.36 + ipc_semaphore_up(ipc_cap_alloc_semaphore);
3.37
3.38 return cap;
3.39 }
3.40 @@ -179,14 +179,15 @@
3.41 int ipc_cap_free(l4_cap_idx_t cap)
3.42 {
3.43 int result;
3.44 + long err;
3.45
3.46 - if (l4_is_valid_cap(ipc_cap_alloc_semaphore ))
3.47 - l4_semaphore_down(ipc_cap_alloc_semaphore, L4_IPC_NEVER);
3.48 + err = ipc_semaphore_down(ipc_cap_alloc_semaphore);
3.49 + if (err)
3.50 + return 0;
3.51
3.52 result = _ipc_cap_free(cap);
3.53
3.54 - if (l4_is_valid_cap(ipc_cap_alloc_semaphore))
3.55 - l4_semaphore_up(ipc_cap_alloc_semaphore);
3.56 + ipc_semaphore_up(ipc_cap_alloc_semaphore);
3.57
3.58 return result;
3.59 }
3.60 @@ -207,14 +208,17 @@
3.61 int ipc_cap_free_um(l4_cap_idx_t cap)
3.62 {
3.63 int result;
3.64 + long err;
3.65
3.66 - if (l4_is_valid_cap(ipc_cap_alloc_semaphore))
3.67 - l4_semaphore_down(ipc_cap_alloc_semaphore, L4_IPC_NEVER);
3.68 + err = ipc_semaphore_down(ipc_cap_alloc_semaphore);
3.69 + if (err)
3.70 + return 0;
3.71
3.72 result = _ipc_cap_free_um(cap);
3.73
3.74 - if (l4_is_valid_cap(ipc_cap_alloc_semaphore))
3.75 - l4_semaphore_up(ipc_cap_alloc_semaphore);
3.76 + ipc_semaphore_up(ipc_cap_alloc_semaphore);
3.77
3.78 return result;
3.79 }
3.80 +
3.81 +// vim: tabstop=2 expandtab shiftwidth=2
4.1 --- a/lib/src/mem_ipc.c Mon Mar 08 01:03:49 2021 +0100
4.2 +++ b/lib/src/mem_ipc.c Thu Mar 11 00:00:52 2021 +0100
4.3 @@ -23,12 +23,12 @@
4.4 #include <l4/re/protocols.h>
4.5 #include <l4/re/c/rm.h>
4.6 #include <l4/sys/err.h>
4.7 -#include <l4/sys/semaphore.h>
4.8 #include <l4/sys/task.h>
4.9 #include <l4/sys/types.h>
4.10
4.11 #include "cap_alloc.h"
4.12 #include "mem_ipc.h"
4.13 +#include "semaphore.h"
4.14
4.15 /* Client interfaces. */
4.16
4.17 @@ -79,8 +79,9 @@
4.18 {
4.19 long err;
4.20
4.21 - if (l4_is_valid_cap(ipc_mem_semaphore))
4.22 - l4_semaphore_down(ipc_mem_semaphore, L4_IPC_NEVER);
4.23 + err = ipc_semaphore_down(ipc_mem_semaphore);
4.24 + if (err)
4.25 + return err;
4.26
4.27 /* Attach a region starting at or after start/addr, offset is zero. */
4.28
4.29 @@ -88,8 +89,7 @@
4.30 (l4_addr_t *) addr, size, L4RE_RM_SEARCH_ADDR, cap,
4.31 0, L4_PAGESHIFT);
4.32
4.33 - if (l4_is_valid_cap(ipc_mem_semaphore))
4.34 - l4_semaphore_up(ipc_mem_semaphore);
4.35 + ipc_semaphore_up(ipc_mem_semaphore);
4.36
4.37 return err;
4.38 }
4.39 @@ -103,8 +103,9 @@
4.40 unsigned long size;
4.41 long err;
4.42
4.43 - if (l4_is_valid_cap(ipc_mem_semaphore))
4.44 - l4_semaphore_down(ipc_mem_semaphore, L4_IPC_NEVER);
4.45 + err = ipc_semaphore_down(ipc_mem_semaphore);
4.46 + if (err)
4.47 + return err;
4.48
4.49 /* Detach a region of at least size 1, flags 2 (overlap). */
4.50
4.51 @@ -116,8 +117,7 @@
4.52 if (!err)
4.53 ipc_unmap_region(start, size);
4.54
4.55 - if (l4_is_valid_cap(ipc_mem_semaphore))
4.56 - l4_semaphore_up(ipc_mem_semaphore);
4.57 + ipc_semaphore_up(ipc_mem_semaphore);
4.58
4.59 return err;
4.60 }
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/lib/src/semaphore.c Thu Mar 11 00:00:52 2021 +0100
5.3 @@ -0,0 +1,85 @@
5.4 +/*
5.5 + * Semaphore utilities.
5.6 + *
5.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
5.8 + *
5.9 + * This program is free software; you can redistribute it and/or
5.10 + * modify it under the terms of the GNU General Public License as
5.11 + * published by the Free Software Foundation; either version 2 of
5.12 + * the License, or (at your option) any later version.
5.13 + *
5.14 + * This program is distributed in the hope that it will be useful,
5.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.17 + * GNU General Public License for more details.
5.18 + *
5.19 + * You should have received a copy of the GNU General Public License
5.20 + * along with this program; if not, write to the Free Software
5.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
5.22 + * Boston, MA 02110-1301, USA
5.23 + */
5.24 +
5.25 +#include <l4/sys/err.h>
5.26 +#include <l4/sys/semaphore.h>
5.27 +
5.28 +#include "semaphore.h"
5.29 +
5.30 +
5.31 +
5.32 +/* Obtain control of a semaphore. */
5.33 +
5.34 +long ipc_semaphore_down(l4_cap_idx_t semaphore)
5.35 +{
5.36 + long err;
5.37 +
5.38 + if (l4_is_valid_cap(semaphore))
5.39 + {
5.40 + do
5.41 + {
5.42 + err = l4_error(l4_semaphore_down(semaphore, L4_IPC_NEVER));
5.43 +
5.44 + /* Handle EAGAIN ("Resource temporarily unavailable") by just retrying. */
5.45 +
5.46 + if (err)
5.47 + {
5.48 + if (err == -L4_EAGAIN)
5.49 + continue;
5.50 + else
5.51 + return err;
5.52 + }
5.53 + }
5.54 + while (0);
5.55 + }
5.56 +
5.57 + return L4_EOK;
5.58 +}
5.59 +
5.60 +/* Relinquish control of a semaphore. */
5.61 +
5.62 +long ipc_semaphore_up(l4_cap_idx_t semaphore)
5.63 +{
5.64 + long err;
5.65 +
5.66 + if (l4_is_valid_cap(semaphore))
5.67 + {
5.68 + do
5.69 + {
5.70 + err = l4_error(l4_semaphore_up(semaphore));
5.71 +
5.72 + /* Handle EAGAIN ("Resource temporarily unavailable") by just retrying. */
5.73 +
5.74 + if (err)
5.75 + {
5.76 + if (err == -L4_EAGAIN)
5.77 + continue;
5.78 + else
5.79 + return err;
5.80 + }
5.81 + }
5.82 + while (0);
5.83 + }
5.84 +
5.85 + return L4_EOK;
5.86 +}
5.87 +
5.88 +// vim: tabstop=2 expandtab shiftwidth=2
6.1 --- a/lib/src/thread.c Mon Mar 08 01:03:49 2021 +0100
6.2 +++ b/lib/src/thread.c Thu Mar 11 00:00:52 2021 +0100
6.3 @@ -46,10 +46,10 @@
6.4 return -L4_ENOENT;
6.5 }
6.6
6.7 - l4_semaphore_up(*semaphore);
6.8 + return l4_error(l4_semaphore_up(*semaphore));
6.9 }
6.10 -
6.11 - return L4_EOK;
6.12 + else
6.13 + return L4_EOK;
6.14 }
6.15
6.16 /* Bootstrap the semaphore support needed by the allocation functions. */