1 /* 2 * Copyright 2020 Google LLC 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 #include "fcp/base/simulated_clock.h" 18 19 namespace fcp { 20 Now()21absl::Time SimulatedClock::Now() { 22 absl::MutexLock lock(mutex()); 23 return NowLocked(); 24 } 25 NowLocked()26absl::Time SimulatedClock::NowLocked() { 27 mutex()->AssertHeld(); 28 return now_; 29 } 30 Sleep(absl::Duration d)31void SimulatedClock::Sleep(absl::Duration d) { 32 absl::Time current; 33 { 34 absl::MutexLock lock(mutex()); 35 current = now_; 36 } 37 absl::Time deadline = current + d; 38 while (true) { 39 { 40 absl::MutexLock lock(mutex()); 41 current = now_; 42 } 43 if (current >= deadline) { 44 return; 45 } 46 } 47 } 48 SetTime(absl::Time t)49void SimulatedClock::SetTime(absl::Time t) { 50 { 51 absl::MutexLock lock(mutex()); 52 now_ = t; 53 } 54 DispatchWakeups(); 55 } 56 AdvanceTime(absl::Duration d)57void SimulatedClock::AdvanceTime(absl::Duration d) { 58 { 59 absl::MutexLock lock(mutex()); 60 now_ += d; 61 } 62 DispatchWakeups(); 63 } 64 65 } // namespace fcp 66 67