123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- #include "pthread.h"
- #include "semaphore.h"
- #include "implement.h"
- int
- sem_trywait (sem_t * sem)
- {
- int result = 0;
- sem_t s = *sem;
- if (s == NULL)
- {
- result = EINVAL;
- }
- else if ((result = pthread_mutex_lock (&s->lock)) == 0)
- {
-
- if (*sem == NULL)
- {
- (void) pthread_mutex_unlock (&s->lock);
- errno = EINVAL;
- return -1;
- }
- if (s->value > 0)
- {
- s->value--;
- }
- else
- {
- result = EAGAIN;
- }
- (void) pthread_mutex_unlock (&s->lock);
- }
- if (result != 0)
- {
- errno = result;
- return -1;
- }
- return 0;
- }
|