-
-
Save roxlu/afd4535bd6401184d17e to your computer and use it in GitHub Desktop.
Windows (work in progress) shared memory through memory mapped files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MemoryMappedFile.h" | |
MemoryMappedFile::MemoryMappedFile() | |
:handle(NULL) | |
,buffer(NULL) | |
{ | |
} | |
MemoryMappedFile::~MemoryMappedFile() { | |
if (NULL != buffer) { | |
if (0 == UnmapViewOfFile(buffer)) { | |
printf("error: failed to close the view of file (memory mapped file0: %d)\n", GetLastError()); | |
} | |
buffer = NULL; | |
} | |
if (NULL != handle) { | |
if (0 == CloseHandle(handle)) { | |
printf("error: failed to close the handle: %d (not supposed to happen).\n", GetLastError()); | |
} | |
handle = NULL; | |
} | |
} | |
int MemoryMappedFile::open(std::string name, size_t len) { | |
std::string shared_name = "Local\\" +name; | |
handle = CreateFileMapping(INVALID_HANDLE_VALUE, | |
NULL, | |
PAGE_READWRITE, | |
0, | |
len, | |
shared_name.c_str()); | |
if (NULL == handle) { | |
printf("error: failed to create the file mapping: %d.\n", GetLastError()); | |
return -1; | |
} | |
buffer = (uint8_t*) MapViewOfFile(handle, | |
FILE_MAP_ALL_ACCESS, | |
0, | |
0, | |
len); | |
if (NULL == buffer) { | |
printf("error: failed to create the view for the memory mapped file: %d.\n", GetLastError()); | |
CloseHandle(handle); | |
handle = NULL; | |
return -2; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MMF_H | |
#define MMF_H | |
#include <windows.h> | |
#include <string> | |
#include <stdint.h> | |
class MemoryMappedFile { | |
public: | |
MemoryMappedFile(); | |
~MemoryMappedFile(); | |
int open(std::string name, size_t len); | |
public: | |
HANDLE handle; | |
uint8_t* buffer; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Threading.h" | |
int semaphore_open(semaphore* s, const char* name, int initialCount, int maxCount) { | |
if (NULL == s) { | |
return -1; | |
} | |
if (NULL == name) { | |
return -2; | |
} | |
if (maxCount < initialCount) { | |
return -3; | |
} | |
std::string semname = "Local\\"; | |
semname.append(name); | |
s->handle = CreateSemaphore(NULL, (LONG)initialCount, (LONG)maxCount, semname.c_str()); | |
if (NULL == s->handle) { | |
return -4; | |
} | |
return 0; | |
} | |
int semaphore_close(semaphore* s) { | |
if (NULL == s) { | |
return -1; | |
} | |
if (NULL == s->handle) { | |
return -2; | |
} | |
if (0 != CloseHandle(s->handle)) { | |
return -3; | |
} | |
return 0; | |
} | |
int semaphore_wait(semaphore* s) { | |
if (NULL == s) { | |
return -1; | |
} | |
if (NULL == s->handle) { | |
return -2; | |
} | |
WaitForSingleObject(s->handle, INFINITE); | |
return 0; | |
} | |
int semaphore_post(semaphore* s) { | |
if (NULL == s) { | |
return -1; | |
} | |
if (NULL == s->handle) { | |
return -2; | |
} | |
ReleaseSemaphore(s->handle, 1, NULL); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MEM_THREAD_H | |
#define MEM_THREAD_H | |
#include <windows.h> | |
#include <string> | |
struct semaphore { | |
HANDLE handle; | |
}; | |
int semaphore_open(semaphore* s, const char* name, int initialCount = 0, int maxCount = 100); | |
int semaphore_close(semaphore* s); | |
int semaphore_wait(semaphore* s); | |
int semaphore_post(semaphore* s); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment