~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/base/mock_timer.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 2010 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
 
#ifndef PAGESPEED_KERNEL_BASE_MOCK_TIMER_H_
20
 
#define PAGESPEED_KERNEL_BASE_MOCK_TIMER_H_
21
 
 
22
 
#include <vector>                       // for vector
23
 
 
24
 
#include "pagespeed/kernel/base/basictypes.h"
25
 
#include "pagespeed/kernel/base/scoped_ptr.h"
26
 
#include "pagespeed/kernel/base/timer.h"
27
 
 
28
 
namespace net_instaweb {
29
 
 
30
 
class AbstractMutex;
31
 
class Function;
32
 
 
33
 
class MockTimer : public Timer {
34
 
 public:
35
 
  typedef void (*Callback)(void* user_data);
36
 
 
37
 
  // A useful recent time-constant for testing.
38
 
  static const int64 kApr_5_2010_ms;
39
 
 
40
 
  // Takes ownership of mutex.
41
 
  MockTimer(AbstractMutex* mutex, int64 time_ms);
42
 
  virtual ~MockTimer();
43
 
 
44
 
  // Sets the time as in microseconds, calling any outstanding alarms
45
 
  // with wakeup times up to and including time_us.
46
 
  void SetTimeUs(int64 new_time_us);
47
 
  void SetTimeMs(int64 new_time_ms) { SetTimeUs(1000 * new_time_ms); }
48
 
 
49
 
  // Advance forward time by the specified number of microseconds.
50
 
  void AdvanceUs(int64 delta_us) { SetTimeUs(time_us_ + delta_us); }
51
 
 
52
 
  // Advance time, in milliseconds.
53
 
  void AdvanceMs(int64 delta_ms) { AdvanceUs(1000 * delta_ms); }
54
 
 
55
 
  // Set time advances in microseconds for the next calls to NowUs/NowMs.
56
 
  void SetTimeDeltaUs(int64 delta_us) {
57
 
    SetTimeDeltaUsWithCallback(delta_us, NULL);
58
 
  }
59
 
 
60
 
  // Set time advances in microseconds for the next calls to
61
 
  // NowUs/NowMs, with the corresponding callback to execute right
62
 
  // before that time is returned.
63
 
  void SetTimeDeltaUsWithCallback(int64 delta_us,
64
 
                                  Function* callback);
65
 
 
66
 
  // Set time advances in milliseconds for the next calls to NowUs/NowMs.
67
 
  void SetTimeDeltaMs(int64 delta_ms) { SetTimeDeltaUs(1000 * delta_ms); }
68
 
 
69
 
  // Returns number of microseconds since 1970.
70
 
  virtual int64 NowUs() const;
71
 
  virtual void SleepUs(int64 us) { AdvanceUs(us); }
72
 
  virtual void SleepMs(int64 ms) { AdvanceUs(1000 * ms); }
73
 
 
74
 
 private:
75
 
  typedef struct {
76
 
    int64 time;
77
 
    Function* callback;
78
 
  } TimeAndCallback;
79
 
  mutable int64 time_us_;
80
 
  scoped_ptr<AbstractMutex> mutex_;
81
 
  std::vector<TimeAndCallback> deltas_us_;
82
 
  mutable unsigned int next_delta_;
83
 
 
84
 
  DISALLOW_COPY_AND_ASSIGN(MockTimer);
85
 
};
86
 
 
87
 
}  // namespace net_instaweb
88
 
 
89
 
#endif  // PAGESPEED_KERNEL_BASE_MOCK_TIMER_H_