vxworks_partition.c 4.1 KB

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