~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/base/null_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 2013 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: jmarantz@google.com (Joshua Marantz)
18
 
//
19
 
// Zero-dependency mock thread-system for use in tests that don't
20
 
// actually use threads, to help test classes that need some mutexing
21
 
// or other thread-safety hooks.
22
 
//
23
 
// Note that this thread-system does not currently make threads (even
24
 
// co-routines), but check-fails if you attempt to spawn a new thread.
25
 
 
26
 
#ifndef PAGESPEED_KERNEL_BASE_NULL_THREAD_SYSTEM_H_
27
 
#define PAGESPEED_KERNEL_BASE_NULL_THREAD_SYSTEM_H_
28
 
 
29
 
#include "pagespeed/kernel/base/basictypes.h"
30
 
#include "pagespeed/kernel/base/condvar.h"
31
 
#include "pagespeed/kernel/base/thread_system.h"
32
 
#include "pagespeed/kernel/base/string.h"
33
 
#include "pagespeed/kernel/base/string_util.h"
34
 
 
35
 
namespace net_instaweb {
36
 
 
37
 
class Timer;
38
 
 
39
 
class NullCondvar : public ThreadSystem::Condvar {
40
 
 public:
41
 
  explicit NullCondvar(ThreadSystem::CondvarCapableMutex* m)
42
 
      : mutex_(m), timed_wait_callback_(NULL) {}
43
 
  virtual ~NullCondvar();
44
 
  virtual ThreadSystem::CondvarCapableMutex* mutex() const { return mutex_; }
45
 
  virtual void Signal() { actions_.push_back("Signal()"); }
46
 
  virtual void Broadcast() { actions_.push_back("Broadcast()"); }
47
 
  virtual void Wait() { actions_.push_back("Wait()"); }
48
 
  virtual void TimedWait(int64 timeout_ms);
49
 
  GoogleString ActionsSinceLastCall();
50
 
 
51
 
  class TimedWaitCallback {
52
 
   public:
53
 
    virtual ~TimedWaitCallback() {}
54
 
    virtual void Call() = 0;
55
 
  };
56
 
 
57
 
  // Calls callback once the next time TimedWait() is called.  If TimedWait() is
58
 
  // not called we will CHECK-fail.  Doesn't take ownership of the callback.
59
 
  void set_timed_wait_callback(TimedWaitCallback* x) {
60
 
    CHECK(timed_wait_callback_ == NULL);
61
 
    timed_wait_callback_ = x;
62
 
  }
63
 
 
64
 
 private:
65
 
  ThreadSystem::CondvarCapableMutex* mutex_;
66
 
  StringVector actions_;
67
 
  TimedWaitCallback* timed_wait_callback_;
68
 
 
69
 
  DISALLOW_COPY_AND_ASSIGN(NullCondvar);
70
 
};
71
 
 
72
 
// Mock condvar-capable mutex.  Note that this does no actual locking,
73
 
// and any condvars it creates are mocks.
74
 
class NullCondvarCapableMutex : public ThreadSystem::CondvarCapableMutex {
75
 
 public:
76
 
  NullCondvarCapableMutex() {}
77
 
  virtual ~NullCondvarCapableMutex();
78
 
  virtual bool TryLock() { return true; }
79
 
  virtual void Lock() {}
80
 
  virtual void Unlock() {}
81
 
  virtual NullCondvar* NewCondvar();
82
 
 
83
 
 private:
84
 
  DISALLOW_COPY_AND_ASSIGN(NullCondvarCapableMutex);
85
 
};
86
 
 
87
 
// Mock thread system.  This can create mutexes that do no locking, condvars
88
 
// that do no waiting, and can't create threads.  Trying to create a thread will
89
 
// result in a fatal error.
90
 
class NullThreadSystem : public ThreadSystem {
91
 
 public:
92
 
  NullThreadSystem() : thread_id_(1) {}
93
 
  virtual ~NullThreadSystem();
94
 
  virtual NullCondvarCapableMutex* NewMutex();
95
 
  virtual RWLock* NewRWLock();
96
 
  virtual Timer* NewTimer();
97
 
  virtual ThreadId* GetThreadId() const;
98
 
 
99
 
  // Provide injection/observation of current thread IDs.
100
 
  void set_current_thread(int id) { thread_id_ = id; }
101
 
  int current_thread() const { return thread_id_; }
102
 
 
103
 
 private:
104
 
  virtual ThreadImpl* NewThreadImpl(Thread* wrapper, ThreadFlags flags);
105
 
 
106
 
 private:
107
 
  int thread_id_;
108
 
 
109
 
  DISALLOW_COPY_AND_ASSIGN(NullThreadSystem);
110
 
};
111
 
 
112
 
}  // namespace net_instaweb
113
 
 
114
 
#endif  // PAGESPEED_KERNEL_BASE_NULL_THREAD_SYSTEM_H_