[Author: zork]
[google-gears.git] / gears / localserver / common / critical_section.h
blob9b8ca9fe995b8adcf2af659779c490bd9765b620
1 // Copyright 2005, Google Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // *** DEPRECATED ***
28 // Use /gears/base/common/mutex.h, which implements the Google interface.
30 // This file remains only for WebCache AsyncTask. The synchronization semantics
31 // of that class were unclear, making conversion to Mutex too difficult for now.
33 // *** DEPRECATED ***
35 #ifndef GEARS_LOCALSERVER_COMMON_CRITICAL_SECTION_H__
36 #define GEARS_LOCALSERVER_COMMON_CRITICAL_SECTION_H__
38 // TODO(mpcomplete): implement these.
39 #if BROWSER_NPAPI && defined(WIN32)
40 #define BROWSER_IE 1
41 #endif
43 #if BROWSER_IE
44 //------------------------------------------------------------------------------
45 // BROWSER_IE
46 //------------------------------------------------------------------------------
47 #include <atlsync.h>
49 class CriticalSection : protected CCriticalSection {
50 public:
51 CriticalSection() {}
52 void Enter() { CCriticalSection::Enter(); }
53 void Leave() { CCriticalSection::Leave(); }
54 friend class CritSecLock;
57 class CritSecLock : protected CCritSecLock {
58 public:
59 CritSecLock(CriticalSection &cs, bool initially_locked = true)
60 : CCritSecLock(cs, initially_locked) {}
61 void Lock() { CCritSecLock::Lock(); }
62 void Unlock() { CCritSecLock::Unlock(); }
66 #elif BROWSER_FF
67 //------------------------------------------------------------------------------
68 // BROWSER_FF
69 //------------------------------------------------------------------------------
70 #include <assert.h>
71 #include <gecko_sdk/include/prmon.h>
73 class CriticalSection {
74 public:
75 CriticalSection() { monitor_ = PR_NewMonitor(); }
76 ~CriticalSection() { if (monitor_) PR_DestroyMonitor(monitor_); }
77 void Enter() { PR_EnterMonitor(monitor_); }
78 void Leave() { PR_ExitMonitor(monitor_); }
79 operator PRMonitor*() { return monitor_; }
80 private:
81 PRMonitor *monitor_;
82 friend class CritSecLock;
85 class CritSecLock {
86 public:
87 CritSecLock(PRMonitor *monitor, bool initially_locked = true)
88 : monitor_(monitor), locked_(false) { if (initially_locked) Lock(); }
89 ~CritSecLock() { if (locked_) Unlock(); }
90 void Lock() { assert(!locked_); PR_EnterMonitor(monitor_); locked_ = true; }
91 void Unlock() { assert(locked_); PR_ExitMonitor(monitor_); locked_ = false; }
92 private:
93 PRMonitor *monitor_;
94 bool locked_;
98 #elif BROWSER_SAFARI
99 //------------------------------------------------------------------------------
100 // BROWSER_SAFARI
101 //------------------------------------------------------------------------------
102 #include <pthread.h>
104 class CriticalSection {
105 public:
106 CriticalSection() {
107 pthread_mutexattr_t attr;
108 pthread_mutexattr_init(&attr);
109 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
110 pthread_mutex_init(&monitor_, &attr);
111 pthread_mutexattr_destroy(&attr);
113 ~CriticalSection() { pthread_mutex_destroy(&monitor_); }
114 void Enter() { pthread_mutex_lock(&monitor_); }
115 void Leave() { pthread_mutex_unlock(&monitor_); }
116 private:
117 pthread_mutex_t monitor_;
118 friend class CritSecLock;
121 class CritSecLock {
122 public:
123 CritSecLock(CriticalSection &cs, bool initially_locked = true)
124 : cs_(cs), locked_(false) { if (initially_locked) Lock(); }
125 ~CritSecLock() { if (locked_) Unlock(); }
126 void Lock() { assert(!locked_); cs_.Enter(); locked_ = true; }
127 void Unlock() { assert(locked_); cs_.Leave(); locked_ = false; }
128 private:
129 CriticalSection &cs_;
130 bool locked_;
132 #endif
134 // TODO(mpcomplete): remove
135 #if BROWSER_NPAPI
136 #undef BROWSER_IE
137 #endif
139 #endif // GEARS_LOCALSERVER_COMMON_CRITICAL_SECTION_H__