~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/base/checking_thread_system.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2011 Google Inc.
3
 
 *
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
// Author: jmaessen@google.com (Jan-Willem Maessen)
18
 
 
19
 
#ifndef PAGESPEED_KERNEL_BASE_CHECKING_THREAD_SYSTEM_H_
20
 
#define PAGESPEED_KERNEL_BASE_CHECKING_THREAD_SYSTEM_H_
21
 
 
22
 
#include "pagespeed/kernel/base/thread_system.h"
23
 
 
24
 
#include "pagespeed/kernel/base/atomic_bool.h"
25
 
#include "pagespeed/kernel/base/atomic_int32.h"
26
 
#include "pagespeed/kernel/base/basictypes.h"
27
 
#include "pagespeed/kernel/base/scoped_ptr.h"
28
 
#include "pagespeed/kernel/base/thread_annotations.h"
29
 
 
30
 
namespace net_instaweb {
31
 
 
32
 
class Timer;
33
 
 
34
 
// A thread system whose mutex and condvar factories yield implementations that
35
 
// permit checking of lock invariants using DCheckLocked().  This can be wrapped
36
 
// around an unchecked implementation.  This implementation checks invariants
37
 
// using CHECK (so does checking unconditionally).  To check conditionally, do
38
 
// the wrapping depending upon the setting of NDEBUG.  This is done by the
39
 
// Platform::CreateThreadSystem() factory by default, which is why the
40
 
// invariant checking method is called DCheckLock (Debug check lock) and not
41
 
// CheckLock.
42
 
class CheckingThreadSystem : public ThreadSystem {
43
 
 private:
44
 
  // C++ requires us to forward-declare this private class so that we can
45
 
  // friend it in CheckingThreadSystem::Mutex.
46
 
  // We otherwise try to keep it hidden from view.
47
 
  class CheckingCondvar;
48
 
 public:
49
 
  // We also expose CheckingThreadSystem::Mutex, which wraps a
50
 
  // CondvarCapableMutex to provide checked condvars and lock checking (these
51
 
  // two must be done together, so we must wrap the mutex from which the condvar
52
 
  // is created and use the wrapped mutex to create the condvar).  This class
53
 
  // can be used to wrap unchecked mutexes provided by other
54
 
  // CheckingThreadSystems.
55
 
  class LOCKABLE Mutex : public ThreadSystem::CondvarCapableMutex {
56
 
   public:
57
 
    explicit Mutex(ThreadSystem::CondvarCapableMutex* mutex) : mutex_(mutex) { }
58
 
    virtual ~Mutex();
59
 
 
60
 
    virtual bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true);
61
 
    virtual void Lock() EXCLUSIVE_LOCK_FUNCTION();
62
 
    virtual void Unlock() UNLOCK_FUNCTION();
63
 
    // This implementation of DCheckLocked CHECK-fails if lock is not held.
64
 
    virtual void DCheckLocked();
65
 
 
66
 
    // This implementation of DCheckUnlocked CHECK-fails if lock is held.
67
 
    virtual void DCheckUnlocked();
68
 
 
69
 
    // The condvars provided perform lock checking for ....Wait operations.
70
 
    virtual ThreadSystem::Condvar* NewCondvar();
71
 
 
72
 
   private:
73
 
    friend class CheckingCondvar;
74
 
    void TakeLockControl();
75
 
    void DropLockControl();
76
 
 
77
 
    scoped_ptr<ThreadSystem::CondvarCapableMutex> mutex_;
78
 
    AtomicBool locked_;
79
 
    DISALLOW_COPY_AND_ASSIGN(Mutex);
80
 
  };
81
 
 
82
 
  // We also expose CheckingThreadSystem::RWLock, which wraps a
83
 
  // RWLock to provide read/write capable locks. This class
84
 
  // can be used to wrap unchecked mutexes provided by other
85
 
  // CheckingThreadSystems.
86
 
  class LOCKABLE RWLock : public ThreadSystem::RWLock {
87
 
   public:
88
 
    explicit RWLock(ThreadSystem::RWLock* lock) : lock_(lock) { }
89
 
    virtual ~RWLock();
90
 
 
91
 
    virtual bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true);
92
 
    virtual void Lock() EXCLUSIVE_LOCK_FUNCTION();
93
 
    virtual void Unlock() UNLOCK_FUNCTION();
94
 
    virtual bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true);
95
 
    virtual void ReaderLock() SHARED_LOCK_FUNCTION();
96
 
    virtual void ReaderUnlock() UNLOCK_FUNCTION();
97
 
 
98
 
    // This implementation of DCheckLocked CHECK-fails if lock is not held.
99
 
    virtual void DCheckLocked();
100
 
    virtual void DCheckReaderLocked();
101
 
 
102
 
   private:
103
 
    void TakeLockControl();
104
 
    void DropLockControl();
105
 
    void TakeReaderLockControl();
106
 
    void DropReaderLockControl();
107
 
 
108
 
    scoped_ptr<ThreadSystem::RWLock> lock_;
109
 
    AtomicInt32 locked_;
110
 
    DISALLOW_COPY_AND_ASSIGN(RWLock);
111
 
  };
112
 
 
113
 
  explicit CheckingThreadSystem(ThreadSystem* thread_system)
114
 
      : thread_system_(thread_system) { }
115
 
  virtual ~CheckingThreadSystem();
116
 
 
117
 
  virtual Mutex* NewMutex();
118
 
  virtual RWLock* NewRWLock();
119
 
  virtual Timer* NewTimer();
120
 
  virtual ThreadId* GetThreadId() const {
121
 
    return thread_system_->GetThreadId();
122
 
  }
123
 
 
124
 
 private:
125
 
  friend class Mutex;
126
 
 
127
 
  virtual ThreadImpl* NewThreadImpl(Thread* wrapper, ThreadFlags flags);
128
 
 
129
 
  scoped_ptr<ThreadSystem> thread_system_;
130
 
  DISALLOW_COPY_AND_ASSIGN(CheckingThreadSystem);
131
 
};
132
 
 
133
 
}  // namespace net_instaweb
134
 
 
135
 
#endif  // PAGESPEED_KERNEL_BASE_CHECKING_THREAD_SYSTEM_H_