-
Notifications
You must be signed in to change notification settings - Fork 8
/
pool_lib.c
134 lines (98 loc) · 2.43 KB
/
pool_lib.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
struct pthread_object {
pthread_t tid;
int work_count;
void *thread_data;
void (*work)(void *thread_data);
pthread_mutex_t lock;
pthread_cond_t cond;
};
struct pthread_pool_ctx {
int n_threads;
struct pthread_object *n_obj;
};
void *thread_worker(void *thread_data)
{
struct pthread_object *ctx = thread_data;
while (1) {
pthread_mutex_lock(&ctx->lock);
pthread_cond_wait(&ctx->cond, &ctx->lock);
ctx->work(ctx->thread_data);
//ctx->work_count --;
pthread_mutex_unlock(&ctx->lock);
}
}
void * pthread_pool_create(int n_workers)
{
struct pthread_pool_ctx *ctx;
int i;
int ret;
ctx = calloc(1, sizeof(struct pthread_pool_ctx));
if (!ctx) {
return NULL;
}
ctx->n_threads = n_workers;
ctx->n_obj = calloc(n_workers, sizeof(struct pthread_object));
if (!ctx->n_obj) {
return NULL;
}
for (i = 0; i < n_workers; i ++) {
ctx->n_obj[i].work_count = 0;
ret = pthread_create(&ctx->n_obj[i].tid, NULL, thread_worker, &ctx->n_obj[i]);
if (ret < 0) {
return NULL;
}
}
return ctx;
}
void pthread_schedule_work(void *priv, void (*work)(void *thread_data), void *work_data)
{
struct pthread_pool_ctx *ctx = priv;
static int min = -1;
int idx = 0;
int i;
min = ctx->n_obj[0].work_count;
for (i = 1; i < ctx->n_threads; i ++) {
if (ctx->n_obj[i].work_count < min) {
min = ctx->n_obj[i].work_count;
idx = i;
}
}
if (idx == -1) {
return;
}
pthread_mutex_lock(&ctx->n_obj[idx].lock);
ctx->n_obj[idx].work = work;
ctx->n_obj[idx].thread_data = work_data;
ctx->n_obj[idx].work_count ++;
printf("out worker loads ==== ");
for (i = 0; i < ctx->n_threads; i ++) {
printf("| %d ", ctx->n_obj[i].work_count);
}
printf(" |\n");
pthread_cond_signal(&ctx->n_obj[idx].cond);
pthread_mutex_unlock(&ctx->n_obj[idx].lock);
}
void work_func(void *priv)
{
int *i = priv;
printf("value at i %d\n", *i);
(*i) ++;
}
int main()
{
void *priv;
priv = pthread_pool_create(8);
if (!priv) {
return -1;
}
int work_data = 0;
while (1) {
usleep(200 * 1000);
pthread_schedule_work(priv, work_func, &work_data);
}
return 0;
}