xref: /titanic_41/usr/src/cmd/lms/SyncLib/Include/Lock.h (revision 2b637af7da499c08e98bb81902f8992f624e8d86)
1 /*******************************************************************************
2  * Copyright (C) 2004-2008 Intel Corp. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *  - Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *
10  *  - Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  *  - Neither the name of Intel Corp. nor the names of its
15  *    contributors may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *******************************************************************************/
30 
31 //////////////////////////////////////////////////////////////////////////
32 // Lock.h
33 //
34 // This file contains the definition and implementation of the Lock class
35 // and the TryLock class
36 //////////////////////////////////////////////////////////////////////////
37 #ifndef _LAD_LOCK_H
38 #define _LAD_LOCK_H
39 #include "RWLock.h"
40 
41 #ifndef NULL
42 #define NULL 0
43 #endif
44 
45 class Lock
46 {
47 public:
48 	Lock(Semaphore &sem) : _sem(&sem), _rw_lock(NULL)
49 	{
50 		_sem->acquire();
51 	}
52 
53 	Lock(RWLock &rw_lock, RWLock::RWMode mode = RWLock::READ_ONLY) :
54 	_sem(NULL), _rw_lock(&rw_lock)
55 	{
56 		_rw_lock->acquire(mode);
57 	}
58 
59 	~Lock()
60 	{
61 		if (_sem) {
62 			_sem->release();
63 		}
64 		if (_rw_lock) {
65 			_rw_lock->release();
66 		}
67 
68 	}
69 
70 private:
71 	Semaphore *_sem;
72 	RWLock *_rw_lock;
73 };
74 
75 class TryLock
76 {
77 public:
78 	TryLock(Semaphore &sem, bool &is_locked) : _sem(&sem)
79 	{
80 		_locked = _sem->acquireTry();
81 		is_locked = _locked;
82 	}
83 
84 	~TryLock()
85 	{
86 		if (_locked) {
87 			_sem->release();
88 		}
89 	}
90 
91 private:
92 	bool _locked;
93 	Semaphore *_sem;
94 };
95 
96 #endif //_LAD_LOCK_H
97 
98