Hi all!
After a week of bus-hacking I've put together a small egg to assist in
the creation and management of /protected/ shared memory across processes.
The egg provides a data structure and a handful of routines that
encapsulate the creation of a shared memory object, the management of
the memory map, a semaphore to block competing processes, and pass
counting to allow nested locking within a single process.
shared-mem objects can be created with the various flags and options
specified for each of their components, allowing advanced users great
flexibility in how they behave. However, for most users it can be as
simple as:
(use posix-safe-mem)
;; Create the safe-memory object with an initial value
(define m (make-safe-mem '(1 2 3 4 5)))
;; Fork a worker process
;; ...
;; Now lock and do work
(with-safe-mem m
(let ((data (safe-mem-get m)))
;; Do work on data
;; ...
;; Now write
(safe-mem-set! m data)))
Depending on whether the grow or shrink flags are set (both default to
#t), the shared memory object will automatically grow or shrink
(surprise!) when safe-mem-set! is called, in order to avoid a bus fault.
If grow is not set then you risk a bus fault, of course.
The behaviour defaults to copy-on-read, but this too can be switched off
via the copy flag. Most users probably want copy-on-read, unless you're
intending to work with very large data structures in shared memory. If
copy is not set then the same restrictions incurred by any evicted
object are applied, including the loss of type information for records.
There's some error checking in the egg but it's not complete, and I have
yet to write the full docs. However, a commented test is available:
https://github.com/dleslie/posix-safe-mem-egg/blob/master/tests/run.scm
Please file bugs on github, it would be appreciated! But do please keep
it congenial and pleasant. :)
Thanks,
-Dan
_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users