pthread condition variables
Posted: Fri Nov 18, 2016 9:12 am
I am currently writing a hypervisor in C++, and we are using libc++.
https://github.com/Bareflank/hypervisor
I got rid of the "-fno-threadsafe-statics" and added my own implementation of pthread_cond_wait and pthread_cond_broadcast (which is what libcxxabi uses). We don't have threads in the hypervisor, but we do have multiple CPUs so in a way, we always have 1 thread per CPU (i.e. no scheduling, but things like a mutex are still needed). Because we don't have the ability to schedule a thread, a mutex and a condition are really just spin_locks.
Implementing a mutex with a spin_lock seems pretty straight forward, but implementing pthread_cond_wait was a little more complicated. I was wondering if you guys wouldn't mind taking a look at what I have done, and try to poke holes:
https://github.com/Bareflank/libbfc/blo ... d.cpp#L249
I do no intend to implement pthread_cond_signal... just pthread_cond_broadcast (i.e. I don't think I need a Semaphore) so I set the condition to 1, unlock the mutex, and wait for a broadcast which sets condition back to 0, and then I lock again. My major concern are race conditions with this implementation. How is this usually implemented? Does anyone have any decent examples on how I might go about this if I have done it wrong?
Thanks in advance,
- Rian
https://github.com/Bareflank/hypervisor
I got rid of the "-fno-threadsafe-statics" and added my own implementation of pthread_cond_wait and pthread_cond_broadcast (which is what libcxxabi uses). We don't have threads in the hypervisor, but we do have multiple CPUs so in a way, we always have 1 thread per CPU (i.e. no scheduling, but things like a mutex are still needed). Because we don't have the ability to schedule a thread, a mutex and a condition are really just spin_locks.
Implementing a mutex with a spin_lock seems pretty straight forward, but implementing pthread_cond_wait was a little more complicated. I was wondering if you guys wouldn't mind taking a look at what I have done, and try to poke holes:
https://github.com/Bareflank/libbfc/blo ... d.cpp#L249
I do no intend to implement pthread_cond_signal... just pthread_cond_broadcast (i.e. I don't think I need a Semaphore) so I set the condition to 1, unlock the mutex, and wait for a broadcast which sets condition back to 0, and then I lock again. My major concern are race conditions with this implementation. How is this usually implemented? Does anyone have any decent examples on how I might go about this if I have done it wrong?
Thanks in advance,
- Rian