2
* Copyright 2013 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: jmarantz@google.com (Joshua Marantz)
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.
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.
26
#ifndef PAGESPEED_KERNEL_BASE_NULL_THREAD_SYSTEM_H_
27
#define PAGESPEED_KERNEL_BASE_NULL_THREAD_SYSTEM_H_
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"
35
namespace net_instaweb {
39
class NullCondvar : public ThreadSystem::Condvar {
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();
51
class TimedWaitCallback {
53
virtual ~TimedWaitCallback() {}
54
virtual void Call() = 0;
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;
65
ThreadSystem::CondvarCapableMutex* mutex_;
66
StringVector actions_;
67
TimedWaitCallback* timed_wait_callback_;
69
DISALLOW_COPY_AND_ASSIGN(NullCondvar);
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 {
76
NullCondvarCapableMutex() {}
77
virtual ~NullCondvarCapableMutex();
78
virtual bool TryLock() { return true; }
79
virtual void Lock() {}
80
virtual void Unlock() {}
81
virtual NullCondvar* NewCondvar();
84
DISALLOW_COPY_AND_ASSIGN(NullCondvarCapableMutex);
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 {
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;
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_; }
104
virtual ThreadImpl* NewThreadImpl(Thread* wrapper, ThreadFlags flags);
109
DISALLOW_COPY_AND_ASSIGN(NullThreadSystem);
112
} // namespace net_instaweb
114
#endif // PAGESPEED_KERNEL_BASE_NULL_THREAD_SYSTEM_H_