summaryrefslogtreecommitdiff
path: root/cpp/src
diff options
context:
space:
mode:
authorBenoit Foucher <benoit@zeroc.com>2006-03-24 14:32:35 +0000
committerBenoit Foucher <benoit@zeroc.com>2006-03-24 14:32:35 +0000
commitc9a1e3aad627ee02dd5a17b374528f48c9ebe157 (patch)
tree912ba1a8707db30c1ce7fa6792691e90c824732a /cpp/src
parentFix (diff)
downloadice-c9a1e3aad627ee02dd5a17b374528f48c9ebe157.tar.bz2
ice-c9a1e3aad627ee02dd5a17b374528f48c9ebe157.tar.xz
ice-c9a1e3aad627ee02dd5a17b374528f48c9ebe157.zip
Fixed bug 916.
Diffstat (limited to 'cpp/src')
-rw-r--r--cpp/src/Ice/Instance.cpp6
-rw-r--r--cpp/src/Ice/Reference.cpp5
-rw-r--r--cpp/src/IceGrid/AdapterCache.cpp7
-rw-r--r--cpp/src/IceGrid/Database.cpp7
-rw-r--r--cpp/src/IceUtil/Random.cpp10
-rw-r--r--cpp/src/IceUtil/Thread.cpp15
6 files changed, 29 insertions, 21 deletions
diff --git a/cpp/src/Ice/Instance.cpp b/cpp/src/Ice/Instance.cpp
index f1a092270db..2860ba27f50 100644
--- a/cpp/src/Ice/Instance.cpp
+++ b/cpp/src/Ice/Instance.cpp
@@ -458,12 +458,6 @@ IceInternal::Instance::Instance(const CommunicatorPtr& communicator, const Prope
}
}
- unsigned int seed = static_cast<unsigned int>(IceUtil::Time::now().toMicroSeconds());
- srand(seed);
-#ifndef _WIN32
- srand48(seed);
-#endif
-
if(_properties->getPropertyAsInt("Ice.NullHandleAbort") > 0)
{
IceUtil::nullHandleAbort = true;
diff --git a/cpp/src/Ice/Reference.cpp b/cpp/src/Ice/Reference.cpp
index 784d6e5ba2f..9d6cfd9d1a5 100644
--- a/cpp/src/Ice/Reference.cpp
+++ b/cpp/src/Ice/Reference.cpp
@@ -25,6 +25,7 @@
#include <Ice/TraceLevels.h>
#include <Ice/DefaultsAndOverrides.h>
#include <IceUtil/StringUtil.h>
+#include <IceUtil/Random.h>
using namespace std;
using namespace Ice;
@@ -635,7 +636,7 @@ IceInternal::FixedReference::filterConnections(const vector<ConnectionIPtr>& all
//
// Randomize the order of connections.
//
- random_shuffle(connections.begin(), connections.end());
+ random_shuffle(connections.begin(), connections.end(), IceUtil::random);
//
// If a secure connection is requested, remove all non-secure
@@ -931,7 +932,7 @@ IceInternal::RoutableReference::createConnection(const vector<EndpointIPtr>& all
{
case Random:
{
- random_shuffle(endpoints.begin(), endpoints.end());
+ random_shuffle(endpoints.begin(), endpoints.end(), IceUtil::random);
break;
}
case Ordered:
diff --git a/cpp/src/IceGrid/AdapterCache.cpp b/cpp/src/IceGrid/AdapterCache.cpp
index e7df0dd82b2..b6bc81a2014 100644
--- a/cpp/src/IceGrid/AdapterCache.cpp
+++ b/cpp/src/IceGrid/AdapterCache.cpp
@@ -7,6 +7,7 @@
//
// **********************************************************************
+#include <IceUtil/Random.h>
#include <Ice/LoggerUtil.h>
#include <Ice/Locator.h>
#include <IceGrid/AdapterCache.h>
@@ -337,7 +338,7 @@ ReplicaGroupEntry::getProxies(bool allRegistered, int& nReplicas)
else// if(RandomLoadBalancingPolicyPtr::dynamicCast(_loadBalancing))
{
replicas = _replicas;
- random_shuffle(replicas.begin(), replicas.end());
+ random_shuffle(replicas.begin(), replicas.end(), IceUtil::random);
}
}
@@ -347,7 +348,7 @@ ReplicaGroupEntry::getProxies(bool allRegistered, int& nReplicas)
// This must be done outside the synchronization block since
// the sort() will call and lock each server entry.
//
- random_shuffle(replicas.begin(), replicas.end());
+ random_shuffle(replicas.begin(), replicas.end(), IceUtil::random);
sort(replicas.begin(), replicas.end(), ServerLoadCI(loadSample));
}
@@ -390,7 +391,7 @@ ReplicaGroupEntry::getLeastLoadedNodeLoad(LoadSample loadSample) const
// This must be done outside the synchronization block since
// min_element() will call and lock each server entry.
//
- random_shuffle(replicas.begin(), replicas.end());
+ random_shuffle(replicas.begin(), replicas.end(), IceUtil::random);
AdapterEntryPtr adpt = min_element(replicas.begin(), replicas.end(), ServerLoadCI(loadSample))->second;
return adpt->getLeastLoadedNodeLoad(loadSample);
}
diff --git a/cpp/src/IceGrid/Database.cpp b/cpp/src/IceGrid/Database.cpp
index e7e20fd3a43..c77ad832dfd 100644
--- a/cpp/src/IceGrid/Database.cpp
+++ b/cpp/src/IceGrid/Database.cpp
@@ -8,6 +8,7 @@
// **********************************************************************
#include <IceUtil/StringUtil.h>
+#include <IceUtil/Random.h>
#include <Freeze/Freeze.h>
#include <IceGrid/Database.h>
#include <IceGrid/TraceLevels.h>
@@ -765,7 +766,7 @@ Database::getAdapters(const string& id, bool allRegistered, int& endpointCount)
adpts.push_back(make_pair(p->first, adpt));
++p;
}
- random_shuffle(adpts.begin(), adpts.end());
+ random_shuffle(adpts.begin(), adpts.end(), IceUtil::random);
endpointCount = static_cast<int>(adpts.size());
return adpts;
}
@@ -927,14 +928,14 @@ Ice::ObjectPrx
Database::getObjectByType(const string& type)
{
Ice::ObjectProxySeq objs = getObjectsByType(type);
- return objs[rand() % objs.size()];
+ return objs[IceUtil::random(objs.size())];
}
Ice::ObjectPrx
Database::getObjectByTypeOnLeastLoadedNode(const string& type, LoadSample sample)
{
Ice::ObjectProxySeq objs = getObjectsByType(type);
- random_shuffle(objs.begin(), objs.end());
+ random_shuffle(objs.begin(), objs.end(), IceUtil::random);
vector<pair<Ice::ObjectPrx, float> > objectsWithLoad;
objectsWithLoad.reserve(objs.size());
for(Ice::ObjectProxySeq::const_iterator p = objs.begin(); p != objs.end(); ++p)
diff --git a/cpp/src/IceUtil/Random.cpp b/cpp/src/IceUtil/Random.cpp
index 501e6582cd8..d3e217a2fc1 100644
--- a/cpp/src/IceUtil/Random.cpp
+++ b/cpp/src/IceUtil/Random.cpp
@@ -197,5 +197,13 @@ IceUtil::random(int limit)
{
int r;
generateRandom(reinterpret_cast<char*>(&r), sizeof(int));
- return limit == 0 ? r : r % limit;
+ if(limit > 0)
+ {
+ r = r % limit;
+ }
+ if(r < 0)
+ {
+ r = -r;
+ }
+ return r;
}
diff --git a/cpp/src/IceUtil/Thread.cpp b/cpp/src/IceUtil/Thread.cpp
index ecb41166ea3..5efb71f1aab 100644
--- a/cpp/src/IceUtil/Thread.cpp
+++ b/cpp/src/IceUtil/Thread.cpp
@@ -117,18 +117,21 @@ WINAPI startHook(void* arg)
{
IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg);
- //
- // Initialize the random number generator in each thread.
- //
- unsigned int seed = static_cast<unsigned int>(IceUtil::Time::now().toMicroSeconds());
- srand(seed);
-
//
// Ensure that the thread doesn't go away until run() has
// completed.
//
thread = rawThread;
+#ifdef _WIN32
+ //
+ // Initialize the random number generator in each thread on
+ // Windows (the rand() seed is thread specific).
+ //
+ unsigned int seed = static_cast<unsigned int>(IceUtil::Time::now().toMicroSeconds());
+ srand(seed ^ thread->getThreadControl().id());
+#endif
+
//
// See the comment in IceUtil::Thread::start() for details.
//