From 576aca9ac6cf2f0a199a53dcd236e7a2056ddd2e Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Fri, 7 Apr 2023 15:21:56 -0400 Subject: [PATCH] fix thread spawning bug --- doc/programmers_manual.md | 3 +++ software/prj.conf | 1 + software/src/main.c | 31 ++++++++++++++++--------------- software/src/sock.c | 6 ++++-- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/doc/programmers_manual.md b/doc/programmers_manual.md index 162cc48..abcf2a1 100644 --- a/doc/programmers_manual.md +++ b/doc/programmers_manual.md @@ -220,6 +220,9 @@ documentation. The kernel is `/software/build/zephyr/zephyr.bin` +If you make a change to `CMakeLists.txt` or to `prj.conf`, run `make clean` +before `make`. + # Loading the Software and Firmware ## Network Setup diff --git a/software/prj.conf b/software/prj.conf index d65a2b6..faa5f28 100644 --- a/software/prj.conf +++ b/software/prj.conf @@ -2,6 +2,7 @@ CONFIG_LOG=y CONFIG_NET_LOG=y CONFIG_LOG_BUFFER_SIZE=1024 CONFIG_LOG_PRINTK=y +CONFIG_LOG_DEFAULT_LEVEL=4 CONFIG_NETWORKING=y CONFIG_NET_IPV4=y diff --git a/software/src/main.c b/software/src/main.c index c4aaccd..d27f418 100644 --- a/software/src/main.c +++ b/software/src/main.c @@ -20,17 +20,6 @@ static unsigned char readbuf[THREADNUM][READBUF_SIZ]; static struct k_thread threads[THREADNUM]; static bool thread_ever_used[THREADNUM]; -static int -read_size(int s) -{ - char buf[2]; - struct bufptr bp = {buf, sizeof(buf)}; - int e = sock_read_buf(s, &bp, true); - if (e != 0) - return e; - return (unsigned char)buf[0] | (unsigned char) buf[1] << 8; -} - static const char *const compiler_ret_str[CREOLE_COMPILE_RET_LEN] = { [CREOLE_COMPILE_OK] = "compile ok", [CREOLE_OPCODE_READ_ERROR] = "opcode read error", @@ -125,20 +114,30 @@ exec_creole(unsigned char *buf, int size, int sock) } } +static int +read_size(int s) +{ + char buf[2]; + struct bufptr bp = {buf, sizeof(buf)}; + int e = sock_read_buf(s, &bp, true); + if (e != 0) + return e; + return (unsigned char)buf[0] | (unsigned char) buf[1] << 8; +} + static void exec_entry(void *client_p, void *threadnum_p, void *unused __attribute__((unused))) { intptr_t client = (intptr_t)client_p; intptr_t threadnum = (intptr_t)threadnum_p; + LOG_DBG("Entered thread %d", (int)threadnum); int size = read_size(client); char thread_name[64]; snprintk(thread_name, sizeof(thread_name), "%"PRIdPTR":%"PRIdPTR, client, threadnum); k_thread_name_set(k_current_get(), thread_name); - LOG_INF("%s: Connection initiated", thread_name); - if (size < 0) { LOG_WRN("%s: error in read size: %d", get_thread_name(), size); zsock_close(client); @@ -167,14 +166,16 @@ main_loop(int srvsock) int i; for (i = 0; i < THREADNUM; i++) { - if (!thread_ever_used[i] - || k_thread_join(&threads[i], K_NO_WAIT) == 0) { + if (!thread_ever_used[i] || k_thread_join(&threads[i], K_NO_WAIT) == 0) { + LOG_DBG("launching thread %d", i); connection_counter++; + thread_ever_used[i] = true; k_thread_create(&threads[i], stacks[i], THREAD_STACK_SIZ, exec_entry, (void*) client, (void*) i, (void*) connection_counter, 1, 0, K_NO_WAIT); + break; } } diff --git a/software/src/sock.c b/software/src/sock.c index 99c8019..0c53329 100644 --- a/software/src/sock.c +++ b/software/src/sock.c @@ -35,7 +35,6 @@ server_init_sock(int port) LOG_ERR("error: listen: %d", errno); k_fatal_halt(K_ERR_KERNEL_PANIC); } - LOG_INF("Upsilon waiting on %d", port); return sock; @@ -52,6 +51,9 @@ server_accept_client(int server) * so other threads can run. */ do { + LOG_DBG("Accept"); + struct zsock_pollfd server_fd = { .fd = server, .events = ZSOCK_POLLIN }; + zsock_poll(&server_fd, 1, -1); client = zsock_accept(server, (struct sockaddr *)&addr, &len); if (client < 0) LOG_WRN("error in accept: %d", errno); @@ -67,7 +69,7 @@ int sock_read_buf(int sock, struct bufptr *bp, bool entire) { do { - ssize_t l = zsock_recv(sock, bp->p, bp->left - 1, 0); + ssize_t l = zsock_recv(sock, bp->p, bp->left, 0); if (l < 0) return -errno;