Skip to content

Commit d94449b

Browse files
committed
futex: Futex based synchronization
1 parent 3dc0ae5 commit d94449b

File tree

5 files changed

+458
-56
lines changed

5 files changed

+458
-56
lines changed

include/sys/futex.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Phoenix-RTOS
3+
*
4+
* libphoenix
5+
*
6+
* sys/futex.h
7+
*
8+
* Copyright 2025 Phoenix Systems
9+
* Author: kamil kowalczyk
10+
*
11+
* This file is part of Phoenix-RTOS.
12+
*
13+
* %LICENSE%
14+
*/
15+
16+
#ifndef _LIBPHOENIX_SYS_FUTEX_H_
17+
#define _LIBPHOENIX_SYS_FUTEX_H_
18+
19+
#include <time.h>
20+
#include <stdint.h>
21+
22+
#define FUTEX_WAKEUP_ALL ((uint32_t) - 1)
23+
24+
25+
extern int phFutexWait(_Atomic(uint32_t) *address, uint32_t value, time_t timeout, int clockType);
26+
27+
28+
extern int phFutexWakeup(_Atomic(uint32_t) *address, uint32_t n_threads);
29+
30+
31+
#endif

include/sys/interrupt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
#include <sys/types.h>
21+
#include <stdint.h>
2122

2223

2324
#ifdef __cplusplus

include/sys/threads.h

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ extern "C" {
2929
#endif
3030

3131

32+
extern void _uresource_init(void);
33+
34+
35+
extern int mutexUnlock(handle_t m);
36+
37+
38+
extern int mutexTry(handle_t m);
39+
40+
41+
#define phMutexLock(m) mutexLock((m))
42+
43+
44+
extern int __resourceDestroy(handle_t r);
45+
#define resourceDestroy(r) __resourceDestroy((r))
46+
47+
48+
extern int condBroadcast(handle_t h);
49+
50+
51+
extern int condSignal(handle_t h);
52+
53+
54+
#define phCondWait(c, m, t) condWait((c), (m), (t))
55+
56+
3257
typedef struct {
3358
handle_t mutex;
3459
handle_t cond;
@@ -73,8 +98,7 @@ extern int threadJoin(int tid, time_t timeout);
7398
extern int beginthreadex(void (*start)(void *), unsigned int priority, void *stack, unsigned int stacksz, void *arg, handle_t *id);
7499

75100

76-
__attribute__((noreturn))
77-
extern void endthread(void);
101+
__attribute__((noreturn)) extern void endthread(void);
78102

79103

80104
static inline int beginthread(void (*start)(void *), unsigned int priority, void *stack, unsigned int stacksz, void *arg)
@@ -89,19 +113,10 @@ extern int threadsinfo(int n, threadinfo_t *info);
89113
extern int priority(int priority);
90114

91115

92-
extern int phMutexCreate(handle_t *h, const struct lockAttr *attr);
93-
94-
95116
extern int mutexCreate(handle_t *h);
96117

97118

98-
static inline int mutexCreateWithAttr(handle_t *h, const struct lockAttr *attr)
99-
{
100-
return phMutexCreate(h, attr);
101-
}
102-
103-
104-
extern int phMutexLock(handle_t h);
119+
extern int mutexCreateWithAttr(handle_t *h, const struct lockAttr *attr);
105120

106121

107122
extern int mutexLock(handle_t h);
@@ -110,12 +125,6 @@ extern int mutexLock(handle_t h);
110125
extern int mutexLock2(handle_t h1, handle_t h2);
111126

112127

113-
extern int mutexTry(handle_t h);
114-
115-
116-
extern int mutexUnlock(handle_t h);
117-
118-
119128
extern int semaphoreCreate(semaphore_t *s, unsigned int v);
120129

121130

@@ -128,33 +137,15 @@ extern int semaphoreUp(semaphore_t *s);
128137
extern int semaphoreDone(semaphore_t *s);
129138

130139

131-
extern int phCondCreate(handle_t *h, const struct condAttr *attr);
132-
133-
134140
extern int condCreate(handle_t *h);
135141

136142

137-
static inline int condCreateWithAttr(handle_t *h, const struct condAttr *attr)
138-
{
139-
return phCondCreate(h, attr);
140-
}
143+
extern int condCreateWithAttr(handle_t *h, const struct condAttr *attr);
141144

142145

143146
extern int condWait(handle_t h, handle_t m, time_t timeout);
144147

145148

146-
extern int phCondWait(handle_t h, handle_t m, time_t timeout);
147-
148-
149-
extern int condSignal(handle_t h);
150-
151-
152-
extern int condBroadcast(handle_t h);
153-
154-
155-
extern int resourceDestroy(handle_t h);
156-
157-
158149
extern int signalHandle(void (*handler)(void), unsigned mask, unsigned mmask);
159150

160151

misc/init.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ extern void _errno_init(void);
2222
extern void _atexit_init(void);
2323
extern void _init_array(void);
2424
extern void _pthread_init(void);
25+
extern void _uresource_init(void);
2526

2627

2728
void _libc_init(void)
2829
{
2930
_atexit_init();
3031
_errno_init();
32+
_uresource_init();
3133
_malloc_init();
3234
_env_init();
3335
_signals_init();

0 commit comments

Comments
 (0)