xref: /aosp_15_r20/external/cronet/net/disk_cache/blockfile/file_lock.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
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_BLOCKFILE_FILE_LOCK_H_
8 #define NET_DISK_CACHE_BLOCKFILE_FILE_LOCK_H_
9 
10 #include <stdint.h>
11 
12 #include "base/memory/raw_ptr.h"
13 #include "net/base/net_export.h"
14 #include "net/disk_cache/blockfile/disk_format_base.h"
15 
16 namespace disk_cache {
17 
18 // This class implements a file lock that lives on the header of a memory mapped
19 // file. This is NOT a thread related lock, it is a lock to detect corruption
20 // of the file when the process crashes in the middle of an update.
21 // The lock is acquired on the constructor and released on the destructor.
22 // The typical use of the class is:
23 //    {
24 //      BlockFileHeader* header = GetFileHeader();
25 //      FileLock lock(header);
26 //      header->max_entries = num_entries;
27 //      // At this point the destructor is going to release the lock.
28 //    }
29 // It is important to perform Lock() and Unlock() operations in the right order,
30 // because otherwise the desired effect of the "lock" will not be achieved. If
31 // the operations are inlined / optimized, the "locked" operations can happen
32 // outside the lock.
33 class NET_EXPORT_PRIVATE FileLock {
34  public:
35   explicit FileLock(BlockFileHeader* header);
36   virtual ~FileLock();
37 
38   // Virtual to make sure the compiler never inlines the calls.
39   virtual void Lock();
40   virtual void Unlock();
41  private:
42   bool acquired_;
43   raw_ptr<volatile int32_t> updating_;
44 };
45 
46 }  // namespace disk_cache
47 
48 #endif  // NET_DISK_CACHE_BLOCKFILE_FILE_LOCK_H_
49