summaryrefslogtreecommitdiff
path: root/subprojects/boost-sqlite/include/boost/sqlite/mutex.hpp
blob: 63caf0e14f2e9131c5a0bd013a7245e4d3edfc6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BOOST_SQLITE_MUTEX_HPP
#define BOOST_SQLITE_MUTEX_HPP

#include <boost/sqlite/detail/config.hpp>
#include <memory>

BOOST_SQLITE_BEGIN_NAMESPACE
/// A mutex class that maybe a noop depending on the mode sqlite3 was compiled as.
struct mutex
{
  bool try_lock()
  {
    if (!impl_)
      return false;
    return sqlite3_mutex_try(impl_.get()) == SQLITE_OK;
  }
  void lock()   { sqlite3_mutex_enter(impl_.get()); }
  void unlock() { sqlite3_mutex_leave(impl_.get()); }

  mutex() : impl_(sqlite3_mutex_alloc(SQLITE_MUTEX_FAST)) {}
  mutex(mutex && ) = delete;
 private:
  struct deleter_ {void operator()(sqlite3_mutex *mtx) {sqlite3_mutex_free(mtx);}};
  std::unique_ptr<sqlite3_mutex, deleter_> impl_;
};

/// A recursive mutex class that maybe a noop depending on the mode sqlite3 was compiled as.
struct recursive_mutex
{
  bool try_lock()
  {
    if (!impl_)
      return false;
    return sqlite3_mutex_try(impl_.get()) == SQLITE_OK;
  }
  void lock()   { sqlite3_mutex_enter(impl_.get()); }
  void unlock() { sqlite3_mutex_leave(impl_.get()); }

  recursive_mutex() : impl_(sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE)) {}
  recursive_mutex(recursive_mutex && ) = delete;
 private:
  struct deleter_ {void operator()(sqlite3_mutex *mtx) {sqlite3_mutex_free(mtx);}};
  std::unique_ptr<sqlite3_mutex, deleter_> impl_;
};

BOOST_SQLITE_END_NAMESPACE

#endif //BOOST_SQLITE_MUTEX_HPP