worker.c 3.3 KB

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