fix thread spawning bug

This commit is contained in:
Peter McGoron 2023-04-07 15:21:56 -04:00
parent ec665217e1
commit 576aca9ac6
4 changed files with 24 additions and 17 deletions

View File

@ -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

View File

@ -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

View File

@ -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;
}
}

View File

@ -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;