blob: cbaabc784b109cb0723a71417d6ac4881de064bc [file] [log] [blame]
brettw@chromium.orge439a962011-01-02 08:16:20 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// 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
brettw@chromium.orge439a962011-01-02 08:16:20 +09005#ifndef BASE_SYNCHRONIZATION_LOCK_IMPL_H_
6#define BASE_SYNCHRONIZATION_LOCK_IMPL_H_
mmentovai@google.comf5a40002008-08-09 01:19:43 +09007
avia6a6a682015-12-27 07:15:14 +09008#include "base/base_export.h"
9#include "base/macros.h"
mmentovai@google.comf5a40002008-08-09 01:19:43 +090010#include "build/build_config.h"
11
12#if defined(OS_WIN)
13#include <windows.h>
14#elif defined(OS_POSIX)
15#include <pthread.h>
16#endif
initial.commit3f4a7322008-07-27 06:49:38 +090017
brettw@chromium.orge439a962011-01-02 08:16:20 +090018namespace base {
19namespace internal {
20
initial.commit3f4a7322008-07-27 06:49:38 +090021// This class implements the underlying platform-specific spin-lock mechanism
22// used for the Lock class. Most users should not use LockImpl directly, but
23// should instead use Lock.
darin@chromium.orge585bed2011-08-06 00:34:00 +090024class BASE_EXPORT LockImpl {
initial.commit3f4a7322008-07-27 06:49:38 +090025 public:
mmentovai@google.comf5a40002008-08-09 01:19:43 +090026#if defined(OS_WIN)
robliaoe7ee1a62016-04-12 04:39:42 +090027 using NativeHandle = SRWLOCK;
mmentovai@google.comf5a40002008-08-09 01:19:43 +090028#elif defined(OS_POSIX)
robliaoe7ee1a62016-04-12 04:39:42 +090029 using NativeHandle = pthread_mutex_t;
mmentovai@google.comf5a40002008-08-09 01:19:43 +090030#endif
31
initial.commit3f4a7322008-07-27 06:49:38 +090032 LockImpl();
33 ~LockImpl();
34
35 // If the lock is not held, take it and return true. If the lock is already
36 // held by something else, immediately return false.
37 bool Try();
38
39 // Take the lock, blocking until it is available if necessary.
40 void Lock();
41
42 // Release the lock. This must only be called by the lock's holder: after
43 // a successful call to Try, or a call to Lock.
44 void Unlock();
45
cpu@chromium.orgb1eaabe2011-12-16 03:57:23 +090046 // Return the native underlying lock.
mmentovai@google.comf5a40002008-08-09 01:19:43 +090047 // TODO(awalker): refactor lock and condition variables so that this is
48 // unnecessary.
tfarina@chromium.orgbd138ea2013-09-14 14:27:08 +090049 NativeHandle* native_handle() { return &native_handle_; }
initial.commit3f4a7322008-07-27 06:49:38 +090050
mmentovai@google.comf5a40002008-08-09 01:19:43 +090051 private:
tfarina@chromium.orgbd138ea2013-09-14 14:27:08 +090052 NativeHandle native_handle_;
mmentovai@google.comf5a40002008-08-09 01:19:43 +090053
54 DISALLOW_COPY_AND_ASSIGN(LockImpl);
initial.commit3f4a7322008-07-27 06:49:38 +090055};
56
brettw@chromium.orge439a962011-01-02 08:16:20 +090057} // namespace internal
58} // namespace base
initial.commit3f4a7322008-07-27 06:49:38 +090059
brettw@chromium.orge439a962011-01-02 08:16:20 +090060#endif // BASE_SYNCHRONIZATION_LOCK_IMPL_H_