Skip to content

Commit dd93f80

Browse files
authored
feat: add option to force use of custom futex implementation (#58)
Change - add the option `FUTEX_CUSTOM` to force futex.h to rely on user defined `vfutex_wait` and `vfutex_wake`. That was already the case for unknown OS targets (eg, HMOS). With `FUTEX_CUSTOM` defined, the user can bring their own `vfutex_` implementation on Linux. ---- Signed-off-by: Diogo Behrens <db7@sdf.org>
1 parent b8f1dd5 commit dd93f80

4 files changed

Lines changed: 67 additions & 4 deletions

File tree

include/vsync/thread/internal/futex.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved.
2+
* Copyright (C) Huawei Technologies Co., Ltd. 2024-2026. All rights reserved.
33
* SPDX-License-Identifier: MIT
44
*/
55

@@ -9,7 +9,8 @@
99
* futex syscalls
1010
*
1111
* If the macros are not defined, we mock the futex syscall with a simple
12-
* spinning mechanism.
12+
* spinning mechanism. Define `FUTEX_CUSTOM` to provide `vfutex_wait` and
13+
* `vfutex_wake` in user code, even on Linux.
1314
*
1415
* @note on linux compile with `-D_GNU_SOURCE`.
1516
******************************************************************************/
@@ -25,7 +26,12 @@
2526
#define FUTEX_USERSPACE
2627
#endif
2728

28-
#if defined(VSYNC_VERIFICATION) || defined(FUTEX_USERSPACE)
29+
#if defined(FUTEX_CUSTOM)
30+
31+
void vfutex_wait(vatomic32_t *m, vuint32_t v);
32+
void vfutex_wake(vatomic32_t *m, vuint32_t v);
33+
34+
#elif defined(VSYNC_VERIFICATION) || defined(FUTEX_USERSPACE)
2935

3036
#if defined(VFUTEX_LIVENESS)
3137
#include <vsync/thread/internal/futex_mock_liveness.h>

scripts/clang-format.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ if [ "${STYLE}" != "" ]; then
1919
STYLE=":${STYLE}"
2020
fi
2121

22+
if which clang-format-14 >/dev/null 2>&1; then
23+
CLANG_FORMAT=clang-format-14
24+
else
25+
CLANG_FORMAT=clang-format
26+
fi
27+
2228
# Apply clang-format to all *.h and *.c files in src folder.
2329
find "$@" \( -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.hpp' -o -name '*.cxx' \) \
2430
-not -path '*/build/*' \
2531
-not -path '*/deps/*' \
2632
-not -path '*/vatomic/*' \
2733
-type f \
28-
-exec clang-format -style=file${STYLE} -i {} +
34+
-exec ${CLANG_FORMAT} -style=file${STYLE} -i {} +
2935

3036
if [ "${SILENT}" != "true" ]; then
3137
# Display changed files and exit with 1 if there were differences.

test/thread/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
file(GLOB SRCS *.c)
2+
foreach(SRC ${SRCS})
3+
get_filename_component(TEST ${SRC} NAME_WE)
4+
5+
add_executable(${TEST} ${SRC})
6+
target_link_libraries(${TEST} vsync pthread)
7+
v_add_bin_test(NAME ${TEST} COMMAND ${TEST})
8+
endforeach()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) Huawei Technologies Co., Ltd. 2026. All rights reserved.
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
6+
#define FUTEX_CUSTOM
7+
#include <vsync/thread/internal/futex.h>
8+
9+
static vatomic32_t g_word = VATOMIC_INIT(0);
10+
static vuint32_t g_wait_count;
11+
static vuint32_t g_wake_count;
12+
static vuint32_t g_wait_value;
13+
static vuint32_t g_wake_value;
14+
15+
void
16+
vfutex_wait(vatomic32_t *m, vuint32_t v)
17+
{
18+
ASSERT(m == &g_word);
19+
g_wait_count++;
20+
g_wait_value = v;
21+
}
22+
23+
void
24+
vfutex_wake(vatomic32_t *m, vuint32_t v)
25+
{
26+
ASSERT(m == &g_word);
27+
g_wake_count++;
28+
g_wake_value = v;
29+
}
30+
31+
int
32+
main(void)
33+
{
34+
vfutex_wait(&g_word, 7U);
35+
vfutex_wake(&g_word, FUTEX_WAKE_ONE);
36+
37+
ASSERT(g_wait_count == 1U);
38+
ASSERT(g_wake_count == 1U);
39+
ASSERT(g_wait_value == 7U);
40+
ASSERT(g_wake_value == FUTEX_WAKE_ONE);
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)