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 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
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> |
---|
28 | #include <rtems/seterr.h> |
---|
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, |
---|
39 | const struct timespec *abstime |
---|
40 | ) |
---|
41 | { |
---|
42 | Watchdog_Interval ticks; |
---|
43 | boolean do_wait = TRUE; |
---|
44 | POSIX_Absolute_timeout_conversion_results_t status; |
---|
45 | int lock_status; |
---|
46 | |
---|
47 | /* |
---|
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. |
---|
55 | */ |
---|
56 | status = _POSIX_Absolute_timeout_to_ticks( abstime, &ticks ); |
---|
57 | switch ( status ) { |
---|
58 | case POSIX_ABSOLUTE_TIMEOUT_INVALID: |
---|
59 | case POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST: |
---|
60 | case POSIX_ABSOLUTE_TIMEOUT_IS_NOW: |
---|
61 | do_wait = FALSE; |
---|
62 | break; |
---|
63 | case POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE: |
---|
64 | do_wait = TRUE; |
---|
65 | break; |
---|
66 | } |
---|
67 | |
---|
68 | lock_status = _POSIX_Semaphore_Wait_support( sem, do_wait, ticks ); |
---|
69 | |
---|
70 | /* |
---|
71 | * This service only gives us the option to block. We used a polling |
---|
72 | * attempt to obtain if the abstime was not in the future. If we did |
---|
73 | * not obtain the semaphore, then not look at the status immediately, |
---|
74 | * make sure the right reason is returned. |
---|
75 | */ |
---|
76 | if ( !do_wait && (lock_status == EBUSY) ) { |
---|
77 | switch (lock_status) { |
---|
78 | case POSIX_ABSOLUTE_TIMEOUT_INVALID: |
---|
79 | return EINVAL; |
---|
80 | case POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST: |
---|
81 | case POSIX_ABSOLUTE_TIMEOUT_IS_NOW: |
---|
82 | return ETIMEDOUT; |
---|
83 | case POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE: |
---|
84 | break; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | return lock_status; |
---|
89 | } |
---|