Max OS 0.3
Loading...
Searching...
No Matches
ipc.cpp
Go to the documentation of this file.
1
12#include <processes/ipc.h>
13
14using namespace MaxOS;
15using namespace MaxOS::processes;
16using namespace MaxOS::common;
17using namespace MaxOS::memory;
18
19#include <common/logger.h>
20#include <processes/scheduler.h>
21
29SharedMemory::SharedMemory(const string& name, size_t size, resource_type_t type)
30: Resource(name, size, syscore::ResourceType::SHARED_MEMORY),
31 m_size(size)
32{
33
34 m_physical_address = (uintptr_t) PhysicalMemoryManager::s_current_manager->allocate_area(0, size);
35}
36
37SharedMemory::~SharedMemory() = default;
38
47int SharedMemory::read(void* buffer, size_t size, size_t flags) {
48
49 // Process hasn't opened the resource
50 auto it = m_mappings.find(GlobalScheduler::current_process()->pid());
51 if(it == m_mappings.end())
52 return 0;
53
54 return it->second;
55}
56
63
64 return m_physical_address;
65}
66
72size_t SharedMemory::size() const {
73
74 return m_size;
75}
76
82void SharedMemory::open(size_t flags) {
83
84 // Process has already opened this memory
85 auto it = m_mappings.find(GlobalScheduler::current_process()->pid());
86 if(it != m_mappings.end())
87 return;
88
89 auto virtual_address = (uintptr_t)GlobalScheduler::current_process()->memory_manager->vmm()->load_shared_memory(m_physical_address, m_size);
90 m_mappings.insert(GlobalScheduler::current_process()->pid(), virtual_address);
91
92}
93
99void SharedMemory::close(size_t flags) {
100
101 PhysicalMemoryManager::s_current_manager->free_area(m_physical_address, m_size);
102
103}
104
113: Resource(name, size, type)
114{
115
116}
117
122
123 // Free the messages
124 for(auto& message : m_queue)
125 delete message;
126}
127
136int SharedMessageEndpoint::read(void* buffer, size_t size, size_t flags) {
137
138 // Wait for a message
139 if(m_queue.empty())
140 return -1 * (int)resource_error_base_t::SHOULD_BLOCK;
141
142 // Read the message into the buffer
143 buffer_t* message = m_queue.pop_front();
144 size_t readable = size > message->capacity() ? message->capacity() : size;
145 memcpy(buffer, message -> raw(), readable);
146
147 return readable;
148}
149
158int SharedMessageEndpoint::write(void const* buffer, size_t size, size_t flags) {
159
160 m_message_lock.lock();
161
162 // Create the message
163 auto* new_message = new buffer_t(size);
164 new_message->copy_from(buffer, size);
165 m_queue.push_back(new_message);
166
167 m_message_lock.unlock();
168 return size;
169}
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
iterator find(Key)
Finds an element in the map based on the key.
Definition map.h:153
iterator end()
Returns the end of the map.
Definition map.h:141
void insert(Key, Value)
Updates the value of an element, or adds a new element if it does not exist.
Definition map.h:253
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
void lock()
Lock the spinlock once it is available.
Definition spinlock.cpp:24
void unlock()
Unlock the spinlock.
Definition spinlock.cpp:32
static PhysicalMemoryManager * s_current_manager
The current physical memory manager in use.
Definition physical.h:185
static Process * current_process()
Gets the process on the currently executing core.
Represents a generic resource that can be opened, closed, read from and written to.
Definition resource.h:29
size_t size() const
Gets the size of the shared memory block.
Definition ipc.cpp:72
int read(void *buffer, size_t size, size_t flags) final
Get the address that the current process has this shared memory mapped to.
Definition ipc.cpp:47
void close(size_t flags) final
Frees the page containing the shared memory.
Definition ipc.cpp:99
uintptr_t physical_address() const
Gets the physical address of the shared memory block.
Definition ipc.cpp:62
SharedMemory(const string &name, size_t size, resource_type_t type)
Creates a new shared memory block.
Definition ipc.cpp:29
void open(size_t flags) final
Map the shared memory into the address space of the owning process.
Definition ipc.cpp:82
int write(const void *buffer, size_t size, size_t flags) final
Handles writing to the message endpoint resource by writing the buffer as a message.
Definition ipc.cpp:158
int read(void *buffer, size_t size, size_t flags) final
Reads the first message from the endpoint or will yield until a message has been written.
Definition ipc.cpp:136
SharedMessageEndpoint(const string &name, size_t size, resource_type_t type)
Creates a new shared message endpoint.
Definition ipc.cpp:112
~SharedMessageEndpoint() final
Destroys the shared message endpoint and frees all messages.
Definition ipc.cpp:121
Defines inter-process communication (IPC) methods such as shared memory and message endpoints.
Defines a Logger class for logging messages with different severity levels to multiple output streams...
void * memcpy(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
Definition memoryIO.cpp:165
::syscore::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Definition resource.h:22
Defines a GlobalScheduler and Scheduler for managing processes and threads.