1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Attempt to Obtain a Read Lock on a RWLock Instance |
---|
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 <pthread.h> |
---|
22 | #include <errno.h> |
---|
23 | |
---|
24 | #include <rtems/posix/rwlockimpl.h> |
---|
25 | #include <rtems/score/thread.h> |
---|
26 | #include <rtems/score/todimpl.h> |
---|
27 | |
---|
28 | /* |
---|
29 | * pthread_rwlock_timedrdlock |
---|
30 | * |
---|
31 | * This directive attempts to obtain a read only lock on an rwlock instance. |
---|
32 | * |
---|
33 | * Input parameters: |
---|
34 | * rwlock - pointer to rwlock id |
---|
35 | * |
---|
36 | * Output parameters: |
---|
37 | * 0 - if successful |
---|
38 | * error code - if unsuccessful |
---|
39 | */ |
---|
40 | |
---|
41 | int pthread_rwlock_timedrdlock( |
---|
42 | pthread_rwlock_t *rwlock, |
---|
43 | const struct timespec *abstime |
---|
44 | ) |
---|
45 | { |
---|
46 | POSIX_RWLock_Control *the_rwlock; |
---|
47 | Objects_Locations location; |
---|
48 | Watchdog_Interval ticks; |
---|
49 | bool do_wait = true; |
---|
50 | TOD_Absolute_timeout_conversion_results status; |
---|
51 | Thread_Control *executing; |
---|
52 | |
---|
53 | /* |
---|
54 | * POSIX requires that blocking calls with timeouts that take |
---|
55 | * an absolute timeout must ignore issues with the absolute |
---|
56 | * time provided if the operation would otherwise succeed. |
---|
57 | * So we check the abstime provided, and hold on to whether it |
---|
58 | * is valid or not. If it isn't correct and in the future, |
---|
59 | * then we do a polling operation and convert the UNSATISFIED |
---|
60 | * status into the appropriate error. |
---|
61 | * |
---|
62 | * If the status is TOD_ABSOLUTE_TIMEOUT_INVALID, |
---|
63 | * TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST, or TOD_ABSOLUTE_TIMEOUT_IS_NOW, |
---|
64 | * then we should not wait. |
---|
65 | */ |
---|
66 | status = _TOD_Absolute_timeout_to_ticks( abstime, &ticks ); |
---|
67 | if ( status != TOD_ABSOLUTE_TIMEOUT_IS_IN_FUTURE ) |
---|
68 | do_wait = false; |
---|
69 | |
---|
70 | the_rwlock = _POSIX_RWLock_Get( rwlock, &location ); |
---|
71 | switch ( location ) { |
---|
72 | |
---|
73 | case OBJECTS_LOCAL: |
---|
74 | |
---|
75 | executing = _Thread_Executing; |
---|
76 | _CORE_RWLock_Obtain_for_reading( |
---|
77 | &the_rwlock->RWLock, |
---|
78 | executing, |
---|
79 | do_wait, |
---|
80 | ticks |
---|
81 | ); |
---|
82 | |
---|
83 | _Objects_Put( &the_rwlock->Object ); |
---|
84 | if ( !do_wait ) { |
---|
85 | if ( executing->Wait.return_code == CORE_RWLOCK_UNAVAILABLE ) { |
---|
86 | if ( status == TOD_ABSOLUTE_TIMEOUT_INVALID ) |
---|
87 | return EINVAL; |
---|
88 | if ( status == TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST || |
---|
89 | status == TOD_ABSOLUTE_TIMEOUT_IS_NOW ) |
---|
90 | return ETIMEDOUT; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | return _POSIX_RWLock_Translate_core_RWLock_return_code( |
---|
95 | (CORE_RWLock_Status) executing->Wait.return_code |
---|
96 | ); |
---|
97 | |
---|
98 | #if defined(RTEMS_MULTIPROCESSING) |
---|
99 | case OBJECTS_REMOTE: |
---|
100 | #endif |
---|
101 | case OBJECTS_ERROR: |
---|
102 | break; |
---|
103 | } |
---|
104 | |
---|
105 | return EINVAL; |
---|
106 | } |
---|