1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Function applies a Write lock to RWLock referenced by rwlock |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * POSIX RWLock Manager -- Attempt to Obtain a Write Lock on a RWLock Instance |
---|
10 | * |
---|
11 | * COPYRIGHT (c) 1989-2008. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.com/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #if HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <pthread.h> |
---|
24 | #include <errno.h> |
---|
25 | |
---|
26 | #include <rtems/system.h> |
---|
27 | #include <rtems/posix/rwlock.h> |
---|
28 | #include <rtems/posix/time.h> |
---|
29 | |
---|
30 | /* |
---|
31 | * pthread_rwlock_timedwrlock |
---|
32 | * |
---|
33 | * This directive attempts to obtain a write only lock on an rwlock instance. |
---|
34 | * |
---|
35 | * Input parameters: |
---|
36 | * rwlock - pointer to rwlock id |
---|
37 | * |
---|
38 | * Output parameters: |
---|
39 | * 0 - if successful |
---|
40 | * error code - if unsuccessful |
---|
41 | */ |
---|
42 | |
---|
43 | int pthread_rwlock_timedwrlock( |
---|
44 | pthread_rwlock_t *rwlock, |
---|
45 | const struct timespec *abstime |
---|
46 | ) |
---|
47 | { |
---|
48 | POSIX_RWLock_Control *the_rwlock; |
---|
49 | Objects_Locations location; |
---|
50 | Watchdog_Interval ticks; |
---|
51 | bool do_wait = true; |
---|
52 | POSIX_Absolute_timeout_conversion_results_t status; |
---|
53 | |
---|
54 | if ( !rwlock ) |
---|
55 | return EINVAL; |
---|
56 | |
---|
57 | /* |
---|
58 | * POSIX requires that blocking calls with timeouts that take |
---|
59 | * an absolute timeout must ignore issues with the absolute |
---|
60 | * time provided if the operation would otherwise succeed. |
---|
61 | * So we check the abstime provided, and hold on to whether it |
---|
62 | * is valid or not. If it isn't correct and in the future, |
---|
63 | * then we do a polling operation and convert the UNSATISFIED |
---|
64 | * status into the appropriate error. |
---|
65 | * |
---|
66 | * If the status is POSIX_ABSOLUTE_TIMEOUT_INVALID, |
---|
67 | * POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST, or POSIX_ABSOLUTE_TIMEOUT_IS_NOW, |
---|
68 | * then we should not wait. |
---|
69 | */ |
---|
70 | status = _POSIX_Absolute_timeout_to_ticks( abstime, &ticks ); |
---|
71 | if ( status != POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE ) |
---|
72 | do_wait = false; |
---|
73 | |
---|
74 | the_rwlock = _POSIX_RWLock_Get( rwlock, &location ); |
---|
75 | switch ( location ) { |
---|
76 | |
---|
77 | case OBJECTS_LOCAL: |
---|
78 | |
---|
79 | _CORE_RWLock_Obtain_for_writing( |
---|
80 | &the_rwlock->RWLock, |
---|
81 | *rwlock, |
---|
82 | do_wait, |
---|
83 | ticks, |
---|
84 | NULL |
---|
85 | ); |
---|
86 | |
---|
87 | _Thread_Enable_dispatch(); |
---|
88 | if ( !do_wait && |
---|
89 | (_Thread_Executing->Wait.return_code == CORE_RWLOCK_UNAVAILABLE) ) { |
---|
90 | if ( status == POSIX_ABSOLUTE_TIMEOUT_INVALID ) |
---|
91 | return EINVAL; |
---|
92 | if ( status == POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST || |
---|
93 | status == POSIX_ABSOLUTE_TIMEOUT_IS_NOW ) |
---|
94 | return ETIMEDOUT; |
---|
95 | } |
---|
96 | |
---|
97 | return _POSIX_RWLock_Translate_core_RWLock_return_code( |
---|
98 | (CORE_RWLock_Status) _Thread_Executing->Wait.return_code |
---|
99 | ); |
---|
100 | |
---|
101 | #if defined(RTEMS_MULTIPROCESSING) |
---|
102 | case OBJECTS_REMOTE: |
---|
103 | #endif |
---|
104 | case OBJECTS_ERROR: |
---|
105 | break; |
---|
106 | } |
---|
107 | |
---|
108 | return EINVAL; |
---|
109 | } |
---|