gc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 University of Bordeaux
  4. * Copyright (C) 2012 CNRS
  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 pthread_mutex_t gc_mutex = PTHREAD_MUTEX_INITIALIZER;
  30. static pthread_cond_t gc_cond = PTHREAD_COND_INITIALIZER;
  31. /* Set to 1 to stop release thread execution */
  32. static volatile int gc_stop_required = 0;
  33. #define GC_LOCK pthread_mutex_lock(&gc_mutex)
  34. #define GC_UNLOCK { pthread_cond_signal(&gc_cond); \
  35. pthread_mutex_unlock(&gc_mutex);}
  36. #define GC_UNLOCK_NO_SIGNAL 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. pthread_cond_wait(&gc_cond, &gc_mutex);
  67. } while (1);
  68. pthread_exit(NULL);
  69. }
  70. static pthread_t gc_thread;
  71. /* Start garbage collection */
  72. void gc_start(void) {
  73. 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. pthread_join(gc_thread, NULL);
  81. }
  82. int gc_entity_release_ex(entity e, const char * DEBUG_PARAM(caller)) {
  83. /* Decrement reference count */
  84. int refs = __sync_sub_and_fetch(&e->refs, 1);
  85. DEBUG_MSG("[%s] Decrementing refcount of %s %p to %d\n", caller, e->name, e, refs);
  86. assert(refs >= 0);
  87. if (refs != 0)
  88. return 0;
  89. DEBUG_MSG("[%s] Releasing %s %p\n", caller, e->name, e);
  90. GC_LOCK;
  91. /* Remove entity from the entities list */
  92. if (e->prev != NULL)
  93. e->prev->next = e->next;
  94. if (e->next != NULL)
  95. e->next->prev = e->prev;
  96. if (entities == e)
  97. entities = e->next;
  98. /* Put entity in the release queue */
  99. e->next = gc_list;
  100. gc_list = e;
  101. GC_UNLOCK;
  102. return 1;
  103. }
  104. /**
  105. * Initialize entity
  106. */
  107. void gc_entity_init(void *arg, void (*release_callback)(void*), char * name) {
  108. DEBUG_MSG("Initializing entity %p (%s)\n", arg, name);
  109. struct entity * e = (entity)arg;
  110. e->dispatch = &socl_master_dispatch;
  111. e->refs = 1;
  112. e->release_callback = release_callback;
  113. e->prev = NULL;
  114. e->name = name;
  115. GC_LOCK;
  116. e->next = entities;
  117. if (entities != NULL)
  118. entities->prev = e;
  119. entities = e;
  120. GC_UNLOCK_NO_SIGNAL;
  121. }
  122. /**
  123. * Allocate and initialize entity
  124. */
  125. void * gc_entity_alloc(unsigned int size, void (*release_callback)(void*), char * name) {
  126. void * e = malloc(size);
  127. gc_entity_init(e, release_callback, name);
  128. return e;
  129. }
  130. /** Retain entity */
  131. void gc_entity_retain_ex(void *arg, const char * DEBUG_PARAM(caller)) {
  132. struct entity * e = (entity)arg;
  133. #ifdef DEBUG
  134. int refs =
  135. #endif
  136. __sync_add_and_fetch(&e->refs, 1);
  137. DEBUG_MSG("[%s] Incrementing refcount of %s %p to %d\n", caller, e->name, e, refs);
  138. }
  139. int gc_active_entity_count(void) {
  140. int i = 0;
  141. entity e = entities;
  142. while (e != NULL) {
  143. i++;
  144. e = e->next;
  145. }
  146. return i;
  147. }
  148. void gc_print_remaining_entities(void) {
  149. DEBUG_MSG("Remaining entities:\n");
  150. GC_LOCK;
  151. entity e = entities;
  152. while (e != NULL) {
  153. DEBUG_MSG(" - %s %p\n", e->name, e);
  154. e = e->next;
  155. }
  156. GC_UNLOCK;
  157. }
  158. #undef GC_LOCK
  159. #undef GC_UNLOCK
  160. #undef GC_UNLOCK_NO_SIGNAL