Tuesday, September 2, 2008

Threads module almost completed!

It was like a rollercoaster ride. At first I thought it would be much easier, like "Ok just do your stuff on the wrappers and built a bit more wrappers around pthreads and windows functions. How hard can it be?"

Oh, boy. As I was examining the wxWidgets code that I modelled the classes after (turns out I ended up copying a lot of it - don't worry, all copyright notices are left intact), I realized that Windows XP doesn't implement condition variables (but Vista does - who'd tell?).


Ok, to get you in perspective: There are three basic kinds of thread organization objects: Mutexes, Semaphores, and Conditions.

----------- begin boring explanation -----------
A mutex is an object that can be locked by ONLY one thread. The locking of a mutex is called an "atomic operation", it means that you won't be interrupted by another thread while you're doing your stuff. And as long as the mutex is locked, you're safe.

(There is an equivalent of mutexes for threads of only one process, they're called critical sections. Instead of locking / unlocking, you enter and leave them. But they're practically the same.)

A semaphore is like a racing semaphore: One process is the car that "waits" for the semaphore to turn green, another process is the guy in charge of the semaphore. The interesting thing is that ANY process can set the semaphore to green, and this is where things get interesting.

A "condition" is kinda the opposite of a semaphore: Instead of many guys signaling one car, it's one guy signaling ONE or MANY cars. You can signal all (it's called a broadcast), or only one. But a condition requires a mutex for organization, it's kinda complicated. But it lets you send signals to a thread when you want to tell it to do something (if it's not busy already).
----------- end boring explanation -----------
Anyway, here's the trick: Conditions aren't implemented in windows, so you have to implement them using Semaphores and Mutexes (which are implemented).
But in Posix, you don't have semaphores, so you have to implement them using conditions and mutexes.

So here I was, copying code and organizing it. Then I realized that the most difficult part was the mess of creating a thread. It's not just a single call, you needto set up parameters, implement error handling functions ,etc. etc.

And I realized that the posix implementation of threads in wxWidgets was much cleaner than the windows one. So I reorganized it and spared some windows-specific "optimizations" (they're actually more like shortcuts, for example, in Windows you can start a thread in dormant state, but in posix you need to use a semaphore to make it sleep). So by doing that the code looked much cleaner.

I still need to implement/copy/adapt the code for killing a thread, and the code to delete all threads on program shutdown. It's easier now that I've implemented/copied/adapted nearly everything.

When I do my first thread experiment, it'll be quite fun. I spent many nights this week trying to finish this.

The bad news, is that as things look, it's probable we won't have a workable video editor by the end of this year (I began in May, and it's September already? Aw crap). It might take another 6 months or more. But at least I know that I'm much more advanced than I was when I started the project.

But let me tell you. When I get the core finished, things will start to get REALLY interesting. I'll keep you updated.

No comments: