2 * This works well as long as the number of contending threads
3 * is less than the number of processors. This is because of
4 * the fair locking scheme. If the thread that is next in line
5 * for acquiring the lock is not currently running, no other
6 * thread can acquire the lock. This is terrible for performance,
7 * and it seems that all fair locking schemes suffer from this
13 fair_ticket_lock::fair_ticket_lock() {
14 next_ticket = now_serving = 0;
17 fair_ticket_lock::~fair_ticket_lock() {
21 void fair_ticket_lock::lock() {
22 unsigned ticket = __sync_fetch_and_add(&next_ticket, 1);
23 while (now_serving != ticket) {
27 printf("locked nextTicket: %d nowServing: %d",
28 next_ticket, now_serving);
32 void fair_ticket_lock::unlock() {
35 printf("unlocked nextTicket: %d nowServing: %d",
36 next_ticket, now_serving);
40 void fair_ticket_lock::pause() {
41 asm volatile("pause\n" : : : "memory");