diff --git a/src/thread.c b/src/thread.c new file mode 100644 index 0000000..1b6ea16 --- /dev/null +++ b/src/thread.c @@ -0,0 +1,69 @@ +/* thread.c - thread management. + * + * Copyright (C) 2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include + +#include +#include +#include +#include + +#include "thread.h" + +/* Still have to decide: thread or process ? + * + */ +thread_pool_t threadpool; + +/** + * thrd_create - initialize thrd. + */ +int thrd_create(__unused int num) +{ + int fd[2]; + /* shall we make a communication channel via a pipe or socket ? */ + int __unused ret = socketpair(AF_LOCAL, SOCK_SEQPACKET, PF_LOCAL, fd); + return 1; +} + +/** + * thread_init - initialize thread pool. + */ +int thread_init(int nb) +{ + nb = clamp(nb, MIN_THRDS, MAX_THRDS); + + /* stop unwanted threads, always keep 1 */ + for (int i = nb + 1; i < threadpool.nb; ++i) { + printf("stopping thread %d - status = \n", i); + threadpool.thread[i].cmd = THRD_DO_QUIT; + } + for (int i = threadpool.nb; i < nb; ++i) { + printf("creating thread %d - status = \n", i); + thrd_create(i); + + } + + return nb; +} + +/* + communication: + main thread -> thread + commands via memory + thread -> main thread + status via memory + output via pipe/socket + thread output will be output/filtered by main thread + + */ diff --git a/src/thread.h b/src/thread.h new file mode 100644 index 0000000..9a5d835 --- /dev/null +++ b/src/thread.h @@ -0,0 +1,55 @@ +/* thread.h - thread management. + * + * Copyright (C) 2021-2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef THREAD_H +#define THREAD_H + +#include + +#include + +#include "position.h" + +#define MIN_THRDS 1 +#define MAX_THRDS 16 + +typedef enum { + THRD_DEAD, + THRD_IDLE, + THRD_WORKING, +} thread_status_t; + +typedef enum { + /* main thread to subs */ + THRD_DO_SEARCH, + THRD_DO_STOP, + THRD_DO_QUIT, +} thread_cmd_t; + +typedef struct { + int id; + thread_status_t status; + thread_cmd_t cmd; + int fd[2]; + pos_t pos; +} thread_t; + +typedef struct { + int nb; + thread_t thread[MAX_THRDS + 1]; +} thread_pool_t; + +int thrd_create(__unused int num); +int thread_init(int nb); + +#endif /* THREAD_H */