Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rwlock (and bswap8 because oops) #38

Merged
merged 3 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions substrate/bits
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
namespace substrate
{
/* endian swapping facilities */
SUBSTRATE_NO_DISCARD(inline constexpr uint8_t swap8(const uint8_t x) noexcept)
{
return uint8_t(
((x & 0x0FU) << 4U) |
((x & 0xF0U) >> 4U)
);
}

SUBSTRATE_NO_DISCARD(inline constexpr uint16_t swap16(const uint16_t x) noexcept)
{
return uint16_t(
Expand Down
55 changes: 55 additions & 0 deletions substrate/rwlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SUBSTRATE_RWLOCK
#define SUBSTRATE_RWLOCK

#if __cplusplus < 201703L
#error "rwlock is on available on C++17 and above"
#endif

#include <substrate/internal/defs>

#include <utility>
#include <mutex>
#include <shared_mutex>

namespace substrate
{
template<typename T>
struct rwlock_t final
{
private:
template<typename U, template<typename> typename lock_t>
struct lockResult_t final
{
private:
lock_t<std::shared_mutex> _lock;
U &_obj;

public:
constexpr lockResult_t(std::shared_mutex &mut, U &obj) noexcept :
_lock{mut}, _obj{obj} { }

SUBSTRATE_NO_DISCARD(U *operator ->() noexcept) { return &_obj; }
SUBSTRATE_NO_DISCARD(U &operator *() noexcept) { return _obj; }
};
std::shared_mutex _mutex{};
T _obj;

public:
template<typename ...args_t>
constexpr rwlock_t(args_t &&...args) noexcept :
_obj{std::forward<args_t>(args)...} { }

SUBSTRATE_NO_DISCARD(lockResult_t<const T, std::shared_lock> read() noexcept)
{
return {_mutex, _obj};
}
SUBSTRATE_NO_DISCARD(lockResult_t<T, std::unique_lock> write() noexcept)
{
return {_mutex, _obj};
}
};
}

#endif /* SUBSTRATE_RWLOCK */
/* vim: set ft=cpp ts=4 sw=4 noexpandtab: */
9 changes: 9 additions & 0 deletions test/bits.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,20 @@ TEST_CASE("shift nibble", "[bits]")
REQUIRE(shift_nibble<decltype(value2)>(value2, 16U) == 0x0123456789ABCDEFLLU);
}

using substrate::swap8;
using substrate::swap16;
using substrate::swap32;
using substrate::swap64;
TEST_CASE("bswap tests", "[bits]")
{
SECTION("bswap8")
{
REQUIRE(swap8(0x34U) == 0x43U);
REQUIRE(swap8(0x12U) == 0x21U);
REQUIRE(swap8(0x24U) == 0x42U);
REQUIRE(swap8(0x23U) == 0x32U);
}

SECTION("bswap16")
{
REQUIRE(swap16(0x3412U) == 0x1234U);
Expand Down
Loading