Need more info about Locks
AGCB
Posts: 327
I've tried searching for a tutorial on using locks but not found one that explains in more depth.
My main interest at the present is how (if possible) to lock 1 hub variable while letting other variables be free to be changed.
I'm building a programmable room thermostat and want to be able to HOLD the temperature, overriding the auto program. There are things that need to continue while in HOLD like LCD display etc. which use hub ram.
Your suggestions are valued.
Thanks
Aaron
My main interest at the present is how (if possible) to lock 1 hub variable while letting other variables be free to be changed.
I'm building a programmable room thermostat and want to be able to HOLD the temperature, overriding the auto program. There are things that need to continue while in HOLD like LCD display etc. which use hub ram.
Your suggestions are valued.
Thanks
Aaron
Comments
You have a choice of dynamically allocating them (locknew, lockret) or statically by number. If you want
to play nice with objects that might also use locks, then dynamically allocate at initialization...
There are only 8 locks, so be aware of that limitation.
I think I will use a flag instead but for future reference and learning, how can I specify only locking one variable?
Also, what do you mean by dynamically or statically?
@kuroneko
Would you explain that for dummies
I mean the same as everyone else when I say dynamic and static, runtime v. compile time.
I'm only mentioning it as I've seen it fail like this. Your code/usage may be robust enough so that this doesn't matter.
Aaron,
That's a key point. Locks, by themselves, do not lock the actual data. They only have function when used by cooperating routines that are written to exclusively attempt access of the "lock"ed data only when the lock is acquired. What that means is your cooperating routines enter blocking states while waiting to acquire the relevant lock and only continue when the lock is acquired.
The purpose of a lock is to provide a way to prevent multiple concurrent accesses on a group of data from multiple tasks. This allows one task to change all the required details without another task getting confused. This is know as an atomic operation.
If only one integer is of concern then a lock is not the droid you are looking for.
There is a common alternative to synchronising with locks; and that is double buffering and it's ilk. The various buffering solutions solve many timing issues and also add significant throughput benefits. Their downside is they consume memory, sometimes lots of memory.
My understanding is that the locks don't actually 'lock' anything. They're flags that can be set programmatically to indicate that a hub variable is being manipulated by a process. This would prevent two processes from trying to change the same variables at the same time.
Hub access prevents a variable from being accessed simultaneously by multiple cogs so my take on it is that they are useful in cases where multiple variables need to be updated and the result is only reliable when ALL variables have been updated.
Sandy
BTW, a hub variable as a flag cannot be a replacement for a lock. Locks are special flags that require only one hub access to check and set them. Variables take two.
-Phil
Good point. The key part of this is both processes modify that one variable.
EDIT:
An alternative way to do this without a lock is have a third managerial task that hides the real variable storage and instead services requests from the other two tasks. Eg: Tasks one and two both want to add 1 to the variable so each issue the request of ADD with a value of +1 in their respective request buffers. Task three then checks each buffer in turn and modifies the real stored variable accordingly.
To keep some order, the request mechanism would need a synchronising handshake. Something like a request flag to say a request is pending, this is held active until a separate replying acknowledge flag is activated. And likewise the acknowledging flag stays active until the request flag has deactivated.
Importantly, each flag is written by only one task each, which means they can't be at the same word address. Shared writes cannot be if there is no hardware locks provided to facilitate them.
One could get around this by using the cnt register in conjunction with the cogid to allow a read/modify/write in quick succession only when the last n bits of cnt equal a certain value dictated by cogid. That way, you produce wider, non-overlapping windows that allow multiple hub accesses.
-Phil
Thanks
Aaron
-Phil
So thanks again
Aaron
Nice! Excellent trick. CNT is simultaneously identical for all Cogs and is a single fast instruction away. I'd imagine that's not something every multiprocessor system has at it's disposal.
Ah, the set temperature ... What exactly is changing the set temperature anyway? Isn't it just operator adjusted?
1st the set temperature is adjusted with an up or down button, then when the hold button is pressed, the program jumps to the infinite repeat hold method with each loop testing for the up button press which ends the loop and the program returns to the auto mode. The auto mode changes the set temperature according to the time of day(input from RTC). The hold method could run from hours to days. When in the hold program, the set temperature must not be reset by the auto program.
Aaron