summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/src/IceUtil/Random.cpp32
-rw-r--r--cpp/src/IceUtil/UUID.cpp37
2 files changed, 25 insertions, 44 deletions
diff --git a/cpp/src/IceUtil/Random.cpp b/cpp/src/IceUtil/Random.cpp
index ff6f89b254b..52d4c3ead3b 100644
--- a/cpp/src/IceUtil/Random.cpp
+++ b/cpp/src/IceUtil/Random.cpp
@@ -36,13 +36,10 @@ const char* IceUtil::RandomGeneratorException::_name = "IceUtil::RandomGenerator
// linux-kernel mailing list archive for additional details.
// Since /dev/urandom on other platforms is usually a port from Linux, this problem
// could be widespread. Therefore, we serialize access to /dev/urandom using a static
-// mutex. We also cache the process id for convenience (needed by generateUUID() to
-// make sure 2 different processes can't return the same UUID because of this problem,
-// see UUID.cpp).
+// mutex.
//
static IceUtil::StaticMutex staticMutex = ICE_STATIC_MUTEX_INITIALIZER;
static int fd = -1;
-static char myPid[2];
namespace
{
@@ -88,26 +85,6 @@ IceUtil::RandomGeneratorException::ice_throw() const
throw *this;
}
-//
-// Used by generateUUID() to get a random sequence and the pid of
-// current process.
-//
-#ifndef _WIN32
-namespace IceUtil
-{
-
-void
-generateRandomAndGetPid(char* buffer, int size, char* pid)
-{
- assert(pid);
- generateRandom(buffer, size);
- pid[0] = myPid[0];
- pid[1] = myPid[1];
-}
-
-}
-#endif
-
void
IceUtil::generateRandom(char* buffer, int size)
{
@@ -132,13 +109,6 @@ IceUtil::generateRandom(char* buffer, int size)
assert(0);
throw RandomGeneratorException(__FILE__, __LINE__);
}
-
- //
- // Initialize myPid as well
- //
- pid_t p = getpid();
- myPid[0] = (p >> 8) & 0x7F;
- myPid[1] = p & 0xFF;
}
//
diff --git a/cpp/src/IceUtil/UUID.cpp b/cpp/src/IceUtil/UUID.cpp
index f6a32118a7b..77f01bb8bfd 100644
--- a/cpp/src/IceUtil/UUID.cpp
+++ b/cpp/src/IceUtil/UUID.cpp
@@ -23,14 +23,27 @@
using namespace std;
#ifndef _WIN32
+
+static char myPid[2];
+
namespace IceUtil
{
//
-// This method is defined in Random.cpp as a convenience for
-// generateUUID.
+// Initialize the pid.
//
-void generateRandomAndGetPid(char*, int, char*);
+class PidInitializer
+{
+public:
+
+ PidInitializer()
+ {
+ pid_t p = getpid();
+ myPid[0] = (p >> 8) & 0x7F;
+ myPid[1] = p & 0xFF;
+ }
+};
+static PidInitializer pidInitializer;
};
#endif
@@ -90,16 +103,14 @@ IceUtil::generateUUID()
assert(sizeof(UUID) == 16);
//
- // Get a random sequence of bytes and the pid of the current
- // process. Instead of using 122 random bits that could be
- // duplicated (because of a bug with some Linux kernels and
- // potentially other Unix platforms -- see comment in Random.cpp),
- // we replace the last 15 bits of all "random" Randoms by the last
- // 15 bits of the process id.
+ // Get a random sequence of bytes. Instead of using 122 random
+ // bits that could be duplicated (because of a bug with some Linux
+ // kernels and potentially other Unix platforms -- see comment in
+ // Random.cpp), we replace the last 15 bits of all "random"
+ // Randoms by the last 15 bits of the process id.
//
char* buffer = reinterpret_cast<char*>(&uuid);
- char pid[2];
- generateRandomAndGetPid(buffer, sizeof(UUID), pid);
+ generateRandom(buffer, sizeof(UUID));
//
// Adjust the bits that say "version 4" UUID
@@ -112,8 +123,8 @@ IceUtil::generateUUID()
//
// Replace the end of the node by myPid (15 bits)
//
- uuid.node[4] = (uuid.node[4] & 0x80) | pid[0];
- uuid.node[5] = pid[1];
+ uuid.node[4] = (uuid.node[4] & 0x80) | myPid[0];
+ uuid.node[5] = myPid[1];
//
// Convert to a UUID string