1/*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions, and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions, and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32// ILockable.h (Cortex)
33// * PURPOSE
34//   Simple interface by which an object's locking capabilites
35//   are declared/exposed.  Includes support for multiple lock
36//   modes (read/write) which objects can choose to support if
37//   they wish.
38//
39// * IMPLEMENTATION/USAGE
40//   Implementations are expected to follow the rules described
41//   in the MultiLock documentation (Be Developer Newsletter
42//   Vol.2 #36, Sep '98).  In brief: write locks can be nested
43//   (a thread holding the write lock may nest as many read or
44//   or write locks as it likes) but read locks may not (only one
45//   per thread.)
46//
47//   If using a simple BLocker to implement, ignore the mode.
48//
49// * HISTORY
50//   e.moon		26jul99		Moved Autolock into this file to
51//											resolve header-naming conflict.
52//   e.moon		7jul99		Reworked slightly for Cortex.
53//   e.moon		13apr99		Begun (beDub)
54
55#ifndef __ILockable_H__
56#define __ILockable_H__
57
58#include <Debug.h>
59
60#include "cortex_defs.h"
61__BEGIN_CORTEX_NAMESPACE
62
63// the locking interface
64
65class ILockable {
66public:
67	ILockable() { }
68	virtual ~ILockable() { }
69public:						// constants
70	enum lock_t {
71		WRITE,
72		READ
73	};
74
75public:						// interface
76	virtual bool lock(
77		lock_t type=WRITE,
78		bigtime_t timeout=B_INFINITE_TIMEOUT)=0;
79
80	virtual bool unlock(
81		lock_t type=WRITE)=0;
82
83	virtual bool isLocked(
84		lock_t type=WRITE) const=0;
85};
86
87
88
89// a simple hands-free lock helper
90
91class Autolock {
92public:				// *** ctor/dtor
93	virtual ~Autolock() {
94		ASSERT(m_target);
95		if(m_locked)
96			m_target->unlock();
97	}
98
99	Autolock(ILockable& target) : m_target(&target) { init(); }
100	Autolock(ILockable* target) : m_target( target) { init(); }
101
102	Autolock(const ILockable& target) :
103		m_target(const_cast<ILockable*>(&target)) { init(); }
104	Autolock(const ILockable* target) :
105		m_target(const_cast<ILockable*>( target)) { init(); }
106
107private:
108	void init() {
109		ASSERT(m_target);
110		m_locked = m_target->lock();
111	}
112
113	ILockable*			m_target;
114	bool						m_locked;
115};
116
117__END_CORTEX_NAMESPACE
118#endif /*__ILockable_H__*/
119