int main() { ticketlock_t mylock = TICKETLOCK_UNLOCKED; ticketlock_lock(&mylock); /* do critical magic here */ ticketlock_unlock(&mylock); return 0; }
static void* simple_ticketlock_yield(void* ignore) { (void)ignore; for (uint64_t i = 0; i < ITERATIONS; ++i) { ticketlock_yieldlock(&simple_ticket); WORK(); ticketlock_unlock(&simple_ticket); } return 0; }
static void test_splinlock_speed() { spinlock_init(&simple_spin); ticketlock_init(&simple_ticket); pthread_t threads[10]; printf("spinlock speed test (lock):\n"); for (int num = 1; num <= 16; num *= 2) { printf("%2d threads: ", num); fflush(stdout); spinlock_lock(&simple_spin); for (int t = 0; t < num; ++t) { pthread_create(&threads[t], 0, simple_spinlock, 0); } int64_t start = ustime(); spinlock_unlock(&simple_spin); for (int t = 0; t < num; ++t) { pthread_join(threads[t], 0); } int64_t end = ustime(); printf("%6ld milliseconds\n", (end - start) / 1000); } printf("spinlock speed test (yieldock):\n"); for (int num = 1; num <= 16; num *= 2) { printf("%2d threads: ", num); fflush(stdout); spinlock_lock(&simple_spin); for (int t = 0; t < num; ++t) { pthread_create(&threads[t], 0, simple_spinlock_yield, 0); } int64_t start = ustime(); spinlock_unlock(&simple_spin); for (int t = 0; t < num; ++t) { pthread_join(threads[t], 0); } int64_t end = ustime(); printf("%6ld milliseconds\n", (end - start) / 1000); } printf("ticketlock speed test (lock):\n"); for (int num = 1; num <= 8; num *= 2) { printf("%2d threads: ", num); fflush(stdout); ticketlock_lock(&simple_ticket); for (int t = 0; t < num; ++t) { pthread_create(&threads[t], 0, simple_ticketlock, 0); } int64_t start = ustime(); ticketlock_unlock(&simple_ticket); for (int t = 0; t < num; ++t) { pthread_join(threads[t], 0); } int64_t end = ustime(); printf("%6ld milliseconds\n", (end - start) / 1000); } printf("ticketlock speed test (yieldlock):\n"); for (int num = 1; num <= 8; num *= 2) { printf("%2d threads: ", num); fflush(stdout); ticketlock_lock(&simple_ticket); for (int t = 0; t < num; ++t) { pthread_create(&threads[t], 0, simple_ticketlock_yield, 0); } int64_t start = ustime(); ticketlock_unlock(&simple_ticket); for (int t = 0; t < num; ++t) { pthread_join(threads[t], 0); } int64_t end = ustime(); printf("%6ld milliseconds\n", (end - start) / 1000); } }