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