gc.c 4.7 KB

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