Sabina Davis | 2ed5ea2 | 2017-09-26 22:27:42 -0700 | [diff] [blame] | 1 | #ifndef AOS_ONCE_H_ |
| 2 | #define AOS_ONCE_H_ |
brians | 2fdfc07 | 2013-02-26 05:35:15 +0000 | [diff] [blame] | 3 | |
| 4 | #include <stdint.h> |
| 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/gtest_prod.h" |
brians | 2fdfc07 | 2013-02-26 05:35:15 +0000 | [diff] [blame] | 7 | |
| 8 | namespace aos { |
| 9 | namespace testing { |
| 10 | |
| 11 | FORWARD_DECLARE_TEST_CASE(OnceTest, MemoryClearing); |
| 12 | |
| 13 | } // namespace testing |
| 14 | |
| 15 | // Designed for the same thing as pthread_once: to run something exactly 1 time. |
| 16 | // |
| 17 | // Intended use case: |
| 18 | // const char *CalculateSomethingCool() { |
| 19 | // static ::aos::Once<const char> once(DoCalculateSomethingCool); |
| 20 | // return once.Get(); |
| 21 | // } |
| 22 | // |
| 23 | // IMPORTANT: Instances _must_ be placed in memory that gets 0-initialized |
| 24 | // automatically or Reset() must be called exactly once!! |
| 25 | // The expected use case is to use one of these as a static variable, and those |
| 26 | // do get 0-initialized under the C++ standard. Global variables are treated the |
| 27 | // same way by the C++ Standard. |
| 28 | // Placing an instance in shared memory (and using Reset()) is also supported. |
| 29 | // The constructor does not initialize all of the member variables! |
| 30 | // This is because initializing them in the constructor creates a race condition |
| 31 | // if initialization of static variables isn't thread safe. |
| 32 | template<typename T> |
| 33 | class Once { |
| 34 | public: |
| 35 | typedef T *(*Function)(); |
| 36 | explicit Once(Function function); |
| 37 | |
| 38 | // Returns the result of calling function_. The first call will actually run |
| 39 | // it and then any other ones will block (if necessary) until it's finished |
| 40 | // and then return the same thing. |
| 41 | T *Get(); |
| 42 | |
| 43 | // Will clear out all the member variables. If this is going to be used |
| 44 | // instead of creating an instance in 0-initialized memory, then this method |
| 45 | // must be called exactly once before Get() is called anywhere. |
| 46 | // This method can also be called to run the function again the next time |
| 47 | // Get() is called. However, calling it again is not thread safe. |
| 48 | void Reset(); |
| 49 | |
| 50 | private: |
| 51 | // The function to run to calculate result_. |
| 52 | Function function_; |
Brian Silverman | 4968c20 | 2014-05-19 17:04:02 -0700 | [diff] [blame] | 53 | // Whether or not it is running. Gets atomically swapped from false to true by |
| 54 | // the thread that actually runs function_. |
| 55 | bool run_; |
brians | 2fdfc07 | 2013-02-26 05:35:15 +0000 | [diff] [blame] | 56 | // Whether or not it is done. Gets set to true after the thread that is |
| 57 | // running function_ finishes running it and storing the result in result_. |
Brian Silverman | 4968c20 | 2014-05-19 17:04:02 -0700 | [diff] [blame] | 58 | bool done_; |
brians | 2fdfc07 | 2013-02-26 05:35:15 +0000 | [diff] [blame] | 59 | // What function_ returned when it was executed. |
| 60 | T *result_; |
| 61 | |
| 62 | FRIEND_TEST_NAMESPACE(OnceTest, MemoryClearing, testing); |
| 63 | }; |
| 64 | |
| 65 | } // namespace aos |
| 66 | |
Sabina Davis | 2ed5ea2 | 2017-09-26 22:27:42 -0700 | [diff] [blame] | 67 | #include "aos/once-tmpl.h" |
brians | 2fdfc07 | 2013-02-26 05:35:15 +0000 | [diff] [blame] | 68 | |
Sabina Davis | 2ed5ea2 | 2017-09-26 22:27:42 -0700 | [diff] [blame] | 69 | #endif // AOS_ONCE_H_ |