From 4e8ab73a88d353aa2a4aff08b91d5e14a5b1c357 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sun, 15 Apr 2018 18:39:10 +0200 Subject: [PATCH] Updated readme again --- README.md | 61 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7491743..c34ac4d 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,12 @@ Traps are used in the main loop to break on signals // ... } -## Timers +### Timers Timers fire asynchronously at fixed 1 second intervals. It requires signals to be processed; see above. + // Once every second... $timer = new Timer(function () { echo "."; }); @@ -61,22 +62,64 @@ processed; see above. File locks uses a shared file as a resource for locking. -// Creating the lock will not acquire it -$lock = new FileLock(__FILE__); + // Creating the lock will not acquire it + $lock = new FileLock(__FILE__); -if (!$lock->acquire()) { - echo "fail!\n"; -} else { - $lock->release(); -} + if (!$lock->acquire()) { + echo "fail!\n"; + } else { + $lock->release(); + } ## SysV wrappers +All these wrappers depend on a `KeyInterface` being passed to the constructor. +This is usually an instance of a `FileKey`, created as such: + + $key = new FileKey(__FILE__); + +The key has a project identifier that starts at `chr(0)`, or `"\0"`. To increase +this identifier, and thus point to another segment, just clone it. + + $key2 = clone $key1; + ### Semaphores ### Mutexes -### Messagae Queues +### Message Queues + + $key = new FileKey(__FILE__); + $msgq = new Queue($key); + + $msgq->send(1, [ "Some data", [ "format"=>"foo" ]]); + + $data = $msgq->receive(1, $type); + + $msgq->destroy(); + +### Shared Memory + +Shared memory using `SharedData` supports integrity checking when setting, using +the third parameter to `set()`. + + $key = new FileKey(__FILE__); + $shm = new SharedData($key); + + do { + $counter = $shm->get("counter") + 1; + } while (!$shm->set("counter", $counter, true)); + + $shm->destroy(); + +The `SharedMemory` class is a simple integer-indexed array + + $key = new FileKey(__FILE__); + $shm = new SharedMemory($key); + + $shm[0] = 42; + + $shm->destroy(); ## Communication