BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / cortex / support / ILockable.h
blob6965798e7bb31a34f87d1a71d56af7985a6435b4
1 /*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
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.
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.
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.
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.
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.)
47 // If using a simple BLocker to implement, ignore the mode.
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)
55 #ifndef __ILockable_H__
56 #define __ILockable_H__
58 #include <Debug.h>
60 #include "cortex_defs.h"
61 __BEGIN_CORTEX_NAMESPACE
63 // the locking interface
65 class ILockable {
66 public:
67 ILockable() { }
68 virtual ~ILockable() { }
69 public: // constants
70 enum lock_t {
71 WRITE,
72 READ
75 public: // interface
76 virtual bool lock(
77 lock_t type=WRITE,
78 bigtime_t timeout=B_INFINITE_TIMEOUT)=0;
80 virtual bool unlock(
81 lock_t type=WRITE)=0;
83 virtual bool isLocked(
84 lock_t type=WRITE) const=0;
89 // a simple hands-free lock helper
91 class Autolock {
92 public: // *** ctor/dtor
93 virtual ~Autolock() {
94 ASSERT(m_target);
95 if(m_locked)
96 m_target->unlock();
99 Autolock(ILockable& target) : m_target(&target) { init(); }
100 Autolock(ILockable* target) : m_target( target) { init(); }
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(); }
107 private:
108 void init() {
109 ASSERT(m_target);
110 m_locked = m_target->lock();
113 ILockable* m_target;
114 bool m_locked;
117 __END_CORTEX_NAMESPACE
118 #endif /*__ILockable_H__*/