Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
extern "C" {
#endif

/*
* We want to be able to include our headers in C++ source and it uses a different
* keyword for static assertions, the same goes for C from C23 onwards
*/
#if defined(__cplusplus) || __STDC_VERSION__ == 202311L
#define STATIC_ASSERT static_assert
#else
#define STATIC_ASSERT _Static_assert
#endif


#ifndef NDEBUG
#define assert(__expr) \
Expand Down
7 changes: 5 additions & 2 deletions include/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*
* limits.h
*
* Copyright 2019 Phoenix Systems
* Author: Andrzej Glowinski
* Copyright 2019, 2026 Phoenix Systems
* Author: Andrzej Glowinski, Michal Lach
*
* This file is part of Phoenix-RTOS.
*
Expand Down Expand Up @@ -36,4 +36,7 @@

#include <posix/limits.h>

#define SEM_VALUE_MAX INT_MAX
#define SEM_NSEMS_MAX _POSIX_SEM_NSEMS_MAX

#endif
61 changes: 61 additions & 0 deletions include/semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Phoenix-RTOS
*
* libphoenix
*
* POSIX implementation - semaphores
*
* Copyright 2026 Phoenix Systems
* Author: Michal Lach
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include <time.h>
#include <limits.h>
#include <sys/threads.h>
#include <sys/semaphore.h>

#define SEM_FAILED ((sem_t *)0xDAAB0000)

typedef struct _sem_t {
/* clang-format off */
enum { smNamed, smUnnamed } type;
/* clang-format on */

union {
semaphore_t unnamed;
oid_t named;
};
} sem_t;

extern int sem_wait(sem_t *sem);


extern int sem_trywait(sem_t *sem);


extern int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict abs_timeout);


extern int sem_getvalue(sem_t *restrict sem, int *restrict value);


extern int sem_post(sem_t *sem);


extern int sem_close(sem_t *sem);


extern sem_t *sem_open(const char *name, int oflag, ... /* mode_t mode, unsigned int value */);


extern int sem_unlink(const char *name);


extern int sem_destroy(sem_t *sem);


extern int sem_init(sem_t *sem, int pshared, unsigned int value);
29 changes: 29 additions & 0 deletions include/sys/semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Phoenix-RTOS
*
* libphoenix
*
* POSIX implementation - semaphores
*
* Copyright 2026 Phoenix Systems
* Author: Michal Lach
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include <assert.h>
#include <limits.h>
#include <sys/ioctl.h>

#define SEMAPHORE_PATH "/dev/posix/sem/"
#define SEMCTL_PATH "/dev/posix/semctl"

STATIC_ASSERT(SEM_VALUE_MAX >= _POSIX_SEM_VALUE_MAX, "SEM_VALUE_MAX shall be greater or equal to _POSIX_SEM_VALUE_MAX");
STATIC_ASSERT(SEM_NSEMS_MAX >= _POSIX_SEM_NSEMS_MAX, "SEM_NSEMS_MAX shall be greater or equal to _POSIX_SEM_NSEMS_MAX");

#define SEM_UP _IO('s', 0x1)
#define SEM_DOWN _IO('s', 0x2)
#define SEM_DOWN_TRY _IO('s', 0x3)
#define SEM_DOWN_TIMEOUT _IOW('s', 0x4, time_t)
15 changes: 13 additions & 2 deletions include/sys/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*
* sys/threads
*
* Copyright 2017, 2018 Phoenix Systems
* Author: Pawel Pisarczyk, Aleksander Kaminski
* Copyright 2017, 2018, 2026 Phoenix Systems
* Author: Pawel Pisarczyk, Aleksander Kaminski, Michal Lach
*
* This file is part of Phoenix-RTOS.
*
Expand All @@ -16,6 +16,8 @@
#ifndef _LIBPHOENIX_SYS_THREADS_H_
#define _LIBPHOENIX_SYS_THREADS_H_

#include <assert.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/rb.h>
#include <stddef.h>
Expand All @@ -36,6 +38,9 @@ typedef struct {
} semaphore_t;


STATIC_ASSERT(SEM_VALUE_MAX <= UINT_MAX, "The maximum semaphore value SEM_VALUE_MAX shall be less or equal to the maximum possible value of its container");


struct __errno_t {
rbnode_t linkage;
int tid;
Expand Down Expand Up @@ -113,12 +118,18 @@ extern int semaphoreCreate(semaphore_t *s, unsigned int v);
extern int semaphoreDown(semaphore_t *s, time_t timeout);


extern int semaphoreTryDown(semaphore_t *s);


extern int semaphoreUp(semaphore_t *s);


extern int semaphoreDone(semaphore_t *s);


extern int semaphoreCount(semaphore_t *s);


extern int phCondCreate(handle_t *h, const struct condAttr *attr);


Expand Down
14 changes: 8 additions & 6 deletions include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ extern "C" {
#define X_OK (1 << 0)


#define _SC_OPEN_MAX 0
#define _SC_IOV_MAX 1
#define _SC_ATEXIT_MAX 2
#define _SC_CLK_TCK 3
#define _SC_PAGESIZE 4
#define _SC_PAGE_SIZE _SC_PAGESIZE /* spec. 1170 compatibility */
#define _SC_OPEN_MAX 0
#define _SC_IOV_MAX 1
#define _SC_ATEXIT_MAX 2
#define _SC_CLK_TCK 3
#define _SC_PAGESIZE 4
#define _SC_SEM_VALUE_MAX 5
#define _SC_SEM_NSEMS_MAX 6
#define _SC_PAGE_SIZE _SC_PAGESIZE /* spec. 1170 compatibility */

#define _POSIX_NO_TRUNC 1
#define _POSIX_ASYNC_IO -1 /* Async IO not implemented. */
Expand Down
2 changes: 1 addition & 1 deletion posix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# Author: Pawel Pisarczyk
#

OBJS += $(addprefix $(PREFIX_O)posix/, stubs.o utils.o idtree.o)
OBJS += $(addprefix $(PREFIX_O)posix/, stubs.o utils.o idtree.o sem.o)
Loading
Loading