-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtpool.h
More file actions
50 lines (39 loc) · 1.1 KB
/
tpool.h
File metadata and controls
50 lines (39 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* SPDX-License-Identifier: GPL-2.0+
* Copyright (C) 2018 Abinav Puthan Purayil
* */
#ifndef TPOOLH
#define TPOOLH
#include "queue.h"
#include <pthread.h>
struct tpool_task {
void *(*run)(void *arg);
void *arg;
};
struct tpool {
/* worker threads */
pthread_t *thread;
size_t n_thread;
/* task-queue */
struct queue_struct task_queue;
pthread_mutex_t task_queue_lock;
pthread_cond_t task_queue_empty;
pthread_cond_t task_queue_not_empty;
void (*free_task_arg)(void *);
int8_t terminate;
};
#define TPOOL_TASK_NEW zalloc(sizeof(struct tpool_task));
#define TPOOL_NEW zalloc(sizeof(struct tpool));
#define TPOOL_INIT(tp, n, fta) \
do { \
(tp)->thread = umalloc(sizeof(pthread_t) * n); \
(tp)->n_thread = n; \
pthread_mutex_init(&(tp)->task_queue_lock, NULL); \
pthread_cond_init(&(tp)->task_queue_empty, NULL); \
pthread_cond_init(&(tp)->task_queue_not_empty, NULL); \
(tp)->free_task_arg = fta; \
} while (0);
extern void tpool_add_task(struct tpool *tp, void *(*run)(void *), void *arg);
extern void tpool_start(struct tpool *tp);
extern void tpool_terminate(struct tpool *tp);
#endif