static void ts_event_free(ethr_ts_event *ts_ev) { ETHR_ASSERT(!ts_ev->udata); ethr_spin_lock(&ts_ev_alloc_lock); ts_ev->next = free_ts_ev; free_ts_ev = ts_ev; ethr_spin_unlock(&ts_ev_alloc_lock); }
static ethr_ts_event *ts_event_alloc(void) { ethr_ts_event *ts_ev; ethr_spin_lock(&ts_ev_alloc_lock); if (free_ts_ev) { ts_ev = free_ts_ev; free_ts_ev = ts_ev->next; ethr_spin_unlock(&ts_ev_alloc_lock); } else { ethr_ts_event *ts_ev_pool_end; ethr_spin_unlock(&ts_ev_alloc_lock); ts_ev = ts_event_pool(ERTS_TS_EV_ALLOC_POOL_SIZE, &ts_ev_pool_end); if (!ts_ev) return NULL; ethr_spin_lock(&ts_ev_alloc_lock); ts_ev_pool_end->next = free_ts_ev; free_ts_ev = ts_ev->next; ethr_spin_unlock(&ts_ev_alloc_lock); } return ts_ev; }
void * st_thread(void *unused) { print_line("Aux thread tries to lock spinlock"); ethr_spin_lock(&st_spinlock); print_line("Aux thread locked spinlock"); ASSERT(st_data == 0); st_data = 1; print_line("Aux thread wrote"); print_line("Aux thread goes to sleep for 1 second"); do_sleep(1); print_line("Aux thread woke up"); ASSERT(st_data == 1); ethr_spin_unlock(&st_spinlock); print_line("Aux thread unlocked spinlock"); return NULL; }
static ERTS_INLINE void lc_lock(void) { ethr_spin_lock(&free_blocks_lock); }
static void spinlock_test(void) { int res; ethr_tid tid; print_line("Trying to initialize spinlock"); res = ethr_spinlock_init(&st_spinlock); ASSERT(res == 0); print_line("Initialized spinlock"); st_data = 0; print_line("Main thread tries to lock spinlock"); ethr_spin_lock(&st_spinlock); print_line("Main thread locked spinlock"); ASSERT(st_data == 0); print_line("Main thread about to create aux thread"); res = ethr_thr_create(&tid, st_thread, NULL, NULL); ASSERT(res == 0); print_line("Main thread created aux thread"); print_line("Main thread goes to sleep for 1 second"); do_sleep(1); print_line("Main thread woke up"); ASSERT(st_data == 0); ethr_spin_unlock(&st_spinlock); print_line("Main thread unlocked spinlock"); print_line("Main thread goes to sleep for 1 second"); do_sleep(1); print_line("Main thread woke up"); print_line("Main thread tries to lock spinlock"); ethr_spin_lock(&st_spinlock); print_line("Main thread locked spinlock"); ASSERT(st_data == 1); print_line("Main thread goes to sleep for 1 second"); do_sleep(1); print_line("Main thread woke up"); ASSERT(st_data == 1); ethr_spin_unlock(&st_spinlock); print_line("Main thread unlocked spinlock"); res = ethr_thr_join(tid, NULL); ASSERT(res == 0); print_line("Main thread joined aux thread"); res = ethr_spinlock_destroy(&st_spinlock); ASSERT(res == 0); print_line("Main thread destroyed spinlock"); }
static ERTS_INLINE void lc_lock_threads(void) { ethr_spin_lock(&lc_threads_lock); }