00001 // @file file_allocator.h 00002 00003 /* Copyright 2009 10gen Inc. 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #include "../pch.h" 00019 00020 namespace mongo { 00021 00022 /* 00023 * Handles allocation of contiguous files on disk. Allocation may be 00024 * requested asynchronously or synchronously. 00025 * singleton 00026 */ 00027 class FileAllocator : boost::noncopyable { 00028 /* 00029 * The public functions may not be called concurrently. The allocation 00030 * functions may be called multiple times per file, but only the first 00031 * size specified per file will be used. 00032 */ 00033 public: 00034 void start(); 00035 00040 void requestAllocation( const string &name, long &size ); 00041 00042 00047 void allocateAsap( const string &name, unsigned long long &size ); 00048 00049 void waitUntilFinished() const; 00050 00051 static void ensureLength(int fd , long size); 00052 00054 static FileAllocator * get(); 00055 00056 private: 00057 00058 FileAllocator(); 00059 00060 #if !defined(_WIN32) 00061 void checkFailure(); 00062 00063 // caller must hold pendingMutex_ lock. Returns size if allocated or 00064 // allocation requested, -1 otherwise. 00065 long prevSize( const string &name ) const; 00066 00067 // caller must hold pendingMutex_ lock. 00068 bool inProgress( const string &name ) const; 00069 00071 static void run( FileAllocator * fa ); 00072 00073 mutable mongo::mutex _pendingMutex; 00074 mutable boost::condition _pendingUpdated; 00075 00076 list< string > _pending; 00077 mutable map< string, long > _pendingSize; 00078 00079 bool _failed; 00080 #endif 00081 00082 static FileAllocator* _instance; 00083 00084 }; 00085 00087 void ensureParentDirCreated(const boost::filesystem::path& p); 00088 00089 } // namespace mongo