|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Friedt Professional Engineering Services, Inc |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#if defined(__ZEPHYR__) && !(defined(CONFIG_BOARD_NATIVE_POSIX_32BIT) \ |
| 7 | + || defined(CONFIG_BOARD_NATIVE_POSIX_64BIT) \ |
| 8 | + || defined(CONFIG_SOC_SERIES_BSIM_NRFXX)) |
| 9 | + |
| 10 | +#include <net/socket.h> |
| 11 | +#include <posix/pthread.h> |
| 12 | +#include <sys/util.h> |
| 13 | +#include <posix/unistd.h> |
| 14 | + |
| 15 | +#else |
| 16 | + |
| 17 | +#include <poll.h> |
| 18 | +#include <pthread.h> |
| 19 | +#include <sys/socket.h> |
| 20 | +#include <sys/types.h> |
| 21 | +#include <unistd.h> |
| 22 | + |
| 23 | +#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) |
| 24 | + |
| 25 | +#endif |
| 26 | + |
| 27 | +#include <errno.h> |
| 28 | +#include <stdbool.h> |
| 29 | +#include <stdio.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +#define NUM_SOCKETPAIRS 3 |
| 33 | +#define NUM_REPITITIONS 3 |
| 34 | + |
| 35 | +struct ctx { |
| 36 | + int spair[2]; |
| 37 | + pthread_t thread; |
| 38 | + char *name; |
| 39 | +}; |
| 40 | + |
| 41 | +static const char *const names[] = { |
| 42 | + "Alpha", |
| 43 | + "Bravo", |
| 44 | + "Charlie", |
| 45 | +}; |
| 46 | + |
| 47 | +static void hello(int fd, const char *name) |
| 48 | +{ |
| 49 | + /* write(2) should be used after #25443 */ |
| 50 | + send(fd, name, strlen(name), 0); |
| 51 | +} |
| 52 | + |
| 53 | +static void *fun(void *arg) |
| 54 | +{ |
| 55 | + struct ctx *const ctx = (struct ctx *)arg; |
| 56 | + int fd = ctx->spair[1]; |
| 57 | + const char *name = ctx->name; |
| 58 | + |
| 59 | + for (size_t i = 0; i < NUM_REPITITIONS; ++i) { |
| 60 | + hello(fd, name); |
| 61 | + } |
| 62 | + |
| 63 | + close(fd); |
| 64 | + printf("%s closed fd %d\n", name, fd); |
| 65 | + ctx->spair[1] = -1; |
| 66 | + |
| 67 | + return NULL; |
| 68 | +} |
| 69 | + |
| 70 | +static int fd_to_idx(int fd, struct ctx *ctx, size_t n) |
| 71 | +{ |
| 72 | + int r = -1; |
| 73 | + size_t i; |
| 74 | + |
| 75 | + for (i = 0; i < n; ++i) { |
| 76 | + if (ctx[i].spair[0] == fd) { |
| 77 | + r = i; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return r; |
| 83 | +} |
| 84 | + |
| 85 | +#ifdef __ZEPHYR__ |
| 86 | +void zephyr_app_main(void) |
| 87 | +{ |
| 88 | +#else |
| 89 | +int main(int argc, char *argv[]) |
| 90 | +{ |
| 91 | + (void) argc; |
| 92 | + (void) argv; |
| 93 | +#endif |
| 94 | + |
| 95 | + int r; |
| 96 | + int fd; |
| 97 | + int idx; |
| 98 | + int poll_r; |
| 99 | + size_t i; |
| 100 | + size_t num_active; |
| 101 | + char buf[32]; |
| 102 | + struct ctx ctx[NUM_SOCKETPAIRS] = {}; |
| 103 | + struct pollfd fds[NUM_SOCKETPAIRS] = {}; |
| 104 | + void *unused; |
| 105 | + |
| 106 | + for (i = 0; i < ARRAY_SIZE(ctx); ++i) { |
| 107 | + ctx[i].name = (char *)names[i]; |
| 108 | + socketpair(AF_UNIX, SOCK_STREAM, 0, ctx[i].spair); |
| 109 | + pthread_create(&ctx[i].thread, NULL, fun, &ctx[i]); |
| 110 | + printf("%s: socketpair: %d <=> %d\n", |
| 111 | + ctx[i].name, ctx[i].spair[0], ctx[i].spair[1]); |
| 112 | + } |
| 113 | + |
| 114 | + /* loop until all threads are done */ |
| 115 | + for (;;) { |
| 116 | + |
| 117 | + /* count threads that are still running and fill in pollfds */ |
| 118 | + for (i = 0, num_active = 0; i < ARRAY_SIZE(ctx); ++i) { |
| 119 | + if (ctx[i].spair[0] == -1) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + fds[num_active].fd = ctx[i].spair[0]; |
| 123 | + fds[num_active].events = POLLIN; |
| 124 | + fds[num_active].revents = 0; |
| 125 | + num_active++; |
| 126 | + } |
| 127 | + |
| 128 | + if (num_active == 0) { |
| 129 | + /* all threads are done */ |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + poll_r = poll(fds, num_active, -1); |
| 134 | + |
| 135 | + for (size_t i = 0; i < num_active; ++i) { |
| 136 | + |
| 137 | + fd = fds[i].fd; |
| 138 | + idx = fd_to_idx(fd, ctx, ARRAY_SIZE(ctx)); |
| 139 | + |
| 140 | + if ((fds[i].revents & POLLIN) != 0) { |
| 141 | + |
| 142 | + memset(buf, '\0', sizeof(buf)); |
| 143 | + |
| 144 | + /* read(2) should be used after #25443 */ |
| 145 | + r = recv(fd, buf, sizeof(buf), 0); |
| 146 | + printf("fd: %d: read %d bytes\n", fd, r); |
| 147 | + } |
| 148 | + |
| 149 | + if ((fds[i].revents & POLLERR) != 0) { |
| 150 | + printf("fd: %d: error\n", fd); |
| 151 | + } |
| 152 | + |
| 153 | + if ((fds[i].revents & POLLHUP) != 0) { |
| 154 | + printf("fd: %d: hung up\n", fd); |
| 155 | + close(ctx[idx].spair[0]); |
| 156 | + printf("main: closed fd %d\n", |
| 157 | + ctx[idx].spair[0]); |
| 158 | + pthread_join(ctx[idx].thread, &unused); |
| 159 | + printf("joined %s\n", ctx[idx].name); |
| 160 | + ctx[idx].spair[0] = -1; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + printf("finished!\n"); |
| 166 | + |
| 167 | +#ifndef __ZEPHYR__ |
| 168 | + return 0; |
| 169 | +#endif |
| 170 | +} |
0 commit comments