forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Reland: implement a function to convert a storage to copy-on-…
…write (pytorch#110022)" This reverts commit dddf07e. Reverted pytorch#110022 on behalf of https://github.com/atalman due to New tests are failing in internal CI ([comment](pytorch#110022 (comment)))
- Loading branch information
1 parent
7dbdf3b
commit 1265400
Showing
7 changed files
with
105 additions
and
283 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,76 @@ | ||
#include <c10/core/impl/cow/context.h> | ||
|
||
#include <c10/core/impl/cow/deleter.h> | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
namespace c10::impl { | ||
namespace { | ||
|
||
class DeleteTracker { | ||
public: | ||
explicit DeleteTracker(int& delete_count) : delete_count_(delete_count) {} | ||
~DeleteTracker() { | ||
++delete_count_; | ||
} | ||
|
||
private: | ||
int& delete_count_; | ||
}; | ||
|
||
class ContextTest : public testing::Test { | ||
protected: | ||
auto delete_count() const -> int { | ||
return delete_count_; | ||
} | ||
auto new_delete_tracker() -> std::unique_ptr<void, DeleterFnPtr> { | ||
return {new DeleteTracker(delete_count_), +[](void* ptr) { | ||
delete static_cast<DeleteTracker*>(ptr); | ||
}}; | ||
} | ||
|
||
private: | ||
int delete_count_ = 0; | ||
}; | ||
|
||
TEST_F(ContextTest, Basic) { | ||
auto& context = *new cow::Context(new_delete_tracker()); | ||
ASSERT_THAT(delete_count(), testing::Eq(0)); | ||
|
||
context.increment_refcount(); | ||
|
||
{ | ||
// This is in a sub-scope because this call to decrement_refcount | ||
// is expected to give us a shared lock. | ||
auto result = context.decrement_refcount(); | ||
ASSERT_THAT( | ||
std::holds_alternative<cow::Context::NotLastReference>(result), | ||
testing::IsTrue()); | ||
ASSERT_THAT(delete_count(), testing::Eq(0)); | ||
} | ||
|
||
{ | ||
auto result = context.decrement_refcount(); | ||
ASSERT_THAT( | ||
std::holds_alternative<cow::Context::LastReference>(result), | ||
testing::IsTrue()); | ||
// Result holds the DeleteTracker. | ||
ASSERT_THAT(delete_count(), testing::Eq(0)); | ||
} | ||
|
||
// When result is deleted, the DeleteTracker is also deleted. | ||
ASSERT_THAT(delete_count(), testing::Eq(1)); | ||
} | ||
|
||
TEST_F(ContextTest, delete_context) { | ||
// This is effectively the same thing as decrement_refcount() above. | ||
auto& context = *new cow::Context(new_delete_tracker()); | ||
ASSERT_THAT(delete_count(), testing::Eq(0)); | ||
|
||
cow::delete_context(&context); | ||
ASSERT_THAT(delete_count(), testing::Eq(1)); | ||
} | ||
|
||
} // namespace | ||
} // namespace c10::impl |
Oops, something went wrong.