Motion Master 6.0.0-alpha.111
Next-generation motion control software
Loading...
Searching...
No Matches
control_plane_locks.h
Go to the documentation of this file.
1#pragma once
2
3#include <condition_variable>
4#include <cstddef>
5#include <cstdint>
6#include <memory>
7#include <mutex>
8#include <semaphore>
9#include <shared_mutex>
10
11namespace mm::comm {
12
23class BusLock {
24 public:
25 void lock();
26 void unlock();
27 void lock_shared();
28 void unlock_shared();
29
30 private:
31 std::mutex mutex_;
32 std::condition_variable readerCanEnter_;
33 std::condition_variable writerCanEnter_;
34 int readers_ = 0;
35 int writersWaiting_ = 0;
36 bool writer_ = false;
37};
38
45class FrameSlot {
46 public:
47 explicit FrameSlot(std::counting_semaphore<>& slots);
48 ~FrameSlot();
49 FrameSlot(FrameSlot&& other) noexcept;
50 FrameSlot(const FrameSlot&) = delete;
51 FrameSlot& operator=(const FrameSlot&) = delete;
53
54 private:
55 std::counting_semaphore<>* slots_;
56};
57
61struct DriveLock {
62 std::shared_lock<BusLock> bus;
63 std::unique_lock<std::mutex> drive;
65};
66
68struct BusIoLock {
69 std::shared_lock<BusLock> bus;
71};
72
88 public:
102 static constexpr std::ptrdiff_t kFrameSlots = 8;
103
106 explicit ControlPlaneLocks(std::size_t positions);
107
109 [[nodiscard]] std::unique_lock<BusLock> lockBus();
110
113 [[nodiscard]] std::shared_lock<BusLock> lockBusShared();
114
117 [[nodiscard]] BusIoLock lockBusIo();
118
120 [[nodiscard]] DriveLock lockDrive(uint16_t position);
121
122 private:
123 BusLock bus_;
124 std::unique_ptr<std::mutex[]> drives_;
125 std::counting_semaphore<> frameSlots_{kFrameSlots};
126};
127
128} // namespace mm::comm
A reader/writer lock that lets no new reader in while a writer waits.
Definition control_plane_locks.h:23
void unlock()
Definition control_plane_locks.cc:22
void lock_shared()
Definition control_plane_locks.cc:32
void unlock_shared()
Definition control_plane_locks.cc:38
void lock()
Definition control_plane_locks.cc:14
The control-plane locks of one fieldbus driver: one for the bus, one per drive.
Definition control_plane_locks.h:87
std::shared_lock< BusLock > lockBusShared()
Excludes only work on the whole bus. For reads of what a scan or a re-map produced,...
Definition control_plane_locks.cc:65
static constexpr std::ptrdiff_t kFrameSlots
Definition control_plane_locks.h:102
std::unique_lock< BusLock > lockBus()
Excludes all other control-plane work. For work on the whole bus.
Definition control_plane_locks.cc:63
DriveLock lockDrive(uint16_t position)
Excludes work on the whole bus and other work on the same drive.
Definition control_plane_locks.cc:74
BusIoLock lockBusIo()
Excludes only work on the whole bus, and takes a frame slot. For reads that send frames to several dr...
Definition control_plane_locks.cc:69
One of a fixed number of slots for a frame on the wire. Released when destroyed.
Definition control_plane_locks.h:45
FrameSlot(const FrameSlot &)=delete
~FrameSlot()
Definition control_plane_locks.cc:52
FrameSlot & operator=(const FrameSlot &)=delete
FrameSlot & operator=(FrameSlot &&)=delete
Definition al_status_codes.cc:5
The bus lock held shared, and a frame slot. The slot goes first.
Definition control_plane_locks.h:68
FrameSlot frame
Definition control_plane_locks.h:70
std::shared_lock< BusLock > bus
Definition control_plane_locks.h:69
The bus lock held shared, the lock of one drive, and a frame slot.
Definition control_plane_locks.h:61
std::shared_lock< BusLock > bus
Definition control_plane_locks.h:62
std::unique_lock< std::mutex > drive
Definition control_plane_locks.h:63
FrameSlot frame
Definition control_plane_locks.h:64