1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup POSIXAPI |
---|
7 | * |
---|
8 | * @brief Support Call to function Enables Locking of Mutex Object |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * COPYRIGHT (c) 1989-2014. |
---|
13 | * On-Line Applications Research Corporation (OAR). |
---|
14 | * |
---|
15 | * Redistribution and use in source and binary forms, with or without |
---|
16 | * modification, are permitted provided that the following conditions |
---|
17 | * are met: |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * |
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
34 | * POSSIBILITY OF SUCH DAMAGE. |
---|
35 | */ |
---|
36 | |
---|
37 | #ifdef HAVE_CONFIG_H |
---|
38 | #include "config.h" |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <rtems/posix/muteximpl.h> |
---|
42 | #include <rtems/posix/posixapi.h> |
---|
43 | |
---|
44 | Status_Control _POSIX_Mutex_Seize_slow( |
---|
45 | POSIX_Mutex_Control *the_mutex, |
---|
46 | const Thread_queue_Operations *operations, |
---|
47 | Thread_Control *executing, |
---|
48 | const struct timespec *abstime, |
---|
49 | Thread_queue_Context *queue_context |
---|
50 | ) |
---|
51 | { |
---|
52 | if ( (uintptr_t) abstime != POSIX_MUTEX_ABSTIME_TRY_LOCK ) { |
---|
53 | _Thread_queue_Context_set_thread_state( |
---|
54 | queue_context, |
---|
55 | STATES_WAITING_FOR_MUTEX |
---|
56 | ); |
---|
57 | _Thread_queue_Context_set_deadlock_callout( |
---|
58 | queue_context, |
---|
59 | _Thread_queue_Deadlock_status |
---|
60 | ); |
---|
61 | _Thread_queue_Enqueue( |
---|
62 | &the_mutex->Recursive.Mutex.Queue.Queue, |
---|
63 | operations, |
---|
64 | executing, |
---|
65 | queue_context |
---|
66 | ); |
---|
67 | return _Thread_Wait_get_status( executing ); |
---|
68 | } else { |
---|
69 | _POSIX_Mutex_Release( the_mutex, queue_context ); |
---|
70 | return STATUS_UNAVAILABLE; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | int _POSIX_Mutex_Lock_support( |
---|
75 | pthread_mutex_t *mutex, |
---|
76 | const struct timespec *abstime, |
---|
77 | Thread_queue_Enqueue_callout enqueue_callout |
---|
78 | ) |
---|
79 | { |
---|
80 | POSIX_Mutex_Control *the_mutex; |
---|
81 | unsigned long flags; |
---|
82 | Thread_queue_Context queue_context; |
---|
83 | Thread_Control *executing; |
---|
84 | Status_Control status; |
---|
85 | |
---|
86 | the_mutex = _POSIX_Mutex_Get( mutex ); |
---|
87 | POSIX_MUTEX_VALIDATE_OBJECT( the_mutex, flags ); |
---|
88 | |
---|
89 | executing = _POSIX_Mutex_Acquire( the_mutex, &queue_context ); |
---|
90 | _Thread_queue_Context_set_enqueue_callout( &queue_context, enqueue_callout); |
---|
91 | _Thread_queue_Context_set_timeout_argument( &queue_context, abstime, true ); |
---|
92 | |
---|
93 | switch ( _POSIX_Mutex_Get_protocol( flags ) ) { |
---|
94 | case POSIX_MUTEX_PRIORITY_CEILING: |
---|
95 | status = _POSIX_Mutex_Ceiling_seize( |
---|
96 | the_mutex, |
---|
97 | flags, |
---|
98 | executing, |
---|
99 | abstime, |
---|
100 | &queue_context |
---|
101 | ); |
---|
102 | break; |
---|
103 | case POSIX_MUTEX_NO_PROTOCOL: |
---|
104 | status = _POSIX_Mutex_Seize( |
---|
105 | the_mutex, |
---|
106 | flags, |
---|
107 | POSIX_MUTEX_NO_PROTOCOL_TQ_OPERATIONS, |
---|
108 | executing, |
---|
109 | abstime, |
---|
110 | &queue_context |
---|
111 | ); |
---|
112 | break; |
---|
113 | default: |
---|
114 | _Assert( |
---|
115 | _POSIX_Mutex_Get_protocol( flags ) == POSIX_MUTEX_PRIORITY_INHERIT |
---|
116 | ); |
---|
117 | status = _POSIX_Mutex_Seize( |
---|
118 | the_mutex, |
---|
119 | flags, |
---|
120 | POSIX_MUTEX_PRIORITY_INHERIT_TQ_OPERATIONS, |
---|
121 | executing, |
---|
122 | abstime, |
---|
123 | &queue_context |
---|
124 | ); |
---|
125 | break; |
---|
126 | } |
---|
127 | |
---|
128 | return _POSIX_Get_error( status ); |
---|
129 | } |
---|