improve server and fix memory leaks

This commit is contained in:
2025-08-04 00:39:23 +02:00
parent 4d8d901bd3
commit 2934de12dd
10 changed files with 251 additions and 174 deletions

View File

@@ -1,7 +1,7 @@
#ifndef CWS_THREADPOOL_H
#define CWS_THREADPOOL_H
#include "myclib/hashmap/myhashmap.h"
#include "myclib/queue/myqueue.h"
#include "server.h"
#include "utils/config.h"
@@ -10,25 +10,26 @@
typedef struct cws_task_t {
int client_fd;
int epfd;
mcl_hashmap *clients;
cws_config *config;
} cws_task;
typedef struct cws_thread_task_t {
void (*function)(void *);
void *arg;
} cws_thread_task;
typedef struct cws_threadpool_t {
cws_task queue[CWS_MAX_QUEUE_TASKS];
int front, rear;
mcl_queue *queue;
pthread_mutex_t lock;
pthread_cond_t cond;
pthread_t threads[CWS_MAX_THREADS];
pthread_cond_t notify;
size_t threads_num;
pthread_t *threads;
int shutdown;
} cws_threadpool;
cws_threadpool *cws_threadpool_init();
cws_server_ret cws_threadpool_add_task(cws_threadpool *pool, cws_task *task);
cws_server_ret cws_threadpool_worker(cws_threadpool *pool);
cws_server_ret cws_threadpool_destroy(cws_threadpool *pool);
cws_threadpool *cws_threadpool_init(size_t threads_num, size_t queue_size);
cws_server_ret cws_threadpool_submit(cws_threadpool *pool, cws_thread_task *task);
void *cws_threadpool_worker(void *arg);
void cws_threadpool_destroy(cws_threadpool *pool);
#endif