Skip to content

Commit

Permalink
framework: add static CleanUp interface for DECLARE_SINGLETON
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongjunni authored and Jiangtao Hu committed Dec 13, 2018
1 parent ef79ed4 commit a124911
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 28 deletions.
2 changes: 2 additions & 0 deletions cyber/base/for_each.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef CYBER_BASE_FOR_EACH_H_
#define CYBER_BASE_FOR_EACH_H_

#include <type_traits>

namespace apollo {
namespace cyber {
namespace base {
Expand Down
17 changes: 17 additions & 0 deletions cyber/base/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define CYBER_BASE_MACROS_H_

#include <cstdlib>
#include <new>

#if __GNUC__ >= 3
#define likely(x) (__builtin_expect((x), 1))
Expand All @@ -29,6 +30,22 @@

#define CACHELINE_SIZE 64

#define DEFINE_TYPE_TRAIT(name, func) \
template <typename T> \
class name { \
private: \
template <typename Class> \
static char Test(decltype(&Class::func)*); \
template <typename> \
static int Test(...); \
\
public: \
static constexpr bool value = sizeof(Test<T>(nullptr)) == 1; \
}; \
\
template <typename T> \
constexpr bool name<T>::value;

inline void cpu_relax() { asm volatile("rep; nop" ::: "memory"); }

inline void* CheckedMalloc(size_t size) {
Expand Down
15 changes: 15 additions & 0 deletions cyber/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,26 @@ cc_library(
],
)

cc_test(
name = "macros_test",
size = "small",
srcs = [
"macros_test.cc",
],
deps = [
"//cyber",
"@gtest//:main",
],
)

cc_library(
name = "macros",
hdrs = [
"macros.h",
],
deps = [
"//cyber/base:macros",
],
)

cc_library(
Expand Down
48 changes: 39 additions & 9 deletions cyber/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@

#include <iostream>
#include <memory>
#include <mutex>
#include <type_traits>
#include <utility>

#include "cyber/base/macros.h"

DEFINE_TYPE_TRAIT(HasShutdown, Shutdown)

template <typename T>
typename std::enable_if<HasShutdown<T>::value>::type CallShutdown(T *instance) {
instance->Shutdown();
}

template <typename T>
typename std::enable_if<!HasShutdown<T>::value>::type CallShutdown(
T *instance) {
(void)instance;
}

// There must be many copy-paste versions of these macros which are same
// things, undefine them to avoid conflict.
Expand All @@ -31,15 +49,27 @@
classname(const classname &) = delete; \
classname &operator=(const classname &) = delete;

#define DECLARE_SINGLETON(classname) \
public: \
static classname *Instance() { \
static auto instance = new (std::nothrow) classname(); \
return instance; \
} \
\
private: \
classname(); \
#define DECLARE_SINGLETON(classname) \
public: \
static classname *Instance(bool create_if_needed = true) { \
static classname *instance = nullptr; \
if (!instance && create_if_needed) { \
static std::once_flag flag; \
std::call_once(flag, \
[&] { instance = new (std::nothrow) classname(); }); \
} \
return instance; \
} \
\
static void CleanUp() { \
auto instance = Instance(false); \
if (instance != nullptr) { \
CallShutdown(instance); \
} \
} \
\
private: \
classname(); \
DISALLOW_COPY_AND_ASSIGN(classname)

#endif // CYBER_COMMON_MACROS_H_
67 changes: 67 additions & 0 deletions cyber/common/macros_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* 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 "cyber/common/macros.h"

#include "gtest/gtest.h"

namespace apollo {
namespace cyber {
namespace common {

class ClassWithShutdown {
public:
void Shutdown() { set_foo(1); }

static int foo() { return foo_; }
static void set_foo(int val) { foo_ = val; }

private:
static int foo_;
DECLARE_SINGLETON(ClassWithShutdown)
};

int ClassWithShutdown::foo_ = 0;
inline ClassWithShutdown::ClassWithShutdown() {}

class ClassWithoutShutdown {
private:
DECLARE_SINGLETON(ClassWithoutShutdown)
};

inline ClassWithoutShutdown::ClassWithoutShutdown() {}

TEST(MacrosTest, has_shut_down_test) {
EXPECT_TRUE(HasShutdown<ClassWithShutdown>::value);
EXPECT_FALSE(HasShutdown<ClassWithoutShutdown>::value);
}

TEST(MacrosTest, shut_down_test) {
EXPECT_EQ(ClassWithShutdown::foo(), 0);
ClassWithShutdown::CleanUp();
EXPECT_EQ(ClassWithShutdown::foo(), 0);
ClassWithShutdown::Instance();
ClassWithShutdown::CleanUp();
EXPECT_EQ(ClassWithShutdown::foo(), 1);

ClassWithoutShutdown::CleanUp();
ClassWithoutShutdown::Instance();
ClassWithoutShutdown::CleanUp();
}

} // namespace common
} // namespace cyber
} // namespace apollo
6 changes: 3 additions & 3 deletions cyber/init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ void Shutdown() {
if (GetState() == STATE_SHUTDOWN || GetState() == STATE_UNINITIALIZED) {
return;
}
TaskManager::Instance()->Shutdown();
TaskManager::CleanUp();
scheduler::Instance()->Shutdown();
service_discovery::TopologyManager::Instance()->Shutdown();
transport::Transport::Instance()->Shutdown();
service_discovery::TopologyManager::CleanUp();
transport::Transport::CleanUp();
StopLogger();
SetState(STATE_SHUTDOWN);
}
Expand Down
1 change: 1 addition & 0 deletions cyber/message/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cc_library(
"protobuf_traits",
"py_message_traits",
"raw_message_traits",
"//cyber/base:macros",
],
)

Expand Down
17 changes: 1 addition & 16 deletions cyber/message/message_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <string>

#include "cyber/base/macros.h"
#include "cyber/message/intra_message.h"
#include "cyber/message/protobuf_traits.h"
#include "cyber/message/py_message_traits.h"
Expand All @@ -28,22 +29,6 @@ namespace apollo {
namespace cyber {
namespace message {

#define DEFINE_TYPE_TRAIT(name, func) \
template <typename T> \
class name { \
private: \
template <typename Class> \
static char Test(decltype(&Class::func)*); \
template <typename> \
static int Test(...); \
\
public: \
static constexpr bool value = sizeof(Test<T>(nullptr)) == 1; \
}; \
\
template <typename T> \
constexpr bool name<T>::value;

DEFINE_TYPE_TRAIT(HasByteSize, ByteSize)
DEFINE_TYPE_TRAIT(HasType, TypeName)
DEFINE_TYPE_TRAIT(HasDescriptor, GetDescriptorString)
Expand Down

0 comments on commit a124911

Please sign in to comment.