Skip to content

Commit 7c5362a

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into inference_lib_size
2 parents bc07423 + b0a1d12 commit 7c5362a

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

paddle/fluid/framework/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,5 @@ cc_library(paddle_framework DEPS ${FLUID_FRAMEWORK_MODULES})
447447
if(WITH_TESTING AND TEST selected_rows_test)
448448
set_tests_properties(selected_rows_test PROPERTIES TIMEOUT 120)
449449
endif()
450+
451+
cc_test(scope_guard_test SRCS scope_guard_test.cc)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <type_traits>
18+
#include <utility>
19+
#include "paddle/fluid/platform/macros.h"
20+
21+
namespace paddle {
22+
namespace framework {
23+
24+
template <typename ReleaseCallback>
25+
class ScopeGuard {
26+
public:
27+
explicit ScopeGuard(const ReleaseCallback &callback) : callback_(callback) {}
28+
29+
~ScopeGuard() { callback_(); }
30+
31+
private:
32+
DISABLE_COPY_AND_ASSIGN(ScopeGuard);
33+
34+
private:
35+
ReleaseCallback callback_;
36+
};
37+
38+
// Two macros are needed here.
39+
// See:
40+
// https://stackoverflow.com/questions/10379691/creating-macro-using-line-for-different-variable-names
41+
#define _PADDLE_CONCAT_TOKEN(x, y) x##y
42+
#define PADDLE_CONCAT_TOKEN(x, y) _PADDLE_CONCAT_TOKEN(x, y)
43+
44+
#define DEFINE_PADDLE_SCOPE_GUARD(...) \
45+
auto PADDLE_CONCAT_TOKEN(__scope_guard_func, __LINE__) = __VA_ARGS__; \
46+
::paddle::framework::ScopeGuard<typename std::remove_reference<decltype( \
47+
PADDLE_CONCAT_TOKEN(__scope_guard_func, __LINE__))>::type> \
48+
PADDLE_CONCAT_TOKEN(__scope_guard, __LINE__)( \
49+
PADDLE_CONCAT_TOKEN(__scope_guard_func, __LINE__))
50+
51+
} // namespace framework
52+
} // namespace paddle
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/framework/scope_guard.h"
16+
#include "gtest/gtest.h"
17+
18+
namespace paddle {
19+
namespace framework {
20+
21+
TEST(scope_guard, scope_guard_test) {
22+
int n = 10;
23+
{
24+
DEFINE_PADDLE_SCOPE_GUARD([&n] { ++n; });
25+
}
26+
EXPECT_EQ(n, 11);
27+
try {
28+
DEFINE_PADDLE_SCOPE_GUARD([&] { --n; });
29+
DEFINE_PADDLE_SCOPE_GUARD([&] { --n; });
30+
throw std::runtime_error("any exception");
31+
} catch (std::runtime_error &) {
32+
EXPECT_EQ(n, 9);
33+
}
34+
}
35+
36+
} // namespace framework
37+
} // namespace paddle

0 commit comments

Comments
 (0)