Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created November 21, 2014 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roxlu/afd4535bd6401184d17e to your computer and use it in GitHub Desktop.
Save roxlu/afd4535bd6401184d17e to your computer and use it in GitHub Desktop.
Windows (work in progress) shared memory through memory mapped files.
#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;
}
#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
#include <stdio.h>
#include <stdlib.h>
#include "MemoryMappedFile.h"
#include "Threading.h"
int main() {
printf("\n\ntest_shared_mem_client\n\n");
MemoryMappedFile mem;
semaphore sem;
if (0 != semaphore_open(&sem, "mysem", 0, 100)) {
printf("error: failed to open the semaphore.\n");
exit(EXIT_FAILURE);
}
if (0 != mem.open("test", 1024)) {
printf("error: failed to open the memory mapped file.\n");
exit(EXIT_FAILURE);
}
int count = 0;
while (true) {
//semaphore_wait(&sem);
mem.buffer[0] = 'a' + count;
printf("%04d: sending: %c\n", count, mem.buffer[0]);
semaphore_post(&sem);
Sleep(1000);
count++;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include "MemoryMappedFile.h"
#include "Threading.h"
int main() {
printf("\n\ntest_shared_mem\n\n");
MemoryMappedFile mem;
semaphore sem;
if (0 != semaphore_open(&sem, "mysem", 0, 100)) {
printf("error: failed to open the semaphore.\n");
exit(EXIT_FAILURE);
}
if (0 != mem.open("test", 1024)) {
printf("error: failed to open the memory mapped file.\n");
exit(EXIT_FAILURE);
}
int count = 0;
while (true) {
semaphore_wait(&sem);
printf("%04d: received: %c\n", count, mem.buffer[0]);
++count;
}
return 0;
}
#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;
}
#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