[1/n] optimize CPU code.

Limit the amount of atomic operations while decrementing ref count from 2 to 1

Change-Id: I0e9e9f07abd1aa62a3967ce4f83ffe2cc288765a
This commit is contained in:
Mrozek, Michal 2018-02-02 13:46:30 +01:00 committed by sys_ocldev
parent f13bc4d462
commit 5167e3da69
2 changed files with 14 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Intel Corporation
* Copyright (c) 2017 - 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@ -54,6 +54,10 @@ class RefCounter {
return (curr == 0);
}
CT decAndReturnCurrent() {
return --val;
}
bool peekIsZero() const {
return (val == 0);
}
@ -144,8 +148,9 @@ class ReferenceTrackedObject {
unique_ptr_if_unused<DerivedClass> decRefInternal() {
auto customDeleter = tryGetCustomDeleter();
bool unused = refInternal.dec();
UNRECOVERABLE_IF(refInternal.peek() < 0);
auto current = refInternal.decAndReturnCurrent();
bool unused = (current == 0);
UNRECOVERABLE_IF(current < 0);
return unique_ptr_if_unused<DerivedClass>(static_cast<DerivedClass *>(this), unused, customDeleter);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Intel Corporation
* Copyright (c) 2017 - 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@ -45,6 +45,11 @@ TEST(RefCounter, referenceCount) {
ASSERT_TRUE(rc.peekIsZero());
}
TEST(RefCounter, givenReferenceTrackedObjectWhenDecAndReturnCurrentIsCalledThenMinusOneIsReturned) {
RefCounter<> rc;
EXPECT_EQ(-1, rc.decAndReturnCurrent());
}
TEST(unique_ptr_if_unused, InitializedWithDefaultConstructorAtQueryReturnsNullptr) {
unique_ptr_if_unused<int> uptr;
ASSERT_EQ(nullptr, uptr.get());