15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See net/disk_cache/disk_cache.h for the public interface of the cache. 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef NET_DISK_CACHE_BLOCKFILE_FILE_LOCK_H_ 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define NET_DISK_CACHE_BLOCKFILE_FILE_LOCK_H_ 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h" 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/disk_cache/blockfile/disk_format_base.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace disk_cache { 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class implements a file lock that lives on the header of a memory mapped 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// file. This is NOT a thread related lock, it is a lock to detect corruption 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the file when the process crashes in the middle of an update. 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The lock is acquired on the constructor and released on the destructor. 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The typical use of the class is: 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// { 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// BlockFileHeader* header = GetFileHeader(); 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FileLock lock(header); 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// header->max_entries = num_entries; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// // At this point the destructor is going to release the lock. 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// } 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It is important to perform Lock() and Unlock() operations in the right order, 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// because otherwise the desired effect of the "lock" will not be achieved. If 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the operations are inlined / optimized, the "locked" operations can happen 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// outside the lock. 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT_PRIVATE FileLock { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) explicit FileLock(BlockFileHeader* header); 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~FileLock(); 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Virtual to make sure the compiler never inlines the calls. 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Lock(); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Unlock(); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool acquired_; 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) volatile int32* updating_; 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace disk_cache 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif // NET_DISK_CACHE_BLOCKFILE_FILE_LOCK_H_ 46