1 // Copyright 2005, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
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.
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.
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)
44 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
49 class CriticalSection
: protected CCriticalSection
{
52 void Enter() { CCriticalSection::Enter(); }
53 void Leave() { CCriticalSection::Leave(); }
54 friend class CritSecLock
;
57 class CritSecLock
: protected CCritSecLock
{
59 CritSecLock(CriticalSection
&cs
, bool initially_locked
= true)
60 : CCritSecLock(cs
, initially_locked
) {}
61 void Lock() { CCritSecLock::Lock(); }
62 void Unlock() { CCritSecLock::Unlock(); }
67 //------------------------------------------------------------------------------
69 //------------------------------------------------------------------------------
71 #include <gecko_sdk/include/prmon.h>
73 class CriticalSection
{
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_
; }
82 friend class CritSecLock
;
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; }
99 //------------------------------------------------------------------------------
101 //------------------------------------------------------------------------------
104 class 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_
); }
117 pthread_mutex_t monitor_
;
118 friend class CritSecLock
;
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; }
129 CriticalSection
&cs_
;
134 // TODO(mpcomplete): remove
139 #endif // GEARS_LOCALSERVER_COMMON_CRITICAL_SECTION_H__