gc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #warning FIXME: free memory
  52. // free(r);
  53. r = next;
  54. }
  55. GC_LOCK;
  56. /* Check if new entities have been added */
  57. if (gc_list != NULL)
  58. continue;
  59. /* Stop if required */
  60. if (gc_stop_required) {
  61. GC_UNLOCK_NO_SIGNAL;
  62. break;
  63. }
  64. /* Otherwise we sleep */
  65. pthread_cond_wait(&gc_cond, &gc_mutex);
  66. } while (1);
  67. pthread_exit(NULL);
  68. }
  69. static pthread_t gc_thread;
  70. /* Start garbage collection */
  71. void gc_start(void) {
  72. pthread_create(&gc_thread, NULL, gc_thread_routine, NULL);
  73. }
  74. /* Stop garbage collection */
  75. void gc_stop(void) {
  76. GC_LOCK;
  77. gc_stop_required = 1;
  78. GC_UNLOCK;
  79. pthread_join(gc_thread, NULL);
  80. }
  81. int gc_entity_release_ex(entity e) {
  82. /* Decrement reference count */
  83. int refs = __sync_sub_and_fetch(&e->refs, 1);
  84. if (refs != 0)
  85. return 0;
  86. DEBUG_MSG("Releasing entity %lx\n", e);
  87. GC_LOCK;
  88. /* Remove entity from the entities list */
  89. if (e->prev != NULL)
  90. e->prev->next = e->next;
  91. if (e->next != NULL)
  92. e->next->prev = e->prev;
  93. if (entities == e)
  94. entities = e->next;
  95. /* Put entity in the release queue */
  96. e->next = gc_list;
  97. gc_list = e;
  98. GC_UNLOCK;
  99. return 1;
  100. }
  101. /**
  102. * Initialize entity
  103. */
  104. void gc_entity_init(void *arg, void (*release_callback)(void*)) {
  105. struct entity * e = (entity)arg;
  106. e->refs = 1;
  107. e->release_callback = release_callback;
  108. e->prev = NULL;
  109. GC_LOCK;
  110. e->next = entities;
  111. if (entities != NULL)
  112. entities->prev = e;
  113. entities = e;
  114. GC_UNLOCK_NO_SIGNAL;
  115. }
  116. /**
  117. * Allocate and initialize entity
  118. */
  119. void * gc_entity_alloc(unsigned int size, void (*release_callback)(void*)) {
  120. void * e = malloc(size);
  121. gc_entity_init(e, release_callback);
  122. return e;
  123. }
  124. /** Retain entity */
  125. void gc_entity_retain(void *arg) {
  126. struct entity * e = (entity)arg;
  127. __sync_fetch_and_add(&e->refs, 1);
  128. }
  129. int gc_active_entity_count(void) {
  130. int i = 0;
  131. entity e = entities;
  132. while (e != NULL) {
  133. i++;
  134. e = e->next;
  135. }
  136. return i;
  137. }
  138. #undef GC_LOCK
  139. #undef GC_UNLOCK
  140. #undef GC_UNLOCK_NO_SIGNAL