123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include "pte_osal.h"
- #include "pthread.h"
- #include "implement.h"
- #define PTE_ONCE_STARTED 1
- #define PTE_ONCE_INIT 0
- #define PTE_ONCE_DONE 2
- static void
- pte_once_init_routine_cleanup(void * arg)
- {
- pthread_once_t * once_control = (pthread_once_t *) arg;
- (void) PTE_ATOMIC_EXCHANGE(&once_control->state,PTE_ONCE_INIT);
- if (PTE_ATOMIC_EXCHANGE_ADD((int*)&once_control->semaphore, 0L))
- {
- pte_osSemaphorePost((pte_osSemaphoreHandle) once_control->semaphore, 1);
- }
- }
- int
- pthread_once (pthread_once_t * once_control, void (*init_routine) (void))
- {
- int result;
- int state;
- pte_osSemaphoreHandle sema;
- if (once_control == NULL || init_routine == NULL)
- {
- result = EINVAL;
- goto FAIL0;
- }
- else
- {
- result = 0;
- }
- while ((state =
- PTE_ATOMIC_COMPARE_EXCHANGE(&once_control->state,
- PTE_ONCE_STARTED,
- PTE_ONCE_INIT))
- != PTE_ONCE_DONE)
- {
- if (PTE_ONCE_INIT == state)
- {
- pthread_cleanup_push(pte_once_init_routine_cleanup, (void *) once_control);
- (*init_routine)();
- pthread_cleanup_pop(0);
- (void) PTE_ATOMIC_EXCHANGE(&once_control->state,PTE_ONCE_DONE);
-
- if (PTE_ATOMIC_EXCHANGE_ADD((int*)&once_control->semaphore, 0L))
- {
- pte_osSemaphorePost((pte_osSemaphoreHandle) once_control->semaphore,once_control->numSemaphoreUsers);
- }
- }
- else
- {
- PTE_ATOMIC_INCREMENT(&once_control->numSemaphoreUsers);
- if (!PTE_ATOMIC_EXCHANGE_ADD((int*)&once_control->semaphore, 0L))
- {
- pte_osSemaphoreCreate(0, (pte_osSemaphoreHandle*) &sema);
- if (PTE_ATOMIC_COMPARE_EXCHANGE((int *) &once_control->semaphore,
- (int) sema,
- 0))
- {
- pte_osSemaphoreDelete((pte_osSemaphoreHandle)sema);
- }
- }
-
- if (PTE_ATOMIC_EXCHANGE_ADD(&once_control->state, 0L) == PTE_ONCE_STARTED)
- {
- pte_osSemaphorePend((pte_osSemaphoreHandle) once_control->semaphore,NULL);
- }
- if (0 == PTE_ATOMIC_DECREMENT(&once_control->numSemaphoreUsers))
- {
-
- if ((sema =
- (pte_osSemaphoreHandle) PTE_ATOMIC_EXCHANGE((int *) &once_control->semaphore,0)))
- {
- pte_osSemaphoreDelete(sema);
- }
- }
- }
- }
-
- FAIL0:
- return (result);
- }
|