Tue, 12 Sep 2023 18:08:11 +0200
update uwproj
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
1 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <unistd.h> | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
32 | |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
33 | #include "atomic.h" |
1 | 34 | #include "thrpool.h" |
35 | ||
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
36 | static threadpool_job kill_job; |
1 | 37 | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
38 | threadpool_t* threadpool_new(int min, int max) { |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
39 | log_ereport(LOG_VERBOSE, "new threadpool (min: %d, max: %d)", min, max); |
1 | 40 | threadpool_t *pool = malloc(sizeof(threadpool_t)); |
41 | pool->queue = NULL; | |
42 | pool->queue_len = 0; | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
43 | pool->num_idle = 0; |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
44 | pool->min_threads = min; |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
45 | pool->max_threads = max; |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
46 | pool->num_threads = 0; |
1 | 47 | |
48 | pthread_mutex_init(&pool->queue_lock, NULL); | |
49 | pthread_mutex_init(&pool->avlbl_lock, NULL); | |
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
50 | pthread_cond_init(&pool->available, NULL); |
1 | 51 | |
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
52 | return pool; |
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
53 | } |
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
54 | |
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
55 | int threadpool_start(threadpool_t *pool) { |
1 | 56 | /* create pool threads */ |
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
57 | for(int i=0;i<pool->min_threads;i++) { |
1 | 58 | pthread_t t; |
59 | if (pthread_create(&t, NULL, threadpool_func, pool) != 0) { | |
408
56edda8701e0
replace perror() messages with log_ereport in thrpool.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
358
diff
changeset
|
60 | log_ereport(LOG_FAILURE, "threadpool_start: pthread_create failed: %s", strerror(errno)); |
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
61 | return 1; |
1 | 62 | } |
63 | } | |
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
64 | return 0; |
1 | 65 | } |
66 | ||
67 | void* threadpool_func(void *data) { | |
68 | threadpool_t *pool = (threadpool_t*)data; | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
69 | |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
70 | ws_atomic_inc32(&pool->num_threads); |
1 | 71 | for(;;) { |
72 | threadpool_job *job = threadpool_get_job(pool); | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
73 | if(job == &kill_job) { |
1 | 74 | break; |
75 | } | |
76 | ||
77 | job->callback(job->data); | |
78 | ||
79 | free(job); | |
80 | } | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
81 | ws_atomic_dec32(&pool->num_threads); |
1 | 82 | return NULL; |
83 | } | |
84 | ||
85 | threadpool_job* threadpool_get_job(threadpool_t *pool) { | |
86 | pthread_mutex_lock(&pool->queue_lock); | |
87 | ||
88 | threadpool_job *job = NULL; | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
89 | pool->num_idle++; |
1 | 90 | while(job == NULL) { |
91 | if(pool->queue_len == 0) { | |
92 | pthread_cond_wait(&pool->available, &pool->queue_lock); | |
93 | continue; | |
94 | } else { | |
95 | pool_queue_t *q = pool->queue; | |
96 | job = q->job; | |
97 | pool->queue = q->next; | |
98 | pool->queue_len--; | |
99 | free(q); | |
100 | } | |
101 | } | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
102 | pool->num_idle--; |
1 | 103 | |
104 | pthread_mutex_unlock(&pool->queue_lock); | |
105 | return job; | |
106 | } | |
107 | ||
108 | void threadpool_run(threadpool_t *pool, job_callback_f func, void *data) { | |
358
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
109 | // TODO: handle errors |
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
110 | |
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
111 | if(pool->num_threads == 0) { |
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
112 | threadpool_start(pool); |
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
113 | } |
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
114 | |
1 | 115 | threadpool_job *job = malloc(sizeof(threadpool_job)); |
116 | job->callback = func; | |
117 | job->data = data; | |
118 | ||
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
119 | pthread_mutex_lock(&pool->queue_lock); |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
120 | threadpool_enqueue_job(pool, job); |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
121 | |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
122 | int create_thread = 0; |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
123 | int destroy_thread = 0; |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
124 | int diff = pool->queue_len - pool->num_idle; |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
125 | if(diff > 0 && pool->num_threads < pool->max_threads) { |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
126 | create_thread = 1; |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
127 | } else if(diff < 0 && pool->num_threads > pool->min_threads) { |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
128 | destroy_thread = 1; |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
129 | } |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
130 | |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
131 | //if(pool->queue_len == 1) { |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
132 | pthread_cond_signal(&pool->available); |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
133 | //} |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
134 | |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
135 | if(create_thread) { |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
136 | pthread_t t; |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
137 | if (pthread_create(&t, NULL, threadpool_func, pool) != 0) { |
408
56edda8701e0
replace perror() messages with log_ereport in thrpool.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
358
diff
changeset
|
138 | log_ereport(LOG_FAILURE, "threadpool_run: pthread_create failed: %s", strerror(errno)); |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
139 | } |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
140 | } |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
141 | if(destroy_thread) { |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
142 | threadpool_enqueue_job(pool, &kill_job); |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
143 | pthread_cond_signal(&pool->available); |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
144 | } |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
145 | |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
146 | pthread_mutex_unlock(&pool->queue_lock); |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
147 | } |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
148 | |
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
149 | void threadpool_enqueue_job(threadpool_t *pool, threadpool_job *job) { |
1 | 150 | pool_queue_t *q = malloc(sizeof(pool_queue_t)); |
151 | q->job = job; | |
152 | q->next = NULL; | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
153 | |
1 | 154 | if(pool->queue == NULL) { |
155 | pool->queue = q; | |
156 | } else { | |
157 | pool_queue_t *last_elem = pool->queue; | |
158 | while(last_elem->next != NULL) { | |
159 | last_elem = last_elem->next; | |
160 | } | |
161 | last_elem->next = q; | |
162 | } | |
163 | pool->queue_len++; | |
164 | } |