diff options
author | Hari Khalsa <hkhalsa@10gen.com> | 2013-01-17 13:22:24 -0500 |
---|---|---|
committer | Hari Khalsa <hkhalsa@10gen.com> | 2013-01-17 16:34:21 -0500 |
commit | 3a44cf876b94ec892e4a0cfb3885e25f24978bd8 (patch) | |
tree | cc1b809f725860abdbf15edd1de03bcc7ee45c16 /src/third_party | |
parent | e0ae3e12c01498ba716b72fd4302b7f7adfc9d13 (diff) | |
download | mongo-3a44cf876b94ec892e4a0cfb3885e25f24978bd8.tar.gz |
use MONGO_INITIALIZER to ensure ordering of s2's static inits
Diffstat (limited to 'src/third_party')
-rw-r--r-- | src/third_party/s2/s2cellid.cc | 23 | ||||
-rw-r--r-- | src/third_party/s2/s2regioncoverer.cc | 20 |
2 files changed, 11 insertions, 32 deletions
diff --git a/src/third_party/s2/s2cellid.cc b/src/third_party/s2/s2cellid.cc index bff623b7f66..3e3b7bb2d2a 100644 --- a/src/third_party/s2/s2cellid.cc +++ b/src/third_party/s2/s2cellid.cc @@ -27,6 +27,8 @@ using std::vector; #include "util/math/mathutil.h" #include "util/math/vector2-inl.h" +#include "mongo/base/init.h" + // The following lookup tables are used to convert efficiently between an // (i,j) cell index and the corresponding position along the Hilbert curve. // "lookup_pos" maps 4 bits of "i", 4 bits of "j", and 2 bits representing the @@ -86,24 +88,17 @@ static void InitLookupCell(int level, int i, int j, int orig_orientation, } } -static int Init() { +static void Init() { InitLookupCell(0, 0, 0, 0, 0, 0); InitLookupCell(0, 0, 0, kSwapMask, 0, kSwapMask); InitLookupCell(0, 0, 0, kInvertMask, 0, kInvertMask); InitLookupCell(0, 0, 0, kSwapMask|kInvertMask, 0, kSwapMask|kInvertMask); - return 5; } -#ifndef _WIN32 -static pthread_once_t init_once = PTHREAD_ONCE_INIT; -static void voidInit() { (void)Init(); } -inline static void MaybeInit() { - pthread_once(&init_once, voidInit); +MONGO_INITIALIZER(S2CellIdInit)(mongo::InitializerContext *context) { + Init(); + return mongo::Status::OK(); } -#else -static const int foo = Init(); -inline static void MaybeInit() {} -#endif int S2CellId::level() const { // Fast path for leaf cells. @@ -216,9 +211,6 @@ inline int S2CellId::STtoIJ(double s) { S2CellId S2CellId::FromFaceIJ(int face, int i, int j) { - // Initialization if not done yet - MaybeInit(); - // Optimization notes: // - Non-overlapping bit fields can be combined with either "+" or "|". // Generally "+" seems to produce better code, but not always. @@ -278,9 +270,6 @@ S2CellId S2CellId::FromLatLng(S2LatLng const& ll) { } int S2CellId::ToFaceIJOrientation(int* pi, int* pj, int* orientation) const { - // Initialization if not done yet - MaybeInit(); - int i = 0, j = 0; int face = this->face(); int bits = (face & kSwapMask); diff --git a/src/third_party/s2/s2regioncoverer.cc b/src/third_party/s2/s2regioncoverer.cc index 0d01be43561..fb1cb2a0715 100644 --- a/src/third_party/s2/s2regioncoverer.cc +++ b/src/third_party/s2/s2regioncoverer.cc @@ -26,6 +26,7 @@ using std::vector; #include "s2.h" #include "s2cap.h" #include "s2cellunion.h" +#include "mongo/base/init.h" // Define storage for header file constants (the values are not needed here). int const S2RegionCoverer::kDefaultMaxCells = 8; @@ -43,25 +44,16 @@ struct S2RegionCoverer::CompareQueueEntries : public less<QueueEntry> { static S2Cell face_cells[6]; -static int Init() { +static void Init() { for (int face = 0; face < 6; ++face) { face_cells[face] = S2Cell::FromFacePosLevel(face, 0, 0); } - // arbitrary value. We want this to be called without pthread_once, so we populate - // a variable with the return value of this function. - return 5; } -#ifndef _WIN32 -static pthread_once_t init_once = PTHREAD_ONCE_INIT; -static void voidInit() { Init(); } -inline static void MaybeInit() { - pthread_once(&init_once, voidInit); +MONGO_INITIALIZER_WITH_PREREQUISITES(S2RegionCovererInit, ("S2CellIdInit"))(mongo::InitializerContext *context) { + Init(); + return mongo::Status::OK(); } -#else -static const int foo = Init(); -inline static void MaybeInit() { } -#endif S2RegionCoverer::S2RegionCoverer() : min_level_(0), @@ -71,8 +63,6 @@ S2RegionCoverer::S2RegionCoverer() : region_(NULL), result_(new vector<S2CellId>), pq_(new CandidateQueue) { - // Initialize the constants - MaybeInit(); } S2RegionCoverer::~S2RegionCoverer() { |