1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Mutex Timed Lock |
---|
5 | * @ingroup POSIXAPI |
---|
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 <errno.h> |
---|
22 | #include <pthread.h> |
---|
23 | |
---|
24 | #include <rtems/system.h> |
---|
25 | #include <rtems/score/coremuteximpl.h> |
---|
26 | #include <rtems/score/todimpl.h> |
---|
27 | #include <rtems/posix/muteximpl.h> |
---|
28 | #include <rtems/posix/priorityimpl.h> |
---|
29 | |
---|
30 | /** |
---|
31 | * 11.3.3 Locking and Unlocking a Mutex, P1003.1c/Draft 10, p. 93 |
---|
32 | * |
---|
33 | * NOTE: P1003.4b/D8 adds pthread_mutex_timedlock(), p. 29 |
---|
34 | */ |
---|
35 | int pthread_mutex_timedlock( |
---|
36 | pthread_mutex_t *mutex, |
---|
37 | const struct timespec *abstime |
---|
38 | ) |
---|
39 | { |
---|
40 | Watchdog_Interval ticks; |
---|
41 | bool do_wait = true; |
---|
42 | TOD_Absolute_timeout_conversion_results status; |
---|
43 | int lock_status; |
---|
44 | |
---|
45 | /* |
---|
46 | * POSIX requires that blocking calls with timeouts that take |
---|
47 | * an absolute timeout must ignore issues with the absolute |
---|
48 | * time provided if the operation would otherwise succeed. |
---|
49 | * So we check the abstime provided, and hold on to whether it |
---|
50 | * is valid or not. If it isn't correct and in the future, |
---|
51 | * then we do a polling operation and convert the UNSATISFIED |
---|
52 | * status into the appropriate error. |
---|
53 | * |
---|
54 | * If the status is TOD_ABSOLUTE_TIMEOUT_INVALID, |
---|
55 | * TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST, or TOD_ABSOLUTE_TIMEOUT_IS_NOW, |
---|
56 | * then we should not wait. |
---|
57 | */ |
---|
58 | status = _TOD_Absolute_timeout_to_ticks( abstime, &ticks ); |
---|
59 | if ( status != TOD_ABSOLUTE_TIMEOUT_IS_IN_FUTURE ) |
---|
60 | do_wait = false; |
---|
61 | |
---|
62 | lock_status = _POSIX_Mutex_Lock_support( mutex, do_wait, ticks ); |
---|
63 | /* |
---|
64 | * This service only gives us the option to block. We used a polling |
---|
65 | * attempt to lock if the abstime was not in the future. If we did |
---|
66 | * not obtain the mutex, 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 ( status == TOD_ABSOLUTE_TIMEOUT_INVALID ) |
---|
71 | return EINVAL; |
---|
72 | if ( status == TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST || |
---|
73 | status == TOD_ABSOLUTE_TIMEOUT_IS_NOW ) |
---|
74 | return ETIMEDOUT; |
---|
75 | } |
---|
76 | |
---|
77 | return lock_status; |
---|
78 | } |
---|