gc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011 University of Bordeaux
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "gc.h"
  17. #include "event.h"
  18. #include "socl.h"
  19. #include <stdlib.h>
  20. /**
  21. * Garbage collection thread
  22. */
  23. /* List of entities to be released */
  24. static volatile entity gc_list = NULL;
  25. static volatile entity entities = NULL;
  26. /* Mutex and cond for release */
  27. static pthread_mutex_t gc_mutex = PTHREAD_MUTEX_INITIALIZER;
  28. static pthread_cond_t gc_cond = PTHREAD_COND_INITIALIZER;
  29. /* Set to 1 to stop release thread execution */
  30. static volatile int gc_stop_required = 0;
  31. #define GC_LOCK pthread_mutex_lock(&gc_mutex)
  32. #define GC_UNLOCK { pthread_cond_signal(&gc_cond); \
  33. pthread_mutex_unlock(&gc_mutex);}
  34. #define GC_UNLOCK_NO_SIGNAL pthread_mutex_unlock(&gc_mutex)
  35. /* Thread routine */
  36. static void * gc_thread_routine(void *UNUSED(arg)) {
  37. GC_LOCK;
  38. do {
  39. /* Make a copy of the gc_list to allow callbacks to add things into it */
  40. entity rs = gc_list;
  41. gc_list = NULL;
  42. GC_UNLOCK_NO_SIGNAL;
  43. entity r = rs;
  44. while (r != NULL) {
  45. /* Call entity release callback */
  46. if (r->release_callback != NULL) {
  47. r->release_callback(r);
  48. }
  49. /* Release entity */
  50. entity next = r->next;
  51. free(r);
  52. r = next;
  53. }
  54. GC_LOCK;
  55. /* Check if new entities have been added */
  56. if (gc_list != NULL)
  57. continue;
  58. /* Stop if required */
  59. if (gc_stop_required) {
  60. GC_UNLOCK_NO_SIGNAL;
  61. break;
  62. }
  63. /* Otherwise we sleep */
  64. pthread_cond_wait(&gc_cond, &gc_mutex);
  65. } while (1);
  66. pthread_exit(NULL);
  67. }
  68. static pthread_t gc_thread;
  69. /* Start garbage collection */
  70. void gc_start(void) {
  71. pthread_create(&gc_thread, NULL, gc_thread_routine, NULL);
  72. }
  73. /* Stop garbage collection */
  74. void gc_stop(void) {
  75. GC_LOCK;
  76. gc_stop_required = 1;
  77. GC_UNLOCK;
  78. pthread_join(gc_thread, NULL);
  79. }
  80. int gc_entity_release_ex(entity e) {
  81. /* Decrement reference count */
  82. int refs = __sync_sub_and_fetch(&e->refs, 1);
  83. if (refs != 0)
  84. return 0;
  85. DEBUG_MSG("Releasing entity %lx\n", e);
  86. GC_LOCK;
  87. /* Remove entity from the entities list */
  88. if (e->prev != NULL)
  89. e->prev->next = e->next;
  90. if (e->next != NULL)
  91. e->next->prev = e->prev;
  92. if (entities == e)
  93. entities = e->next;
  94. /* Put entity in the release queue */
  95. e->next = gc_list;
  96. gc_list = e;
  97. GC_UNLOCK;
  98. return 1;
  99. }
  100. /**
  101. * Initialize entity
  102. */
  103. void gc_entity_init(void *arg, void (*release_callback)(void*)) {
  104. struct entity * e = (entity)arg;
  105. e->refs = 1;
  106. e->release_callback = release_callback;
  107. e->prev = NULL;
  108. GC_LOCK;
  109. e->next = entities;
  110. if (entities != NULL)
  111. entities->prev = e;
  112. entities = e;
  113. GC_UNLOCK_NO_SIGNAL;
  114. }
  115. /**
  116. * Allocate and initialize entity
  117. */
  118. void * gc_entity_alloc(unsigned int size, void (*release_callback)(void*)) {
  119. void * e = malloc(size);
  120. gc_entity_init(e, release_callback);
  121. return e;
  122. }
  123. /** Retain entity */
  124. void gc_entity_retain(void *arg) {
  125. struct entity * e = (entity)arg;
  126. __sync_fetch_and_add(&e->refs, 1);
  127. }
  128. int gc_active_entity_count(void) {
  129. int i = 0;
  130. entity e = entities;
  131. while (e != NULL) {
  132. i++;
  133. e = e->next;
  134. }
  135. return i;
  136. }
  137. #undef GC_LOCK
  138. #undef GC_UNLOCK
  139. #undef GC_UNLOCK_NO_SIGNAL