create (unused) thread.[ch] files.

I am still unsure if I will go for thread or process...
This commit is contained in:
2024-07-31 07:54:12 +02:00
parent 01af1f5c49
commit c0bbf03551
2 changed files with 124 additions and 0 deletions

69
src/thread.c Normal file
View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h>
#include <brlib.h>
#include <pthread.h>
#include <poll.h>
#include <sys/socket.h>
#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
*/

55
src/thread.h Normal file
View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef THREAD_H
#define THREAD_H
#include <pthread.h>
#include <brlib.h>
#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 */