Implement CCLite 0.1.2 IPC primitives
This commit is contained in:
@@ -13,7 +13,8 @@ module ccl_errors
|
||||
integer, parameter :: CCL_ERROR_IPC_OPEN_FAILED = CCL_ERROR_IPC_BASE + 2
|
||||
integer, parameter :: CCL_ERROR_IPC_MAP_FAILED = CCL_ERROR_IPC_BASE + 3
|
||||
integer, parameter :: CCL_ERROR_IPC_TIMEOUT = CCL_ERROR_IPC_BASE + 4
|
||||
integer, parameter :: CCL_ERROR_IPC_BAD_HEADER = CCL_ERROR_IPC_BASE + 5
|
||||
integer, parameter :: CCL_ERROR_IPC_BAD_HANDLE = CCL_ERROR_IPC_BASE + 5
|
||||
integer, parameter :: CCL_ERROR_IPC_BAD_HEADER = CCL_ERROR_IPC_BASE + 6
|
||||
|
||||
integer, parameter :: CCL_ERROR_WORKER_BASE = 3000
|
||||
integer, parameter :: CCL_ERROR_WORKER_START_FAILED = CCL_ERROR_WORKER_BASE + 1
|
||||
|
||||
@@ -3,8 +3,8 @@ module ccl_version
|
||||
|
||||
integer, parameter :: CCL_VERSION_MAJOR = 0
|
||||
integer, parameter :: CCL_VERSION_MINOR = 1
|
||||
integer, parameter :: CCL_VERSION_PATCH = 1
|
||||
character(len=*), parameter :: CCL_VERSION_LABEL = "0.1.1"
|
||||
integer, parameter :: CCL_VERSION_PATCH = 2
|
||||
character(len=*), parameter :: CCL_VERSION_LABEL = "0.1.2"
|
||||
|
||||
contains
|
||||
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
module ccl_ipc
|
||||
use iso_c_binding, only: c_char, c_int, c_ptr, c_size_t, c_associated, c_null_char, c_null_ptr
|
||||
implicit none
|
||||
|
||||
type :: CCL_Shared_Memory
|
||||
type(c_ptr) :: native = c_null_ptr
|
||||
type(c_ptr) :: payload = c_null_ptr
|
||||
integer(c_size_t) :: byte_size = 0_c_size_t
|
||||
end type CCL_Shared_Memory
|
||||
|
||||
type :: CCL_Named_Event
|
||||
type(c_ptr) :: native = c_null_ptr
|
||||
end type CCL_Named_Event
|
||||
|
||||
interface
|
||||
integer(c_int) function ccl_ipc_shm_create_c(name, byte_size, out_handle, out_payload) &
|
||||
bind(C, name="ccl_ipc_shm_create")
|
||||
import :: c_char, c_int, c_ptr, c_size_t
|
||||
character(kind=c_char), intent(in) :: name(*)
|
||||
integer(c_size_t), value :: byte_size
|
||||
type(c_ptr), intent(out) :: out_handle
|
||||
type(c_ptr), intent(out) :: out_payload
|
||||
end function ccl_ipc_shm_create_c
|
||||
|
||||
integer(c_int) function ccl_ipc_shm_open_c(name, byte_size, out_handle, out_payload) &
|
||||
bind(C, name="ccl_ipc_shm_open")
|
||||
import :: c_char, c_int, c_ptr, c_size_t
|
||||
character(kind=c_char), intent(in) :: name(*)
|
||||
integer(c_size_t), value :: byte_size
|
||||
type(c_ptr), intent(out) :: out_handle
|
||||
type(c_ptr), intent(out) :: out_payload
|
||||
end function ccl_ipc_shm_open_c
|
||||
|
||||
integer(c_int) function ccl_ipc_shm_close_c(handle) bind(C, name="ccl_ipc_shm_close")
|
||||
import :: c_int, c_ptr
|
||||
type(c_ptr), value :: handle
|
||||
end function ccl_ipc_shm_close_c
|
||||
|
||||
integer(c_int) function ccl_ipc_event_create_c(name, initial_state, out_handle) &
|
||||
bind(C, name="ccl_ipc_event_create")
|
||||
import :: c_char, c_int, c_ptr
|
||||
character(kind=c_char), intent(in) :: name(*)
|
||||
integer(c_int), value :: initial_state
|
||||
type(c_ptr), intent(out) :: out_handle
|
||||
end function ccl_ipc_event_create_c
|
||||
|
||||
integer(c_int) function ccl_ipc_event_open_c(name, out_handle) bind(C, name="ccl_ipc_event_open")
|
||||
import :: c_char, c_int, c_ptr
|
||||
character(kind=c_char), intent(in) :: name(*)
|
||||
type(c_ptr), intent(out) :: out_handle
|
||||
end function ccl_ipc_event_open_c
|
||||
|
||||
integer(c_int) function ccl_ipc_event_wait_c(handle, timeout_ms) bind(C, name="ccl_ipc_event_wait")
|
||||
import :: c_int, c_ptr
|
||||
type(c_ptr), value :: handle
|
||||
integer(c_int), value :: timeout_ms
|
||||
end function ccl_ipc_event_wait_c
|
||||
|
||||
integer(c_int) function ccl_ipc_event_signal_c(handle) bind(C, name="ccl_ipc_event_signal")
|
||||
import :: c_int, c_ptr
|
||||
type(c_ptr), value :: handle
|
||||
end function ccl_ipc_event_signal_c
|
||||
|
||||
integer(c_int) function ccl_ipc_event_reset_c(handle) bind(C, name="ccl_ipc_event_reset")
|
||||
import :: c_int, c_ptr
|
||||
type(c_ptr), value :: handle
|
||||
end function ccl_ipc_event_reset_c
|
||||
|
||||
integer(c_int) function ccl_ipc_event_close_c(handle) bind(C, name="ccl_ipc_event_close")
|
||||
import :: c_int, c_ptr
|
||||
type(c_ptr), value :: handle
|
||||
end function ccl_ipc_event_close_c
|
||||
end interface
|
||||
|
||||
contains
|
||||
|
||||
subroutine ccl_shm_create(name, byte_size, shm, ierr)
|
||||
character(len=*), intent(in) :: name
|
||||
integer(c_size_t), intent(in) :: byte_size
|
||||
type(CCL_Shared_Memory), intent(out) :: shm
|
||||
integer, intent(out) :: ierr
|
||||
character(kind=c_char), allocatable :: c_name(:)
|
||||
|
||||
call make_c_string(name, c_name)
|
||||
ierr = ccl_ipc_shm_create_c(c_name, byte_size, shm%native, shm%payload)
|
||||
if (ierr == 0) shm%byte_size = byte_size
|
||||
end subroutine ccl_shm_create
|
||||
|
||||
subroutine ccl_shm_open(name, byte_size, shm, ierr)
|
||||
character(len=*), intent(in) :: name
|
||||
integer(c_size_t), intent(in) :: byte_size
|
||||
type(CCL_Shared_Memory), intent(out) :: shm
|
||||
integer, intent(out) :: ierr
|
||||
character(kind=c_char), allocatable :: c_name(:)
|
||||
|
||||
call make_c_string(name, c_name)
|
||||
ierr = ccl_ipc_shm_open_c(c_name, byte_size, shm%native, shm%payload)
|
||||
if (ierr == 0) shm%byte_size = byte_size
|
||||
end subroutine ccl_shm_open
|
||||
|
||||
subroutine ccl_shm_close(shm, ierr)
|
||||
type(CCL_Shared_Memory), intent(inout) :: shm
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
if (.not. c_associated(shm%native)) then
|
||||
ierr = 0
|
||||
return
|
||||
end if
|
||||
ierr = ccl_ipc_shm_close_c(shm%native)
|
||||
shm%native = c_null_ptr
|
||||
shm%payload = c_null_ptr
|
||||
shm%byte_size = 0_c_size_t
|
||||
end subroutine ccl_shm_close
|
||||
|
||||
subroutine ccl_event_create(name, initial_state, event, ierr)
|
||||
character(len=*), intent(in) :: name
|
||||
logical, intent(in) :: initial_state
|
||||
type(CCL_Named_Event), intent(out) :: event
|
||||
integer, intent(out) :: ierr
|
||||
character(kind=c_char), allocatable :: c_name(:)
|
||||
integer(c_int) :: c_initial
|
||||
|
||||
call make_c_string(name, c_name)
|
||||
c_initial = 0_c_int
|
||||
if (initial_state) c_initial = 1_c_int
|
||||
ierr = ccl_ipc_event_create_c(c_name, c_initial, event%native)
|
||||
end subroutine ccl_event_create
|
||||
|
||||
subroutine ccl_event_open(name, event, ierr)
|
||||
character(len=*), intent(in) :: name
|
||||
type(CCL_Named_Event), intent(out) :: event
|
||||
integer, intent(out) :: ierr
|
||||
character(kind=c_char), allocatable :: c_name(:)
|
||||
|
||||
call make_c_string(name, c_name)
|
||||
ierr = ccl_ipc_event_open_c(c_name, event%native)
|
||||
end subroutine ccl_event_open
|
||||
|
||||
subroutine ccl_event_wait(event, timeout_ms, ierr)
|
||||
type(CCL_Named_Event), intent(in) :: event
|
||||
integer, intent(in) :: timeout_ms
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
ierr = ccl_ipc_event_wait_c(event%native, int(timeout_ms, c_int))
|
||||
end subroutine ccl_event_wait
|
||||
|
||||
subroutine ccl_event_signal(event, ierr)
|
||||
type(CCL_Named_Event), intent(in) :: event
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
ierr = ccl_ipc_event_signal_c(event%native)
|
||||
end subroutine ccl_event_signal
|
||||
|
||||
subroutine ccl_event_reset(event, ierr)
|
||||
type(CCL_Named_Event), intent(in) :: event
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
ierr = ccl_ipc_event_reset_c(event%native)
|
||||
end subroutine ccl_event_reset
|
||||
|
||||
subroutine ccl_event_close(event, ierr)
|
||||
type(CCL_Named_Event), intent(inout) :: event
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
if (.not. c_associated(event%native)) then
|
||||
ierr = 0
|
||||
return
|
||||
end if
|
||||
ierr = ccl_ipc_event_close_c(event%native)
|
||||
event%native = c_null_ptr
|
||||
end subroutine ccl_event_close
|
||||
|
||||
subroutine make_c_string(text, c_text)
|
||||
character(len=*), intent(in) :: text
|
||||
character(kind=c_char), allocatable, intent(out) :: c_text(:)
|
||||
integer :: i
|
||||
integer :: n
|
||||
|
||||
n = len_trim(text)
|
||||
allocate(c_text(n + 1))
|
||||
do i = 1, n
|
||||
c_text(i) = text(i:i)
|
||||
end do
|
||||
c_text(n + 1) = c_null_char
|
||||
end subroutine make_c_string
|
||||
|
||||
end module ccl_ipc
|
||||
@@ -0,0 +1,19 @@
|
||||
module ccl_ipc_names
|
||||
implicit none
|
||||
|
||||
contains
|
||||
|
||||
subroutine ccl_ipc_make_name(prefix, role, solver_id, name)
|
||||
character(len=*), intent(in) :: prefix
|
||||
character(len=*), intent(in) :: role
|
||||
character(len=*), intent(in) :: solver_id
|
||||
character(len=*), intent(out) :: name
|
||||
|
||||
if (len_trim(solver_id) > 0) then
|
||||
write(name, '(a,"_",a,"_",a)') trim(prefix), trim(role), trim(solver_id)
|
||||
else
|
||||
write(name, '(a,"_",a)') trim(prefix), trim(role)
|
||||
end if
|
||||
end subroutine ccl_ipc_make_name
|
||||
|
||||
end module ccl_ipc_names
|
||||
@@ -0,0 +1,272 @@
|
||||
#include "cclite_ipc.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
typedef struct CCL_SharedMemoryHandle {
|
||||
HANDLE mapping;
|
||||
void *view;
|
||||
size_t byte_size;
|
||||
} CCL_SharedMemoryHandle;
|
||||
|
||||
typedef struct CCL_EventHandle {
|
||||
HANDLE event;
|
||||
} CCL_EventHandle;
|
||||
|
||||
static int map_shared_memory(HANDLE mapping, size_t byte_size, void **out_handle, void **out_payload)
|
||||
{
|
||||
CCL_SharedMemoryHandle *handle = NULL;
|
||||
void *view = NULL;
|
||||
|
||||
if (mapping == NULL || out_handle == NULL || out_payload == NULL || byte_size == 0) {
|
||||
return CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
|
||||
view = MapViewOfFile(mapping, FILE_MAP_ALL_ACCESS, 0, 0, byte_size);
|
||||
if (view == NULL) {
|
||||
CloseHandle(mapping);
|
||||
return CCL_IPC_MAP_FAILED;
|
||||
}
|
||||
|
||||
handle = (CCL_SharedMemoryHandle *)calloc(1, sizeof(CCL_SharedMemoryHandle));
|
||||
if (handle == NULL) {
|
||||
UnmapViewOfFile(view);
|
||||
CloseHandle(mapping);
|
||||
return CCL_IPC_CREATE_FAILED;
|
||||
}
|
||||
|
||||
handle->mapping = mapping;
|
||||
handle->view = view;
|
||||
handle->byte_size = byte_size;
|
||||
*out_handle = handle;
|
||||
*out_payload = view;
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
|
||||
int ccl_ipc_shm_create(const char *name, size_t byte_size, void **out_handle, void **out_payload)
|
||||
{
|
||||
HANDLE mapping = NULL;
|
||||
DWORD size_low = 0;
|
||||
DWORD size_high = 0;
|
||||
|
||||
if (name == NULL || out_handle == NULL || out_payload == NULL || byte_size == 0) {
|
||||
return CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
|
||||
size_low = (DWORD)(byte_size & 0xffffffffu);
|
||||
size_high = (DWORD)((byte_size >> 32) & 0xffffffffu);
|
||||
mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, size_high, size_low, name);
|
||||
if (mapping == NULL) {
|
||||
return CCL_IPC_CREATE_FAILED;
|
||||
}
|
||||
|
||||
return map_shared_memory(mapping, byte_size, out_handle, out_payload);
|
||||
}
|
||||
|
||||
int ccl_ipc_shm_open(const char *name, size_t byte_size, void **out_handle, void **out_payload)
|
||||
{
|
||||
HANDLE mapping = NULL;
|
||||
|
||||
if (name == NULL || out_handle == NULL || out_payload == NULL || byte_size == 0) {
|
||||
return CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
|
||||
mapping = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, name);
|
||||
if (mapping == NULL) {
|
||||
return CCL_IPC_OPEN_FAILED;
|
||||
}
|
||||
|
||||
return map_shared_memory(mapping, byte_size, out_handle, out_payload);
|
||||
}
|
||||
|
||||
int ccl_ipc_shm_close(void *raw_handle)
|
||||
{
|
||||
CCL_SharedMemoryHandle *handle = (CCL_SharedMemoryHandle *)raw_handle;
|
||||
|
||||
if (handle == NULL) {
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
|
||||
if (handle->view != NULL) {
|
||||
UnmapViewOfFile(handle->view);
|
||||
}
|
||||
if (handle->mapping != NULL) {
|
||||
CloseHandle(handle->mapping);
|
||||
}
|
||||
free(handle);
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_create(const char *name, int initial_state, void **out_handle)
|
||||
{
|
||||
CCL_EventHandle *handle = NULL;
|
||||
HANDLE event = NULL;
|
||||
|
||||
if (name == NULL || out_handle == NULL) {
|
||||
return CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
|
||||
event = CreateEventA(NULL, TRUE, initial_state ? TRUE : FALSE, name);
|
||||
if (event == NULL) {
|
||||
return CCL_IPC_CREATE_FAILED;
|
||||
}
|
||||
|
||||
handle = (CCL_EventHandle *)calloc(1, sizeof(CCL_EventHandle));
|
||||
if (handle == NULL) {
|
||||
CloseHandle(event);
|
||||
return CCL_IPC_CREATE_FAILED;
|
||||
}
|
||||
|
||||
handle->event = event;
|
||||
*out_handle = handle;
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_open(const char *name, void **out_handle)
|
||||
{
|
||||
CCL_EventHandle *handle = NULL;
|
||||
HANDLE event = NULL;
|
||||
|
||||
if (name == NULL || out_handle == NULL) {
|
||||
return CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
|
||||
event = OpenEventA(EVENT_MODIFY_STATE | SYNCHRONIZE, FALSE, name);
|
||||
if (event == NULL) {
|
||||
return CCL_IPC_OPEN_FAILED;
|
||||
}
|
||||
|
||||
handle = (CCL_EventHandle *)calloc(1, sizeof(CCL_EventHandle));
|
||||
if (handle == NULL) {
|
||||
CloseHandle(event);
|
||||
return CCL_IPC_CREATE_FAILED;
|
||||
}
|
||||
|
||||
handle->event = event;
|
||||
*out_handle = handle;
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_wait(void *raw_handle, int timeout_ms)
|
||||
{
|
||||
CCL_EventHandle *handle = (CCL_EventHandle *)raw_handle;
|
||||
DWORD result = 0;
|
||||
|
||||
if (handle == NULL || handle->event == NULL) {
|
||||
return CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
|
||||
result = WaitForSingleObject(handle->event, (DWORD)timeout_ms);
|
||||
if (result == WAIT_OBJECT_0) {
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
if (result == WAIT_TIMEOUT) {
|
||||
return CCL_IPC_TIMEOUT;
|
||||
}
|
||||
return CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_signal(void *raw_handle)
|
||||
{
|
||||
CCL_EventHandle *handle = (CCL_EventHandle *)raw_handle;
|
||||
|
||||
if (handle == NULL || handle->event == NULL) {
|
||||
return CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
return SetEvent(handle->event) ? CCL_IPC_SUCCESS : CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_reset(void *raw_handle)
|
||||
{
|
||||
CCL_EventHandle *handle = (CCL_EventHandle *)raw_handle;
|
||||
|
||||
if (handle == NULL || handle->event == NULL) {
|
||||
return CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
return ResetEvent(handle->event) ? CCL_IPC_SUCCESS : CCL_IPC_BAD_HANDLE;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_close(void *raw_handle)
|
||||
{
|
||||
CCL_EventHandle *handle = (CCL_EventHandle *)raw_handle;
|
||||
|
||||
if (handle == NULL) {
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
if (handle->event != NULL) {
|
||||
CloseHandle(handle->event);
|
||||
}
|
||||
free(handle);
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int ccl_ipc_shm_create(const char *name, size_t byte_size, void **out_handle, void **out_payload)
|
||||
{
|
||||
(void)name;
|
||||
(void)byte_size;
|
||||
(void)out_handle;
|
||||
(void)out_payload;
|
||||
return CCL_IPC_UNSUPPORTED;
|
||||
}
|
||||
|
||||
int ccl_ipc_shm_open(const char *name, size_t byte_size, void **out_handle, void **out_payload)
|
||||
{
|
||||
(void)name;
|
||||
(void)byte_size;
|
||||
(void)out_handle;
|
||||
(void)out_payload;
|
||||
return CCL_IPC_UNSUPPORTED;
|
||||
}
|
||||
|
||||
int ccl_ipc_shm_close(void *handle)
|
||||
{
|
||||
(void)handle;
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_create(const char *name, int initial_state, void **out_handle)
|
||||
{
|
||||
(void)name;
|
||||
(void)initial_state;
|
||||
(void)out_handle;
|
||||
return CCL_IPC_UNSUPPORTED;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_open(const char *name, void **out_handle)
|
||||
{
|
||||
(void)name;
|
||||
(void)out_handle;
|
||||
return CCL_IPC_UNSUPPORTED;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_wait(void *handle, int timeout_ms)
|
||||
{
|
||||
(void)handle;
|
||||
(void)timeout_ms;
|
||||
return CCL_IPC_UNSUPPORTED;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_signal(void *handle)
|
||||
{
|
||||
(void)handle;
|
||||
return CCL_IPC_UNSUPPORTED;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_reset(void *handle)
|
||||
{
|
||||
(void)handle;
|
||||
return CCL_IPC_UNSUPPORTED;
|
||||
}
|
||||
|
||||
int ccl_ipc_event_close(void *handle)
|
||||
{
|
||||
(void)handle;
|
||||
return CCL_IPC_SUCCESS;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user