1// Copyright (c) 2013 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.
4
5#ifndef LIBRARIES_SDK_UTIL_SIMPLE_LOCK_H_
6#define LIBRARIES_SDK_UTIL_SIMPLE_LOCK_H_
7
8#include "pthread.h"
9#include "sdk_util/macros.h"
10
11namespace sdk_util {
12
13/*
14 * SimpleLock
15 *
16 * A pthread mutex object, with automatic initialization and destruction.
17 * Should be used with AutoLock where possible.
18 */
19class SimpleLock {
20 public:
21  SimpleLock() {
22    pthread_mutex_init(&lock_, NULL);
23  }
24
25  ~SimpleLock() {
26    pthread_mutex_destroy(&lock_);
27  }
28
29  void Lock() const   { pthread_mutex_lock(&lock_); }
30  void Unlock() const { pthread_mutex_unlock(&lock_); }
31
32  pthread_mutex_t* mutex() const { return &lock_; }
33
34 private:
35  mutable pthread_mutex_t lock_;
36
37  DISALLOW_COPY_AND_ASSIGN(SimpleLock);
38};
39
40}  // namespace sdk_util
41
42#endif  // LIBRARIES_SDK_UTIL_SIMPLE_LOCK_H_
43