PDA

View Full Version : MREWS, Semaphores, and Lazarus



jdarling
13-01-2010, 03:00 PM
I've been working on some backend code that will run on Windows, Linux, and MAC boxes. This wasn't at all a problem in Lazarus until I realized that I had to have some DB synchronization built into the application (some versions of SQLite aren't thread-safe).

Of course I've read Martin Harvey's lessons on threading multiple times (had to when I converted them to go live) and am quite familiar with writing MREWS (basic one at http://www.eonclash.com/Tutorials/Multithreading/MartinHarvey1.1/Ch11.html would work here).

Looking into it, there isn't really a good implementation (cross platform) of Semaphores (TSemaphore in some Linux versions, Pipes in MAC, API in Windows) within Lazarus. I don't actually need cross process communications, so I'm thinking of writing a simple spin lock based MREWS that supports an operation stack (Finish existing reads, start write, queue reads/writes, finish write, process queue). Just want to make sure that something like this doesn't already exist before I re-invent this particular wheel.

Once complete, I'll post up for everyone of course :)

- Jeremy

JSoftware
13-01-2010, 04:08 PM
I'm not really an expert on thread synchronizaiton primitives, but can't the BasicEvent* and RtlEvent* event functions be used?

jdarling
14-01-2010, 01:31 AM
I'll have to look into those and see how well they are supported. I also thought of using interlockedincrement and interlockeddecrement, but it seems those are "kernel" methods in Linux only and should not be used in user mode :(.

If nothing else I can use CriticalSections and create my own spin locks (as I stated above). Guess I could also try testing it for a while on each platform to see what breaks LOL

- Jeremy

JSoftware
14-01-2010, 06:04 AM
I'll have to look into those and see how well they are supported. I also thought of using interlockedincrement and interlockeddecrement, but it seems those are "kernel" methods in Linux only and should not be used in user mode :(.

I don't believe that's the case. They are assembler functions and implemented in usermode for all platforms.

phibermon
27-02-2010, 06:41 PM
Just do what i do, use SDL. it's semaphores and mutexes work perfectly fine when used with Tthread.

phibermon
10-04-2010, 06:49 PM
In fact, why not just get the SDL source, extract the methods for the semaphores/mutexes for all fpc supported platforms and convert them to pascal? They can't be reliant on SDL threads implementation because I use them in my tthreads, SDL threads are buggy when you try to wait on terminate with jedi-sdl :(