diff options
author | Benoit Foucher <benoit@zeroc.com> | 2006-03-24 14:32:35 +0000 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2006-03-24 14:32:35 +0000 |
commit | c9a1e3aad627ee02dd5a17b374528f48c9ebe157 (patch) | |
tree | 912ba1a8707db30c1ce7fa6792691e90c824732a /cpp | |
parent | Fix (diff) | |
download | ice-c9a1e3aad627ee02dd5a17b374528f48c9ebe157.tar.bz2 ice-c9a1e3aad627ee02dd5a17b374528f48c9ebe157.tar.xz ice-c9a1e3aad627ee02dd5a17b374528f48c9ebe157.zip |
Fixed bug 916.
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/src/Ice/Instance.cpp | 6 | ||||
-rw-r--r-- | cpp/src/Ice/Reference.cpp | 5 | ||||
-rw-r--r-- | cpp/src/IceGrid/AdapterCache.cpp | 7 | ||||
-rw-r--r-- | cpp/src/IceGrid/Database.cpp | 7 | ||||
-rw-r--r-- | cpp/src/IceUtil/Random.cpp | 10 | ||||
-rw-r--r-- | cpp/src/IceUtil/Thread.cpp | 15 | ||||
-rw-r--r-- | cpp/test/Glacier2/attack/Client.cpp | 9 | ||||
-rw-r--r-- | cpp/test/Glacier2/router/Client.cpp | 3 | ||||
-rw-r--r-- | cpp/test/Ice/binding/AllTests.cpp | 17 |
9 files changed, 45 insertions, 34 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. // diff --git a/cpp/test/Glacier2/attack/Client.cpp b/cpp/test/Glacier2/attack/Client.cpp index fe076021ce4..c2213c49be5 100644 --- a/cpp/test/Glacier2/attack/Client.cpp +++ b/cpp/test/Glacier2/attack/Client.cpp @@ -7,6 +7,7 @@ // // ********************************************************************** +#include <IceUtil/Random.h> #include <Ice/Application.h> #include <Glacier2/Router.h> #include <Backend.h> @@ -75,16 +76,16 @@ CallbackClient::run(int argc, char* argv[]) Identity ident; string::iterator p; - ident.name.resize(1 + rand() % 2); + ident.name.resize(1 + IceUtil::random() % 2); for(p = ident.name.begin(); p != ident.name.end(); ++p) { - *p = static_cast<char>('A' + rand() % 26); + *p = static_cast<char>('A' + IceUtil::random() % 26); } - ident.category.resize(1); // (rand() % 2); + ident.category.resize(1); // (IceUtil::random() % 2); for(p = ident.category.begin(); p != ident.category.end(); ++p) { - *p = static_cast<char>('a' + rand() % 26); + *p = static_cast<char>('a' + IceUtil::random() % 26); } backend = BackendPrx::uncheckedCast(backendBase->ice_newIdentity(ident)); diff --git a/cpp/test/Glacier2/router/Client.cpp b/cpp/test/Glacier2/router/Client.cpp index 4aff56b6a75..9b6489a75c2 100644 --- a/cpp/test/Glacier2/router/Client.cpp +++ b/cpp/test/Glacier2/router/Client.cpp @@ -7,6 +7,7 @@ // // ********************************************************************** +#include <IceUtil/Random.h> #include <Ice/Application.h> #include <Glacier2/Router.h> #include <TestCommon.h> @@ -705,7 +706,7 @@ CallbackClient::run(int argc, char* argv[]) vector<StressClientPtr> clients; for(i = 0; i < nClients; ++i) { - switch(rand() % 3) + switch(IceUtil::random(3)) { case 0: clients.push_back(new PingStressClient(i)); diff --git a/cpp/test/Ice/binding/AllTests.cpp b/cpp/test/Ice/binding/AllTests.cpp index 7380badbd6b..901ef628aca 100644 --- a/cpp/test/Ice/binding/AllTests.cpp +++ b/cpp/test/Ice/binding/AllTests.cpp @@ -7,6 +7,7 @@ // // ********************************************************************** +#include <IceUtil/Random.h> #include <Ice/Ice.h> #include <TestCommon.h> #include <Test.h> @@ -135,9 +136,9 @@ allTests(const Ice::CommunicatorPtr& communicator) vector<RemoteObjectAdapterPrx> adpts = adapters; TestIntfPrx test1 = createTestIntfPrx(adpts); - random_shuffle(adpts.begin(), adpts.end()); + random_shuffle(adpts.begin(), adpts.end(), IceUtil::random); TestIntfPrx test2 = createTestIntfPrx(adpts); - random_shuffle(adpts.begin(), adpts.end()); + random_shuffle(adpts.begin(), adpts.end(), IceUtil::random); TestIntfPrx test3 = createTestIntfPrx(adpts); test(test1->ice_connection() == test2->ice_connection()); @@ -182,9 +183,9 @@ allTests(const Ice::CommunicatorPtr& communicator) vector<RemoteObjectAdapterPrx> adpts = adapters; TestIntfPrx test1 = createTestIntfPrx(adpts); - random_shuffle(adpts.begin(), adpts.end()); + random_shuffle(adpts.begin(), adpts.end(), IceUtil::random); TestIntfPrx test2 = createTestIntfPrx(adpts); - random_shuffle(adpts.begin(), adpts.end()); + random_shuffle(adpts.begin(), adpts.end(), IceUtil::random); TestIntfPrx test3 = createTestIntfPrx(adpts); test(test1->ice_connection() == test2->ice_connection()); @@ -226,9 +227,9 @@ allTests(const Ice::CommunicatorPtr& communicator) vector<RemoteObjectAdapterPrx> adpts = adapters; TestIntfPrx test1 = createTestIntfPrx(adpts); - random_shuffle(adpts.begin(), adpts.end()); + random_shuffle(adpts.begin(), adpts.end(), IceUtil::random); TestIntfPrx test2 = createTestIntfPrx(adpts); - random_shuffle(adpts.begin(), adpts.end()); + random_shuffle(adpts.begin(), adpts.end(), IceUtil::random); TestIntfPrx test3 = createTestIntfPrx(adpts); test(test1->ice_connection() == test2->ice_connection()); @@ -273,9 +274,9 @@ allTests(const Ice::CommunicatorPtr& communicator) vector<RemoteObjectAdapterPrx> adpts = adapters; TestIntfPrx test1 = createTestIntfPrx(adpts); - random_shuffle(adpts.begin(), adpts.end()); + random_shuffle(adpts.begin(), adpts.end(), IceUtil::random); TestIntfPrx test2 = createTestIntfPrx(adpts); - random_shuffle(adpts.begin(), adpts.end()); + random_shuffle(adpts.begin(), adpts.end(), IceUtil::random); TestIntfPrx test3 = createTestIntfPrx(adpts); test(test1->ice_connection() == test2->ice_connection()); |