xref: /freebsd/sys/contrib/dev/acpica/components/utilities/utlock.c (revision 0d84335f991f528c6f038e79dd5cc0a7770532da)
1a159c266SJung-uk Kim /******************************************************************************
2a159c266SJung-uk Kim  *
3a159c266SJung-uk Kim  * Module Name: utlock - Reader/Writer lock interfaces
4a159c266SJung-uk Kim  *
5a159c266SJung-uk Kim  *****************************************************************************/
6a159c266SJung-uk Kim 
7*0d84335fSJung-uk Kim /******************************************************************************
8*0d84335fSJung-uk Kim  *
9*0d84335fSJung-uk Kim  * 1. Copyright Notice
10*0d84335fSJung-uk Kim  *
11*0d84335fSJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12a159c266SJung-uk Kim  * All rights reserved.
13a159c266SJung-uk Kim  *
14*0d84335fSJung-uk Kim  * 2. License
15*0d84335fSJung-uk Kim  *
16*0d84335fSJung-uk Kim  * 2.1. This is your license from Intel Corp. under its intellectual property
17*0d84335fSJung-uk Kim  * rights. You may have additional license terms from the party that provided
18*0d84335fSJung-uk Kim  * you this software, covering your right to use that party's intellectual
19*0d84335fSJung-uk Kim  * property rights.
20*0d84335fSJung-uk Kim  *
21*0d84335fSJung-uk Kim  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22*0d84335fSJung-uk Kim  * copy of the source code appearing in this file ("Covered Code") an
23*0d84335fSJung-uk Kim  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24*0d84335fSJung-uk Kim  * base code distributed originally by Intel ("Original Intel Code") to copy,
25*0d84335fSJung-uk Kim  * make derivatives, distribute, use and display any portion of the Covered
26*0d84335fSJung-uk Kim  * Code in any form, with the right to sublicense such rights; and
27*0d84335fSJung-uk Kim  *
28*0d84335fSJung-uk Kim  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29*0d84335fSJung-uk Kim  * license (with the right to sublicense), under only those claims of Intel
30*0d84335fSJung-uk Kim  * patents that are infringed by the Original Intel Code, to make, use, sell,
31*0d84335fSJung-uk Kim  * offer to sell, and import the Covered Code and derivative works thereof
32*0d84335fSJung-uk Kim  * solely to the minimum extent necessary to exercise the above copyright
33*0d84335fSJung-uk Kim  * license, and in no event shall the patent license extend to any additions
34*0d84335fSJung-uk Kim  * to or modifications of the Original Intel Code. No other license or right
35*0d84335fSJung-uk Kim  * is granted directly or by implication, estoppel or otherwise;
36*0d84335fSJung-uk Kim  *
37*0d84335fSJung-uk Kim  * The above copyright and patent license is granted only if the following
38*0d84335fSJung-uk Kim  * conditions are met:
39*0d84335fSJung-uk Kim  *
40*0d84335fSJung-uk Kim  * 3. Conditions
41*0d84335fSJung-uk Kim  *
42*0d84335fSJung-uk Kim  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43*0d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
44*0d84335fSJung-uk Kim  * Code or modification with rights to further distribute source must include
45*0d84335fSJung-uk Kim  * the above Copyright Notice, the above License, this list of Conditions,
46*0d84335fSJung-uk Kim  * and the following Disclaimer and Export Compliance provision. In addition,
47*0d84335fSJung-uk Kim  * Licensee must cause all Covered Code to which Licensee contributes to
48*0d84335fSJung-uk Kim  * contain a file documenting the changes Licensee made to create that Covered
49*0d84335fSJung-uk Kim  * Code and the date of any change. Licensee must include in that file the
50*0d84335fSJung-uk Kim  * documentation of any changes made by any predecessor Licensee. Licensee
51*0d84335fSJung-uk Kim  * must include a prominent statement that the modification is derived,
52*0d84335fSJung-uk Kim  * directly or indirectly, from Original Intel Code.
53*0d84335fSJung-uk Kim  *
54*0d84335fSJung-uk Kim  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55*0d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
56*0d84335fSJung-uk Kim  * Code or modification without rights to further distribute source must
57*0d84335fSJung-uk Kim  * include the following Disclaimer and Export Compliance provision in the
58*0d84335fSJung-uk Kim  * documentation and/or other materials provided with distribution. In
59*0d84335fSJung-uk Kim  * addition, Licensee may not authorize further sublicense of source of any
60*0d84335fSJung-uk Kim  * portion of the Covered Code, and must include terms to the effect that the
61*0d84335fSJung-uk Kim  * license from Licensee to its licensee is limited to the intellectual
62*0d84335fSJung-uk Kim  * property embodied in the software Licensee provides to its licensee, and
63*0d84335fSJung-uk Kim  * not to intellectual property embodied in modifications its licensee may
64*0d84335fSJung-uk Kim  * make.
65*0d84335fSJung-uk Kim  *
66*0d84335fSJung-uk Kim  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67*0d84335fSJung-uk Kim  * substantial portion of the Covered Code or modification must reproduce the
68*0d84335fSJung-uk Kim  * above Copyright Notice, and the following Disclaimer and Export Compliance
69*0d84335fSJung-uk Kim  * provision in the documentation and/or other materials provided with the
70*0d84335fSJung-uk Kim  * distribution.
71*0d84335fSJung-uk Kim  *
72*0d84335fSJung-uk Kim  * 3.4. Intel retains all right, title, and interest in and to the Original
73*0d84335fSJung-uk Kim  * Intel Code.
74*0d84335fSJung-uk Kim  *
75*0d84335fSJung-uk Kim  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76*0d84335fSJung-uk Kim  * Intel shall be used in advertising or otherwise to promote the sale, use or
77*0d84335fSJung-uk Kim  * other dealings in products derived from or relating to the Covered Code
78*0d84335fSJung-uk Kim  * without prior written authorization from Intel.
79*0d84335fSJung-uk Kim  *
80*0d84335fSJung-uk Kim  * 4. Disclaimer and Export Compliance
81*0d84335fSJung-uk Kim  *
82*0d84335fSJung-uk Kim  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83*0d84335fSJung-uk Kim  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84*0d84335fSJung-uk Kim  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85*0d84335fSJung-uk Kim  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86*0d84335fSJung-uk Kim  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87*0d84335fSJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88*0d84335fSJung-uk Kim  * PARTICULAR PURPOSE.
89*0d84335fSJung-uk Kim  *
90*0d84335fSJung-uk Kim  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91*0d84335fSJung-uk Kim  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92*0d84335fSJung-uk Kim  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93*0d84335fSJung-uk Kim  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94*0d84335fSJung-uk Kim  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95*0d84335fSJung-uk Kim  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96*0d84335fSJung-uk Kim  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97*0d84335fSJung-uk Kim  * LIMITED REMEDY.
98*0d84335fSJung-uk Kim  *
99*0d84335fSJung-uk Kim  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100*0d84335fSJung-uk Kim  * software or system incorporating such software without first obtaining any
101*0d84335fSJung-uk Kim  * required license or other approval from the U. S. Department of Commerce or
102*0d84335fSJung-uk Kim  * any other agency or department of the United States Government. In the
103*0d84335fSJung-uk Kim  * event Licensee exports any such software from the United States or
104*0d84335fSJung-uk Kim  * re-exports any such software from a foreign destination, Licensee shall
105*0d84335fSJung-uk Kim  * ensure that the distribution and export/re-export of the software is in
106*0d84335fSJung-uk Kim  * compliance with all laws, regulations, orders, or other restrictions of the
107*0d84335fSJung-uk Kim  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108*0d84335fSJung-uk Kim  * any of its subsidiaries will export/re-export any technical data, process,
109*0d84335fSJung-uk Kim  * software, or service, directly or indirectly, to any country for which the
110*0d84335fSJung-uk Kim  * United States government or any agency thereof requires an export license,
111*0d84335fSJung-uk Kim  * other governmental approval, or letter of assurance, without first obtaining
112*0d84335fSJung-uk Kim  * such license, approval or letter.
113*0d84335fSJung-uk Kim  *
114*0d84335fSJung-uk Kim  *****************************************************************************
115*0d84335fSJung-uk Kim  *
116*0d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
117*0d84335fSJung-uk Kim  * following license:
118*0d84335fSJung-uk Kim  *
119a159c266SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
120a159c266SJung-uk Kim  * modification, are permitted provided that the following conditions
121a159c266SJung-uk Kim  * are met:
122a159c266SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
123a159c266SJung-uk Kim  *    notice, this list of conditions, and the following disclaimer,
124a159c266SJung-uk Kim  *    without modification.
125a159c266SJung-uk Kim  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126a159c266SJung-uk Kim  *    substantially similar to the "NO WARRANTY" disclaimer below
127a159c266SJung-uk Kim  *    ("Disclaimer") and any redistribution must be conditioned upon
128a159c266SJung-uk Kim  *    including a substantially similar Disclaimer requirement for further
129a159c266SJung-uk Kim  *    binary redistribution.
130a159c266SJung-uk Kim  * 3. Neither the names of the above-listed copyright holders nor the names
131a159c266SJung-uk Kim  *    of any contributors may be used to endorse or promote products derived
132a159c266SJung-uk Kim  *    from this software without specific prior written permission.
133a159c266SJung-uk Kim  *
134*0d84335fSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135*0d84335fSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136*0d84335fSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137*0d84335fSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138*0d84335fSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139*0d84335fSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140*0d84335fSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141*0d84335fSJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142*0d84335fSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143*0d84335fSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144*0d84335fSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145*0d84335fSJung-uk Kim  *
146*0d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
147a159c266SJung-uk Kim  * GNU General Public License ("GPL") version 2 as published by the Free
148a159c266SJung-uk Kim  * Software Foundation.
149a159c266SJung-uk Kim  *
150*0d84335fSJung-uk Kim  *****************************************************************************/
151a159c266SJung-uk Kim 
152a159c266SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
153a159c266SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
154a159c266SJung-uk Kim 
155a159c266SJung-uk Kim 
156a159c266SJung-uk Kim #define _COMPONENT          ACPI_UTILITIES
157a159c266SJung-uk Kim         ACPI_MODULE_NAME    ("utlock")
158a159c266SJung-uk Kim 
159a159c266SJung-uk Kim 
160a159c266SJung-uk Kim /*******************************************************************************
161a159c266SJung-uk Kim  *
162a159c266SJung-uk Kim  * FUNCTION:    AcpiUtCreateRwLock
163a159c266SJung-uk Kim  *              AcpiUtDeleteRwLock
164a159c266SJung-uk Kim  *
165a159c266SJung-uk Kim  * PARAMETERS:  Lock                - Pointer to a valid RW lock
166a159c266SJung-uk Kim  *
167a159c266SJung-uk Kim  * RETURN:      Status
168a159c266SJung-uk Kim  *
169a159c266SJung-uk Kim  * DESCRIPTION: Reader/writer lock creation and deletion interfaces.
170a159c266SJung-uk Kim  *
171a159c266SJung-uk Kim  ******************************************************************************/
172a159c266SJung-uk Kim 
173a159c266SJung-uk Kim ACPI_STATUS
174a159c266SJung-uk Kim AcpiUtCreateRwLock (
175a159c266SJung-uk Kim     ACPI_RW_LOCK            *Lock)
176a159c266SJung-uk Kim {
177a159c266SJung-uk Kim     ACPI_STATUS             Status;
178a159c266SJung-uk Kim 
179a159c266SJung-uk Kim 
180a159c266SJung-uk Kim     Lock->NumReaders = 0;
181a159c266SJung-uk Kim     Status = AcpiOsCreateMutex (&Lock->ReaderMutex);
182a159c266SJung-uk Kim     if (ACPI_FAILURE (Status))
183a159c266SJung-uk Kim     {
184a159c266SJung-uk Kim         return (Status);
185a159c266SJung-uk Kim     }
186a159c266SJung-uk Kim 
187a159c266SJung-uk Kim     Status = AcpiOsCreateMutex (&Lock->WriterMutex);
188a159c266SJung-uk Kim     return (Status);
189a159c266SJung-uk Kim }
190a159c266SJung-uk Kim 
191a159c266SJung-uk Kim 
192a159c266SJung-uk Kim void
193a159c266SJung-uk Kim AcpiUtDeleteRwLock (
194a159c266SJung-uk Kim     ACPI_RW_LOCK            *Lock)
195a159c266SJung-uk Kim {
196a159c266SJung-uk Kim 
197a159c266SJung-uk Kim     AcpiOsDeleteMutex (Lock->ReaderMutex);
198a159c266SJung-uk Kim     AcpiOsDeleteMutex (Lock->WriterMutex);
199a159c266SJung-uk Kim 
200a159c266SJung-uk Kim     Lock->NumReaders = 0;
201a159c266SJung-uk Kim     Lock->ReaderMutex = NULL;
202a159c266SJung-uk Kim     Lock->WriterMutex = NULL;
203a159c266SJung-uk Kim }
204a159c266SJung-uk Kim 
205a159c266SJung-uk Kim 
206a159c266SJung-uk Kim /*******************************************************************************
207a159c266SJung-uk Kim  *
208a159c266SJung-uk Kim  * FUNCTION:    AcpiUtAcquireReadLock
209a159c266SJung-uk Kim  *              AcpiUtReleaseReadLock
210a159c266SJung-uk Kim  *
211a159c266SJung-uk Kim  * PARAMETERS:  Lock                - Pointer to a valid RW lock
212a159c266SJung-uk Kim  *
213a159c266SJung-uk Kim  * RETURN:      Status
214a159c266SJung-uk Kim  *
215a159c266SJung-uk Kim  * DESCRIPTION: Reader interfaces for reader/writer locks. On acquisition,
216a159c266SJung-uk Kim  *              only the first reader acquires the write mutex. On release,
217a159c266SJung-uk Kim  *              only the last reader releases the write mutex. Although this
218a159c266SJung-uk Kim  *              algorithm can in theory starve writers, this should not be a
219a159c266SJung-uk Kim  *              problem with ACPICA since the subsystem is infrequently used
220a159c266SJung-uk Kim  *              in comparison to (for example) an I/O system.
221a159c266SJung-uk Kim  *
222a159c266SJung-uk Kim  ******************************************************************************/
223a159c266SJung-uk Kim 
224a159c266SJung-uk Kim ACPI_STATUS
225a159c266SJung-uk Kim AcpiUtAcquireReadLock (
226a159c266SJung-uk Kim     ACPI_RW_LOCK            *Lock)
227a159c266SJung-uk Kim {
228a159c266SJung-uk Kim     ACPI_STATUS             Status;
229a159c266SJung-uk Kim 
230a159c266SJung-uk Kim 
231a159c266SJung-uk Kim     Status = AcpiOsAcquireMutex (Lock->ReaderMutex, ACPI_WAIT_FOREVER);
232a159c266SJung-uk Kim     if (ACPI_FAILURE (Status))
233a159c266SJung-uk Kim     {
234a159c266SJung-uk Kim         return (Status);
235a159c266SJung-uk Kim     }
236a159c266SJung-uk Kim 
237a159c266SJung-uk Kim     /* Acquire the write lock only for the first reader */
238a159c266SJung-uk Kim 
239a159c266SJung-uk Kim     Lock->NumReaders++;
240a159c266SJung-uk Kim     if (Lock->NumReaders == 1)
241a159c266SJung-uk Kim     {
242a159c266SJung-uk Kim         Status = AcpiOsAcquireMutex (Lock->WriterMutex, ACPI_WAIT_FOREVER);
243a159c266SJung-uk Kim     }
244a159c266SJung-uk Kim 
245a159c266SJung-uk Kim     AcpiOsReleaseMutex (Lock->ReaderMutex);
246a159c266SJung-uk Kim     return (Status);
247a159c266SJung-uk Kim }
248a159c266SJung-uk Kim 
249a159c266SJung-uk Kim 
250a159c266SJung-uk Kim ACPI_STATUS
251a159c266SJung-uk Kim AcpiUtReleaseReadLock (
252a159c266SJung-uk Kim     ACPI_RW_LOCK            *Lock)
253a159c266SJung-uk Kim {
254a159c266SJung-uk Kim     ACPI_STATUS             Status;
255a159c266SJung-uk Kim 
256a159c266SJung-uk Kim 
257a159c266SJung-uk Kim     Status = AcpiOsAcquireMutex (Lock->ReaderMutex, ACPI_WAIT_FOREVER);
258a159c266SJung-uk Kim     if (ACPI_FAILURE (Status))
259a159c266SJung-uk Kim     {
260a159c266SJung-uk Kim         return (Status);
261a159c266SJung-uk Kim     }
262a159c266SJung-uk Kim 
263a159c266SJung-uk Kim     /* Release the write lock only for the very last reader */
264a159c266SJung-uk Kim 
265a159c266SJung-uk Kim     Lock->NumReaders--;
266a159c266SJung-uk Kim     if (Lock->NumReaders == 0)
267a159c266SJung-uk Kim     {
268a159c266SJung-uk Kim         AcpiOsReleaseMutex (Lock->WriterMutex);
269a159c266SJung-uk Kim     }
270a159c266SJung-uk Kim 
271a159c266SJung-uk Kim     AcpiOsReleaseMutex (Lock->ReaderMutex);
272a159c266SJung-uk Kim     return (Status);
273a159c266SJung-uk Kim }
274a159c266SJung-uk Kim 
275a159c266SJung-uk Kim 
276a159c266SJung-uk Kim /*******************************************************************************
277a159c266SJung-uk Kim  *
278a159c266SJung-uk Kim  * FUNCTION:    AcpiUtAcquireWriteLock
279a159c266SJung-uk Kim  *              AcpiUtReleaseWriteLock
280a159c266SJung-uk Kim  *
281a159c266SJung-uk Kim  * PARAMETERS:  Lock                - Pointer to a valid RW lock
282a159c266SJung-uk Kim  *
283a159c266SJung-uk Kim  * RETURN:      Status
284a159c266SJung-uk Kim  *
285a159c266SJung-uk Kim  * DESCRIPTION: Writer interfaces for reader/writer locks. Simply acquire or
286a159c266SJung-uk Kim  *              release the writer mutex associated with the lock. Acquisition
287a159c266SJung-uk Kim  *              of the lock is fully exclusive and will block all readers and
288a159c266SJung-uk Kim  *              writers until it is released.
289a159c266SJung-uk Kim  *
290a159c266SJung-uk Kim  ******************************************************************************/
291a159c266SJung-uk Kim 
292a159c266SJung-uk Kim ACPI_STATUS
293a159c266SJung-uk Kim AcpiUtAcquireWriteLock (
294a159c266SJung-uk Kim     ACPI_RW_LOCK            *Lock)
295a159c266SJung-uk Kim {
296a159c266SJung-uk Kim     ACPI_STATUS             Status;
297a159c266SJung-uk Kim 
298a159c266SJung-uk Kim 
299a159c266SJung-uk Kim     Status = AcpiOsAcquireMutex (Lock->WriterMutex, ACPI_WAIT_FOREVER);
300a159c266SJung-uk Kim     return (Status);
301a159c266SJung-uk Kim }
302a159c266SJung-uk Kim 
303a159c266SJung-uk Kim 
304a159c266SJung-uk Kim void
305a159c266SJung-uk Kim AcpiUtReleaseWriteLock (
306a159c266SJung-uk Kim     ACPI_RW_LOCK            *Lock)
307a159c266SJung-uk Kim {
308a159c266SJung-uk Kim 
309a159c266SJung-uk Kim     AcpiOsReleaseMutex (Lock->WriterMutex);
310a159c266SJung-uk Kim }
311