13f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
53f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#ifndef BASE_SYNCHRONIZATION_LOCK_IMPL_H_
63f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#define BASE_SYNCHRONIZATION_LOCK_IMPL_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "build/build_config.h"
10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <windows.h>
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX)
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <pthread.h>
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
174154342345fc67972570f59528233826cd397f1eKristian Monsen#include "base/base_api.h"
18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h"
19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
203f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsennamespace base {
213f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsennamespace internal {
223f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This class implements the underlying platform-specific spin-lock mechanism
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// used for the Lock class.  Most users should not use LockImpl directly, but
25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// should instead use Lock.
264154342345fc67972570f59528233826cd397f1eKristian Monsenclass BASE_API LockImpl {
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  typedef CRITICAL_SECTION OSLockType;
30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX)
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  typedef pthread_mutex_t OSLockType;
32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  LockImpl();
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ~LockImpl();
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // If the lock is not held, take it and return true.  If the lock is already
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // held by something else, immediately return false.
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool Try();
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Take the lock, blocking until it is available if necessary.
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void Lock();
43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Release the lock.  This must only be called by the lock's holder: after
45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // a successful call to Try, or a call to Lock.
46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void Unlock();
47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Return the native underlying lock.  Not supported for Windows builds.
49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // TODO(awalker): refactor lock and condition variables so that this is
50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // unnecessary.
51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if !defined(OS_WIN)
52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  OSLockType* os_lock() { return &os_lock_; }
53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  OSLockType os_lock_;
57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(LockImpl);
59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
613f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen}  // namespace internal
623f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen}  // namespace base
63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
643f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#endif  // BASE_SYNCHRONIZATION_LOCK_IMPL_H_
65