gc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012 Inria
  4. * Copyright (C) 2012,2014-2015,2017 CNRS
  5. * Copyright (C) 2010-2013 Université de Bordeaux
  6. * Copyright (C) 2012 Vincent Danjean
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include "gc.h"
  20. #include "event.h"
  21. #include "socl.h"
  22. #include <stdlib.h>
  23. /**
  24. * Garbage collection thread
  25. */
  26. /* List of entities to be released */
  27. static volatile entity gc_list = NULL;
  28. static volatile entity entities = NULL;
  29. /* Mutex and cond for release */
  30. static starpu_pthread_mutex_t gc_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  31. static starpu_pthread_cond_t gc_cond = STARPU_PTHREAD_COND_INITIALIZER;
  32. /* Set to 1 to stop release thread execution */
  33. static volatile int gc_stop_required = 0;
  34. #define GC_LOCK STARPU_PTHREAD_MUTEX_LOCK(&gc_mutex)
  35. #define GC_UNLOCK { STARPU_PTHREAD_COND_SIGNAL(&gc_cond); \
  36. STARPU_PTHREAD_MUTEX_UNLOCK(&gc_mutex);}
  37. #define GC_UNLOCK_NO_SIGNAL STARPU_PTHREAD_MUTEX_UNLOCK(&gc_mutex)
  38. /* Thread routine */
  39. static void * gc_thread_routine(void *UNUSED(arg))
  40. {
  41. GC_LOCK;
  42. do
  43. {
  44. /* Make a copy of the gc_list to allow callbacks to add things into it */
  45. entity rs = gc_list;
  46. gc_list = NULL;
  47. GC_UNLOCK_NO_SIGNAL;
  48. entity r = rs;
  49. while (r != NULL)
  50. {
  51. /* Call entity release callback */
  52. if (r->release_callback != NULL)
  53. {
  54. r->release_callback(r);
  55. }
  56. /* Release entity */
  57. entity next = r->next;
  58. free(r);
  59. r = next;
  60. }
  61. GC_LOCK;
  62. /* Check if new entities have been added */
  63. if (gc_list != NULL)
  64. continue;
  65. /* Stop if required */
  66. if (gc_stop_required)
  67. {
  68. GC_UNLOCK_NO_SIGNAL;
  69. break;
  70. }
  71. /* Otherwise we sleep */
  72. STARPU_PTHREAD_COND_WAIT(&gc_cond, &gc_mutex);
  73. } while (1);
  74. starpu_pthread_exit(NULL);
  75. }
  76. static starpu_pthread_t gc_thread;
  77. /* Start garbage collection */
  78. void gc_start(void)
  79. {
  80. STARPU_PTHREAD_CREATE(&gc_thread, NULL, gc_thread_routine, NULL);
  81. }
  82. /* Stop garbage collection */
  83. void gc_stop(void)
  84. {
  85. GC_LOCK;
  86. gc_stop_required = 1;
  87. GC_UNLOCK;
  88. STARPU_PTHREAD_JOIN(gc_thread, NULL);
  89. }
  90. int gc_entity_release_ex(entity e, const char * DEBUG_PARAM(caller))
  91. {
  92. DEBUG_MSG("[%s] Decrementing refcount of %s %p to ", caller, e->name, (void *)e);
  93. /* Decrement reference count */
  94. int refs = __sync_sub_and_fetch(&e->refs, 1);
  95. DEBUG_MSG_NOHEAD("%d\n", refs);
  96. assert(refs >= 0);
  97. if (refs != 0)
  98. return 0;
  99. DEBUG_MSG("[%s] Releasing %s %p\n", caller, e->name, (void *)e);
  100. GC_LOCK;
  101. /* Remove entity from the entities list */
  102. if (e->prev != NULL)
  103. e->prev->next = e->next;
  104. if (e->next != NULL)
  105. e->next->prev = e->prev;
  106. if (entities == e)
  107. entities = e->next;
  108. /* Put entity in the release queue */
  109. e->next = gc_list;
  110. gc_list = e;
  111. GC_UNLOCK;
  112. return 1;
  113. }
  114. /**
  115. * Initialize entity
  116. */
  117. void gc_entity_init(void *arg, void (*release_callback)(void*), char * name)
  118. {
  119. DEBUG_MSG("Initializing entity %p (%s)\n", arg, name);
  120. struct entity * e = (entity)arg;
  121. e->dispatch = &socl_master_dispatch;
  122. e->refs = 1;
  123. e->release_callback = release_callback;
  124. e->prev = NULL;
  125. e->name = name;
  126. GC_LOCK;
  127. e->next = entities;
  128. if (entities != NULL)
  129. entities->prev = e;
  130. entities = e;
  131. GC_UNLOCK_NO_SIGNAL;
  132. }
  133. /**
  134. * Allocate and initialize entity
  135. */
  136. void * gc_entity_alloc(unsigned int size, void (*release_callback)(void*), char * name)
  137. {
  138. void * e = malloc(size);
  139. gc_entity_init(e, release_callback, name);
  140. return e;
  141. }
  142. /** Retain entity */
  143. void gc_entity_retain_ex(void *arg, const char * DEBUG_PARAM(caller))
  144. {
  145. struct entity * e = (entity)arg;
  146. #ifdef DEBUG
  147. int refs =
  148. #endif
  149. __sync_add_and_fetch(&e->refs, 1);
  150. DEBUG_MSG("[%s] Incrementing refcount of %s %p to %d\n", caller, e->name, e, refs);
  151. }
  152. int gc_active_entity_count(void)
  153. {
  154. int i = 0;
  155. entity e = entities;
  156. while (e != NULL)
  157. {
  158. i++;
  159. e = e->next;
  160. }
  161. return i;
  162. }
  163. void gc_print_remaining_entities(void)
  164. {
  165. DEBUG_MSG("Remaining entities:\n");
  166. GC_LOCK;
  167. entity e = entities;
  168. while (e != NULL)
  169. {
  170. DEBUG_MSG(" - %s %p\n", e->name, (void *)e);
  171. e = e->next;
  172. }
  173. GC_UNLOCK;
  174. }
  175. #undef GC_LOCK
  176. #undef GC_UNLOCK
  177. #undef GC_UNLOCK_NO_SIGNAL