/* * Como adaptar? * * TODO (?) da glibc: for adaptive mutexes: when releasing, determine * whether somebody spins. If yes, for a short time release lock. If * someone else locks no wakeup syscall needed. * */ #define _GNU_SOURCE #include #include #include #include #include "myfutex.h" int lock_val = 0; int N_spins = 4; void lock() { int c; int n_spins = N_spins; while (n_spins > 0 && (c = __sync_val_compare_and_swap(&lock_val, 0, 1)) != 0) { n_spins--; } while ((c = __sync_val_compare_and_swap(&lock_val, 0, 1)) != 0) { futex_wait(&lock_val, 1); } } void unlock() { lock_val = 0; futex_wake(&lock_val, 1); } void* f_thread0(void *v) { lock(); printf("Thread 0.\n"); sleep(5); unlock(); return NULL; } void* f_thread1(void *v) { lock(); printf("Thread 1.\n"); sleep(5); unlock(); return NULL; } int main() { pthread_t thr0, thr1; pthread_create(&thr0, NULL, f_thread0, NULL); pthread_create(&thr1, NULL, f_thread1, NULL); pthread_exit(0); }