Expand/Shrink

ipc

Simple ipc using shared memory.

Allows two or more applications to share a block of contiguous memory, identified by a unique null-terminated string.

The shared memory address used/returned by the routines is a standard memory address which is similar to that returned by allocate() and can be referenced using the standard routines peek() and poke(), and contain any Phix object thanks to serialize() and deserialize().

Each application that requests a handle to shared memory must close its handle before the memory will be returned to the system. This operates a little differently from standard allocated memory, in that the application that originally allocated the memory can close its handle, but the memory will continue to exist for other applications to use as long as at least one other application has an open handle to it.

Based heavily on the work of Jason Mirwald and Mario Steele (Windows), and Elliott Sales de Andrade (Linux), however the file builtins\ipc.e (not an autoinclude) unifies those two (memshare) code bases into a single cross-platform file.

Predefined Error Values:
The routines contained in this library will return one of the following values if an error occurs while trying to create or open shared memory.
The values are all negative integers, and can be used to help identify what specific error occurred during the calls.
SM_CREATE_EXIST - Shared memory already exists
SM_CREATE_FAIL - Failed to create Shared Memory
SM_OPEN_FAIL - Failed to obtain a valid Memory Address
SM_MEM_FAIL - Failed to allocate Standard Memory for the operation
SM_STAT_FAIL - Failed to retrieve the status of a block (Linux only)

Note: there are (as yet) no synchronisation primitives, for now I suggest using file locking. As it stands, without said, this is only suitable for one-way, 1:1, and non-critical uses, such as:
  • compiler sending error and warning messages to Edix
  • pdemo sending edit requests to Edix
  • p -isense signalling intellisense info ready to read in /tmp
  • implementation of single instance on Linux (may supercede the Windows-only SINGLEINSTANCEx of IUP)
In all the above, the worst case scenario is only going to be "try clicking it again", although single instance creating two instances could potentially be more troublesome than when two overlapping communications mean that eg "C:\UC:\Users\Me\myfile.exwsers\Me\myfile.exw" cannot be opened.

It is also reasonable that, slowly, over time, this is extended with implementions of Message Queues, Named Pipes, and Sockets - there is a reason why there are half a dozen wildly different approaches to ipc, which is that "there is no one size fits all". Progress will most likely be rather leisurely, not least because any and all Windows-only or Linux-only solutions are less than worthless.

None of these routines are supported by pwa/p2js.

sm_create - Create a uniquely-named block of shared memory
sm_open - Retrieve the address of an existing block of shared memory, identified by name
sm_alloc_lpsz - Create and fill a named block of shared memory.
sm_close - Close a handle to a block of shared memory