layout: doc title: “Starboard Module Reference: once.h”

Onces represent initializations that should only ever happen once per process, in a thread-safe way.

Macros

Defines a function that will initialize the indicated type once and return it. This initialization is thread safe if the type being created is side effect free.
These macros CAN ONLY BE USED IN A CC file, never in a header file.
Example (in cc file): SB_ONCE_INITIALIZE_FUNCTION(MyClass, GetOrCreateMyClass) .. MyClass* instance = GetOrCreateMyClass(); MyClass* instance2 = GetOrCreateMyClass(); DCHECK_EQ(instance, instance2);

Functions

Init

Declaration

    static void Init() {                                   \
      s_singleton = new Type();                            \
    }                                                      \
  };                                                       \
  SbOnce(&s_once_flag, Local::Init);                       \
  return s_singleton;                                      \
}

SbOnce

Description

Thread-safely runs init_routine only once.

Declaration and definitions

#include "starboard/once.h"

bool SbOnce(SbOnceControl* /*once_control*/,
            SbOnceInitRoutine /*init_routine*/) {
  return false;
}

Parameters

s_once_flag

Description

Defines a function that will initialize the indicated type once and return it. This initialization is thread safe if the type being created is side effect free.
These macros CAN ONLY BE USED IN A CC file, never in a header file.
Example (in cc file): SB_ONCE_INITIALIZE_FUNCTION(MyClass, GetOrCreateMyClass) .. MyClass* instance = GetOrCreateMyClass(); MyClass* instance2 = GetOrCreateMyClass(); DCHECK_EQ(instance, instance2);

Declaration

  static SbOnceControl s_once_flag = SB_ONCE_INITIALIZER;  \

Parameters

s_singleton

Declaration

  static Type* s_singleton = NULL;                         \
  struct Local {                                           \

Parameters