class MCSLock { private: Queue *tail = NULL; } MCSLock::release() { if (compare_and_swap(&tail, myTCB, NULL)) { // If tail == myTCB, no one is // waiting. MCSLock is now free. } else { // Someone is waiting. while (myTCB->next == NULL) ; // spin until next is set // Tell next thread to proceed. myTCB->next->needToWait = FALSE; } } MCSLock::acquire() { Queue *oldTail = tail; myTCB->next = NULL; while (!compare_and_swap(&tail, oldTail, &myTCB)) { // Try again if someone else // changed tail in the meantime. oldTail = tail; } // If oldTail == NULL, lock acquired. if (oldTail != NULL) { // Need to wait. myTCB->needToWait = TRUE; memory_barrier(); oldTail->next = myTCB; while (myTCB->needToWait) ; //spin } }