vxworks_partition.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * vxworks_partition.c
  3. */
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <taskLib.h>
  7. #include <taskHookLib.h>
  8. #include "private.h"
  9. static lub_vxworks_partition_t *first_partition;
  10. /*-------------------------------------------------------- */
  11. static void lub_vxworks_partition_task_delete_hook(WIND_TCB * pTcb)
  12. {
  13. lub_vxworks_partition_t **ptr;
  14. int tid = (int)pTcb;
  15. /* iterate the list of partitions */
  16. taskLock();
  17. for (ptr = &first_partition; *ptr; ptr = &(*ptr)->m_next_partition) {
  18. int val = taskVarGet(tid, (int *)&(*ptr)->m_local_heap);
  19. if (ERROR != val) {
  20. /* destroy this local heap */
  21. lub_partition_destroy_local_heap(&(*ptr)->m_base,
  22. (void *)val);
  23. /* and remove the task variable */
  24. taskVarDelete(tid, (int *)&(*ptr)->m_local_heap);
  25. }
  26. }
  27. taskUnlock();
  28. }
  29. /*-------------------------------------------------------- */
  30. lub_partition_t *lub_partition_create(const lub_partition_spec_t * spec)
  31. {
  32. lub_vxworks_partition_t *this;
  33. this = calloc(sizeof(lub_vxworks_partition_t), 1);
  34. if (this) {
  35. /* initialise the global mutex */
  36. if (ERROR ==
  37. semMInit(&this->m_sem,
  38. SEM_Q_PRIORITY | SEM_DELETE_SAFE |
  39. SEM_INVERSION_SAFE)) {
  40. assert(NULL == "semMInit() failed!");
  41. }
  42. lub_partition_lock(&this->m_base);
  43. /* initialise the local task variable system */
  44. taskVarInit();
  45. /* initialise the base class */
  46. lub_partition_init(&this->m_base, spec);
  47. /* add to the list of partitions */
  48. this->m_next_partition = first_partition;
  49. first_partition = this;
  50. if (this == first_partition) {
  51. /* register a task deletion hook */
  52. taskDeleteHookAdd((FUNCPTR)
  53. lub_vxworks_partition_task_delete_hook);
  54. }
  55. lub_partition_unlock(&this->m_base);
  56. }
  57. return this ? &this->m_base : 0;
  58. }
  59. /*-------------------------------------------------------- */
  60. void lub_partition_destroy(lub_partition_t * instance)
  61. {
  62. lub_vxworks_partition_t *this = (void *)instance;
  63. lub_vxworks_partition_t **ptr;
  64. /* finalise the base class */
  65. lub_partition_fini(&this->m_base);
  66. lub_partition_lock(&this->m_base);
  67. /* remove from the list of partitions */
  68. for (ptr = &first_partition; *ptr; ptr = &(*ptr)->m_next_partition) {
  69. if (&(*ptr)->m_base == instance) {
  70. /* remove from list */
  71. *ptr = (*ptr)->m_next_partition;
  72. break;
  73. }
  74. }
  75. if (!first_partition) {
  76. /* deregister delete hook */
  77. taskDeleteHookDelete((FUNCPTR)
  78. lub_vxworks_partition_task_delete_hook);
  79. }
  80. lub_partition_unlock(&this->m_base);
  81. free(this);
  82. }
  83. /*-------------------------------------------------------- */
  84. lub_heap_t *lub_partition__get_local_heap(lub_partition_t * instance)
  85. {
  86. lub_vxworks_partition_t *this = (void *)instance;
  87. return this->m_local_heap;
  88. }
  89. /*-------------------------------------------------------- */
  90. void
  91. lub_partition__set_local_heap(lub_partition_t * instance, lub_heap_t * heap)
  92. {
  93. lub_vxworks_partition_t *this = (void *)instance;
  94. if (this->m_local_heap) {
  95. assert(NULL == "Local heap already exists!");
  96. }
  97. /* add this memory to the local task */
  98. taskVarAdd(0, (int *)&this->m_local_heap);
  99. /* add set the value */
  100. this->m_local_heap = heap;
  101. }
  102. /*-------------------------------------------------------- */
  103. void lub_partition_lock(lub_partition_t * instance)
  104. {
  105. lub_vxworks_partition_t *this = (void *)instance;
  106. if (ERROR == semTake(&this->m_sem, WAIT_FOREVER)) {
  107. assert(NULL == "semTake() failed!");
  108. }
  109. }
  110. /*-------------------------------------------------------- */
  111. void lub_partition_unlock(lub_partition_t * instance)
  112. {
  113. lub_vxworks_partition_t *this = (void *)instance;
  114. if (ERROR == semGive(&this->m_sem)) {
  115. assert(NULL == "semGive() failed!");
  116. }
  117. }
  118. /*-------------------------------------------------------- */