sbAutoRWLock.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #ifndef __SB_AUTORWLOCK_H__
28 #define __SB_AUTORWLOCK_H__
29 
30 #include <nscore.h>
31 #include <prlog.h>
32 #include <prrwlock.h>
33 
38 class NS_COM_GLUE sbAutoReadLock {
39 private:
40  PRRWLock* mLock;
41  PRBool mLocked;
42 
43  // Not meant to be implemented. This makes it a compiler error to
44  // construct or assign an nsAutoLock object incorrectly.
45  sbAutoReadLock(void) {}
46  sbAutoReadLock(sbAutoReadLock& /*aLock*/) {}
47  sbAutoReadLock& operator =(sbAutoReadLock& /*aLock*/) {
48  return *this;
49  }
50 
51  // Not meant to be implemented. This makes it a compiler error to
52  // attempt to create an sbAutoReadLock object on the heap.
53  static void* operator new(size_t /*size*/) CPP_THROW_NEW {
54  return nsnull;
55  }
56  static void operator delete(void* /*memory*/) {}
57 
58 public:
59 
68  sbAutoReadLock(PRRWLock* aLock)
69  : mLock(aLock),
70  mLocked(PR_TRUE) {
71  PR_ASSERT(mLock);
72 
73  // This will assert deep in the bowels of NSPR if you attempt
74  // to re-enter the lock.
75  PR_RWLock_Rlock(mLock);
76  }
77 
79  if (mLocked)
80  PR_RWLock_Unlock(mLock);
81  }
82 
88  void lock() {
89  PR_ASSERT(!mLocked);
90  PR_RWLock_Rlock(mLock);
91  mLocked = PR_TRUE;
92  }
93 
94 
100  void unlock() {
101  PR_ASSERT(mLocked);
102  PR_RWLock_Unlock(mLock);
103  mLocked = PR_FALSE;
104  }
105 };
106 
108 {
109 private:
110  PRRWLock *mLock;
111 
112 public:
113  sbAutoReadUnlock(PRRWLock *lock) :
114  mLock(lock)
115  {
116  PR_ASSERT(mLock);
117  PR_RWLock_Unlock(mLock);
118  }
119 
121  PR_RWLock_Rlock(mLock);
122  }
123 };
124 
129 class NS_COM_GLUE sbAutoWriteLock {
130 private:
131  PRRWLock* mLock;
132  PRBool mLocked;
133 
134  // Not meant to be implemented. This makes it a compiler error to
135  // construct or assign an nsAutoLock object incorrectly.
136  sbAutoWriteLock(void) {}
137  sbAutoWriteLock(sbAutoWriteLock& /*aLock*/) {}
138  sbAutoWriteLock& operator =(sbAutoWriteLock& /*aLock*/) {
139  return *this;
140  }
141 
142  // Not meant to be implemented. This makes it a compiler error to
143  // attempt to create an sbAutoWriteLock object on the heap.
144  static void* operator new(size_t /*size*/) CPP_THROW_NEW {
145  return nsnull;
146  }
147  static void operator delete(void* /*memory*/) {}
148 
149 public:
150 
159  sbAutoWriteLock(PRRWLock* aLock)
160  : mLock(aLock),
161  mLocked(PR_TRUE) {
162  PR_ASSERT(mLock);
163 
164  // This will assert deep in the bowels of NSPR if you attempt
165  // to re-enter the lock.
166  PR_RWLock_Wlock(mLock);
167  }
168 
170  if (mLocked)
171  PR_RWLock_Unlock(mLock);
172  }
173 
179  void lock() {
180  PR_ASSERT(!mLocked);
181  PR_RWLock_Wlock(mLock);
182  mLocked = PR_TRUE;
183  }
184 
185 
191  void unlock() {
192  PR_ASSERT(mLocked);
193  PR_RWLock_Unlock(mLock);
194  mLocked = PR_FALSE;
195  }
196 };
197 
199 {
200 private:
201  PRRWLock *mLock;
202 
203 public:
204  sbAutoWriteUnlock(PRRWLock *lock) :
205  mLock(lock)
206  {
207  PR_ASSERT(mLock);
208  PR_RWLock_Unlock(mLock);
209  }
210 
212  PR_RWLock_Wlock(mLock);
213  }
214 };
215 
216 #endif /* __SB_AUTORWLOCK_H__ */
sbAutoReadUnlock(PRRWLock *lock)
Definition: sbAutoRWLock.h:113
sbAutoWriteLock(PRRWLock *aLock)
Definition: sbAutoRWLock.h:159
sbAutoWriteUnlock(PRRWLock *lock)
Definition: sbAutoRWLock.h:204
sbAutoReadLock(PRRWLock *aLock)
Definition: sbAutoRWLock.h:68
~sbAutoWriteLock(void)
Definition: sbAutoRWLock.h:169
~sbAutoReadLock(void)
Definition: sbAutoRWLock.h:78