brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 5 | #ifndef BASE_SYNCHRONIZATION_LOCK_IMPL_H_ |
| 6 | #define BASE_SYNCHRONIZATION_LOCK_IMPL_H_ |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 7 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 8 | #include "base/base_export.h" |
| 9 | #include "base/macros.h" |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 10 | #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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 17 | |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 18 | namespace base { |
| 19 | namespace internal { |
| 20 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 21 | // 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.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 24 | class BASE_EXPORT LockImpl { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 25 | public: |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 26 | #if defined(OS_WIN) |
robliao | e7ee1a6 | 2016-04-12 04:39:42 +0900 | [diff] [blame] | 27 | using NativeHandle = SRWLOCK; |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 28 | #elif defined(OS_POSIX) |
robliao | e7ee1a6 | 2016-04-12 04:39:42 +0900 | [diff] [blame] | 29 | using NativeHandle = pthread_mutex_t; |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 30 | #endif |
| 31 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 32 | 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.org | b1eaabe | 2011-12-16 03:57:23 +0900 | [diff] [blame] | 46 | // Return the native underlying lock. |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 47 | // TODO(awalker): refactor lock and condition variables so that this is |
| 48 | // unnecessary. |
tfarina@chromium.org | bd138ea | 2013-09-14 14:27:08 +0900 | [diff] [blame] | 49 | NativeHandle* native_handle() { return &native_handle_; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 50 | |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 51 | private: |
tfarina@chromium.org | bd138ea | 2013-09-14 14:27:08 +0900 | [diff] [blame] | 52 | NativeHandle native_handle_; |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(LockImpl); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 55 | }; |
| 56 | |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 57 | } // namespace internal |
| 58 | } // namespace base |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 59 | |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 60 | #endif // BASE_SYNCHRONIZATION_LOCK_IMPL_H_ |