1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // See net/disk_cache/disk_cache.h for the public interface of the cache. 6 7 #ifndef NET_DISK_CACHE_FILE_LOCK_H__ 8 #define NET_DISK_CACHE_FILE_LOCK_H__ 9 10 #include "net/disk_cache/disk_format.h" 11 12 namespace disk_cache { 13 14 // This class implements a file lock that lives on the header of a memory mapped 15 // file. This is NOT a thread related lock, it is a lock to detect corruption 16 // of the file when the process crashes in the middle of an update. 17 // The lock is acquired on the constructor and released on the destructor. 18 // The typical use of the class is: 19 // { 20 // BlockFileHeader* header = GetFileHeader(); 21 // FileLock lock(header); 22 // header->max_entries = num_entries; 23 // // At this point the destructor is going to release the lock. 24 // } 25 // It is important to perform Lock() and Unlock() operations in the right order, 26 // because otherwise the desired effect of the "lock" will not be achieved. If 27 // the operations are inlined / optimized, the "locked" operations can happen 28 // outside the lock. 29 class FileLock { 30 public: 31 explicit FileLock(BlockFileHeader* header); ~FileLock()32 virtual ~FileLock() { 33 Unlock(); 34 } 35 // Virtual to make sure the compiler never inlines the calls. 36 virtual void Lock(); 37 virtual void Unlock(); 38 private: 39 bool acquired_; 40 volatile int32* updating_; 41 }; 42 43 } // namespace disk_cache 44 45 #endif // NET_DISK_CACHE_FILE_LOCK_H__ 46