|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2011 Olaf Wintermann. All rights reserved. |
|
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> |
|
32 #include "thrpool.h" |
|
33 |
|
34 |
|
35 |
|
36 threadpool_t* threadpool_new(int n) { |
|
37 threadpool_t *pool = malloc(sizeof(threadpool_t)); |
|
38 pool->queue = NULL; |
|
39 pool->queue_len = 0; |
|
40 |
|
41 pthread_mutex_init(&pool->queue_lock, NULL); |
|
42 pthread_mutex_init(&pool->avlbl_lock, NULL); |
|
43 pthread_cond_init(&pool->available, NULL); |
|
44 |
|
45 /* create pool threads */ |
|
46 for(int i=0;i<n;i++) { |
|
47 pthread_t t; |
|
48 if (pthread_create(&t, NULL, threadpool_func, pool) != 0) { |
|
49 perror("Error: threadpool_new: pthread_create"); |
|
50 return NULL; |
|
51 } |
|
52 } |
|
53 |
|
54 return pool; |
|
55 } |
|
56 |
|
57 void* threadpool_func(void *data) { |
|
58 threadpool_t *pool = (threadpool_t*)data; |
|
59 |
|
60 for(;;) { |
|
61 threadpool_job *job = threadpool_get_job(pool); |
|
62 if(job == NULL) { |
|
63 break; |
|
64 } |
|
65 |
|
66 job->callback(job->data); |
|
67 |
|
68 free(job); |
|
69 } |
|
70 return NULL; |
|
71 } |
|
72 |
|
73 threadpool_job* threadpool_get_job(threadpool_t *pool) { |
|
74 pthread_mutex_lock(&pool->queue_lock); |
|
75 |
|
76 threadpool_job *job = NULL; |
|
77 while(job == NULL) { |
|
78 if(pool->queue_len == 0) { |
|
79 pthread_cond_wait(&pool->available, &pool->queue_lock); |
|
80 continue; |
|
81 } else { |
|
82 pool_queue_t *q = pool->queue; |
|
83 job = q->job; |
|
84 pool->queue = q->next; |
|
85 pool->queue_len--; |
|
86 free(q); |
|
87 } |
|
88 } |
|
89 |
|
90 pthread_mutex_unlock(&pool->queue_lock); |
|
91 |
|
92 return job; |
|
93 } |
|
94 |
|
95 void threadpool_run(threadpool_t *pool, job_callback_f func, void *data) { |
|
96 threadpool_job *job = malloc(sizeof(threadpool_job)); |
|
97 job->callback = func; |
|
98 job->data = data; |
|
99 |
|
100 pool_queue_t *q = malloc(sizeof(pool_queue_t)); |
|
101 q->job = job; |
|
102 q->next = NULL; |
|
103 |
|
104 pthread_mutex_lock(&pool->queue_lock); |
|
105 if(pool->queue == NULL) { |
|
106 pool->queue = q; |
|
107 } else { |
|
108 pool_queue_t *last_elem = pool->queue; |
|
109 while(last_elem->next != NULL) { |
|
110 last_elem = last_elem->next; |
|
111 } |
|
112 last_elem->next = q; |
|
113 } |
|
114 pool->queue_len++; |
|
115 |
|
116 if(pool->queue_len == 1) { |
|
117 pthread_cond_signal(&pool->available); |
|
118 } |
|
119 |
|
120 pthread_mutex_unlock(&pool->queue_lock); |
|
121 |
|
122 } |