Skip to content

Commit 1efbe1a

Browse files
authored
Merge pull request #10 from kubo39/eventfd
Add eventfd example
2 parents 21877f5 + 71e4649 commit 1efbe1a

File tree

11 files changed

+206
-0
lines changed

11 files changed

+206
-0
lines changed

eventfd/CMakeLists.txt

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(ldc_eventfd_example)
6+
7+
target_sources(app PRIVATE src/main.c)
8+
9+
if(${ARCH} STREQUAL "posix" OR ${ARCH} STREQUAL "x86")
10+
set(ldc_target i686-unknown-none-newlibeabi)
11+
elseif(CONFIG_CPU_CORTEX_M)
12+
if(CONFIG_CPU_CORTEX_M3)
13+
set(ldc_target thumbv7em-unknown-none-newlibeabi)
14+
else()
15+
message(FATAL_ERROR "Unknown Cortex-M target")
16+
endif()
17+
elseif(CONFIG_RISCV)
18+
if(CONFIG_RISCV_ISA_RV32I)
19+
set(ldc_target riscv32-unknown-newlib-elf)
20+
else()
21+
message(FATAL_ERROR "Unsupported riscv ISA")
22+
endif()
23+
else()
24+
message(FATAL_ERROR "ARCH ${ARCH} not supported")
25+
endif()
26+
27+
include(ExternalProject)
28+
set(ldc_src_dir ${CMAKE_CURRENT_SOURCE_DIR})
29+
ExternalProject_Add(
30+
ldc_project
31+
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
32+
SOURCE_DIR ${ldc_src_dir}
33+
BUILD_IN_SOURCE 1
34+
BUILD_ALWAYS 1
35+
CONFIGURE_COMMAND ""
36+
BUILD_COMMAND
37+
dub
38+
build
39+
-f
40+
-b release
41+
--compiler=ldc2
42+
--arch=${ldc_target}
43+
INSTALL_COMMAND ""
44+
BUILD_BYPRODUCTS ${ldc_src_dir}/lib/libeventfd_example.a
45+
)
46+
47+
add_library(ldc_lib STATIC IMPORTED GLOBAL)
48+
add_dependencies(
49+
ldc_lib
50+
ldc_project
51+
)
52+
set_target_properties(ldc_lib PROPERTIES IMPORTED_LOCATION ${ldc_src_dir}/lib/libeventfd_example.a)
53+
target_link_libraries(app PUBLIC ldc_lib)

eventfd/dub.sdl

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name "eventfd_example"
2+
authors "Hiroki Noda"
3+
license "BSL-1.0"
4+
targetType "staticLibrary"
5+
targetPath "lib"
6+
targetName "eventfd_example"
7+
dependency "zephyr-core" path="../zephyr-core"

eventfd/ldc2.conf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
default:
2+
{
3+
switches = [
4+
"-Os",
5+
"--betterC",
6+
"--float-abi=soft",
7+
"--relocation-model=static",
8+
"--defaultlib="
9+
];
10+
post-switches = [
11+
"-I%%ldcbinarypath%%/../import",
12+
];
13+
lib-dirs = [];
14+
}

eventfd/prj.conf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# General config
2+
CONFIG_NEWLIB_LIBC=y
3+
CONFIG_POSIX_API=y
4+
CONFIG_LINKER_ORPHAN_SECTION_PLACE=y
5+
CONFIG_EVENTFD=y
6+
7+
# eventfd() implementation currently depends on some networking APIs,
8+
# so provide reasonable defaults so it can be built.
9+
CONFIG_NETWORKING=y
10+
CONFIG_NET_TEST=y
11+
CONFIG_TEST_RANDOM_GENERATOR=y

eventfd/sample.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sample:
2+
description: eventfd example
3+
name: eventfd
4+
common:
5+
build_only: true
6+
platform_allow:
7+
- qemu_cortex_m3
8+
tests:
9+
app.default: {}

eventfd/src/app.d

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import zephyr.core.sys.posix.unistd;
2+
import zephyr.core.sys.posix.sys.types;
3+
import zephyr.core.sys.zephyr.sys.eventfd;
4+
import zephyr.core.stdc.stdio;
5+
import zephyr.core.stdc.stdlib;
6+
7+
extern(C):
8+
@nogc:
9+
nothrow:
10+
11+
noreturn fatal(string msg)
12+
{
13+
perror(msg.ptr);
14+
exit(EXIT_FAILURE);
15+
}
16+
17+
/* As Zephyr doesn't provide command-line args, emulate them. */
18+
__gshared string[] input_argv = ["argv0", "1", "2", "3", "4"];
19+
20+
__gshared int efd;
21+
__gshared int g_argc;
22+
__gshared string[] g_argv;
23+
24+
void writer()
25+
{
26+
foreach (j; 1 .. g_argc)
27+
{
28+
printf("Writing %s to efd\n", g_argv[j].ptr);
29+
ulong u = strtoull(g_argv[j].ptr, null, 10);
30+
ssize_t s = write(efd, &u, ulong.sizeof);
31+
if (s != ulong.sizeof)
32+
{
33+
fatal("write");
34+
}
35+
}
36+
printf("Complete write loop\n");
37+
}
38+
39+
void reader()
40+
{
41+
sleep(1);
42+
43+
ulong u;
44+
printf("About to read\n");
45+
ssize_t s = read(efd, &u, ulong.sizeof);
46+
if (s != ulong.sizeof)
47+
{
48+
fatal("read");
49+
}
50+
printf("Read %llu (0x%llx) from efd\n",
51+
cast(ulong)u, cast(ulong)u);
52+
}
53+
54+
int d_main()
55+
{
56+
auto argv = input_argv;
57+
auto argc = argv.length;
58+
59+
if (argc < 2)
60+
{
61+
printf("Usage: %s <num>...\n", argv[0].ptr);
62+
exit(EXIT_FAILURE);
63+
}
64+
65+
g_argc = argc;
66+
g_argv = argv;
67+
68+
efd = eventfd(0, 0);
69+
if (efd == -1)
70+
{
71+
fatal("eventfd");
72+
}
73+
74+
writer();
75+
reader();
76+
77+
printf("Finished!\n");
78+
79+
return 0;
80+
}

eventfd/src/main.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern int d_main();
2+
3+
int main() {
4+
return d_main();
5+
}

zephyr-core/source/zephyr/core/stdc/stdlib.d

+7
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@ extern (C):
44
nothrow:
55
@nogc:
66

7+
enum EXIT_SUCCESS = 0;
8+
enum EXIT_FAILURE = 1;
9+
10+
noreturn exit(int status);
11+
712
void* malloc(size_t size);
813
void free(void* ptr);
14+
15+
ulong strtoull(const char* nptr, char** endptr, int base);

zephyr-core/source/zephyr/core/sys/posix/sys/types.d

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ nothrow:
66

77
alias time_t = long;
88
alias suseconds_t = long;
9+
alias ssize_t = long;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
module zephyr.core.sys.posix.unistd;
22

3+
import zephyr.core.sys.posix.sys.types;
4+
35
extern (C):
46
nothrow:
57
@nogc:
68

79
uint sleep(uint);
10+
11+
ssize_t read(int, void*, size_t);
12+
ssize_t write(int, const scope void*, size_t);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module zephyr.core.sys.zephyr.sys.eventfd;
2+
3+
extern(C):
4+
nothrow:
5+
@nogc:
6+
7+
enum EFD_SEMAPHORE = 2;
8+
enum EFD_NONBLOCK = 0x4000;
9+
10+
alias eventfd_t = ulong;
11+
12+
int eventfd(uint initval, int flags);
13+
int eventfd_read(int fd, eventfd_t *value);
14+
int eventfd_write(int fd, eventfd_t value);

0 commit comments

Comments
 (0)