2 Check that a thread which yields with pause (rep;nop) makes less
3 progress against a pure spinner.
10 static pthread_mutex_t m_go
= PTHREAD_MUTEX_INITIALIZER
;
11 static pthread_cond_t c_go
= PTHREAD_COND_INITIALIZER
;
12 static pthread_cond_t c_running
= PTHREAD_COND_INITIALIZER
;
14 static volatile int alive
, running
;
19 static void *spinner(void *v
)
21 pthread_mutex_lock(&m_go
);
23 pthread_cond_wait(&c_go
, &m_go
);
25 pthread_cond_signal(&c_running
);
26 pthread_mutex_unlock(&m_go
);
34 static void *rep_nopper(void *v
)
36 pthread_mutex_lock(&m_go
);
38 pthread_cond_wait(&c_go
, &m_go
);
40 pthread_cond_signal(&c_running
);
41 pthread_mutex_unlock(&m_go
);
45 // This gives a hint to a P4, telling it to pause
46 // (ie. we're in a spin-wait loop)
47 asm volatile ("rep; nop" : : : "memory");
57 pthread_create(&a
, NULL
, spinner
, NULL
);
58 pthread_create(&b
, NULL
, rep_nopper
, NULL
);
60 /* make sure both threads start at the same time */
61 pthread_mutex_lock(&m_go
);
63 pthread_cond_broadcast(&c_go
);
65 /* make sure they both get started */
67 pthread_cond_wait(&c_running
, &m_go
);
68 pthread_mutex_unlock(&m_go
);
73 pthread_join(a
, NULL
);
74 pthread_join(b
, NULL
);
77 printf("spin=%d rep_nop=%d rep_nop:spin ratio: %g\n",
78 spin
, rep_nop
, (float)rep_nop
/ spin
);
83 printf("FAIL spin=%d rep_nop=%d rep_nop:spin ratio: %g\n",
84 spin
, rep_nop
, (float)rep_nop
/ spin
);