Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed work with Shared Memory on Windows. #17273

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions TSRM/tsrm_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,20 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
CloseHandle(shm_handle);
return -1;
}
shm->segment = shm_handle;
if (NULL == shm->segment || shm->segment == INVALID_HANDLE_VALUE) {
CoderAImen marked this conversation as resolved.
Show resolved Hide resolved
shm->segment = shm_handle;
shm->created = created;
}
else if (created == TRUE) {
CloseHandle(shm->segment);
shm->segment = shm_handle;
shm->created = TRUE;
}
else {
CloseHandle(shm_handle);
}
if(shm->descriptor==NULL)shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
CoderAImen marked this conversation as resolved.
Show resolved Hide resolved

shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);

if (NULL != shm->descriptor && created) {
Expand Down Expand Up @@ -730,6 +743,7 @@ TSRM_API void *shmat(int key, const void *shmaddr, int flags)
shm->descriptor->shm_atime = time(NULL);
shm->descriptor->shm_lpid = getpid();
shm->descriptor->shm_nattch++;
shm->shm_nattch++;

return shm->addr;
}/*}}}*/
Expand All @@ -746,12 +760,20 @@ TSRM_API int shmdt(const void *shmaddr)
shm->descriptor->shm_dtime = time(NULL);
shm->descriptor->shm_lpid = getpid();
shm->descriptor->shm_nattch--;
shm->shm_nattch--;

ret = 0;
if (shm->descriptor->shm_nattch <= 0) {
ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1;
shm->descriptor = NULL;
if (shm->shm_nattch <= 0) {
shm->shm_nattch = 0;
if (NULL != shm->segment && shm->segment != INVALID_HANDLE_VALUE && shm->created==FALSE) {
ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1;
shm->descriptor = NULL;
CloseHandle(shm->segment);
shm->segment = INVALID_HANDLE_VALUE;
shm->addr = NULL;
}
}

return ret;
}/*}}}*/

Expand Down
2 changes: 2 additions & 0 deletions TSRM/tsrm_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ typedef struct {
typedef struct {
void *addr;
HANDLE segment;
BOOL created;
int shm_nattch;
struct shmid_ds *descriptor;
} shm_pair;

Expand Down
Loading