mirror of https://github.com/grpc/grpc.git
parent
de93112a3f
commit
70db663ae8
20 changed files with 427 additions and 0 deletions
@ -0,0 +1,48 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H |
||||
#define GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H |
||||
|
||||
namespace grpc_core { |
||||
|
||||
#ifndef NDEBUG |
||||
class DebugLocation { |
||||
public: |
||||
DebugLocation(const char* file, int line) : file_(file), line_(line) {} |
||||
bool Log() const { return true; } |
||||
const char* file() const { return file_; } |
||||
int line() const { return line_; } |
||||
private: |
||||
const char* file_; |
||||
const int line_; |
||||
}; |
||||
#define DEBUG_LOCATION DebugLocation(__FILE__, __LINE__) |
||||
#else |
||||
class DebugLocation { |
||||
public: |
||||
bool Log() const { return false; } |
||||
const char* file() const { return nullptr; } |
||||
int line() const { return -1; } |
||||
}; |
||||
#define DEBUG_LOCATION DebugLocation() |
||||
#endif |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H
|
@ -0,0 +1,58 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/lib/support/reference_counted.h" |
||||
|
||||
#include <grpc/support/log.h> |
||||
|
||||
namespace grpc_core { |
||||
|
||||
void ReferenceCounted::Ref(const DebugLocation& location, const char* reason) { |
||||
if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { |
||||
gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); |
||||
gpr_log(GPR_DEBUG, "%s:%p %s:%d ref %" PRIdPTR " -> %" PRIdPTR " %s", |
||||
trace_flag_->name(), this, location.file(), location.line(), |
||||
old_refs, old_refs + 1, reason); |
||||
} |
||||
Ref(); |
||||
} |
||||
|
||||
void ReferenceCounted::Ref() { |
||||
gpr_ref(&refs_); |
||||
} |
||||
|
||||
bool ReferenceCounted::Unref(const DebugLocation& location, |
||||
const char* reason) { |
||||
if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { |
||||
gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); |
||||
gpr_log(GPR_DEBUG, "%s:%p %s:%d unref %" PRIdPTR " -> %" PRIdPTR " %s", |
||||
trace_flag_->name(), this, location.file(), location.line(), |
||||
old_refs, old_refs - 1, reason); |
||||
} |
||||
return Unref(); |
||||
} |
||||
|
||||
bool ReferenceCounted::Unref() { |
||||
if (gpr_unref(&refs_)) { |
||||
delete this; |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} // namespace grpc_core
|
@ -0,0 +1,55 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H |
||||
#define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H |
||||
|
||||
#include <grpc/support/sync.h> |
||||
|
||||
#include "src/core/lib/debug/trace.h" |
||||
#include "src/core/lib/support/debug_location.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
class ReferenceCounted { |
||||
public: |
||||
void Ref(); |
||||
void Ref(const DebugLocation& location, const char* reason); |
||||
|
||||
bool Unref(); |
||||
bool Unref(const DebugLocation& location, const char* reason); |
||||
|
||||
// Not copyable nor movable.
|
||||
ReferenceCounted(const ReferenceCounted&) = delete; |
||||
ReferenceCounted& operator=(const ReferenceCounted&) = delete; |
||||
|
||||
protected: |
||||
explicit ReferenceCounted(TraceFlag* trace_flag) : trace_flag_(trace_flag) { |
||||
gpr_ref_init(&refs_, 1); |
||||
} |
||||
|
||||
virtual ~ReferenceCounted() {} |
||||
|
||||
private: |
||||
TraceFlag* trace_flag_; |
||||
gpr_refcount refs_; |
||||
}; |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H
|
@ -0,0 +1,60 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/lib/support/reference_counted.h" |
||||
#include <gtest/gtest.h> |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
namespace grpc_core { |
||||
namespace testing { |
||||
|
||||
class Foo : public ReferenceCounted { |
||||
public: |
||||
Foo() : ReferenceCounted(nullptr) {} |
||||
}; |
||||
|
||||
TEST(ReferenceCounted, StackAllocated) { |
||||
Foo foo; |
||||
} |
||||
|
||||
TEST(ReferenceCounted, StackAllocatedWithExtraRef) { |
||||
Foo foo; |
||||
foo.Ref(); |
||||
foo.Unref(); |
||||
} |
||||
|
||||
TEST(ReferenceCounted, HeapAllocated) { |
||||
Foo* foo = new Foo(); |
||||
foo->Unref(); |
||||
} |
||||
|
||||
TEST(ReferenceCounted, HeapAllocatedWithExtraRef) { |
||||
Foo* foo = new Foo(); |
||||
foo->Ref(); |
||||
foo->Unref(); |
||||
foo->Unref(); |
||||
} |
||||
|
||||
} // namespace testing
|
||||
} // namespace grpc_core
|
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc_test_init(argc, argv); |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
Loading…
Reference in new issue