aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/linux/guid_creator.cc24
1 files 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 <assert.h>
+#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@@ -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.