void context_switch_test(void) { event_init(&context_switch_event, false, 0); event_init(&context_switch_done_event, false, 0); thread_detach_and_resume(thread_create("context switch idle", &context_switch_tester, (void *)1, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); thread_sleep(100); event_signal(&context_switch_event, true); event_wait(&context_switch_done_event); thread_sleep(100); event_unsignal(&context_switch_event); event_unsignal(&context_switch_done_event); thread_detach_and_resume(thread_create("context switch 2a", &context_switch_tester, (void *)2, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); thread_detach_and_resume(thread_create("context switch 2b", &context_switch_tester, (void *)2, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); thread_sleep(100); event_signal(&context_switch_event, true); event_wait(&context_switch_done_event); thread_sleep(100); event_unsignal(&context_switch_event); event_unsignal(&context_switch_done_event); thread_detach_and_resume(thread_create("context switch 4a", &context_switch_tester, (void *)4, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); thread_detach_and_resume(thread_create("context switch 4b", &context_switch_tester, (void *)4, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); thread_detach_and_resume(thread_create("context switch 4c", &context_switch_tester, (void *)4, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); thread_detach_and_resume(thread_create("context switch 4d", &context_switch_tester, (void *)4, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); thread_sleep(100); event_signal(&context_switch_event, true); event_wait(&context_switch_done_event); thread_sleep(100); }
size_t cbuf_read_char(cbuf_t *cbuf, char *c, bool block) { DEBUG_ASSERT(cbuf); DEBUG_ASSERT(c); enter_critical_section(); if (block) event_wait(&cbuf->event); // see if there's data available size_t ret = 0; if (cbuf->tail != cbuf->head) { *c = cbuf->buf[cbuf->tail]; cbuf->tail = INC_POINTER(cbuf, cbuf->tail, 1); if (cbuf->tail == cbuf->head) { // we've emptied the buffer, unsignal the event event_unsignal(&cbuf->event); } ret = 1; } exit_critical_section(); return ret; }
size_t cbuf_read_char(cbuf_t* cbuf, char* c, bool block) { DEBUG_ASSERT(cbuf); DEBUG_ASSERT(c); retry: if (block) { event_wait(&cbuf->event); } size_t ret = 0; { AutoSpinLock guard(&cbuf->lock); // see if there's data available if (cbuf->tail != cbuf->head) { *c = cbuf->buf[cbuf->tail]; cbuf->tail = inc_pointer(cbuf, cbuf->tail, 1); if (cbuf->tail == cbuf->head) { // we've emptied the buffer, unsignal the event event_unsignal(&cbuf->event); } ret = 1; } } if (block && ret == 0) { goto retry; } return ret; }
static void fastboot_notify(struct udc_gadget *gadget, unsigned event) { if (event == UDC_EVENT_ONLINE) { event_signal(&usb_online, 0); } else if (event == UDC_EVENT_OFFLINE) { event_unsignal(&usb_online); } }
/* * gracefully waits for radio to enter disabled state, which is the starting point for * any radio activity when swtiching between rx and tx mode */ static inline uint32_t _wait_radio_disabled(void) { if (NRF_RADIO->STATE != RADIO_STATE_STATE_Disabled) { // Only yield the thread if we need to NRF_RADIO->TASKS_DISABLE = 1; // Just to be safe, hit the stop button event_wait_timeout(&radio_disabled_evt,10); return 0; } event_unsignal(&radio_disabled_evt); // Unsignal the event in case we had a race return 0; }
size_t cbuf_read(cbuf_t *cbuf, void *_buf, size_t buflen, bool block) { size_t ret; char *buf = (char *)_buf; DEBUG_ASSERT(cbuf); DEBUG_ASSERT(_buf); enter_critical_section(); if (block) event_wait(&cbuf->event); // see if there's data available if (cbuf->tail != cbuf->head) { size_t pos = 0; // loop until we've read everything we need // at most this will make two passes to deal with wraparound while (pos < buflen && cbuf->tail != cbuf->head) { size_t read_len; if (cbuf->head > cbuf->tail) { // simple case where there is no wraparound read_len = MIN(cbuf->head - cbuf->tail, buflen - pos); } else { // read to the end of buffer in this pass read_len = MIN(valpow2(cbuf->len_pow2) - cbuf->tail, buflen - pos); } memcpy(buf + pos, cbuf->buf + cbuf->tail, read_len); cbuf->tail = INC_POINTER(cbuf, cbuf->tail, read_len); pos += read_len; } if (cbuf->tail == cbuf->head) { // we've emptied the buffer, unsignal the event event_unsignal(&cbuf->event); } ret = pos; } else { DEBUG_ASSERT(!block); ret = 0; } exit_critical_section(); return ret; }
uint32_t radio_send_cmd(uint32_t cmd) { #if RADIO_POLLED_MODE while (HWREG(RFC_DBELL_BASE + RFC_DBELL_O_CMDR) != 0) {} HWREG(RFC_DBELL_BASE + RFC_DBELL_O_RFACKIFG) = 0; HWREG(RFC_DBELL_BASE + RFC_DBELL_O_CMDR) = cmd; while (!HWREG(RFC_DBELL_BASE + RFC_DBELL_O_RFACKIFG)) {} HWREG(RFC_DBELL_BASE + RFC_DBELL_O_RFACKIFG) = 0; return HWREG(RFC_DBELL_BASE + RFC_DBELL_O_CMDSTA); #else while(HWREG(RFC_DBELL_BASE + RFC_DBELL_O_CMDR) != 0) {} HWREG(RFC_DBELL_BASE + RFC_DBELL_O_RFACKIFG) = 0; event_unsignal(&ack_evt); HWREG(RFC_DBELL_BASE + RFC_DBELL_O_CMDR) = cmd; event_wait(&ack_evt); #endif return HWREG(RFC_DBELL_BASE + RFC_DBELL_O_CMDSTA); }
static int dpc_thread_routine(void *arg) { for (;;) { event_wait(&dpc_event); enter_critical_section(); struct dpc *dpc = list_remove_head_type(&dpc_list, struct dpc, node); if (!dpc) event_unsignal(&dpc_event); exit_critical_section(); if (dpc) { // dprintf("dpc calling %p, arg %p\n", dpc->cb, dpc->arg); dpc->cb(dpc->arg); free(dpc); } } return 0; }
void display_server_pause(void) { event_unsignal(&e_continue); display_server_refresh(); }