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