library/src/internal.cpp

50 lines
1.0 KiB
C++
Raw Normal View History

#include "internal.h"
2019-11-04 23:17:15 +01:00
#include <sys/shm.h>
2019-11-06 12:47:34 +01:00
#include <csignal>
2019-11-04 23:17:15 +01:00
Impl impl;
2019-11-04 23:17:15 +01:00
void *getSharedMemory(){
impl.sharedMemorySize = 8000;
2019-11-06 12:47:34 +01:00
auto id = shmget(impl.sharedMemoryKey, impl.sharedMemorySize, 0644u | IPC_CREAT);
2019-11-04 23:17:15 +01:00
if(id == -1){
//error
impl.sharedMemorySize = 0;
return nullptr;
}
2019-11-04 23:17:15 +01:00
void *data = shmat(id, nullptr, 0);
if(data == (char*)(-1)){
//error
impl.sharedMemorySize = 0;
return nullptr;
}
return data;
}
int getSharedMemorySize(){
return impl.sharedMemorySize;
}
2019-11-06 12:47:34 +01:00
void sendSignal(pid_t pid, int signalNumber) {
kill(pid, signalNumber);
}
void onSignal(int signalNumber, void(*callback)(int)) {
signal(signalNumber, callback);
}
2019-11-06 13:41:24 +01:00
InterruptEntry *getInterrupTable(){
return (InterruptEntry*)((char*)getSharedMemory() + sizeof(Registers) + 1024/*reserved*/);
}
bool callEvent(EventType type) {
auto ivt = getInterrupTable();
if(ivt[type].pid != 0){
sendSignal(ivt[type].pid, ivt[type].signum);
}
return true;
}