This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make clock decoupled from rdsn runtime (#398)
- Loading branch information
Showing
10 changed files
with
86 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
|
||
namespace dsn { | ||
namespace utils { | ||
|
||
class clock | ||
{ | ||
public: | ||
clock() = default; | ||
virtual ~clock() = default; | ||
|
||
// Gets current time in nanoseconds. | ||
virtual uint64_t now_ns() const; | ||
|
||
// Gets singleton instance. eager singleton, which is thread safe | ||
static const clock *instance(); | ||
|
||
// Resets the global clock implementation (not thread-safety) | ||
static void mock(clock *mock_clock); | ||
|
||
private: | ||
static std::unique_ptr<clock> _clock; | ||
}; | ||
|
||
} // namespace utils | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#include <dsn/utility/clock.h> | ||
#include <dsn/utility/time_utils.h> | ||
#include <dsn/utility/dlib.h> | ||
#include <dsn/utility/smart_pointers.h> | ||
|
||
DSN_API uint64_t dsn_now_ns() { return dsn::utils::clock::instance()->now_ns(); } | ||
|
||
namespace dsn { | ||
namespace utils { | ||
|
||
std::unique_ptr<clock> clock::_clock = make_unique<clock>(); | ||
|
||
const clock *clock::instance() { return _clock.get(); } | ||
|
||
uint64_t clock::now_ns() const { return get_current_physical_time_ns(); } | ||
|
||
void clock::mock(clock *mock_clock) { _clock.reset(mock_clock); } | ||
|
||
} // namespace utils | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <dsn/utility/clock.h> | ||
#include "scheduler.h" | ||
|
||
namespace dsn { | ||
namespace tools { | ||
|
||
class sim_clock : public utils::clock | ||
{ | ||
public: | ||
sim_clock() = default; | ||
virtual ~sim_clock() = default; | ||
|
||
// Gets simulated time in nanoseconds. | ||
virtual uint64_t now_ns() const { return scheduler::instance().now_ns(); } | ||
}; | ||
|
||
} // namespace tools | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters