diff --git a/db/db.cpp b/db/db.cpp index 273d463038d..0f3e3a280aa 100644 --- a/db/db.cpp +++ b/db/db.cpp @@ -546,7 +546,7 @@ sendmore: acquirePathLock(); remove_all( dbpath + "/_tmp/" ); - theFileAllocator().start(); + FileAllocator::get()->start(); BOOST_CHECK_EXCEPTION( clearTmpFiles() ); diff --git a/db/instance.cpp b/db/instance.cpp index ad3062ce63e..ca2d8658bd2 100644 --- a/db/instance.cpp +++ b/db/instance.cpp @@ -712,7 +712,7 @@ namespace mongo { // we would only hang here if the file_allocator code generates a // synchronous signal, which we don't expect log() << "shutdown: waiting for fs preallocator..." << endl; - theFileAllocator().waitUntilFinished(); + FileAllocator::get()->waitUntilFinished(); log() << "shutdown: closing all files..." << endl; if( cmdLine.dur ) { diff --git a/db/pdfile.cpp b/db/pdfile.cpp index d78a4161a2e..ee826446827 100644 --- a/db/pdfile.cpp +++ b/db/pdfile.cpp @@ -391,7 +391,7 @@ namespace mongo { if ( preallocateOnly ) { if ( cmdLine.prealloc ) { - theFileAllocator().requestAllocation( filename, size ); + FileAllocator::get()->requestAllocation( filename, size ); } return; } @@ -1948,7 +1948,7 @@ namespace mongo { void _applyOpToDataFiles( const char *database, FileOp &fo, bool afterAllocator, const string& path ) { if ( afterAllocator ) - theFileAllocator().waitUntilFinished(); + FileAllocator::get()->waitUntilFinished(); string c = database; c += '.'; boost::filesystem::path p(path); diff --git a/dbtests/framework.cpp b/dbtests/framework.cpp index e2e73d37948..2dbb810a665 100644 --- a/dbtests/framework.cpp +++ b/dbtests/framework.cpp @@ -257,7 +257,7 @@ namespace mongo { printSysInfo(); log() << "random seed: " << seed << endl; - theFileAllocator().start(); + FileAllocator::get()->start(); vector suites; if (params.count("suites")) { diff --git a/dbtests/perf/perftest.cpp b/dbtests/perf/perftest.cpp index e233f5c3d21..ef035518ce9 100644 --- a/dbtests/perf/perftest.cpp +++ b/dbtests/perf/perftest.cpp @@ -74,7 +74,7 @@ public: << "}" << endl; } ~Runner() { - theFileAllocator().waitUntilFinished(); + FileAllocator::get()->waitUntilFinished(); client_->dropDatabase( testDb< T >().c_str() ); } }; diff --git a/tools/tool.cpp b/tools/tool.cpp index 0491012b880..dfa5bb01211 100644 --- a/tools/tool.cpp +++ b/tools/tool.cpp @@ -203,7 +203,7 @@ namespace mongo { return -1; } - theFileAllocator().start(); + FileAllocator::get()->start(); } if ( _params.count( "db" ) ) diff --git a/util/file_allocator.cpp b/util/file_allocator.cpp index fe2b616ddee..163d4945d93 100644 --- a/util/file_allocator.cpp +++ b/util/file_allocator.cpp @@ -258,8 +258,12 @@ namespace mongo { #endif - // The mutex contained in this object may be held on shutdown. - FileAllocator &theFileAllocator_ = *(new FileAllocator()); - FileAllocator &theFileAllocator() { return theFileAllocator_; } + FileAllocator* FileAllocator::_instance = 0; + + FileAllocator* FileAllocator::get(){ + if ( ! _instance ) + _instance = new FileAllocator(); + return _instance; + } } // namespace mongo diff --git a/util/file_allocator.h b/util/file_allocator.h index c2688dc8bb7..c6fd927206b 100644 --- a/util/file_allocator.h +++ b/util/file_allocator.h @@ -22,16 +22,15 @@ namespace mongo { /* * Handles allocation of contiguous files on disk. Allocation may be * requested asynchronously or synchronously. + * singleton */ - class FileAllocator { + class FileAllocator : boost::noncopyable { /* * The public functions may not be called concurrently. The allocation * functions may be called multiple times per file, but only the first * size specified per file will be used. */ public: - FileAllocator(); - void start(); /** @@ -51,7 +50,13 @@ namespace mongo { static void ensureLength(int fd , long size); + /** @return the singletone */ + static FileAllocator * get(); + private: + + FileAllocator(); + #if !defined(_WIN32) void checkFailure(); @@ -73,7 +78,9 @@ namespace mongo { bool _failed; #endif + + static FileAllocator* _instance; + }; - FileAllocator &theFileAllocator(); } // namespace mongo diff --git a/util/mmap_posix.cpp b/util/mmap_posix.cpp index b6ce1661d81..4229fbebebb 100644 --- a/util/mmap_posix.cpp +++ b/util/mmap_posix.cpp @@ -55,7 +55,7 @@ namespace mongo { void* MemoryMappedFile::map(const char *filename, unsigned long long &length, int options) { // length may be updated by callee. setFilename(filename); - theFileAllocator().allocateAsap( filename, length ); + FileAllocator::get()->allocateAsap( filename, length ); len = length; massert( 10446 , str::stream() << "mmap: can't map area of size 0 file: " << filename, length > 0 );