xref: /onnv-gate/usr/src/lib/libkmsagent/common/AutoMutex.h (revision 12720:3db6e0082404)
1*12720SWyllys.Ingersoll@Sun.COM /*
2*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER START
3*12720SWyllys.Ingersoll@Sun.COM  *
4*12720SWyllys.Ingersoll@Sun.COM  * The contents of this file are subject to the terms of the
5*12720SWyllys.Ingersoll@Sun.COM  * Common Development and Distribution License (the "License").
6*12720SWyllys.Ingersoll@Sun.COM  * You may not use this file except in compliance with the License.
7*12720SWyllys.Ingersoll@Sun.COM  *
8*12720SWyllys.Ingersoll@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12720SWyllys.Ingersoll@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*12720SWyllys.Ingersoll@Sun.COM  * See the License for the specific language governing permissions
11*12720SWyllys.Ingersoll@Sun.COM  * and limitations under the License.
12*12720SWyllys.Ingersoll@Sun.COM  *
13*12720SWyllys.Ingersoll@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*12720SWyllys.Ingersoll@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12720SWyllys.Ingersoll@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*12720SWyllys.Ingersoll@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*12720SWyllys.Ingersoll@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*12720SWyllys.Ingersoll@Sun.COM  *
19*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER END
20*12720SWyllys.Ingersoll@Sun.COM  */
21*12720SWyllys.Ingersoll@Sun.COM 
22*12720SWyllys.Ingersoll@Sun.COM /*
23*12720SWyllys.Ingersoll@Sun.COM  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24*12720SWyllys.Ingersoll@Sun.COM  */
25*12720SWyllys.Ingersoll@Sun.COM 
26*12720SWyllys.Ingersoll@Sun.COM /*------------------------------------------------------------------------------
27*12720SWyllys.Ingersoll@Sun.COM  * Module           : AutoMutex.h
28*12720SWyllys.Ingersoll@Sun.COM  -----------------------------------------------------------------------------*/
29*12720SWyllys.Ingersoll@Sun.COM 
30*12720SWyllys.Ingersoll@Sun.COM #ifndef AutoMutex_h
31*12720SWyllys.Ingersoll@Sun.COM #define AutoMutex_h
32*12720SWyllys.Ingersoll@Sun.COM 
33*12720SWyllys.Ingersoll@Sun.COM #include "SYSCommon.h"
34*12720SWyllys.Ingersoll@Sun.COM 
35*12720SWyllys.Ingersoll@Sun.COM class CAutoMutex
36*12720SWyllys.Ingersoll@Sun.COM {
37*12720SWyllys.Ingersoll@Sun.COM public:
38*12720SWyllys.Ingersoll@Sun.COM 
39*12720SWyllys.Ingersoll@Sun.COM     /*---------------------------------------------------------------------------
40*12720SWyllys.Ingersoll@Sun.COM      * Constructor:
41*12720SWyllys.Ingersoll@Sun.COM      *  Locks the given mutex handle.
42*12720SWyllys.Ingersoll@Sun.COM      *
43*12720SWyllys.Ingersoll@Sun.COM      * Input
44*12720SWyllys.Ingersoll@Sun.COM      * -----
45*12720SWyllys.Ingersoll@Sun.COM      *    i_hMutex        Mutex handle
46*12720SWyllys.Ingersoll@Sun.COM      *
47*12720SWyllys.Ingersoll@Sun.COM      * Output
48*12720SWyllys.Ingersoll@Sun.COM      * ------
49*12720SWyllys.Ingersoll@Sun.COM      *    (none)
50*12720SWyllys.Ingersoll@Sun.COM      *
51*12720SWyllys.Ingersoll@Sun.COM      * Return value       (none)
52*12720SWyllys.Ingersoll@Sun.COM      *
53*12720SWyllys.Ingersoll@Sun.COM      *--------------------------------------------------------------------------*/
54*12720SWyllys.Ingersoll@Sun.COM 
CAutoMutex(K_MUTEX_HANDLE i_hMutex)55*12720SWyllys.Ingersoll@Sun.COM     CAutoMutex( K_MUTEX_HANDLE i_hMutex )
56*12720SWyllys.Ingersoll@Sun.COM         : m_hMutex( 0 ),
57*12720SWyllys.Ingersoll@Sun.COM           m_bLocked( false )
58*12720SWyllys.Ingersoll@Sun.COM     {
59*12720SWyllys.Ingersoll@Sun.COM         if ( i_hMutex )
60*12720SWyllys.Ingersoll@Sun.COM         {
61*12720SWyllys.Ingersoll@Sun.COM             Lock( i_hMutex );
62*12720SWyllys.Ingersoll@Sun.COM         }
63*12720SWyllys.Ingersoll@Sun.COM     }
64*12720SWyllys.Ingersoll@Sun.COM 
65*12720SWyllys.Ingersoll@Sun.COM 
66*12720SWyllys.Ingersoll@Sun.COM     /*---------------------------------------------------------------------------
67*12720SWyllys.Ingersoll@Sun.COM      * Destructor:
68*12720SWyllys.Ingersoll@Sun.COM      *  Unlocks this mutex.
69*12720SWyllys.Ingersoll@Sun.COM      *
70*12720SWyllys.Ingersoll@Sun.COM      * Input
71*12720SWyllys.Ingersoll@Sun.COM      * -----
72*12720SWyllys.Ingersoll@Sun.COM      *    (none)
73*12720SWyllys.Ingersoll@Sun.COM      *
74*12720SWyllys.Ingersoll@Sun.COM      * Output
75*12720SWyllys.Ingersoll@Sun.COM      * ------
76*12720SWyllys.Ingersoll@Sun.COM      *    (none)
77*12720SWyllys.Ingersoll@Sun.COM      *
78*12720SWyllys.Ingersoll@Sun.COM      * Return value       (none)
79*12720SWyllys.Ingersoll@Sun.COM      *
80*12720SWyllys.Ingersoll@Sun.COM      *--------------------------------------------------------------------------*/
81*12720SWyllys.Ingersoll@Sun.COM 
~CAutoMutex()82*12720SWyllys.Ingersoll@Sun.COM     virtual ~CAutoMutex()
83*12720SWyllys.Ingersoll@Sun.COM     {
84*12720SWyllys.Ingersoll@Sun.COM         if ( m_bLocked )
85*12720SWyllys.Ingersoll@Sun.COM         {
86*12720SWyllys.Ingersoll@Sun.COM             Unlock();
87*12720SWyllys.Ingersoll@Sun.COM         }
88*12720SWyllys.Ingersoll@Sun.COM     }
89*12720SWyllys.Ingersoll@Sun.COM 
90*12720SWyllys.Ingersoll@Sun.COM     /*---------------------------------------------------------------------------
91*12720SWyllys.Ingersoll@Sun.COM      * Function: Lock
92*12720SWyllys.Ingersoll@Sun.COM      *
93*12720SWyllys.Ingersoll@Sun.COM      * Description:
94*12720SWyllys.Ingersoll@Sun.COM      *  Locks this mutex handle.  If i_hMutex is null, the handle passed to the
95*12720SWyllys.Ingersoll@Sun.COM      *  constructor will be used.  Fatals if there is no valid handle.
96*12720SWyllys.Ingersoll@Sun.COM      *
97*12720SWyllys.Ingersoll@Sun.COM      * Input
98*12720SWyllys.Ingersoll@Sun.COM      * -----
99*12720SWyllys.Ingersoll@Sun.COM      *    i_hMutex        Mutex handle to lock
100*12720SWyllys.Ingersoll@Sun.COM      *
101*12720SWyllys.Ingersoll@Sun.COM      * Output
102*12720SWyllys.Ingersoll@Sun.COM      * ------
103*12720SWyllys.Ingersoll@Sun.COM      *    (none)
104*12720SWyllys.Ingersoll@Sun.COM      *
105*12720SWyllys.Ingersoll@Sun.COM      * Return value       (none)
106*12720SWyllys.Ingersoll@Sun.COM      *
107*12720SWyllys.Ingersoll@Sun.COM      *--------------------------------------------------------------------------*/
108*12720SWyllys.Ingersoll@Sun.COM 
109*12720SWyllys.Ingersoll@Sun.COM     void Lock( K_MUTEX_HANDLE i_hMutex = 0 )
110*12720SWyllys.Ingersoll@Sun.COM     {
111*12720SWyllys.Ingersoll@Sun.COM         FATAL_ASSERT( !m_bLocked );
112*12720SWyllys.Ingersoll@Sun.COM 
113*12720SWyllys.Ingersoll@Sun.COM         if ( i_hMutex )
114*12720SWyllys.Ingersoll@Sun.COM         {
115*12720SWyllys.Ingersoll@Sun.COM             m_hMutex = i_hMutex;
116*12720SWyllys.Ingersoll@Sun.COM         }
117*12720SWyllys.Ingersoll@Sun.COM 
118*12720SWyllys.Ingersoll@Sun.COM         FATAL_ASSERT( m_hMutex );
119*12720SWyllys.Ingersoll@Sun.COM         K_LockMutex( m_hMutex );
120*12720SWyllys.Ingersoll@Sun.COM         m_bLocked = true;
121*12720SWyllys.Ingersoll@Sun.COM     }
122*12720SWyllys.Ingersoll@Sun.COM 
123*12720SWyllys.Ingersoll@Sun.COM 
124*12720SWyllys.Ingersoll@Sun.COM     /*---------------------------------------------------------------------------
125*12720SWyllys.Ingersoll@Sun.COM      * Function: Unlock
126*12720SWyllys.Ingersoll@Sun.COM      *
127*12720SWyllys.Ingersoll@Sun.COM      * Description:
128*12720SWyllys.Ingersoll@Sun.COM      *  Unlocks the mutex handle passed to the constructor or to a previous
129*12720SWyllys.Ingersoll@Sun.COM      *  Lock call.  Fatals if the mutex is not locked.
130*12720SWyllys.Ingersoll@Sun.COM      *
131*12720SWyllys.Ingersoll@Sun.COM      * Input
132*12720SWyllys.Ingersoll@Sun.COM      * -----
133*12720SWyllys.Ingersoll@Sun.COM      *    (none)
134*12720SWyllys.Ingersoll@Sun.COM      *
135*12720SWyllys.Ingersoll@Sun.COM      * Output
136*12720SWyllys.Ingersoll@Sun.COM      * ------
137*12720SWyllys.Ingersoll@Sun.COM      *    (none)
138*12720SWyllys.Ingersoll@Sun.COM      *
139*12720SWyllys.Ingersoll@Sun.COM      * Return value       (none)
140*12720SWyllys.Ingersoll@Sun.COM      *
141*12720SWyllys.Ingersoll@Sun.COM      *--------------------------------------------------------------------------*/
142*12720SWyllys.Ingersoll@Sun.COM 
Unlock()143*12720SWyllys.Ingersoll@Sun.COM     void Unlock()
144*12720SWyllys.Ingersoll@Sun.COM     {
145*12720SWyllys.Ingersoll@Sun.COM         FATAL_ASSERT( m_bLocked );
146*12720SWyllys.Ingersoll@Sun.COM         FATAL_ASSERT( m_hMutex );
147*12720SWyllys.Ingersoll@Sun.COM         K_UnlockMutex( m_hMutex );
148*12720SWyllys.Ingersoll@Sun.COM         m_bLocked = false;
149*12720SWyllys.Ingersoll@Sun.COM     }
150*12720SWyllys.Ingersoll@Sun.COM 
151*12720SWyllys.Ingersoll@Sun.COM private:
152*12720SWyllys.Ingersoll@Sun.COM     K_MUTEX_HANDLE m_hMutex;
153*12720SWyllys.Ingersoll@Sun.COM     bool m_bLocked;
154*12720SWyllys.Ingersoll@Sun.COM };
155*12720SWyllys.Ingersoll@Sun.COM 
156*12720SWyllys.Ingersoll@Sun.COM 
157*12720SWyllys.Ingersoll@Sun.COM #endif // AutoMutex_h
158