ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the SQLite API.
Toggle main menu visibility
Loading...
Searching...
No Matches
impl
mutex_iface.hpp
1
/*
2
Copyright 2019 Eugene Gershnik
3
4
Use of this source code is governed by a BSD-style
5
license that can be found in the LICENSE file or at
6
https://github.com/gershnik/thinsqlitepp/blob/master/LICENSE
7
*/
8
9
#ifndef HEADER_SQLITEPP_MUTEX_IFACE_INCLUDED
10
#define HEADER_SQLITEPP_MUTEX_IFACE_INCLUDED
11
12
#include "handle.hpp"
13
14
namespace
thinsqlitepp
15
{
20
44
SQLITEPP_EXPORTED
45
class
mutex
final :
public
handle
<sqlite3_mutex, mutex>
46
{
47
public
:
49
enum
type
{
50
fast = SQLITE_MUTEX_FAST,
51
recursive = SQLITE_MUTEX_RECURSIVE
52
};
53
71
static
std::unique_ptr<mutex>
alloc
([[maybe_unused]]
type
t)
noexcept
72
{
73
#if !THINSQLITEPP_NO_MUTEX
74
return
std::unique_ptr<mutex>
(
from
(
sqlite3_mutex_alloc
(
int
(t))));
75
#else
76
return
{};
77
#endif
78
}
79
85
void
lock
() noexcept
86
{
87
#if !THINSQLITEPP_NO_MUTEX
88
sqlite3_mutex_enter
(
c_ptr
());
89
#endif
90
}
91
96
bool
try_lock
() noexcept
97
{
98
#if !THINSQLITEPP_NO_MUTEX
99
return
sqlite3_mutex_try
(
c_ptr
()) ==
SQLITE_OK
;
100
#else
101
return
true
;
102
#endif
103
}
104
110
void
unlock
() noexcept
111
{
112
#if !THINSQLITEPP_NO_MUTEX
113
sqlite3_mutex_leave
(
c_ptr
());
114
#endif
115
}
116
};
117
119
124
135
class
lock_adapter
136
{
137
public
:
139
lock_adapter
(
mutex
*
mutex
=
nullptr
) noexcept: _mutex(
mutex
)
140
{}
141
142
lock_adapter
(
const
std::unique_ptr<mutex>
&
mutex
)
noexcept
: _mutex(
mutex
.get())
143
{}
144
145
151
void
lock
() noexcept
152
{
153
#if !THINSQLITEPP_NO_MUTEX
154
sqlite3_mutex_enter
(c_ptr(_mutex));
155
#endif
156
}
157
163
bool
try_lock
() noexcept
164
{
165
#if !THINSQLITEPP_NO_MUTEX
166
return
sqlite3_mutex_try
(c_ptr(_mutex)) ==
SQLITE_OK
;
167
#else
168
return
true
;
169
#endif
170
}
171
177
void
unlock
() noexcept
178
{
179
#if !THINSQLITEPP_NO_MUTEX
180
sqlite3_mutex_leave
(c_ptr(_mutex));
181
#endif
182
}
183
private
:
184
mutex
* _mutex;
185
};
186
188
}
189
190
#endif
191
thinsqlitepp::handle< sqlite3_mutex, mutex >::c_ptr
sqlite3_mutex * c_ptr() const noexcept
Definition
handle.hpp:45
thinsqlitepp::handle< sqlite3_mutex, mutex >::handle
handle()=delete
thinsqlitepp::handle< sqlite3_mutex, mutex >::from
static mutex * from(sqlite3_mutex *obj) noexcept
Definition
handle.hpp:41
thinsqlitepp::lock_adapter::unlock
void unlock() noexcept
Unlock the mutex.
Definition
mutex_iface.hpp:177
thinsqlitepp::lock_adapter::lock
void lock() noexcept
Lock the mutex.
Definition
mutex_iface.hpp:151
thinsqlitepp::lock_adapter::lock_adapter
lock_adapter(mutex *mutex=nullptr) noexcept
Adapt a mutex pointer.
Definition
mutex_iface.hpp:139
thinsqlitepp::lock_adapter::try_lock
bool try_lock() noexcept
Try to lock the mutex.
Definition
mutex_iface.hpp:163
thinsqlitepp::lock_adapter::lock_adapter
lock_adapter(const std::unique_ptr< mutex > &mutex) noexcept
Adapt a std::unique_ptr<mutex>.
Definition
mutex_iface.hpp:142
thinsqlitepp::mutex
SQLite Mutex.
Definition
mutex_iface.hpp:46
thinsqlitepp::mutex::unlock
void unlock() noexcept
Unlock the mutex.
Definition
mutex_iface.hpp:110
thinsqlitepp::mutex::lock
void lock() noexcept
Lock the mutex.
Definition
mutex_iface.hpp:85
thinsqlitepp::mutex::alloc
static std::unique_ptr< mutex > alloc(type t) noexcept
Allocate a new mutex.
Definition
mutex_iface.hpp:71
thinsqlitepp::mutex::type
type
Type of mutex to allocate for alloc().
Definition
mutex_iface.hpp:49
thinsqlitepp::mutex::try_lock
bool try_lock() noexcept
Try to lock the mutex.
Definition
mutex_iface.hpp:96
sqlite3_mutex_alloc
sqlite3_mutex_alloc
thinsqlitepp
ThinSQLite++ namespace.
Definition
backup_iface.hpp:17
SQLITE_OK
#define SQLITE_OK
std::unique_ptr
Generated by
1.17.0