123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #include <pte_osal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "pthread.h"
- #include "implement.h"
- static int
- pte_timed_eventwait (pte_osSemaphoreHandle event, const struct timespec *abstime)
- {
- unsigned int milliseconds;
- pte_osResult status;
- int retval;
- if (abstime == NULL)
- {
- status = pte_osSemaphorePend(event, NULL);
- }
- else
- {
-
- milliseconds = pte_relmillisecs (abstime);
- status = pte_osSemaphorePend(event, &milliseconds);
- }
- if (status == PTE_OS_TIMEOUT)
- {
- retval = ETIMEDOUT;
- }
- else
- {
- retval = 0;
- }
- return retval;
- }
- int
- pthread_mutex_timedlock (pthread_mutex_t * mutex,
- const struct timespec *abstime)
- {
- int result;
- pthread_mutex_t mx;
-
-
- if (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
- {
- if ((result = pte_mutex_check_need_init (mutex)) != 0)
- {
- return (result);
- }
- }
- mx = *mutex;
- if (mx->kind == PTHREAD_MUTEX_NORMAL)
- {
- if (PTE_ATOMIC_EXCHANGE(&mx->lock_idx,1) != 0)
- {
- while (PTE_ATOMIC_EXCHANGE(&mx->lock_idx,-1) != 0)
- {
- if (0 != (result = pte_timed_eventwait (mx->handle, abstime)))
- {
- return result;
- }
- }
- }
- }
- else
- {
- pthread_t self = pthread_self();
- if (PTE_ATOMIC_COMPARE_EXCHANGE(&mx->lock_idx,1,0) == 0)
- {
- mx->recursive_count = 1;
- mx->ownerThread = self;
- }
- else
- {
- if (pthread_equal (mx->ownerThread, self))
- {
- if (mx->kind == PTHREAD_MUTEX_RECURSIVE)
- {
- mx->recursive_count++;
- }
- else
- {
- return EDEADLK;
- }
- }
- else
- {
- while (PTE_ATOMIC_EXCHANGE(&mx->lock_idx,-1) != 0)
- {
- if (0 != (result = pte_timed_eventwait (mx->handle, abstime)))
- {
- return result;
- }
- }
- mx->recursive_count = 1;
- mx->ownerThread = self;
- }
- }
- }
- return 0;
- }
|