]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
msg: don't use thread_local variable
authorYan, Zheng <zyan@redhat.com>
Fri, 27 May 2016 09:02:29 +0000 (17:02 +0800)
committerYan, Zheng <zyan@redhat.com>
Mon, 18 Jul 2016 03:08:48 +0000 (11:08 +0800)
thread_local is not supported by OSX

Signed-off-by: Yan, Zheng <zyan@redhat.com>
src/msg/Messenger.cc

index b6534f9a3208eece2cc2800f159fbabf8f3b2204..a271b082b9ac788de677313ddb2f57c9338bbb6b 100644 (file)
@@ -1,7 +1,8 @@
 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
 // vim: ts=8 sw=2 smarttab
 
-#include <thread>
+#include <random>
+#include "include/Spinlock.h"
 #include "include/types.h"
 #include "Messenger.h"
 
@@ -19,15 +20,18 @@ Messenger *Messenger::create_client_messenger(CephContext *cct, string lname)
                           lname, nonce, 0);
 }
 
+static std::default_random_engine random_engine;
+static Spinlock random_lock;
+
 Messenger *Messenger::create(CephContext *cct, const string &type,
                             entity_name_t name, string lname,
                             uint64_t nonce, uint64_t features, uint64_t cflags)
 {
   int r = -1;
   if (type == "random") {
-    thread_local unsigned seed = (unsigned) time(nullptr) +
-      (unsigned) std::hash<std::thread::id>()(std::this_thread::get_id());
-    r = rand_r(&seed) % 2; // random does not include xio
+    std::lock_guard<Spinlock> lock(random_lock);
+    std::uniform_int_distribution<> dis(0, 1);
+    r = dis(random_engine);
   }
   if (r == 0 || type == "simple")
     return new SimpleMessenger(cct, name, lname, nonce, features);