wthread.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Add work to a work group and have threads process the work
  37. * Provide blocking for all work to complete
  38. */
  39. #include <config.h>
  40. #include <stdlib.h>
  41. #include <pthread.h>
  42. #include <errno.h>
  43. #include <corosync/queue.h>
  44. #include "wthread.h"
  45. struct thread_data {
  46. void *thread_state;
  47. void *data;
  48. };
  49. struct worker_thread {
  50. struct worker_thread_group *worker_thread_group;
  51. pthread_mutex_t new_work_mutex;
  52. pthread_cond_t new_work_cond;
  53. pthread_cond_t cond;
  54. pthread_mutex_t done_work_mutex;
  55. pthread_cond_t done_work_cond;
  56. pthread_t thread_id;
  57. struct queue queue;
  58. void *thread_state;
  59. struct thread_data thread_data;
  60. };
  61. void *worker_thread (void *thread_data_in) {
  62. struct thread_data *thread_data = (struct thread_data *)thread_data_in;
  63. struct worker_thread *worker_thread =
  64. (struct worker_thread *)thread_data->data;
  65. void *data_for_worker_fn;
  66. for (;;) {
  67. pthread_mutex_lock (&worker_thread->new_work_mutex);
  68. if (queue_is_empty (&worker_thread->queue) == 1) {
  69. pthread_cond_wait (&worker_thread->new_work_cond,
  70. &worker_thread->new_work_mutex);
  71. }
  72. /*
  73. * We unlock then relock the new_work_mutex to allow the
  74. * worker function to execute and also allow new work to be
  75. * added to the work queue
  76. */
  77. data_for_worker_fn = queue_item_get (&worker_thread->queue);
  78. pthread_mutex_unlock (&worker_thread->new_work_mutex);
  79. worker_thread->worker_thread_group->worker_fn (worker_thread->thread_state, data_for_worker_fn);
  80. pthread_mutex_lock (&worker_thread->new_work_mutex);
  81. queue_item_remove (&worker_thread->queue);
  82. pthread_mutex_unlock (&worker_thread->new_work_mutex);
  83. pthread_mutex_lock (&worker_thread->done_work_mutex);
  84. if (queue_is_empty (&worker_thread->queue) == 1) {
  85. pthread_cond_signal (&worker_thread->done_work_cond);
  86. }
  87. pthread_mutex_unlock (&worker_thread->done_work_mutex);
  88. }
  89. return (0);
  90. }
  91. int worker_thread_group_init (
  92. struct worker_thread_group *worker_thread_group,
  93. int threads,
  94. int items_max,
  95. int item_size,
  96. int thread_state_size,
  97. void (*thread_state_constructor)(void *),
  98. void (*worker_fn)(void *thread_state, void *work_item))
  99. {
  100. int i;
  101. worker_thread_group->threadcount = threads;
  102. worker_thread_group->last_scheduled = 0;
  103. worker_thread_group->worker_fn = worker_fn;
  104. worker_thread_group->threads = malloc (sizeof (struct worker_thread) *
  105. threads);
  106. if (worker_thread_group->threads == 0) {
  107. return (-1);
  108. }
  109. for (i = 0; i < threads; i++) {
  110. if (thread_state_size) {
  111. worker_thread_group->threads[i].thread_state = malloc (thread_state_size);
  112. } else {
  113. worker_thread_group->threads[i].thread_state = NULL;
  114. }
  115. if (thread_state_constructor) {
  116. thread_state_constructor (worker_thread_group->threads[i].thread_state);
  117. }
  118. worker_thread_group->threads[i].worker_thread_group = worker_thread_group;
  119. pthread_mutex_init (&worker_thread_group->threads[i].new_work_mutex, NULL);
  120. pthread_cond_init (&worker_thread_group->threads[i].new_work_cond, NULL);
  121. pthread_mutex_init (&worker_thread_group->threads[i].done_work_mutex, NULL);
  122. pthread_cond_init (&worker_thread_group->threads[i].done_work_cond, NULL);
  123. queue_init (&worker_thread_group->threads[i].queue, items_max,
  124. item_size);
  125. worker_thread_group->threads[i].thread_data.thread_state =
  126. worker_thread_group->threads[i].thread_state;
  127. worker_thread_group->threads[i].thread_data.data = &worker_thread_group->threads[i];
  128. pthread_create (&worker_thread_group->threads[i].thread_id,
  129. NULL, worker_thread, &worker_thread_group->threads[i].thread_data);
  130. }
  131. return (0);
  132. }
  133. int worker_thread_group_work_add (
  134. struct worker_thread_group *worker_thread_group,
  135. void *item)
  136. {
  137. int schedule;
  138. schedule = (worker_thread_group->last_scheduled + 1) % (worker_thread_group->threadcount);
  139. worker_thread_group->last_scheduled = schedule;
  140. pthread_mutex_lock (&worker_thread_group->threads[schedule].new_work_mutex);
  141. if (queue_is_full (&worker_thread_group->threads[schedule].queue)) {
  142. pthread_mutex_unlock (&worker_thread_group->threads[schedule].new_work_mutex);
  143. return (-1);
  144. }
  145. queue_item_add (&worker_thread_group->threads[schedule].queue, item);
  146. pthread_cond_signal (&worker_thread_group->threads[schedule].new_work_cond);
  147. pthread_mutex_unlock (&worker_thread_group->threads[schedule].new_work_mutex);
  148. return (0);
  149. }
  150. void worker_thread_group_wait (
  151. struct worker_thread_group *worker_thread_group)
  152. {
  153. int i;
  154. for (i = 0; i < worker_thread_group->threadcount; i++) {
  155. pthread_mutex_lock (&worker_thread_group->threads[i].done_work_mutex);
  156. if (queue_is_empty (&worker_thread_group->threads[i].queue) == 0) {
  157. pthread_cond_wait (&worker_thread_group->threads[i].done_work_cond,
  158. &worker_thread_group->threads[i].done_work_mutex);
  159. }
  160. pthread_mutex_unlock (&worker_thread_group->threads[i].done_work_mutex);
  161. }
  162. }
  163. void worker_thread_group_exit (
  164. struct worker_thread_group *worker_thread_group)
  165. {
  166. int i;
  167. for (i = 0; i < worker_thread_group->threadcount; i++) {
  168. pthread_cancel (worker_thread_group->threads[i].thread_id);
  169. /* Wait for worker thread to exit gracefully before destroying
  170. * mutexes and processing items in the queue etc.
  171. */
  172. pthread_join (worker_thread_group->threads[i].thread_id, NULL);
  173. pthread_mutex_destroy (&worker_thread_group->threads[i].new_work_mutex);
  174. pthread_cond_destroy (&worker_thread_group->threads[i].new_work_cond);
  175. pthread_mutex_destroy (&worker_thread_group->threads[i].done_work_mutex);
  176. pthread_cond_destroy (&worker_thread_group->threads[i].done_work_cond);
  177. }
  178. }
  179. void worker_thread_group_atsegv (
  180. struct worker_thread_group *worker_thread_group)
  181. {
  182. void *data_for_worker_fn;
  183. struct worker_thread *worker_thread;
  184. unsigned int i;
  185. for (i = 0; i < worker_thread_group->threadcount; i++) {
  186. worker_thread = &worker_thread_group->threads[i];
  187. while (queue_is_empty (&worker_thread->queue) == 0) {
  188. data_for_worker_fn = queue_item_get (&worker_thread->queue);
  189. worker_thread->worker_thread_group->worker_fn (worker_thread->thread_state, data_for_worker_fn);
  190. queue_item_remove (&worker_thread->queue);
  191. }
  192. }
  193. }