[C.CC USERS] placed variables?
Carl Ritson
c.g.ritson at kent.ac.uk
Wed Aug 11 06:29:16 BST 2010
Hi Patrick,
On 2010/08/09, at 21:01, Patrick J.G.C. Weemeeuw wrote:
> I can't solve that with channels, because I want this timer to be unsynchronized, and I want to access it from several places in the code, exactly like the TIMER
> process. I think that to achieve something like this, I have no choice but to bypass the regular occam mechanisms, and access memory directly.
What you want can be done with channels: each process needing access to the extended time counter is a client to the timekeeper process which multiplexes their requests using an alternation construct (PRI ALT).
I believe you probably want something like the code below.
Note: I have changed the INT64 to an INT32.
If you update every 1000ms (once per second), then a 32-bit counter will last several decades (think unix timestamp).
PROC timekeeper ([]CHAN SIGNAL req, []CHAN INT32 resp)
INITIAL INT32 now IS 0:
TIMER tim:
INT next.update:
SEQ
tim ? next.update
next.update := next.update PLUS clockresolution
WHILE TRUE
PRI ALT
-- when time moves past next.update
tim ? AFTER next.update
SEQ
-- maintain 'now' here, for example:
now := now + 1
next.update := next.update PLUS clockresolution
-- respond to requests for clock
ALT i = 0 FOR SIZE req
req[i] ? SIGNAL
resp[i] ! now
:
PROC client (CHAN SIGNAL time.req, CHAN INT32 time.resp)
INT32 now:
SEQ
-- update now to latest timer value
time.req ! SIGNAL
time.resp ? now
-- do something with now ...
serial.write.string (TX0, "time: ")
serial.write.int (TX0, now) -- NOTE: this will need to be different for INT32
serial.write.newline (TX0)
delay (clockresolution)
:
PROC main ()
[1]CHAN SIGNAL time.req:
[1]CHAN INT32 time.resp:
PAR
heartbeat ()
timekeeper (time.req, time.resp)
client (time.req[0], time.resp[0])
:
Hope this helps.
Carl
More information about the users
mailing list