1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2008. |
---|
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 | */ |
---|
9 | |
---|
10 | #if HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <stdarg.h> |
---|
15 | |
---|
16 | #include <errno.h> |
---|
17 | #include <fcntl.h> |
---|
18 | #include <pthread.h> |
---|
19 | #include <semaphore.h> |
---|
20 | #include <limits.h> |
---|
21 | |
---|
22 | #include <rtems/system.h> |
---|
23 | #include <rtems/score/object.h> |
---|
24 | #include <rtems/posix/semaphore.h> |
---|
25 | #include <rtems/posix/time.h> |
---|
26 | #include <rtems/seterr.h> |
---|
27 | |
---|
28 | /* |
---|
29 | * 11.2.6 Lock a Semaphore, P1003.1b-1993, p.226 |
---|
30 | * |
---|
31 | * NOTE: P1003.4b/D8 adds sem_timedwait(), p. 27 |
---|
32 | */ |
---|
33 | |
---|
34 | int sem_timedwait( |
---|
35 | sem_t *sem, |
---|
36 | const struct timespec *abstime |
---|
37 | ) |
---|
38 | { |
---|
39 | Watchdog_Interval ticks; |
---|
40 | bool do_wait = true; |
---|
41 | POSIX_Absolute_timeout_conversion_results_t status; |
---|
42 | int lock_status; |
---|
43 | |
---|
44 | /* |
---|
45 | * POSIX requires that blocking calls with timeouts that take |
---|
46 | * an absolute timeout must ignore issues with the absolute |
---|
47 | * time provided if the operation would otherwise succeed. |
---|
48 | * So we check the abstime provided, and hold on to whether it |
---|
49 | * is valid or not. If it isn't correct and in the future, |
---|
50 | * then we do a polling operation and convert the UNSATISFIED |
---|
51 | * status into the appropriate error. |
---|
52 | * |
---|
53 | * If the status is POSIX_ABSOLUTE_TIMEOUT_INVALID, |
---|
54 | * POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST, or POSIX_ABSOLUTE_TIMEOUT_IS_NOW, |
---|
55 | * then we should not wait. |
---|
56 | */ |
---|
57 | status = _POSIX_Absolute_timeout_to_ticks( abstime, &ticks ); |
---|
58 | if ( status != POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE ) |
---|
59 | do_wait = false; |
---|
60 | |
---|
61 | lock_status = _POSIX_Semaphore_Wait_support( sem, do_wait, ticks ); |
---|
62 | |
---|
63 | /* |
---|
64 | * This service only gives us the option to block. We used a polling |
---|
65 | * attempt to obtain if the abstime was not in the future. If we did |
---|
66 | * not obtain the semaphore, then not look at the status immediately, |
---|
67 | * make sure the right reason is returned. |
---|
68 | */ |
---|
69 | if ( !do_wait && (lock_status == EBUSY) ) { |
---|
70 | if ( lock_status == POSIX_ABSOLUTE_TIMEOUT_INVALID ) |
---|
71 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
72 | if ( lock_status == POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST || |
---|
73 | lock_status == POSIX_ABSOLUTE_TIMEOUT_IS_NOW ) |
---|
74 | rtems_set_errno_and_return_minus_one( ETIMEDOUT ); |
---|
75 | } |
---|
76 | |
---|
77 | return lock_status; |
---|
78 | } |
---|