123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "tls-helper.h"
- static int *keysUsed;
- static int maxTlsValues;
- pte_osMutexHandle globalTlsLock;
- pte_osResult pteTlsGlobalInit(int maxEntries)
- {
- int i;
- pte_osResult result;
- pte_osMutexCreate(&globalTlsLock);
- keysUsed = (int *) malloc(maxEntries * sizeof(int));
- if (keysUsed != NULL)
- {
- for (i=0;i<maxEntries;i++)
- {
- keysUsed[i] = 0;
- }
- maxTlsValues = maxEntries;
- result = PTE_OS_OK;
- }
- else
- {
- result = PTE_OS_NO_RESOURCES;
- }
- return result;
- }
- void * pteTlsThreadInit(void)
- {
- void ** pTlsStruct;
- int i;
- pTlsStruct = (void **) malloc(maxTlsValues * sizeof(void*));
-
- for (i=0; i<maxTlsValues;i++)
- {
- pTlsStruct[i] = 0;
- }
- return (void *) pTlsStruct;
- }
- pte_osResult pteTlsAlloc(unsigned int *pKey)
- {
- int i;
- pte_osResult result = PTE_OS_NO_RESOURCES;
-
- pte_osMutexLock(globalTlsLock);
- for (i=0;i<maxTlsValues;i++)
- {
- if (keysUsed[i] == 0)
- {
- keysUsed[i] = 1;
- *pKey = i+1;
- result = PTE_OS_OK;
- break;
- }
- }
- pte_osMutexUnlock(globalTlsLock);
- return result;
- }
- void * pteTlsGetValue(void *pTlsThreadStruct, unsigned int index)
- {
- void **pTls = (void **) pTlsThreadStruct;
- if (keysUsed[index-1])
- {
- if (pTls != NULL)
- {
- return pTls[index-1];
- }
- else
- {
- return NULL;
- }
- }
- else
- {
- return NULL;
- }
- }
- pte_osResult pteTlsSetValue(void *pTlsThreadStruct, unsigned int index, void * value)
- {
- pte_osResult result;
- void ** pTls = (void **) pTlsThreadStruct;
- if (pTls != NULL)
- {
- pTls[index-1] = value;
- result = PTE_OS_OK;
- }
- else
- {
- result = PTE_OS_INVALID_PARAM;
- }
- return result;
- }
- pte_osResult pteTlsFree(unsigned int index)
- {
- pte_osResult result;
- if (keysUsed != NULL)
- {
- pte_osMutexLock(globalTlsLock);
- keysUsed[index-1] = 0;
- pte_osMutexUnlock(globalTlsLock);
- result = PTE_OS_OK;
- }
- else
- {
- result = PTE_OS_GENERAL_FAILURE;
- }
- return result;
- }
- void pteTlsThreadDestroy(void * pTlsThreadStruct)
- {
- free(pTlsThreadStruct);
- }
- void pteTlsGlobalDestroy(void)
- {
- pte_osMutexDelete(globalTlsLock);
- free(keysUsed);
- }
|