Once files moved to aos folder and files updated accordingly
Change-Id: Idd26fca1d4dc15407e92859400e1a15a8a142ec3
diff --git a/aos/common/BUILD b/aos/common/BUILD
index f4b57d5..e3160c4 100644
--- a/aos/common/BUILD
+++ b/aos/common/BUILD
@@ -200,31 +200,6 @@
],
)
-cc_library(
- name = 'once',
- hdrs = [
- 'once.h',
- ],
- srcs = [
- 'once-tmpl.h',
- ],
- deps = [
- ':gtest_prod',
- ':type_traits',
- ],
-)
-
-cc_test(
- name = 'once_test',
- srcs = [
- 'once_test.cc',
- ],
- deps = [
- '//aos/testing:googletest',
- ':once',
- ],
-)
-
cc_test(
name = 'time_test',
srcs = [
diff --git a/aos/common/logging/BUILD b/aos/common/logging/BUILD
index 8b28482..cc961b3 100644
--- a/aos/common/logging/BUILD
+++ b/aos/common/logging/BUILD
@@ -182,7 +182,7 @@
deps = [
'//aos/common:die',
'//aos/common:time',
- '//aos/common:once',
+ '//aos:once',
'//aos/common:queue_types',
':logging',
'//aos/common:type_traits',
diff --git a/aos/common/logging/implementations.cc b/aos/common/logging/implementations.cc
index 22cd4f4..58359f9 100644
--- a/aos/common/logging/implementations.cc
+++ b/aos/common/logging/implementations.cc
@@ -7,11 +7,11 @@
#include <chrono>
#include "aos/common/die.h"
-#include "aos/common/once.h"
-#include "aos/common/time.h"
-#include "aos/common/queue_types.h"
#include "aos/common/logging/printf_formats.h"
+#include "aos/common/queue_types.h"
+#include "aos/common/time.h"
#include "aos/linux_code/ipc_lib/queue.h"
+#include "aos/once.h"
namespace aos {
namespace logging {
diff --git a/aos/common/logging/implementations.h b/aos/common/logging/implementations.h
index 48b5f0b..142c6ba 100644
--- a/aos/common/logging/implementations.h
+++ b/aos/common/logging/implementations.h
@@ -13,14 +13,14 @@
#include <functional>
#include <atomic>
-#include "aos/common/logging/logging.h"
-#include "aos/common/type_traits.h"
-#include "aos/common/mutex.h"
-#include "aos/common/macros.h"
-#include "aos/common/logging/sizes.h"
-#include "aos/common/logging/interface.h"
#include "aos/common/logging/context.h"
-#include "aos/common/once.h"
+#include "aos/common/logging/interface.h"
+#include "aos/common/logging/logging.h"
+#include "aos/common/logging/sizes.h"
+#include "aos/common/macros.h"
+#include "aos/common/mutex.h"
+#include "aos/common/type_traits.h"
+#include "aos/once.h"
namespace aos {
diff --git a/aos/common/network/BUILD b/aos/common/network/BUILD
index 19335ba..c5476b2 100644
--- a/aos/common/network/BUILD
+++ b/aos/common/network/BUILD
@@ -10,7 +10,7 @@
],
deps = [
'//aos/linux_code:configuration',
- '//aos/common:once',
+ '//aos:once',
'//aos/common/logging',
'//aos/common/util:string_to_num',
],
diff --git a/aos/common/network/team_number.cc b/aos/common/network/team_number.cc
index 4aa7626..0669601 100644
--- a/aos/common/network/team_number.cc
+++ b/aos/common/network/team_number.cc
@@ -7,10 +7,10 @@
#include <string>
-#include "aos/common/once.h"
-#include "aos/linux_code/configuration.h"
#include "aos/common/logging/logging.h"
#include "aos/common/util/string_to_num.h"
+#include "aos/linux_code/configuration.h"
+#include "aos/once.h"
namespace aos {
namespace network {
diff --git a/aos/common/once-tmpl.h b/aos/common/once-tmpl.h
deleted file mode 100644
index 0618395..0000000
--- a/aos/common/once-tmpl.h
+++ /dev/null
@@ -1,43 +0,0 @@
-#ifdef __VXWORKS__
-#include <taskLib.h>
-#else
-#include <sched.h>
-#endif
-
-#include "aos/common/type_traits.h"
-
-// It doesn't use pthread_once, because Brian looked at the pthreads
-// implementation for vxworks and noticed that it is completely and entirely
-// broken for doing just about anything (including its pthread_once). It has the
-// same implementation under linux for simplicity.
-
-namespace aos {
-
-// Setting function_ multiple times would be OK because it'll get set to the
-// same value each time.
-template<typename T>
-Once<T>::Once(Function function)
- : function_(function) {
- static_assert(shm_ok<Once<T>>::value, "Once should work in shared memory");
-}
-
-template<typename T>
-void Once<T>::Reset() {
- __atomic_store_n(&run_, false, __ATOMIC_SEQ_CST);
- __atomic_store_n(&done_, false, __ATOMIC_SEQ_CST);
-}
-
-template<typename T>
-T *Once<T>::Get() {
- if (__atomic_exchange_n(&run_, true, __ATOMIC_RELAXED) == false) {
- result_ = function_();
- __atomic_store_n(&done_, true, __ATOMIC_RELEASE);
- } else {
- while (!__atomic_load_n(&done_, __ATOMIC_ACQUIRE)) {
- sched_yield();
- }
- }
- return result_;
-}
-
-} // namespace aos
diff --git a/aos/common/once.h b/aos/common/once.h
deleted file mode 100644
index b7fbcb1..0000000
--- a/aos/common/once.h
+++ /dev/null
@@ -1,69 +0,0 @@
-#ifndef AOS_COMMON_ONCE_H_
-#define AOS_COMMON_ONCE_H_
-
-#include <stdint.h>
-
-#include "aos/common/gtest_prod.h"
-
-namespace aos {
-namespace testing {
-
-FORWARD_DECLARE_TEST_CASE(OnceTest, MemoryClearing);
-
-} // namespace testing
-
-// Designed for the same thing as pthread_once: to run something exactly 1 time.
-//
-// Intended use case:
-// const char *CalculateSomethingCool() {
-// static ::aos::Once<const char> once(DoCalculateSomethingCool);
-// return once.Get();
-// }
-//
-// IMPORTANT: Instances _must_ be placed in memory that gets 0-initialized
-// automatically or Reset() must be called exactly once!!
-// The expected use case is to use one of these as a static variable, and those
-// do get 0-initialized under the C++ standard. Global variables are treated the
-// same way by the C++ Standard.
-// Placing an instance in shared memory (and using Reset()) is also supported.
-// The constructor does not initialize all of the member variables!
-// This is because initializing them in the constructor creates a race condition
-// if initialization of static variables isn't thread safe.
-template<typename T>
-class Once {
- public:
- typedef T *(*Function)();
- explicit Once(Function function);
-
- // Returns the result of calling function_. The first call will actually run
- // it and then any other ones will block (if necessary) until it's finished
- // and then return the same thing.
- T *Get();
-
- // Will clear out all the member variables. If this is going to be used
- // instead of creating an instance in 0-initialized memory, then this method
- // must be called exactly once before Get() is called anywhere.
- // This method can also be called to run the function again the next time
- // Get() is called. However, calling it again is not thread safe.
- void Reset();
-
- private:
- // The function to run to calculate result_.
- Function function_;
- // Whether or not it is running. Gets atomically swapped from false to true by
- // the thread that actually runs function_.
- bool run_;
- // Whether or not it is done. Gets set to true after the thread that is
- // running function_ finishes running it and storing the result in result_.
- bool done_;
- // What function_ returned when it was executed.
- T *result_;
-
- FRIEND_TEST_NAMESPACE(OnceTest, MemoryClearing, testing);
-};
-
-} // namespace aos
-
-#include "aos/common/once-tmpl.h"
-
-#endif // AOS_COMMON_ONCE_H_
diff --git a/aos/common/once_test.cc b/aos/common/once_test.cc
deleted file mode 100644
index 3773b5b..0000000
--- a/aos/common/once_test.cc
+++ /dev/null
@@ -1,130 +0,0 @@
-#include "aos/common/once.h"
-
-#include <stdlib.h>
-#include <limits.h>
-
-#include "gtest/gtest.h"
-
-namespace aos {
-namespace testing {
-
-class OnceTest : public ::testing::Test {
- public:
- static int *Function() {
- ++times_run_;
- value_ = 971 + times_run_;
- return &value_;
- }
-
- protected:
- void SetUp() {
- value_ = 0;
- times_run_ = 0;
- }
-
- static int value_;
- static int times_run_;
-};
-int OnceTest::value_, OnceTest::times_run_;
-
-// Makes sure that it calls the function at the right time and that it correctly
-// passes the result out.
-TEST_F(OnceTest, Works) {
- static Once<int> once(Function);
-
- EXPECT_EQ(0, value_);
- EXPECT_EQ(0, times_run_);
-
- EXPECT_EQ(value_, *once.Get());
- EXPECT_NE(0, value_);
- EXPECT_NE(0, times_run_);
- // Make sure it's not passing it through an assignment by value or something
- // else weird.
- EXPECT_EQ(&value_, once.Get());
-}
-
-// Makes sure that having a Once at namespace scope works correctly.
-namespace {
-
-Once<int> global_once(OnceTest::Function);
-
-} // namespace
-
-TEST_F(OnceTest, Global) {
- EXPECT_EQ(value_, *global_once.Get());
- EXPECT_NE(0, value_);
- EXPECT_NE(0, times_run_);
-}
-
-// Makes sure that an instance keeps returning the same value without running
-// the function again.
-TEST_F(OnceTest, MultipleGets) {
- static Once<int> once(Function);
-
- EXPECT_EQ(value_, *once.Get());
- EXPECT_EQ(1, times_run_);
- EXPECT_EQ(value_, *once.Get());
- EXPECT_EQ(1, times_run_);
- EXPECT_EQ(value_, *once.Get());
- EXPECT_EQ(1, times_run_);
-}
-
-// Tests to make sure that the right methods clear out the instance variables at
-// the right times.
-TEST_F(OnceTest, MemoryClearing) {
- Once<int> once(NULL);
-
- once.run_ = 1;
- once.done_ = true;
- // Run the constructor again to make sure it doesn't touch the variables set
- // above.
- new (&once)Once<int>(Function);
-
- // Should return a random, (potentially) uninitialized value.
- once.Get();
- EXPECT_EQ(0, times_run_);
-
- once.Reset();
- EXPECT_EQ(0, times_run_);
- EXPECT_EQ(value_, *once.Get());
- EXPECT_EQ(1, times_run_);
-}
-
-namespace {
-
-int second_result = 0;
-int *SecondFunction() {
- static int result = 254;
- second_result = ++result;
- return &second_result;
-}
-
-} // namespace
-
-// Makes sure that multiple instances don't interfere with each other.
-TEST_F(OnceTest, MultipleInstances) {
- static Once<int> once1(Function);
- static Once<int> once2(SecondFunction);
-
- EXPECT_EQ(&value_, once1.Get());
- EXPECT_EQ(&second_result, once2.Get());
- EXPECT_EQ(&value_, once1.Get());
- EXPECT_EQ(&second_result, once2.Get());
-}
-
-// Tests calling Reset() to run the function a second time.
-TEST_F(OnceTest, Recalculate) {
- Once<int> once(Function);
- once.Reset();
-
- EXPECT_EQ(value_, *once.Get());
- EXPECT_EQ(1, times_run_);
-
- value_ = 0;
- once.Reset();
- EXPECT_EQ(value_, *once.Get());
- EXPECT_EQ(2, times_run_);
-}
-
-} // namespace testing
-} // namespace aos