Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
staticsafedeclare.h File Reference
#include <stdint.h>
#include <type_traits>
#include <vector>
#include "ion/base/lockguards.h"
#include "ion/base/shareable.h"
#include "ion/base/sharedptr.h"
#include "ion/base/static_assert.h"
#include "ion/port/atomic.h"
#include "ion/port/mutex.h"
Include dependency graph for staticsafedeclare.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  ion::base::StaticDeleterBase
 
class  ion::base::StaticDeleter< T >
 This class should not be used directly. More...
 
class  ion::base::StaticDeleter< T[]>
 Specialization for arrays. More...
 
class  ion::base::StaticDeleterDeleter
 StaticDeleterDeleter is an internal class that holds and deletes StaticDeleters; it should not be used directly. More...
 

Namespaces

 ion
 Copyright 2016 Google Inc.
 
 ion::base
 EnumHelper instantiations. These must be in the ion::base namespace.
 

Macros

#define ION_SAFE_ASSIGN_STATIC_POINTER(type, variable, new_variable, add_func, destroyer)
 Copyright 2016 Google Inc. More...
 
#define ION_DECLARE_SAFE_STATIC(type, variable, constructor, add_func, destroyer)
 Declares and thread-safely assigns the value of a static variable. More...
 
#define ION_DECLARE_SAFE_STATIC_ARRAY(type, variable, count)
 Use the below ION_DECLARE_STATIC to declare static variables. More...
 
#define ION_DECLARE_SAFE_STATIC_POINTER(type, variable)
 Declare a static non-array pointer and calls a default constructor. More...
 
#define ION_DECLARE_SAFE_STATIC_POINTER_WITH_CONSTRUCTOR(type, variable, constructor)
 Declare a static non-array pointer and calls a non-default constructor. More...
 

Macro Definition Documentation

#define ION_DECLARE_SAFE_STATIC (   type,
  variable,
  constructor,
  add_func,
  destroyer 
)
Value:
static std::atomic<type> atomic_##variable; /* Zero-initialized */ \
"static variables must be of pointer type"); \
type variable = atomic_##variable.load(std::memory_order_acquire); \
if (variable == 0) { \
type new_##variable = constructor; \
type, atomic_##variable, new_##variable, add_func, destroyer); \
variable = atomic_##variable.load(std::memory_order_acquire); \
}
#define ION_SAFE_ASSIGN_STATIC_POINTER(type, variable, new_variable, add_func, destroyer)
Copyright 2016 Google Inc.
std::string type
Definition: printer.cc:353
double value
#define ION_STATIC_ASSERT(expr, message)
Copyright 2016 Google Inc.
Definition: static_assert.h:35

Declares and thread-safely assigns the value of a static variable.

Definition at line 80 of file staticsafedeclare.h.

Referenced by ion::base::StaticDeleterDeleter::GetInstance().

#define ION_DECLARE_SAFE_STATIC_ARRAY (   type,
  variable,
  count 
)
Value:
type*, \
variable, \
new type[count], \
delete[])
std::string type
Definition: printer.cc:353
#define ION_DECLARE_SAFE_STATIC(type, variable, constructor, add_func, destroyer)
Declares and thread-safely assigns the value of a static variable.
static StaticDeleterDeleter * GetInstance()
Returns a pointer to the global instance.

Use the below ION_DECLARE_STATIC to declare static variables.

Declares a static array variable.

Definition at line 102 of file staticsafedeclare.h.

#define ION_DECLARE_SAFE_STATIC_POINTER (   type,
  variable 
)
Value:
type*, \
variable, \
new type, \
delete)
std::string type
Definition: printer.cc:353
#define ION_DECLARE_SAFE_STATIC(type, variable, constructor, add_func, destroyer)
Declares and thread-safely assigns the value of a static variable.
static StaticDeleterDeleter * GetInstance()
Returns a pointer to the global instance.

Declare a static non-array pointer and calls a default constructor.

Definition at line 111 of file staticsafedeclare.h.

Referenced by ion::profile::VSyncProfiler::Get(), ion::gfxprofile::GpuProfiler::Get(), ion::profile::GetCallTraceManager(), and ion::base::logging_internal::Logger::~Logger().

#define ION_DECLARE_SAFE_STATIC_POINTER_WITH_CONSTRUCTOR (   type,
  variable,
  constructor 
)
Value:
type*, \
variable, \
constructor, \
delete)
std::string type
Definition: printer.cc:353
#define ION_DECLARE_SAFE_STATIC(type, variable, constructor, add_func, destroyer)
Declares and thread-safely assigns the value of a static variable.
static StaticDeleterDeleter * GetInstance()
Returns a pointer to the global instance.

Declare a static non-array pointer and calls a non-default constructor.

The constructor parameter must be enclosed in parenthesis for the macro to expand correctly.

Definition at line 122 of file staticsafedeclare.h.

Referenced by ion::base::GetDefaultLogEntryWriter(), and ion::gfx::Renderer::GetStateTable().

#define ION_SAFE_ASSIGN_STATIC_POINTER (   type,
  variable,
  new_variable,
  add_func,
  destroyer 
)
Value:
type null = NULL; \
if (variable.compare_exchange_strong(null, new_variable)) { \
add_func(#type, new_variable); \
} else { \
destroyer new_variable; \
}
std::string type
Definition: printer.cc:353

Copyright 2016 Google Inc.

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.Use the below ION_DECLARE_STATIC_* macros to safely initialize a local static pointer variable with 4- or 8-byte size. These macros will not work for global statics, and these macros are not necessary for POD (Plain Old Datatype) variables.

For example, to safely create a function that returns a singleton instance: MySingletonClass* GetSingleton() { ION_DECLARE_SAFE_STATIC_POINTER_WITH_CONSTRUCTOR( MySingletonClass, s_singleton, (new MySingletonClass(arg1, arg2))); return s_singleton; } The macro declares s_singleton with the right type and initializes it with the passed constructor.

Some more examples:

///< Declare foo_ptr as a pointer to a single int. ION_DECLARE_SAFE_STATIC_POINTER(int, foo_ptr); ///< Declare foo_array as an int* of 10 ints. ION_DECLARE_SAFE_STATIC_ARRAY(int, foo_array, 10); ///< Declare foo_struct as a pointer to MyStructDeletedSecond. ION_DECLARE_SAFE_STATIC_POINTER(MyStructDeletedSecond, foo_struct); ///< Declare foo_struct2 as a pointer to MyStructDeletedFirst, calling a ///< non-default constructor. The constructor must be in parentheses for the ///< macro to expand correctly. ION_DECLARE_SAFE_STATIC_POINTER_WITH_CONSTRUCTOR( MyStructDeletedFirst, foo_struct2, (new MyStructDeletedFirst(2)));

All pointer-types initialized with ION_DECLARE_STATIC_* are added to a static vector of pointers through the StaticDeleter class, below. StaticDeleter is derived from Shareable so it can be managed by a SharedPtr. When it is destroyed as the program exits it cleans up any pointers that were added to it in the reverse order they were created. This ensures proper dependency handling. Uses AtomicCompareAndSwap to uniquely set the value of variable with new_variable. If the swap fails then destroys new_variable.

Definition at line 70 of file staticsafedeclare.h.