ThinSQLite++
A thin, safe and convenient modern C++ wrapper for 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/main/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{
44 class mutex final : public handle<sqlite3_mutex, mutex>
45 {
46 public:
48 enum type {
49 fast = SQLITE_MUTEX_FAST,
50 recursive = SQLITE_MUTEX_RECURSIVE
51 };
52
72
78 void lock() noexcept
85 bool try_lock() noexcept
86 { return sqlite3_mutex_try(c_ptr()) == SQLITE_OK; }
87
93 void unlock() noexcept
95 };
96
115 {
116 public:
118 lock_adapter(mutex * mutex = nullptr) noexcept: _mutex(mutex)
119 {}
121 lock_adapter(const std::unique_ptr<mutex> & mutex) noexcept: _mutex(mutex.get())
122 {}
123
124
130 void lock() noexcept
131 { sqlite3_mutex_enter(c_ptr(_mutex)); }
132
138 bool try_lock() noexcept
139 { return sqlite3_mutex_try(c_ptr(_mutex)) == SQLITE_OK; }
140
146 void unlock() noexcept
147 { sqlite3_mutex_leave(c_ptr(_mutex)); }
148 private:
149 mutex * _mutex;
150 };
151
153}
154
155#endif
156
Base functionality for all fake wrapper classes
Definition handle.hpp:27
sqlite3_mutex * c_ptr() const noexcept
Definition handle.hpp:45
static mutex * from(sqlite3_mutex *obj) noexcept
Definition handle.hpp:41
A mutex adapter for Lockable concept that works with null and non-null mutexes.
Definition mutex_iface.hpp:115
void unlock() noexcept
Unlock the mutex.
Definition mutex_iface.hpp:146
void lock() noexcept
Lock the mutex.
Definition mutex_iface.hpp:130
lock_adapter(mutex *mutex=nullptr) noexcept
Adapt a mutex pointer.
Definition mutex_iface.hpp:118
bool try_lock() noexcept
Try to lock the mutex.
Definition mutex_iface.hpp:138
lock_adapter(const std::unique_ptr< mutex > &mutex) noexcept
Adapt a std::unique_ptr<mutex>
Definition mutex_iface.hpp:121
SQLite Mutex.
Definition mutex_iface.hpp:45
void unlock() noexcept
Unlock the mutex.
Definition mutex_iface.hpp:93
void lock() noexcept
Lock the mutex.
Definition mutex_iface.hpp:78
static std::unique_ptr< mutex > alloc(type t) noexcept
Allocate a new mutex.
Definition mutex_iface.hpp:70
type
Type of mutex to allocate for alloc()
Definition mutex_iface.hpp:48
bool try_lock() noexcept
Try to lock the mutex.
Definition mutex_iface.hpp:85
sqlite3_mutex_alloc
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
#define SQLITE_OK