From fe7f63632f5cd24bea65d1ef2de88ccbab9b8602 Mon Sep 17 00:00:00 2001 From: "qsr@chromium.org" Date: Thu, 8 Mar 2012 18:47:26 +0000 Subject: [PATCH] Remove static initializer in linux/guid_creator.cc. There was a static initializer generated for this file in Chrome for Android. Patch by pliard@chromium.org Original review: http://breakpad.appspot.com/359001/ Review URL: https://breakpad.appspot.com/359002 git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@931 4c0a9323-5329-0410-9bdc-e9ce6186880e --- src/common/linux/guid_creator.cc | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/common/linux/guid_creator.cc b/src/common/linux/guid_creator.cc index 678f5904..426f93a1 100644 --- a/src/common/linux/guid_creator.cc +++ b/src/common/linux/guid_creator.cc @@ -30,6 +30,7 @@ #include "common/linux/guid_creator.h" #include +#include #include #include #include @@ -45,10 +46,6 @@ // class GUIDGenerator { public: - GUIDGenerator() { - srandom(time(NULL)); - } - static u_int32_t BytesToUInt32(const u_int8_t bytes[]) { return ((u_int32_t) bytes[0] | ((u_int32_t) bytes[1] << 8) @@ -63,7 +60,8 @@ class GUIDGenerator { bytes[3] = (n >> 24) & 0xff; } - bool CreateGUID(GUID *guid) const { + static bool CreateGUID(GUID *guid) { + InitOnce(); guid->data1 = random(); guid->data2 = (u_int16_t)(random()); guid->data3 = (u_int16_t)(random()); @@ -71,13 +69,23 @@ class GUIDGenerator { UInt32ToBytes(&guid->data4[4], random()); return true; } + + private: + static void InitOnce() { + pthread_once(&once_control, &InitOnceImpl); + } + + static void InitOnceImpl() { + srandom(time(NULL)); + } + + static pthread_once_t once_control; }; -// Guid generator. -const GUIDGenerator kGuidGenerator; +pthread_once_t GUIDGenerator::once_control = PTHREAD_ONCE_INIT; bool CreateGUID(GUID *guid) { - return kGuidGenerator.CreateGUID(guid); + return GUIDGenerator::CreateGUID(guid); } // Parse guid to string.