boost_sqlite 1
A sqlite C++ library
Loading...
Searching...
No Matches
mutex.hpp
1//
2// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7
8#ifndef BOOST_SQLITE_MUTEX_HPP
9#define BOOST_SQLITE_MUTEX_HPP
10
11#include <boost/sqlite/detail/config.hpp>
12#include <memory>
13
14BOOST_SQLITE_BEGIN_NAMESPACE
16struct mutex
17{
18 bool try_lock()
19 {
20 if (!impl_)
21 return false;
22 return sqlite3_mutex_try(impl_.get()) == SQLITE_OK;
23 }
24 void lock() { sqlite3_mutex_enter(impl_.get()); }
25 void unlock() { sqlite3_mutex_leave(impl_.get()); }
26
27 mutex() : impl_(sqlite3_mutex_alloc(SQLITE_MUTEX_FAST)) {}
28 mutex(mutex && ) = delete;
29 private:
30 struct deleter_ {void operator()(sqlite3_mutex *mtx) {sqlite3_mutex_free(mtx);}};
31 std::unique_ptr<sqlite3_mutex, deleter_> impl_;
32};
33
36{
37 bool try_lock()
38 {
39 if (!impl_)
40 return false;
41 return sqlite3_mutex_try(impl_.get()) == SQLITE_OK;
42 }
43 void lock() { sqlite3_mutex_enter(impl_.get()); }
44 void unlock() { sqlite3_mutex_leave(impl_.get()); }
45
46 recursive_mutex() : impl_(sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE)) {}
47 recursive_mutex(recursive_mutex && ) = delete;
48 private:
49 struct deleter_ {void operator()(sqlite3_mutex *mtx) {sqlite3_mutex_free(mtx);}};
50 std::unique_ptr<sqlite3_mutex, deleter_> impl_;
51};
52
53BOOST_SQLITE_END_NAMESPACE
54
55#endif //BOOST_SQLITE_MUTEX_HPP
A mutex class that maybe a noop depending on the mode sqlite3 was compiled as.
Definition mutex.hpp:17
A recursive mutex class that maybe a noop depending on the mode sqlite3 was compiled as.
Definition mutex.hpp:36