Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / disk_cache / blockfile / file_lock.h
blob3486aba18d19e15b6f01b3c9f174b852251ca6a7
1 // Copyright (c) 2011 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.
5 // See net/disk_cache/disk_cache.h for the public interface of the cache.
7 #ifndef NET_DISK_CACHE_BLOCKFILE_FILE_LOCK_H_
8 #define NET_DISK_CACHE_BLOCKFILE_FILE_LOCK_H_
10 #include "net/base/net_export.h"
11 #include "net/disk_cache/blockfile/disk_format_base.h"
13 namespace disk_cache {
15 // This class implements a file lock that lives on the header of a memory mapped
16 // file. This is NOT a thread related lock, it is a lock to detect corruption
17 // of the file when the process crashes in the middle of an update.
18 // The lock is acquired on the constructor and released on the destructor.
19 // The typical use of the class is:
20 // {
21 // BlockFileHeader* header = GetFileHeader();
22 // FileLock lock(header);
23 // header->max_entries = num_entries;
24 // // At this point the destructor is going to release the lock.
25 // }
26 // It is important to perform Lock() and Unlock() operations in the right order,
27 // because otherwise the desired effect of the "lock" will not be achieved. If
28 // the operations are inlined / optimized, the "locked" operations can happen
29 // outside the lock.
30 class NET_EXPORT_PRIVATE FileLock {
31 public:
32 explicit FileLock(BlockFileHeader* header);
33 virtual ~FileLock();
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_;
43 } // namespace disk_cache
45 #endif // NET_DISK_CACHE_BLOCKFILE_FILE_LOCK_H_