Does anyone use LOCKs???
cgracey
Posts: 14,253
Do any of you use the LOCK mechanisms for any purpose? While they are theoretically useful, I've never used them in any application and I've never seen anyone else use them. I'm just wondering if they would be missed in the next Propeller.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
Comments
I just make sure only 1 cog writes to any data, the other cogs only read.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.fd.com.my
www.mercedes.com.my
Whether or not they were actually required ... well I don't know.
Personally, I've never used the locks, if they no longer existed I wouldn't miss them.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I have an application that uses 2 propellers. One of them takes some samples from an ADC and calculates FFT and displays the results. For that I need 32k RAM (all the HUB) and some extra RAM that has to be shared. I though using locks to signal a few states. I'll probably go that route. This is not a commercial application, just a custom machine for my thesis. As soon as I publish it I'll post it here. It uses the uniqueness of the propeller. Well, what I wanted to say was: if comm channels existed between COGS in addition to shared memory (but not as only comm medium) some things could be done easily, may be more in parallel (i.e. no waiting for a variable to change and wasting power) without putting so much pressure on HUB memory and variables that point to shared HUB addresses.
Not to start another propeller vs. xmos thread but: Working a bit with the XMOS where shared memory is discouraged and everything should be done with channels, I started to appreciate more the benefits of shared memory. I though of new ways of using the parallelism the propeller brings to my application. May be a small channel. just a 1 or 2 longs buffer could open a very interesting applications. I have no idea how feasible that would be.
Bottom line: when I may use them in this particular place... I will hardly miss them. The channels otoh seem like a very useful addition, I if could choose.
I did extensive programs in the 70's & 80's using a minicomputer with similar concepts to the Propeller (hub and cog memory and simulated multiprocessors). Software locks were used.
So, to answer your question, I will not miss them.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Prop Tools under Development or Completed (Index)
· Emulators (Micros eg Altair, and Terminals eg VT100) - index
· Search the Propeller forums (via Google)
My cruising website is: ·www.bluemagic.biz
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
Chip Gracey
Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, E.I.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
www.tdswieter.com
Given that I only have one serious Prop project, PropAltair, and it does not need locks the answer is no.
Then I started thinking......
Back in the day when I used to program rack fulls of 16 bit CPUs with shared memory cards the only lock mechanism we had on those custom CPUs was a LOCK EXCHANGE instruction. That is: the read and the write of an exchange operation between CPU register and RAM could not be interleaved with any other bus activity from other processors. Using that to test and set a semaphore in shared RAM any amount of RAM or other resource on the bus could be "protected".
Things got interesting when I was supplied with prototype CPUs without the lock and no one told me....
Looking at the Intel x86 architecture we see the same lock idea but extended slightly so the lock works with more instructions, namely:
* Bit test and change: BTS, BTR, BTC.
* Exchange: XCHG.
* Two-operand arithmetic and logical: ADD, ADC, SUB, SBB, AND, OR, XOR.
* One-operand arithmetic and logical: INC, DEC, NOT, and NEG.
I have a felling people only ever use the "LOCK" prefix with XCHG on x86 anyway.
Conclusion: Looks like one could throw away the Props locks and all that checking out business and replace it with a LOCK EXCHAGE hubop, say LXLONG, LXWORD, or LXBYTE or all of them.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
Having the locks also makes you want to use coginit istead of cognew, which is a bad idea anyway. Beacuse as I said you need to keep count of what is using them.
Now, if you could like literary assign memory locations to be read only or write only for certain cogs that would be interesting.
But, yeah, I never use locks. Waste of an opticode if you ask me.
Whats easier to use instead of locks is simply making a byte that gets set to true ($FF)·or false ($00)·when you need to synchronize stuff. Or better yet making everything FIFO buffered so you can transfer lots of data predictably.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
You need the TEST_and_SET because it's always possible (but unlikely) that another cog can modify your flag location between your TEST and MOV if you try to implement a lock that way. The reason we've needed them rarely is that most everything we do with multiple cogs involves the special case of the single producer / single consumer where only one cog modifies (fills) the structure, the other cog just accesses (empties) it and this special case does not require a semaphore.
If only there were a way to include a "wait" on such a semaphore so that a COG waiting on access to some data would go low power.
Sounds like Chip has got a cunning and devious plan up his sleeve and would like to make space for it by removing the locks.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
With multiple processors, some sort of locking mechanism is necessary, imho. Don't get rid of locks unless you can provide a functional equivalent.
-Phil
* cogA writes some data to a circular buffer, and updates a table with some info about the last data written, flagging it as new data
* cogB checks the table for new data and may or may not use some data, but either way flags in the table that it is not longer needed by cogB
* cogC checks the table for new data, then once enough data is collected dumps it to a SD card for storage and flags the data in the table as no longer needed by cogC
* cogA may only overwrite a section of the buffer if the data there is flagged as unnecessary by both cogs B & C
Also note that the situation is more complex than above, as multiple cogs get to play the roles of A,B,C.
Is there is a failsafe way to handle something like this without LOCKs?
thanks,
Jonathan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
lonesock
Piranha are people too.
I think that in the current Propeller, due to hub RAM·limitations which dictate the type of applications written, LOCKs are not commonly needed. With 256K of RAM affording more system-level apps, though, there will likely be a more frequent need for them. So, I think I'll leave them in, as they are.
Oh, and about not having a mechanism to just read LOCKs... The conundrum is that if you read one, but don't request control (LOCKSET/LOCKCLR)·at the same time, your result is meaningless, since on the very next cycle, that LOCK's·status might change via another cog's LOCKSET/LOCKCLR activity, rendering your recent observation inaccurate. So, LOCKs are for participants, not for bystanders.
Does anyone think that 8 LOCKs are not enough? Would 16 be better?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
Post Edited (Chip Gracey (Parallax)) : 1/23/2009 6:18:15 PM GMT
Since LOCK ID check-out and check-in is not really required (I did not know that), can that function be eliminated to save op-code space? I would not mind defining my own LOCK IDs.
Mark
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
i.e.
My main routine routine repeats every 1-2ms. New data can be from GPS (200ms), RC receiver (15ms), or ADC (5ms). I only need to do something when the data changes. So each routine sets a lock when it has new data and the main routine only processes when new data is available. It just seems easier than reading a hub memory location to test.
I am still working on the individual routines so this test will have to wait until next week.
Donald
·
One should implement two logical sorts of locks, though:· shared and exclusive.·
One obtains a shared·lock for the resource when one wishes to read it, and doesn't want anyone writing it·at the same time.· Many tasks (programs) can hold a shared lock simultaneously, and read the locked resource simultaneously.·
One obtains an exclusive lock when one intends to·change the resource and·doesn't want anyone else to try to read or write it·at the same time.
With a single exclusive hardware-implemented lock, one can make one's own locks for any number of resources, using one·shared-storage bit per lock if one needs only exclusive locking, perhaps a byte if one needs both kinds (you need a count of how many tasks are sharing).· This is really easy, and I leave the details as an intellectual exercise for the reader, pointing out only that one obtains the hardware lock (waiting if necessary) whenever one obtains a software lock, and immediately releases the hardware lock.· If one designs it right, one needn't obtain the hardware lock in order to free one's exclusive software locks.
A test-and-set instructiion, as in the IBM 360/370/390 mainframes and their successors, is an adequate substitute for locks in many cases, but requires considerably more expertise to use correctly.· The TS instruction fell into general disuse early in the history of OS/360, but was retained in the hardware for compatibility with older programs.· (Microsoft could learn a lot by a study of upward compatibility in the 360/370/390 series, by the way.· Through decades of rapid evolution in mainframes, IBM retained upward compatibility that was essentially complete for application programs.· By contrast, every time MS brings out a new release, old programs stop working.· That never happened with 360/370/390 applications.)
Anyway, I guess a TS instruction would be nice, but a hardware lock is better -- and only one hardware lock is sufficient.
The necessary instructions are :· (1)· Obtain the hardware lock (with an implied wait); (2) Free the hardware lock; and optionally (3)·Test to see if it's available.·The instruction for obtaining it may usefully have two variants:· wait if it's not available, or set a condition if it's not available.· If everyone's well behaved, the wait option is preferable in most cases, for no one will keep the hardware lock·longer than a few microseconds.
There are hints and kinks to using locks, most of which are cautions to observe when one needs to hold more than one lock at a time, in order to avoid the situation in which I have A and need also B,· and you have B and need also A.· If I wait for B and you wait for A, we both wait forever.· In the mainframe world one term of art for this is "deadly embrace".
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
· -- Carl, nn5i@arrl.net
Post Edited (Carl Hayes) : 1/24/2009 1:23:51 AM GMT
While test and set is not used much, the compare and swap that replaces it is based on the same idea, it just works on a full word (or doubleword) instead of testing a single bit. I think that the compare and swap would be a better model to follow than test and set. See: publibz.boulder.ibm.com/epubs/pdf/dz9zr006.pdf for the difference details.