|
| 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 | +} |
0 commit comments