-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsem_and_queue.hpp
More file actions
62 lines (40 loc) · 1.11 KB
/
sem_and_queue.hpp
File metadata and controls
62 lines (40 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <optional>
#define MSG_SIZE 22
#define PERM 0666
class MessageQueue {
public:
MessageQueue() = default;
explicit MessageQueue(key_t key);
MessageQueue(const MessageQueue& other) noexcept;
~MessageQueue() = default;
MessageQueue& operator=(const MessageQueue& other) noexcept;
std::optional<std::pair<int, int>> Receive(int64_t msg_type, bool wait);
void Send(std::pair<int, int> message, int64_t msg_type);
void DeleteQueue() noexcept;
bool IsOwner() noexcept;
private:
int descriptor_;
bool owner_ = false;
struct MsgBuf {
int64_t type;
char msg[MSG_SIZE];
};
};
class Semaphore {
public:
Semaphore() = default;
explicit Semaphore(uint8_t num_of_sems, key_t key);
Semaphore(const Semaphore& other) noexcept;
~Semaphore() = default;
Semaphore& operator=(const Semaphore& other) noexcept;
bool Operation(uint8_t sem_index, short operation, bool wait);
bool IsZero(uint8_t sem_index, bool wait);
void DeleteSem() noexcept;
private:
int descriptor_;
uint8_t num_of_sems_;
};