Mutex for queue in mutex
<p>I have to design a mutex as part of my assignment. No support from external library can be used and only system calls to de-schedule and put the thread back in ready queue are given. I am using atomic Xchg instruction to check the value of mutex. But if the mutex is unavailable I need to put the thread in a queue. Now, to again put this thread in a queue I need a mutex. Which leads me into a recursive problem. My solution is to use spin waiting here. As code for adding the thread into queue is very small, I can expect that spin waiting shouldnt be a problem. But am I missing something here ? Is there any other way than spin waiting ? Also, if I spin wait, there is no guarantee of bounded waiting. Is there a solution for that too ? </p>

<p>I also searched and found that in pthreads, a system call to futex is used. My assumption is that futex being a system call, its atomic in the sense of adding a thread to the queue. But here I don't have any support like this from kernel. The only system calls available to me are descheduling thread and making them runnable.</p>