worker.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*!
  2. * \file lib/gis/worker.c
  3. *
  4. * \brief GIS Library - Worker functions.
  5. *
  6. * (C) 2008-2014 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Glynn Clements
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. #ifdef HAVE_PTHREAD_H
  18. /****************************************************************************/
  19. #include <pthread.h>
  20. #define DEFAULT_WORKERS 0
  21. struct worker {
  22. void (*func)(void *);
  23. void *closure;
  24. void **ref;
  25. pthread_t thread;
  26. pthread_cond_t cond;
  27. pthread_mutex_t mutex;
  28. int cancel;
  29. };
  30. static int num_workers;
  31. static struct worker *workers;
  32. static pthread_cond_t worker_cond;
  33. static pthread_mutex_t worker_mutex;
  34. /****************************************************************************/
  35. static void *worker(void *arg)
  36. {
  37. struct worker *w = arg;
  38. while (!w->cancel) {
  39. pthread_mutex_lock(&w->mutex);
  40. while (!w->func)
  41. pthread_cond_wait(&w->cond, &w->mutex);
  42. (*w->func)(w->closure);
  43. w->func = NULL;
  44. w->closure = NULL;
  45. *w->ref = NULL;
  46. pthread_mutex_unlock(&w->mutex);
  47. pthread_cond_signal(&w->cond);
  48. pthread_cond_signal(&worker_cond);
  49. }
  50. return NULL;
  51. }
  52. static struct worker *get_worker(void)
  53. {
  54. int i;
  55. for (i = 0; i < num_workers; i++) {
  56. struct worker *w = &workers[i];
  57. if (!w->func)
  58. return w;
  59. }
  60. return NULL;
  61. }
  62. void G_begin_execute(void (*func)(void *), void *closure, void **ref, int force)
  63. {
  64. struct worker *w;
  65. if (*ref)
  66. G_fatal_error(_("Task already has a worker"));
  67. pthread_mutex_lock(&worker_mutex);
  68. while (w = get_worker(), force && num_workers > 0 && !w)
  69. pthread_cond_wait(&worker_cond, &worker_mutex);
  70. *ref = w;
  71. if (!w) {
  72. pthread_mutex_unlock(&worker_mutex);
  73. (*func)(closure);
  74. return;
  75. }
  76. pthread_mutex_lock(&w->mutex);
  77. w->func = func;
  78. w->closure = closure;
  79. w->ref = ref;
  80. pthread_cond_signal(&w->cond);
  81. pthread_mutex_unlock(&w->mutex);
  82. pthread_mutex_unlock(&worker_mutex);
  83. }
  84. void G_end_execute(void **ref)
  85. {
  86. struct worker *w = *ref;
  87. if (!w)
  88. return;
  89. pthread_mutex_lock(&w->mutex);
  90. while (*ref)
  91. pthread_cond_wait(&w->cond, &w->mutex);
  92. pthread_mutex_unlock(&w->mutex);
  93. }
  94. void G_init_workers(void)
  95. {
  96. const char *p = getenv("WORKERS");
  97. int i;
  98. pthread_mutex_init(&worker_mutex, NULL);
  99. pthread_cond_init(&worker_cond, NULL);
  100. num_workers = p ? atoi(p) : DEFAULT_WORKERS;
  101. workers = G_calloc(num_workers, sizeof(struct worker));
  102. for (i = 0; i < num_workers; i++) {
  103. struct worker *w = &workers[i];
  104. pthread_mutex_init(&w->mutex, NULL);
  105. pthread_cond_init(&w->cond, NULL);
  106. pthread_create(&w->thread, NULL, worker, w);
  107. }
  108. }
  109. void G_finish_workers(void)
  110. {
  111. int i;
  112. for (i = 0; i < num_workers; i++) {
  113. struct worker *w = &workers[i];
  114. w->cancel = 1;
  115. pthread_cancel(w->thread);
  116. }
  117. for (i = 0; i < num_workers; i++) {
  118. struct worker *w = &workers[i];
  119. pthread_join(w->thread, NULL);
  120. pthread_mutex_destroy(&w->mutex);
  121. pthread_cond_destroy(&w->cond);
  122. }
  123. pthread_mutex_destroy(&worker_mutex);
  124. pthread_cond_destroy(&worker_cond);
  125. }
  126. /****************************************************************************/
  127. #else
  128. /****************************************************************************/
  129. void G_begin_execute(void (*func)(void *), void *closure, void **ref, int force)
  130. {
  131. (*func)(closure);
  132. }
  133. void G_end_execute(void **ref)
  134. {
  135. }
  136. void G_init_workers(void)
  137. {
  138. }
  139. void G_finish_workers(void)
  140. {
  141. }
  142. /****************************************************************************/
  143. #endif