gc.c 4.5 KB

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