[799c767] | 1 | /* |
---|
[6a0898b] | 2 | * COPYRIGHT (c) 1989-2008. |
---|
[412dbff6] | 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
| 7 | * http://www.rtems.com/license/LICENSE. |
---|
| 8 | * |
---|
[799c767] | 9 | * $Id$ |
---|
| 10 | */ |
---|
| 11 | |
---|
[f42b726] | 12 | #if HAVE_CONFIG_H |
---|
| 13 | #include "config.h" |
---|
| 14 | #endif |
---|
| 15 | |
---|
[799c767] | 16 | #include <stdarg.h> |
---|
| 17 | |
---|
| 18 | #include <errno.h> |
---|
| 19 | #include <fcntl.h> |
---|
| 20 | #include <pthread.h> |
---|
| 21 | #include <semaphore.h> |
---|
| 22 | #include <limits.h> |
---|
| 23 | |
---|
| 24 | #include <rtems/system.h> |
---|
| 25 | #include <rtems/score/object.h> |
---|
| 26 | #include <rtems/posix/semaphore.h> |
---|
| 27 | #include <rtems/posix/time.h> |
---|
[188c82b] | 28 | #include <rtems/seterr.h> |
---|
[799c767] | 29 | |
---|
| 30 | /*PAGE |
---|
| 31 | * |
---|
| 32 | * 11.2.6 Lock a Semaphore, P1003.1b-1993, p.226 |
---|
| 33 | * |
---|
| 34 | * NOTE: P1003.4b/D8 adds sem_timedwait(), p. 27 |
---|
| 35 | */ |
---|
| 36 | |
---|
| 37 | int sem_timedwait( |
---|
| 38 | sem_t *sem, |
---|
[84b6c050] | 39 | const struct timespec *abstime |
---|
[799c767] | 40 | ) |
---|
| 41 | { |
---|
[6a0898b] | 42 | Watchdog_Interval ticks; |
---|
[f8437c8] | 43 | bool do_wait = true; |
---|
[6a0898b] | 44 | POSIX_Absolute_timeout_conversion_results_t status; |
---|
| 45 | int lock_status; |
---|
| 46 | |
---|
[84b6c050] | 47 | /* |
---|
[6a0898b] | 48 | * POSIX requires that blocking calls with timeouts that take |
---|
| 49 | * an absolute timeout must ignore issues with the absolute |
---|
| 50 | * time provided if the operation would otherwise succeed. |
---|
| 51 | * So we check the abstime provided, and hold on to whether it |
---|
| 52 | * is valid or not. If it isn't correct and in the future, |
---|
| 53 | * then we do a polling operation and convert the UNSATISFIED |
---|
| 54 | * status into the appropriate error. |
---|
[6805ac37] | 55 | * |
---|
[1de949a8] | 56 | * If the status is POSIX_ABSOLUTE_TIMEOUT_INVALID, |
---|
[6805ac37] | 57 | * POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST, or POSIX_ABSOLUTE_TIMEOUT_IS_NOW, |
---|
| 58 | * then we should not wait. |
---|
[84b6c050] | 59 | */ |
---|
[6a0898b] | 60 | status = _POSIX_Absolute_timeout_to_ticks( abstime, &ticks ); |
---|
[6805ac37] | 61 | if ( status != POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE ) |
---|
| 62 | do_wait = false; |
---|
[6a0898b] | 63 | |
---|
| 64 | lock_status = _POSIX_Semaphore_Wait_support( sem, do_wait, ticks ); |
---|
[84b6c050] | 65 | |
---|
| 66 | /* |
---|
[6a0898b] | 67 | * This service only gives us the option to block. We used a polling |
---|
| 68 | * attempt to obtain if the abstime was not in the future. If we did |
---|
| 69 | * not obtain the semaphore, then not look at the status immediately, |
---|
| 70 | * make sure the right reason is returned. |
---|
[84b6c050] | 71 | */ |
---|
[6a0898b] | 72 | if ( !do_wait && (lock_status == EBUSY) ) { |
---|
| 73 | switch (lock_status) { |
---|
| 74 | case POSIX_ABSOLUTE_TIMEOUT_INVALID: |
---|
| 75 | return EINVAL; |
---|
| 76 | case POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST: |
---|
| 77 | case POSIX_ABSOLUTE_TIMEOUT_IS_NOW: |
---|
| 78 | return ETIMEDOUT; |
---|
| 79 | case POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE: |
---|
| 80 | break; |
---|
[0c2ec7f] | 81 | } |
---|
[6a0898b] | 82 | } |
---|
[84b6c050] | 83 | |
---|
[6a0898b] | 84 | return lock_status; |
---|
[799c767] | 85 | } |
---|