userspace mutex/semaphore
Posted: Fri Aug 25, 2006 2:11 pm
hi,
sorry for just opening another thread about that topic, but I don't think that this fits within the other thread...
is it possible to implement mutexes/semaphores in userspace? I have read about this idee some time ago, but I could not find an implementation of them, so I tried to come up with my own. But I couldn't come up with an implementation that seemed to be right. My latest implementation of a mutex looks like this:
I think the implementation details of atom & spinlock aren't that important, so I leave them ou here. What is important is the problem that I can see:
If one thread (which trying to lock the mutex) is interrupted right before it can suspend itself and then the other thread (which has the mutex) calls resumeThread(...) with the right threadid. Then the other will never be woken up.
Is there anything you could think of, that could solve this problem (without adding other syscalls to the kernel)? If not, what syscalls do I have to add (I still want to do most things in userspace though, although I will add a kernel api for mutex & semaphores)?
Another question: Is it acceptable to disable all interrupts within a microkernel? I'm quite unsure about that.
Thanks in advance!
sorry for just opening another thread about that topic, but I don't think that this fits within the other thread...
is it possible to implement mutexes/semaphores in userspace? I have read about this idee some time ago, but I could not find an implementation of them, so I tried to come up with my own. But I couldn't come up with an implementation that seemed to be right. My latest implementation of a mutex looks like this:
Code: Select all
class mutex
{
public:
inline mutex(bool locked=false)
: mAtom(locked ? 0 : 1){}
inline void lock()
{
while (true)
{
if (mAtom.compare_and_exchange(1, 0)return;
mLock.lock();
mList.push_back(getThreadId());
mLock.unlock();
suspendThread(0);
}
}
inline void unlock()
{
mAtom = 1;
mLock.lock();
if (mList.size() != 0)
{
resumeThread(mList.back());
mList.pop_back();
}
mLock.unlock();
}
private:
atom mAtom;
spinlock mLock;
std::vector<unsigned long> mList;
};
If one thread (which trying to lock the mutex) is interrupted right before it can suspend itself and then the other thread (which has the mutex) calls resumeThread(...) with the right threadid. Then the other will never be woken up.
Is there anything you could think of, that could solve this problem (without adding other syscalls to the kernel)? If not, what syscalls do I have to add (I still want to do most things in userspace though, although I will add a kernel api for mutex & semaphores)?
Another question: Is it acceptable to disable all interrupts within a microkernel? I'm quite unsure about that.
Thanks in advance!