Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2009-08

new TMultiReadExclusiveWriteSynchronizer

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

As I mentioned in the fpc-pascal mailing list. The current implementation is wrong and is actually the equivalent of Delphi's TSimpleRWSync class.

I submitted a patch to Mantis which has a new implementation. I also rename the old implementation to TSimplRWSync. I also changed the instantiation of the instance in the classes.inc file to use the TSimpleRWSync class. So that existing behaviour stays the same.

http://bugs.freepascal.org/view.php?id=14451

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Graeme Geldenhuys
to Graeme Geldenhuys
Graeme Geldenhuys wrote
As I mentioned in the fpc-pascal mailing list. The current implementation is wrong and is actually the equivalent of Delphi's TSimpleRWSync class.

Regarding Florian's comment in the bug tracker. I was using Sleep(100) which Florian says will slow down the class a lot.

Has anybody got a better solution to this? I guess dropping the Sleep time even lower might not be the best solution either. Any suggestions?

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Jonas Maebe
to Graeme Geldenhuys
Graeme Geldenhuys wrote 11 lines
Graeme Geldenhuys wrote: > > As I mentioned in the fpc-pascal mailing list. The current > implementation is wrong and is actually the equivalent of Delphi's > TSimpleRWSync class. Regarding Florian's comment in the bug tracker. I was using Sleep(100) which Florian says will slow down the class a lot. Has anybody got a better solution to this? I guess dropping the Sleep time even lower might not be the best solution either.

It would make things worse on some platforms, because with very low sleep times you don't even get a context switch on Mac OS X (the routine just spins in user space for a short while).

Any suggestions?

Add a flag that indicates that someone is interested in writing, block it on an rtlevent, and then
a) block new readers on some kind of barrier
b) when all current readers are finished, send the event to wake up the writer
c) when the writer is finished, wake up the readers by upping the semaphore
d) if other writers arrived in the mean time, they'll contend with the readers over the critical section

Of course, there is at least one small problem with the above scenario, and that is that the thread manager does not implement semaphores on all platforms (wich would be quite helpful to implement the barrier).

I'd suggest to implement it in a platform-dependent way and use the primitives offered by the standard libraries of every platform. It's not like this causes an extra dependency, so why re-implement (and debug) the whole thing in a generic and probably sub-optimal way?

Jonas

Reply by email

Martin
to Graeme Geldenhuys: Regarding Florian's comment in the bug tracker. I was using Sleep(100) which Florian says…

Is there support for semaphores?
the following should work

you need:
- a WriterSemaphore initialized at 1
(the writer semaphore is like the CriticalSection, it may be better named ExclusiveSemaphore)
- a ReaderSemaphore initalized at 0

* when writing you will do this
- decrease the WriterSemaphore by 1
if a writer already exists (writing or waiting for readers to finish) then it already is zero, and the code will wait
- wait for the ReaderSemaphore to be 0
- WRITE
- increase the WriterSemaphore by 1

* when reading you will do this
- decrease the WriterSemaphore by 1
we cannot start if a writer is active
- increase the ReaderSemaphore by 1
this will prevent writers
- increase the WriterSemaphore by 1
(so other readers can enter read)
- READ
- decrease the ReaderSemaphore by 1

Graeme Geldenhuys wrote 18 lines
Graeme Geldenhuys wrote: > As I mentioned in the fpc-pascal mailing list. The current > implementation is wrong and is actually the equivalent of Delphi's > TSimpleRWSync class. > Regarding Florian's comment in the bug tracker. I was using Sleep(100) which Florian says will slow down the class a lot. Has anybody got a better solution to this? I guess dropping the Sleep time even lower might not be the best solution either. Any suggestions? Regards, - Graeme -

Reply by email