Sofware Mutex/spinlock
TomUdale
Posts: 75
Greetings,
Has anyone come up with some kind locking mechanism that does not use lockset/lockclr at the bottom of it? Those locks are very nice, but somewhat dear since there are only 8 of them. Perhaps someone cleverer than me has come up/knows of with an alternative algorithm that might offer similar functionality?
All the best,
Tom
Has anyone come up with some kind locking mechanism that does not use lockset/lockclr at the bottom of it? Those locks are very nice, but somewhat dear since there are only 8 of them. Perhaps someone cleverer than me has come up/knows of with an alternative algorithm that might offer similar functionality?
All the best,
Tom
Comments
Data structures can be safely shared between a single "producer" process and a single "consumer" process with a simple boolean flag. Just have the producer set the flag when data is ready and have the consumer clear the flag when it has read the data. Have both producer and consumer loop checking the appropriate state of the flag prior to accessing the data.
A producer and consumer can also share a cyclic buffer (FIFO) with only manipulation of a "head" and "tail" pointer. Have a look at the FullDuplexSerial object for an example of how that is done.
-Phil
I thought about this but was fearing that you would wind up with contention issues for the master lock ala the global kernel locks that have plagued some OSes. I gather that has not been a problem in real systems.
Thanks and
best regards,
Tom
I was actually looking for a multiple producer, single consumer system, but I gather that FullDuplexSerial can do that so I will take a look at it.
Knowing that most programs don't use locks is a good data point. I am new to the propeller and don't know many of the tricks yet. Locks are the natural answer for me, but I guess I should think outside the box in this area.
Best regards,
Tom
Now, if you have a "global lock" in your system then all the parallel processes in your system are serialized when they need exclusive access to some resource. This is a bigger overhead because interacting processes can be halted by other processes they don't interact with, except for the use of that global lock. Again this performance hit may or may not be an issue depending on your requirements.
Thanks for the tip. Now I don't have to consider rolling my own.
All the best,
Tom
Heh, heh, of course that does not work for COG memory model. But it turns out that doing something like this:
at least compiles without bringing in the whole thread library without to terribly much thinking on my part.
All the best,
Tom
Just for posterity, this does not work either. __sync_bool_compare_and_swap deadlocks immediately when called from COG code. If I replace it with my own version of __sync_bool_compare_and_swap, it all works fine. And naturally, it all works as advertised from LMM code.
Cheers,
Tom
Thanks,
Eric
It literally deadlocks the moment I call it in the COG code. I had this:
I could only see the pin toggle if I commented out the lock code. The cyiLockSLock functions worked fine in a LMM cog.
To fix it, I basically wrote my own __sync_bool_compare_and_swap function that was a straight copy of the code in propgcc which uses its own lockset/lockclr guard. So I think it is almost exactly what you are doing in the compiler.
I don't really understand why the original did not work. The only thing I could think of was that something was not linking quite right when __sync_bool_compare_and_swap was compiled (it actually compiled as a call to CMPX_something, I don't remember the exact name).
All the best,
Tom