wthread.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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@mvista.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 <stdlib.h>
  40. #include <pthread.h>
  41. #include <errno.h>
  42. #include "wthread.h"
  43. #include "../include/queue.h"
  44. struct thread_data {
  45. void *thread_state;
  46. void *data;
  47. };
  48. struct worker_thread {
  49. struct worker_thread_group *worker_thread_group;
  50. pthread_mutex_t new_work_mutex;
  51. pthread_cond_t new_work_cond;
  52. pthread_cond_t cond;
  53. pthread_mutex_t done_work_mutex;
  54. pthread_cond_t done_work_cond;
  55. pthread_t thread_id;
  56. struct queue queue;
  57. void *thread_state;
  58. struct thread_data thread_data;
  59. };
  60. void *worker_thread (void *thread_data_in) {
  61. struct thread_data *thread_data = (struct thread_data *)thread_data_in;
  62. struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
  63. (struct orf_token_mcast_thread_state *)thread_data->thread_state;
  64. struct worker_thread *worker_thread =
  65. (struct worker_thread *)thread_data->data;
  66. void *data_for_worker_fn;
  67. for (;;) {
  68. pthread_mutex_lock (&worker_thread->new_work_mutex);
  69. if (queue_is_empty (&worker_thread->queue) == 1) {
  70. pthread_cond_wait (&worker_thread->new_work_cond,
  71. &worker_thread->new_work_mutex);
  72. }
  73. /*
  74. * We unlock then relock the new_work_mutex to allow the
  75. * worker function to execute and also allow new work to be
  76. * added to the work queue
  77. */
  78. data_for_worker_fn = queue_item_get (&worker_thread->queue);
  79. pthread_mutex_unlock (&worker_thread->new_work_mutex);
  80. worker_thread->worker_thread_group->worker_fn (orf_token_mcast_thread_state, data_for_worker_fn);
  81. pthread_mutex_lock (&worker_thread->new_work_mutex);
  82. queue_item_remove (&worker_thread->queue);
  83. pthread_mutex_unlock (&worker_thread->new_work_mutex);
  84. pthread_mutex_lock (&worker_thread->done_work_mutex);
  85. if (queue_is_empty (&worker_thread->queue) == 1) {
  86. pthread_cond_signal (&worker_thread->done_work_cond);
  87. }
  88. pthread_mutex_unlock (&worker_thread->done_work_mutex);
  89. }
  90. return (0);
  91. }
  92. int worker_thread_group_init (
  93. struct worker_thread_group *worker_thread_group,
  94. int threads,
  95. int items_max,
  96. int item_size,
  97. int thread_state_size,
  98. void (*thread_state_constructor)(void *),
  99. void (*worker_fn)(void *thread_state, void *work_item))
  100. {
  101. int i;
  102. worker_thread_group->threadcount = threads;
  103. worker_thread_group->last_scheduled = 0;
  104. worker_thread_group->worker_fn = worker_fn;
  105. worker_thread_group->threads = malloc (sizeof (struct worker_thread) *
  106. threads);
  107. if (worker_thread_group->threads == 0) {
  108. return (-1);
  109. }
  110. for (i = 0; i < threads; i++) {
  111. if (thread_state_size) {
  112. worker_thread_group->threads[i].thread_state = malloc (thread_state_size);
  113. } else {
  114. worker_thread_group->threads[i].thread_state = NULL;
  115. }
  116. if (thread_state_constructor) {
  117. thread_state_constructor (worker_thread_group->threads[i].thread_state);
  118. }
  119. worker_thread_group->threads[i].worker_thread_group = worker_thread_group;
  120. pthread_mutex_init (&worker_thread_group->threads[i].new_work_mutex, NULL);
  121. pthread_cond_init (&worker_thread_group->threads[i].new_work_cond, NULL);
  122. pthread_mutex_init (&worker_thread_group->threads[i].done_work_mutex, NULL);
  123. pthread_cond_init (&worker_thread_group->threads[i].done_work_cond, NULL);
  124. queue_init (&worker_thread_group->threads[i].queue, items_max,
  125. item_size);
  126. worker_thread_group->threads[i].thread_data.thread_state =
  127. worker_thread_group->threads[i].thread_state;
  128. worker_thread_group->threads[i].thread_data.data = &worker_thread_group->threads[i];
  129. pthread_create (&worker_thread_group->threads[i].thread_id,
  130. NULL, worker_thread, &worker_thread_group->threads[i].thread_data);
  131. }
  132. return (0);
  133. }
  134. int worker_thread_group_work_add (
  135. struct worker_thread_group *worker_thread_group,
  136. void *item)
  137. {
  138. int schedule;
  139. schedule = (worker_thread_group->last_scheduled + 1) % (worker_thread_group->threadcount);
  140. worker_thread_group->last_scheduled = schedule;
  141. pthread_mutex_lock (&worker_thread_group->threads[schedule].new_work_mutex);
  142. if (queue_is_full (&worker_thread_group->threads[schedule].queue)) {
  143. pthread_mutex_unlock (&worker_thread_group->threads[schedule].new_work_mutex);
  144. return (-1);
  145. }
  146. queue_item_add (&worker_thread_group->threads[schedule].queue, item);
  147. pthread_cond_signal (&worker_thread_group->threads[schedule].new_work_cond);
  148. pthread_mutex_unlock (&worker_thread_group->threads[schedule].new_work_mutex);
  149. return (0);
  150. }
  151. void worker_thread_group_wait (
  152. struct worker_thread_group *worker_thread_group)
  153. {
  154. int i;
  155. for (i = 0; i < worker_thread_group->threadcount; i++) {
  156. pthread_mutex_lock (&worker_thread_group->threads[i].done_work_mutex);
  157. if (queue_is_empty (&worker_thread_group->threads[i].queue) == 0) {
  158. pthread_cond_wait (&worker_thread_group->threads[i].done_work_cond,
  159. &worker_thread_group->threads[i].done_work_mutex);
  160. }
  161. pthread_mutex_unlock (&worker_thread_group->threads[i].done_work_mutex);
  162. }
  163. }
  164. void worker_thread_group_exit (
  165. struct worker_thread_group *worker_thread_group)
  166. {
  167. int i;
  168. for (i = 0; i < worker_thread_group->threadcount; i++) {
  169. pthread_cancel (worker_thread_group->threads[i].thread_id);
  170. }
  171. }