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