aboutsummaryrefslogtreecommitdiffstats
path: root/src/google/protobuf/stubs/once.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/stubs/once.h')
-rw-r--r--src/google/protobuf/stubs/once.h103
1 files changed, 30 insertions, 73 deletions
diff --git a/src/google/protobuf/stubs/once.h b/src/google/protobuf/stubs/once.h
index cc62bba..0dee407 100644
--- a/src/google/protobuf/stubs/once.h
+++ b/src/google/protobuf/stubs/once.h
@@ -1,6 +1,6 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
+// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
@@ -37,22 +37,16 @@
//
// This is basically a portable version of pthread_once().
//
-// This header declares:
+// This header declares three things:
// * A type called ProtobufOnceType.
// * A macro GOOGLE_PROTOBUF_DECLARE_ONCE() which declares a variable of type
// ProtobufOnceType. This is the only legal way to declare such a variable.
-// The macro may only be used at the global scope (you cannot create local or
-// class member variables of this type).
-// * A function GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()).
+// The macro may only be used at the global scope (you cannot create local
+// or class member variables of this type).
+// * A function GogoleOnceInit(ProtobufOnceType* once, void (*init_func)()).
// This function, when invoked multiple times given the same ProtobufOnceType
// object, will invoke init_func on the first call only, and will make sure
// none of the calls return before that first call to init_func has finished.
-// * The user can provide a parameter which GoogleOnceInit() forwards to the
-// user-provided function when it is called. Usage example:
-// int a = 10;
-// GoogleOnceInit(&my_once, &MyFunctionExpectingIntArgument, &a);
-// * This implementation guarantees that ProtobufOnceType is a POD (i.e. no
-// static initializer generated).
//
// This implements a way to perform lazy initialization. It's more efficient
// than using mutexes as no lock is needed if initialization has already
@@ -78,87 +72,50 @@
#ifndef GOOGLE_PROTOBUF_STUBS_ONCE_H__
#define GOOGLE_PROTOBUF_STUBS_ONCE_H__
-#include <google/protobuf/stubs/atomicops.h>
#include <google/protobuf/stubs/common.h>
+#ifndef _WIN32
+#include <pthread.h>
+#endif
+
namespace google {
namespace protobuf {
-#ifdef GOOGLE_PROTOBUF_NO_THREAD_SAFETY
+#ifdef _WIN32
-typedef bool ProtobufOnceType;
+struct ProtobufOnceInternal;
-#define GOOGLE_PROTOBUF_ONCE_INIT false
+struct LIBPROTOBUF_EXPORT ProtobufOnceType {
+ ProtobufOnceType();
+ ~ProtobufOnceType();
+ void Init(void (*init_func)());
-inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) {
- if (!*once) {
- *once = true;
- init_func();
- }
-}
+ volatile bool initialized_;
+ ProtobufOnceInternal* internal_;
+};
-template <typename Arg>
-inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)(Arg),
- Arg arg) {
- if (!*once) {
- *once = true;
- init_func(arg);
+#define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \
+ ::google::protobuf::ProtobufOnceType NAME
+
+inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) {
+ // Note: Double-checked locking is safe on x86.
+ if (!once->initialized_) {
+ once->Init(init_func);
}
}
#else
-enum {
- ONCE_STATE_UNINITIALIZED = 0,
- ONCE_STATE_EXECUTING_CLOSURE = 1,
- ONCE_STATE_DONE = 2
-};
-
-typedef internal::AtomicWord ProtobufOnceType;
+typedef pthread_once_t ProtobufOnceType;
-#define GOOGLE_PROTOBUF_ONCE_INIT ::google::protobuf::ONCE_STATE_UNINITIALIZED
-
-LIBPROTOBUF_EXPORT
-void GoogleOnceInitImpl(ProtobufOnceType* once, Closure* closure);
+#define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \
+ pthread_once_t NAME = PTHREAD_ONCE_INIT
inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) {
- if (internal::Acquire_Load(once) != ONCE_STATE_DONE) {
- internal::FunctionClosure0 func(init_func, false);
- GoogleOnceInitImpl(once, &func);
- }
-}
-
-template <typename Arg>
-inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)(Arg*),
- Arg* arg) {
- if (internal::Acquire_Load(once) != ONCE_STATE_DONE) {
- internal::FunctionClosure1<Arg*> func(init_func, false, arg);
- GoogleOnceInitImpl(once, &func);
- }
+ pthread_once(once, init_func);
}
-#endif // GOOGLE_PROTOBUF_NO_THREAD_SAFETY
-
-class GoogleOnceDynamic {
- public:
- GoogleOnceDynamic() : state_(GOOGLE_PROTOBUF_ONCE_INIT) { }
-
- // If this->Init() has not been called before by any thread,
- // execute (*func_with_arg)(arg) then return.
- // Otherwise, wait until that prior invocation has finished
- // executing its function, then return.
- template<typename T>
- void Init(void (*func_with_arg)(T*), T* arg) {
- GoogleOnceInit<T>(&this->state_,
- func_with_arg,
- arg);
- }
- private:
- ProtobufOnceType state_;
-};
-
-#define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \
- ::google::protobuf::ProtobufOnceType NAME = GOOGLE_PROTOBUF_ONCE_INIT
+#endif
} // namespace protobuf
} // namespace google