mock_time_env.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "test_util/mock_time_env.h"
  6. #include "test_util/sync_point.h"
  7. namespace ROCKSDB_NAMESPACE {
  8. // TODO: this is a workaround for the different behavior on different platform
  9. // for timedwait timeout. Ideally timedwait API should be moved to env.
  10. // details: PR #7101.
  11. void MockSystemClock::InstallTimedWaitFixCallback() {
  12. #ifndef NDEBUG
  13. SyncPoint::GetInstance()->DisableProcessing();
  14. SyncPoint::GetInstance()->ClearAllCallBacks();
  15. #ifdef OS_MACOSX
  16. // This is an alternate way (vs. SpecialEnv) of dealing with the fact
  17. // that on some platforms, pthread_cond_timedwait does not appear to
  18. // release the lock for other threads to operate if the deadline time
  19. // is already passed. (TimedWait calls are currently a bad abstraction
  20. // because the deadline parameter is usually computed from Env time,
  21. // but is interpreted in real clock time.)
  22. SyncPoint::GetInstance()->SetCallBack(
  23. "InstrumentedCondVar::TimedWaitInternal", [&](void* arg) {
  24. uint64_t time_us = *static_cast<uint64_t*>(arg);
  25. if (time_us < this->RealNowMicros()) {
  26. *static_cast<uint64_t*>(arg) = this->RealNowMicros() + 1000;
  27. }
  28. });
  29. #endif // OS_MACOSX
  30. SyncPoint::GetInstance()->EnableProcessing();
  31. #endif // !NDEBUG
  32. }
  33. } // namespace ROCKSDB_NAMESPACE