ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the SQLite API.
Loading...
Searching...
No Matches
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
14namespace 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
75 #else
76 return {};
77 #endif
78 }
79
85 void lock() noexcept
86 {
87 #if !THINSQLITEPP_NO_MUTEX
89 #endif
90 }
91
96 bool try_lock() noexcept
97 {
98 #if !THINSQLITEPP_NO_MUTEX
100 #else
101 return true;
102 #endif
103 }
104
110 void unlock() noexcept
111 {
112 #if !THINSQLITEPP_NO_MUTEX
114 #endif
115 }
116 };
117
119
124
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
sqlite3_mutex * c_ptr() const noexcept
Definition handle.hpp:45
static mutex * from(sqlite3_mutex *obj) noexcept
Definition handle.hpp:41
void unlock() noexcept
Unlock the mutex.
Definition mutex_iface.hpp:177
void lock() noexcept
Lock the mutex.
Definition mutex_iface.hpp:151
lock_adapter(mutex *mutex=nullptr) noexcept
Adapt a mutex pointer.
Definition mutex_iface.hpp:139
bool try_lock() noexcept
Try to lock the mutex.
Definition mutex_iface.hpp:163
lock_adapter(const std::unique_ptr< mutex > &mutex) noexcept
Adapt a std::unique_ptr<mutex>.
Definition mutex_iface.hpp:142
SQLite Mutex.
Definition mutex_iface.hpp:46
void unlock() noexcept
Unlock the mutex.
Definition mutex_iface.hpp:110
void lock() noexcept
Lock the mutex.
Definition mutex_iface.hpp:85
static std::unique_ptr< mutex > alloc(type t) noexcept
Allocate a new mutex.
Definition mutex_iface.hpp:71
type
Type of mutex to allocate for alloc().
Definition mutex_iface.hpp:49
bool try_lock() noexcept
Try to lock the mutex.
Definition mutex_iface.hpp:96
sqlite3_mutex_alloc
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
#define SQLITE_OK