tls-helper.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * tls-helper.c
  3. *
  4. * Description:
  5. *
  6. * --------------------------------------------------------------------------
  7. *
  8. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  9. * Copyright(C) 2008 Jason Schmidlapp
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2 of the License, or (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library in the file COPYING.LIB;
  25. * if not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "tls-helper.h"
  31. static int *keysUsed;
  32. /* We don't protect this - it's only written on startup */
  33. static int maxTlsValues;
  34. pte_osMutexHandle globalTlsLock;
  35. pte_osResult pteTlsGlobalInit(int maxEntries)
  36. {
  37. int i;
  38. pte_osResult result;
  39. pte_osMutexCreate(&globalTlsLock);
  40. keysUsed = (int *) malloc(maxEntries * sizeof(int));
  41. if (keysUsed != NULL)
  42. {
  43. for (i=0;i<maxEntries;i++)
  44. {
  45. keysUsed[i] = 0;
  46. }
  47. maxTlsValues = maxEntries;
  48. result = PTE_OS_OK;
  49. }
  50. else
  51. {
  52. result = PTE_OS_NO_RESOURCES;
  53. }
  54. return result;
  55. }
  56. void * pteTlsThreadInit(void)
  57. {
  58. void ** pTlsStruct;
  59. int i;
  60. pTlsStruct = (void **) malloc(maxTlsValues * sizeof(void*));
  61. // PTE library assumes that keys are initialized to zero
  62. for (i=0; i<maxTlsValues;i++)
  63. {
  64. pTlsStruct[i] = 0;
  65. }
  66. return (void *) pTlsStruct;
  67. }
  68. pte_osResult pteTlsAlloc(unsigned int *pKey)
  69. {
  70. int i;
  71. pte_osResult result = PTE_OS_NO_RESOURCES;
  72. pte_osMutexLock(globalTlsLock);
  73. for (i=0;i<maxTlsValues;i++)
  74. {
  75. if (keysUsed[i] == 0)
  76. {
  77. keysUsed[i] = 1;
  78. *pKey = i+1;
  79. result = PTE_OS_OK;
  80. break;
  81. }
  82. }
  83. pte_osMutexUnlock(globalTlsLock);
  84. return result;
  85. }
  86. void * pteTlsGetValue(void *pTlsThreadStruct, unsigned int index)
  87. {
  88. void **pTls = (void **) pTlsThreadStruct;
  89. if (keysUsed[index-1])
  90. {
  91. if (pTls != NULL)
  92. {
  93. return pTls[index-1];
  94. }
  95. else
  96. {
  97. return NULL;
  98. }
  99. }
  100. else
  101. {
  102. return NULL;
  103. }
  104. }
  105. pte_osResult pteTlsSetValue(void *pTlsThreadStruct, unsigned int index, void * value)
  106. {
  107. pte_osResult result;
  108. void ** pTls = (void **) pTlsThreadStruct;
  109. if (pTls != NULL)
  110. {
  111. pTls[index-1] = value;
  112. result = PTE_OS_OK;
  113. }
  114. else
  115. {
  116. result = PTE_OS_INVALID_PARAM;
  117. }
  118. return result;
  119. }
  120. pte_osResult pteTlsFree(unsigned int index)
  121. {
  122. pte_osResult result;
  123. if (keysUsed != NULL)
  124. {
  125. pte_osMutexLock(globalTlsLock);
  126. keysUsed[index-1] = 0;
  127. pte_osMutexUnlock(globalTlsLock);
  128. result = PTE_OS_OK;
  129. }
  130. else
  131. {
  132. result = PTE_OS_GENERAL_FAILURE;
  133. }
  134. return result;
  135. }
  136. void pteTlsThreadDestroy(void * pTlsThreadStruct)
  137. {
  138. free(pTlsThreadStruct);
  139. }
  140. void pteTlsGlobalDestroy(void)
  141. {
  142. pte_osMutexDelete(globalTlsLock);
  143. free(keysUsed);
  144. }