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::processes::resources;
17using namespace MaxOS::common;
18using namespace MaxOS::memory;
19
20#include <common/logger.h>
21#include <processes/scheduler.h>
22
30SharedMemory::SharedMemory(const string& name, size_t size, resource_type_t type)
31: Resource(name, size, MaxOS::KPI::ResourceType::SHARED_MEMORY),
32 m_size(size)
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
135void SharedMessageEndpoint::send(const ipc_iovec_t *vec, size_t count) {
136
137 m_message_lock.lock();
138
139 // Parse the message parts
140 size_t size = 0;
141 for (size_t i = 0; i < count; ++i)
142 size += vec[i].length;
143
144 // Create the message
145 auto* new_message = new buffer_t(size);
146 for (size_t i = 0; i < count; ++i)
147 new_message->copy_from(vec[i].address, vec[i].length);
148
149 // Send the message
150 m_queue.push_back(new_message);
151 m_message_lock.unlock();
152
153}
154
165int SharedMessageEndpoint::read(void* buffer, size_t size, size_t flags) {
166
167 // Wait for a message
168 while(m_queue.empty()) {
169 if (flags)
170 return 0;
171
172 // Block
174 }
175
176 // Read the message into the buffer
177 buffer_t* message = m_queue.pop_front();
178 size_t readable = size > message->capacity() ? message->capacity() : size;
179 memcpy(buffer, message -> raw(), readable);
180
181 //@todo free the message mem here now aswell?
182
183 return readable;
184}
185
194int SharedMessageEndpoint::write(void const* buffer, size_t size, size_t flags) {
195
196 // Send the buffer as the entire message
197 ipc_iovec_t message = {buffer, size};
198 send(&message, 1);
199
200 return size;
201}
static PhysicalMemoryManager * s_current_manager
The current physical memory manager in use.
Definition physical.h:183
void free_area(uint64_t start_address, size_t size)
Frees an area of physical memory.
Definition physical.cpp:341
void * load_shared_memory(const string &name)
Load shared memory into the VMM's address space.
Definition virtual.cpp:487
void lock()
Lock the spinlock once it is available, sleeping until other processes are done with it.
Definition block.cpp:23
void unlock()
Unlock the spinlock.
Definition block.cpp:30
static Process * current_process()
Gets the process on the currently executing core.
static Thread * current_thread()
Gets the thread on the currently executing core.
uint64_t pid() const
Gets the pid of the process.
Definition process.cpp:429
memory::MemoryManager * memory_manager
The manager for memory used by this process.
Definition process.h:121
size_t size() const
Gets the size of the shared memory block.
Definition ipc.cpp:72
SharedMemory(const string &name, size_t size, resources::resource_type_t type)
Creates a new shared memory block.
Definition ipc.cpp:30
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
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:194
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:165
SharedMessageEndpoint(const string &name, size_t size, resources::resource_type_t type)
Creates a new shared message endpoint.
Definition ipc.cpp:112
void send(const ipc_iovec_t *vec, size_t count)
Combines a multipart message and sends it to the endpoint.
Definition ipc.cpp:135
~SharedMessageEndpoint() final
Destroys the shared message endpoint and frees all messages.
Definition ipc.cpp:121
void yield()
Pass execution to the next ready thread.
Definition process.cpp:103
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...
KPI::Resource Resource
Alias to make the libsyscore Resource accessible here.
Definition resource.h:26
KPI::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Definition resource.h:25
Defines a GlobalScheduler and Scheduler for managing processes and threads.
Read/Write call entry for a list of I/O requests in a RPC call.
Definition ipc.h:34