1 // Copyright 2012 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 #include "net/disk_cache/blockfile/file_lock.h" 6 7 #include <atomic> 8 9 #include "build/build_config.h" 10 11 namespace { 12 Barrier()13void Barrier() { 14 #if !defined(COMPILER_MSVC) 15 // VS uses memory barrier semantics for volatiles. 16 std::atomic_thread_fence(std::memory_order_seq_cst); 17 #endif 18 } 19 20 } // namespace 21 22 namespace disk_cache { 23 FileLock(BlockFileHeader * header)24FileLock::FileLock(BlockFileHeader* header) { 25 updating_ = &header->updating; 26 (*updating_) = (*updating_) + 1; 27 Barrier(); 28 acquired_ = true; 29 } 30 ~FileLock()31FileLock::~FileLock() { 32 Unlock(); 33 } 34 Lock()35void FileLock::Lock() { 36 if (acquired_) 37 return; 38 (*updating_) = (*updating_) + 1; 39 Barrier(); 40 } 41 Unlock()42void FileLock::Unlock() { 43 if (!acquired_) 44 return; 45 Barrier(); 46 (*updating_) = (*updating_) - 1; 47 } 48 49 } // namespace disk_cache 50