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 "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 {
private:
static RandomizerHandle defaultRandomizer;
static BitfieldManFactoryHandle factory;
RandomizerHandle randomizer;
BitfieldManFactory();
public:
~BitfieldManFactory() {}
static BitfieldManFactoryHandle getNewFactory() {
BitfieldManFactoryHandle factory = new BitfieldManFactory();
static BitfieldManFactoryHandle getFactoryInstance() {
if(factory.isNull()) {
factory = new BitfieldManFactory();
}
return factory;
}
@ -64,11 +67,12 @@ public:
}
static void setDefaultRandomizer(const RandomizerHandle& randomizer) {
defaultRandomizer = randomizer;
BitfieldManFactoryHandle factory = getFactoryInstance();
factory->setRandomizer(randomizer);
}
static RandomizerHandle getDefaultRandomizer() {
return defaultRandomizer;
return getFactoryInstance()->getRandomizer();
}
void setRandomizer(const RandomizerHandle& randomizer) {

View File

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

View File

@ -46,7 +46,7 @@ Peer::Peer(string ipaddr, int port, int pieceLength, long long int totalLength):
active(false)
{
resetStatus();
this->bitfield = BitfieldManFactory::getNewFactory()->
this->bitfield = BitfieldManFactory::getFactoryInstance()->
createBitfieldMan(pieceLength, totalLength);
string idSeed = ipaddr+":"+Util::itos(port);
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) {
bitfield =
BitfieldManFactory::getNewFactory()->createBitfieldMan(BLOCK_LENGTH, length);
BitfieldManFactory::getFactoryInstance()->createBitfieldMan(BLOCK_LENGTH, length);
}
Piece::Piece(const Piece& piece) {

View File

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