blob: 49cb37abdba7d9b4a4bda917190170f9809d0f76 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
mmentovai@google.comf5a40002008-08-09 01:19:43 +09005#ifndef BASE_LOCK_H_
6#define BASE_LOCK_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
8#include "base/lock_impl.h"
9
jar@google.com8372da22008-10-10 02:33:43 +090010// A convenient wrapper for an OS specific critical section.
jar@google.com8372da22008-10-10 02:33:43 +090011
initial.commit3f4a7322008-07-27 06:49:38 +090012class Lock {
13 public:
jar@google.com87cf5092008-10-22 08:37:02 +090014 Lock() : lock_() {}
15 ~Lock() {}
16 void Acquire() { lock_.Lock(); }
17 void Release() { lock_.Unlock(); }
initial.commit3f4a7322008-07-27 06:49:38 +090018 // If the lock is not held, take it and return true. If the lock is already
jar@google.com8372da22008-10-10 02:33:43 +090019 // held by another thread, immediately return false.
jar@google.com87cf5092008-10-22 08:37:02 +090020 bool Try() { return lock_.Try(); }
initial.commit3f4a7322008-07-27 06:49:38 +090021
mmentovai@google.comf5a40002008-08-09 01:19:43 +090022 // Return the underlying lock implementation.
23 // TODO(awalker): refactor lock and condition variables so that this is
24 // unnecessary.
25 LockImpl* lock_impl() { return &lock_; }
26
initial.commit3f4a7322008-07-27 06:49:38 +090027 private:
jar@google.com87cf5092008-10-22 08:37:02 +090028 LockImpl lock_; // Platform specific underlying lock implementation.
initial.commit3f4a7322008-07-27 06:49:38 +090029
mmentovai@google.comf5a40002008-08-09 01:19:43 +090030 DISALLOW_COPY_AND_ASSIGN(Lock);
initial.commit3f4a7322008-07-27 06:49:38 +090031};
32
33// A helper class that acquires the given Lock while the AutoLock is in scope.
34class AutoLock {
35 public:
deanm@chromium.org91f3bd32008-10-01 22:47:39 +090036 explicit AutoLock(Lock& lock) : lock_(lock) {
initial.commit3f4a7322008-07-27 06:49:38 +090037 lock_.Acquire();
38 }
39
40 ~AutoLock() {
41 lock_.Release();
42 }
43
44 private:
45 Lock& lock_;
mmentovai@google.comf5a40002008-08-09 01:19:43 +090046 DISALLOW_COPY_AND_ASSIGN(AutoLock);
initial.commit3f4a7322008-07-27 06:49:38 +090047};
48
jar@google.com8372da22008-10-10 02:33:43 +090049// AutoUnlock is a helper class for ConditionVariable that will Release() the
50// lock argument in the constructor, and re-Acquire() it in the destructor.
initial.commit3f4a7322008-07-27 06:49:38 +090051class ConditionVariable;
52class AutoUnlock {
53 private: // Everything is private, so only our friend can use us.
54 friend class ConditionVariable; // The only user of this class.
jar@google.com8372da22008-10-10 02:33:43 +090055
56 explicit AutoUnlock(Lock& lock) : lock_(&lock) {
57 // We require our caller to have the lock.
58 lock_->Release();
59 }
60
61 ~AutoUnlock() {
62 lock_->Acquire();
63 }
initial.commit3f4a7322008-07-27 06:49:38 +090064
65 Lock* lock_;
initial.commit3f4a7322008-07-27 06:49:38 +090066};
67
mmentovai@google.comf5a40002008-08-09 01:19:43 +090068#endif // BASE_LOCK_H_
license.botf003cfe2008-08-24 09:55:55 +090069