blob: 31ad9a0185974fab6c853c0a3a6dfeee80cb3bdf [file] [log] [blame]
[email protected]c9d869c92009-03-18 23:46:271// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]08048c72008-08-08 16:19:435#ifndef BASE_LOCK_H_
6#define BASE_LOCK_H_
initial.commitd7cae122008-07-26 21:49:387
8#include "base/lock_impl.h"
9
[email protected]81898f7b22008-10-09 17:33:4310// A convenient wrapper for an OS specific critical section.
[email protected]81898f7b22008-10-09 17:33:4311
initial.commitd7cae122008-07-26 21:49:3812class Lock {
13 public:
[email protected]3cdb6a72008-10-21 23:37:0214 Lock() : lock_() {}
15 ~Lock() {}
16 void Acquire() { lock_.Lock(); }
17 void Release() { lock_.Unlock(); }
initial.commitd7cae122008-07-26 21:49:3818 // If the lock is not held, take it and return true. If the lock is already
[email protected]81898f7b22008-10-09 17:33:4319 // held by another thread, immediately return false.
[email protected]3cdb6a72008-10-21 23:37:0220 bool Try() { return lock_.Try(); }
initial.commitd7cae122008-07-26 21:49:3821
[email protected]c9d869c92009-03-18 23:46:2722 // In debug builds this method checks that the lock has been acquired by the
23 // calling thread. If the lock has not been acquired, then the method
24 // will DCHECK(). In non-debug builds, the LockImpl's implementation of
25 // AssertAcquired() is an empty inline method.
[email protected]1fff4a02009-03-19 21:03:1326 void AssertAcquired() const { return lock_.AssertAcquired(); }
[email protected]c9d869c92009-03-18 23:46:2727
[email protected]08048c72008-08-08 16:19:4328 // Return the underlying lock implementation.
29 // TODO(awalker): refactor lock and condition variables so that this is
30 // unnecessary.
31 LockImpl* lock_impl() { return &lock_; }
32
initial.commitd7cae122008-07-26 21:49:3833 private:
[email protected]3cdb6a72008-10-21 23:37:0234 LockImpl lock_; // Platform specific underlying lock implementation.
initial.commitd7cae122008-07-26 21:49:3835
[email protected]08048c72008-08-08 16:19:4336 DISALLOW_COPY_AND_ASSIGN(Lock);
initial.commitd7cae122008-07-26 21:49:3837};
38
39// A helper class that acquires the given Lock while the AutoLock is in scope.
40class AutoLock {
41 public:
[email protected]b4fef8722008-10-01 13:47:3942 explicit AutoLock(Lock& lock) : lock_(lock) {
initial.commitd7cae122008-07-26 21:49:3843 lock_.Acquire();
44 }
45
46 ~AutoLock() {
[email protected]c9d869c92009-03-18 23:46:2747 lock_.AssertAcquired();
initial.commitd7cae122008-07-26 21:49:3848 lock_.Release();
49 }
50
51 private:
52 Lock& lock_;
[email protected]08048c72008-08-08 16:19:4353 DISALLOW_COPY_AND_ASSIGN(AutoLock);
initial.commitd7cae122008-07-26 21:49:3854};
55
[email protected]c9d869c92009-03-18 23:46:2756// AutoUnlock is a helper that will Release() the |lock| argument in the
57// constructor, and re-Acquire() it in the destructor.
initial.commitd7cae122008-07-26 21:49:3858class AutoUnlock {
[email protected]c9d869c92009-03-18 23:46:2759 public:
60 explicit AutoUnlock(Lock& lock) : lock_(lock) {
[email protected]81898f7b22008-10-09 17:33:4361 // We require our caller to have the lock.
[email protected]c9d869c92009-03-18 23:46:2762 lock_.AssertAcquired();
63 lock_.Release();
[email protected]81898f7b22008-10-09 17:33:4364 }
65
66 ~AutoUnlock() {
[email protected]c9d869c92009-03-18 23:46:2767 lock_.Acquire();
[email protected]81898f7b22008-10-09 17:33:4368 }
initial.commitd7cae122008-07-26 21:49:3869
[email protected]c9d869c92009-03-18 23:46:2770 private:
71 Lock& lock_;
72 DISALLOW_COPY_AND_ASSIGN(AutoUnlock);
initial.commitd7cae122008-07-26 21:49:3873};
74
[email protected]08048c72008-08-08 16:19:4375#endif // BASE_LOCK_H_