To fix static initialization order problem:

* src/BitfieldManFactory.h
	(defaultRandomizer): Removed.
	(factory): New variable.
	(getNewFactory): Removed.
	(getFactoryInstance): New function.
	(setDefaultRandomizer): Rewritten.
	(getDefaultRandomizer): Rewritten.
	* src/BitfieldManFactory.cc
	(defaultRandomizer): Removed.
	(factory): Initialized to 0.
	(BitfieldManFactory): Initialized randomizer to 0.
	* src/DefaultPieceStorage.cc
	(DefaultPieceStorage): getNewFactory() -> getFactoryInstance()
	* src/Peer.cc
	(Peer): getNewFactory() -> getFactoryInstance()
	* src/SegmentMan.cc
	(initBitfield): getNewFactory() -> getFactoryInstance()
	* src/Piece.cc
	(Piece): getNewFactory() -> getFactoryInstance()
pull/1/head
Tatsuhiro Tsujikawa 2007-02-12 12:05:55 +00:00
parent aa1730d1f3
commit cc24f7cdf8
6 changed files with 15 additions and 11 deletions

View File

@ -35,6 +35,6 @@
#include "BitfieldManFactory.h" #include "BitfieldManFactory.h"
#include "SimpleRandomizer.h" #include "SimpleRandomizer.h"
RandomizerHandle BitfieldManFactory::defaultRandomizer = SimpleRandomizer::getInstance(); BitfieldManFactoryHandle BitfieldManFactory::factory = 0;
BitfieldManFactory::BitfieldManFactory():randomizer(defaultRandomizer) {} BitfieldManFactory::BitfieldManFactory():randomizer(0) {}

View File

@ -45,15 +45,18 @@ typedef SharedHandle<BitfieldManFactory> BitfieldManFactoryHandle;
class BitfieldManFactory { class BitfieldManFactory {
private: private:
static RandomizerHandle defaultRandomizer; static BitfieldManFactoryHandle factory;
RandomizerHandle randomizer; RandomizerHandle randomizer;
BitfieldManFactory(); BitfieldManFactory();
public: public:
~BitfieldManFactory() {} ~BitfieldManFactory() {}
static BitfieldManFactoryHandle getNewFactory() { static BitfieldManFactoryHandle getFactoryInstance() {
BitfieldManFactoryHandle factory = new BitfieldManFactory(); if(factory.isNull()) {
factory = new BitfieldManFactory();
}
return factory; return factory;
} }
@ -64,11 +67,12 @@ public:
} }
static void setDefaultRandomizer(const RandomizerHandle& randomizer) { static void setDefaultRandomizer(const RandomizerHandle& randomizer) {
defaultRandomizer = randomizer; BitfieldManFactoryHandle factory = getFactoryInstance();
factory->setRandomizer(randomizer);
} }
static RandomizerHandle getDefaultRandomizer() { static RandomizerHandle getDefaultRandomizer() {
return defaultRandomizer; return getFactoryInstance()->getRandomizer();
} }
void setRandomizer(const RandomizerHandle& randomizer) { void setRandomizer(const RandomizerHandle& randomizer) {

View File

@ -52,7 +52,7 @@ DefaultPieceStorage::DefaultPieceStorage(BtContextHandle btContext, const Option
option(option) option(option)
{ {
bitfieldMan = bitfieldMan =
BitfieldManFactory::getNewFactory()-> BitfieldManFactory::getFactoryInstance()->
createBitfieldMan(btContext->getPieceLength(), createBitfieldMan(btContext->getPieceLength(),
btContext->getTotalLength()); btContext->getTotalLength());
logger = LogFactory::getInstance(); logger = LogFactory::getInstance();

View File

@ -46,7 +46,7 @@ Peer::Peer(string ipaddr, int port, int pieceLength, long long int totalLength):
active(false) active(false)
{ {
resetStatus(); resetStatus();
this->bitfield = BitfieldManFactory::getNewFactory()-> this->bitfield = BitfieldManFactory::getFactoryInstance()->
createBitfieldMan(pieceLength, totalLength); createBitfieldMan(pieceLength, totalLength);
string idSeed = ipaddr+":"+Util::itos(port); string idSeed = ipaddr+":"+Util::itos(port);
id = Util::simpleMessageDigest(idSeed); id = Util::simpleMessageDigest(idSeed);

View File

@ -40,7 +40,7 @@ Piece::Piece():index(0), length(0), bitfield(0) {}
Piece::Piece(int index, int length):index(index), length(length) { Piece::Piece(int index, int length):index(index), length(length) {
bitfield = bitfield =
BitfieldManFactory::getNewFactory()->createBitfieldMan(BLOCK_LENGTH, length); BitfieldManFactory::getFactoryInstance()->createBitfieldMan(BLOCK_LENGTH, length);
} }
Piece::Piece(const Piece& piece) { Piece::Piece(const Piece& piece) {

View File

@ -238,7 +238,7 @@ void SegmentMan::init() {
void SegmentMan::initBitfield(int segmentLength, long long int totalLength) { void SegmentMan::initBitfield(int segmentLength, long long int totalLength) {
delete bitfield; delete bitfield;
this->bitfield = BitfieldManFactory::getNewFactory()->createBitfieldMan(segmentLength, totalLength); this->bitfield = BitfieldManFactory::getFactoryInstance()->createBitfieldMan(segmentLength, totalLength);
} }
Segment SegmentMan::checkoutSegment(int cuid, int index) { Segment SegmentMan::checkoutSegment(int cuid, int index) {