END_TEST START_TEST (test_to_rb_from_fd) { int fds[2]; ck_assert(pipe(fds) == 0); ck_assert(write(fds[1], sample16, 16) == 16); ck_assert(rb_is_empty(rb)); ck_assert_int_eq(rb_used(rb), 0); ck_assert_int_eq(rb_space(rb), 16); ssize_t result = rb_read(rb, fds[0], 10); ck_assert_int_eq(result, 10); ck_assert_int_eq(rb_used(rb), 10); ck_assert_int_eq(rb_space(rb), 6); ck_assert(!rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); result = rb_read(rb, fds[0], 10); ck_assert_int_eq(result, 6); ck_assert_int_eq(rb_used(rb), 16); ck_assert_int_eq(rb_space(rb), 0); ck_assert(!rb_is_empty(rb)); ck_assert(rb_is_full(rb)); ck_assert(memcmp(rb->buffer, "0123456789ABCDEF", 16) == 0); }
static inline void usart_tx_irq(const usart_dev *dev) { #ifdef ISR_PERF uint32_t t=stopwatch_getticks(); #endif /* Check USART Transmit Data Register Empty Interrupt */ uint16_t sr = dev->USARTx->SR; if( (sr & USART_F_TXE) && (dev->USARTx->CR1 & USART_MASK_TXEIE) ){ if (dev->txrb && !rb_is_empty(dev->txrb)) { dev->USARTx->DR = rb_remove(dev->txrb); dev->state->txbusy = 1; } else { /* Disable the USART Transmit Data Register Empty Interrupt */ dev->USARTx->CR1 &= ~USART_MASK_TXEIE; dev->state->txbusy = 0; // nops needed to deactivate the irq before irq handler is left asm volatile("nop"); asm volatile("nop"); } } #ifdef ISR_PERF t = stopwatch_getticks() - t; isr_time += t; if(t>max_isr_time) max_isr_time=t; #endif }
END_TEST START_TEST (test_to_scattered_memory_from_rb) { rb_append(rb, sample16, 4); rb_append(rb, sample16, 8); rb_discard(rb, 4); rb_append(rb, sample16+8, 8); char outbuf[32]; struct iovec iov[3]; iov[0].iov_base = outbuf+12; iov[0].iov_len = 2; iov[1].iov_base = outbuf+7; iov[1].iov_len = 5; iov[2].iov_base = outbuf; iov[2].iov_len = 7; size_t result = rb_extractv(rb, iov, 3); ck_assert_int_eq(result, 14); ck_assert_int_eq(rb_space(rb), 14); ck_assert(!rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); ck_assert(memcmp(outbuf, "789ABCD2345601", 14) == 0); }
END_TEST START_TEST (test_to_fd_from_rb) { int fds[2]; ck_assert(pipe(fds) == 0); rb_append(rb, sample16, 16); ck_assert(rb_is_full(rb)); ssize_t result = rb_write(rb, fds[1], 10); ck_assert_int_eq(result, 10); ck_assert_int_eq(rb_used(rb), 6); ck_assert_int_eq(rb_space(rb), 10); char outbuf[32]; ck_assert_int_eq(read(fds[0], outbuf, sizeof(outbuf)), 10); ck_assert(memcmp(outbuf, "0123456789", 10) == 0); result = rb_write(rb, fds[1], 10); ck_assert_int_eq(result, 6); ck_assert(rb_is_empty(rb)); ck_assert_int_eq(read(fds[0], outbuf, sizeof(outbuf)), 6); ck_assert(memcmp(outbuf, "ABCDEF", 6) == 0); }
int usb_read(void * buf, unsigned int nbytes) { U16 toread = nbytes; U16 received; U8 *buffer8 = (U8 *)buf; if (usb_ready == 0) { return 0; } received = 0; if (!rxfifo) { return 0; } // blocking mode while(toread) { /* get characters from ring buffer */ if (!rb_is_empty(rxfifo)) { *buffer8 = rb_remove(rxfifo); received++; buffer8++; toread--; } } return received; }
int main(int argc, char* argv[]) { unsigned int left = 0; unsigned int size = 0; unsigned int i = 0; struct ring_buffer rb; char buffer[100]; for(i = 0; i < 100; ++i) { buffer[i] = i; } rb_alloc(&rb, 100); for(i = 0; i < 100; ++i) { rb_write(&rb, buffer, 33); rb_read(&rb, buffer, 32); } printf("%d,%d %d,%d\n", rb_length(&rb), rb_left(&rb), rb_is_full(&rb), rb_is_empty(&rb)); rb_free(&rb); return 0; }
int ringbuffer_empty(RingBuffer *rb) { int ret; pthread_mutex_lock(&rb->lock); ret = rb_is_empty(rb); pthread_mutex_unlock(&rb->lock); return ret; }
END_TEST START_TEST (test_clear) { rb_append(rb, sample16, 16); ck_assert(rb_is_full(rb)); rb_clear(rb); ck_assert(rb_is_empty(rb)); }
END_TEST START_TEST (test_to_memory_from_rb) { rb_append(rb, sample16, 16); char outbuf[32]; size_t result = rb_extract(rb, outbuf, 10); ck_assert_int_eq(result, 10); ck_assert_int_eq(rb_space(rb), 10); ck_assert(!rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); result = rb_extract(rb, outbuf + 10, 10); ck_assert_int_eq(result, 6); ck_assert(rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); ck_assert(memcmp(outbuf, "0123456789ABCDEF", 16) == 0); }
void ipc_read(IPC* ipc) { for (;;) { error(ERROR_OK); if (rb_is_empty(&__GLOBAL->process->ipcs)) svc_call(SVC_IPC_WAIT, ANY_HANDLE, ANY_CMD, ANY_HANDLE); ipc_peek(__GLOBAL->process->ipcs.tail, ipc); if (ipc->cmd == HAL_REQ(HAL_SYSTEM, IPC_PING)) ipc_write(ipc); else break; } }
void scsis_error_get(SCSIS* scsis, SCSIS_ERROR *err) { unsigned int idx; if (rb_is_empty(&scsis->rb_error)) { err->key_sense = SENSE_KEY_NO_SENSE; err->ascq = ASCQ_NO_ADDITIONAL_SENSE_INFORMATION; } else { idx = rb_get(&scsis->rb_error); err->key_sense = scsis->errors[idx].key_sense; err->ascq = scsis->errors[idx].ascq; } }
END_TEST START_TEST (test_to_rb_from_memory_wrapped_around) { ck_assert_int_eq(rb_append(rb, sample16, 8), 8); ck_assert_int_eq(rb_discard(rb, 7), 7); ck_assert_int_eq(rb_used(rb), 1); size_t result = rb_append(rb, sample16, 16); ck_assert_int_eq(result, 15); ck_assert(!rb_is_empty(rb)); ck_assert(rb_is_full(rb)); ck_assert(memcmp(rb->buffer, "89ABCDE701234567", 16) == 0); }
END_TEST START_TEST (test_to_rb_from_memory_in_center) { ck_assert_int_eq(rb_append(rb, sample16, 8), 8); ck_assert_int_eq(rb_discard(rb, 7), 7); ck_assert_int_eq(rb_append(rb, sample16, 11), 11); ck_assert_int_eq(rb_used(rb), 12); ck_assert_int_eq(rb_space(rb), 4); size_t result = rb_append(rb, sample16, 16); ck_assert_int_eq(result, 4); ck_assert(!rb_is_empty(rb)); ck_assert(rb_is_full(rb)); ck_assert(memcmp(rb->buffer, "89A0123701234567", 16) == 0); }
static inline void usart_tx_irq(usart_dev *dev) { /* Check USART Transmit Data Register Empty Interrupt */ if (USART_GetITStatus(dev->USARTx, USART_IT_TXE) != RESET) { if (!rb_is_empty(dev->txrb)) { dev->USARTx->DR = rb_remove(dev->txrb); dev->txbusy = 1; } else { /* Disable the USART Transmit Data Register Empty Interrupt */ USART_ITConfig(dev->USARTx, USART_IT_TXE, DISABLE); dev->txbusy = 0; } } }
END_TEST START_TEST (test_to_memory_from_rb_wrapped_around) { rb_append(rb, sample16, 16); rb_discard(rb, 10); rb_append(rb, sample16, 6); ck_assert_int_eq(rb_space(rb), 4); char outbuf[32]; size_t result = rb_extract(rb, outbuf, 16); ck_assert_int_eq(result, 12); ck_assert(rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); ck_assert(memcmp(outbuf, "ABCDEF012345", 12) == 0); }
int ringbuffer_get(RingBuffer *rb, void *elem) { pthread_mutex_lock(&rb->lock); rb->waiters++; while (rb_is_empty(rb)) { pthread_cond_wait(&rb->cond, &rb->lock); } rb->waiters--; rb->used--; memcpy(elem, rb_item_at(rb, rb->start), rb->elem_size); rb->dump(rb->dump_ud, rb_item_at(rb, rb->start)); rb->start = (rb->start + 1) % rb->size; pthread_mutex_unlock(&rb->lock); return 0; }
END_TEST START_TEST (test_to_rb_from_scattered_memory) { ck_assert_int_eq(rb_append(rb, sample16, 8), 8); ck_assert_int_eq(rb_discard(rb, 7), 7); ck_assert_int_eq(rb_used(rb), 1); struct iovec iov[3]; iov[0].iov_base = sample16 + 4; iov[0].iov_len = 5; iov[1].iov_base = sample16; iov[1].iov_len = 4; iov[2].iov_base = sample16 + 9; iov[2].iov_len = 7; size_t result = rb_appendv(rb, iov, 3); ck_assert_int_eq(result, 15); ck_assert(!rb_is_empty(rb)); ck_assert(rb_is_full(rb)); ck_assert(memcmp(rb->buffer, "39ABCDE745678012", 16) == 0); }
int buffering_flush(neo4j_iostream_t *stream) { struct buffering_iostream *ios = container_of(stream, struct buffering_iostream, _iostream); if (ios->delegate == NULL) { errno = EPIPE; return -1; } if (!rb_is_empty(ios->sndbuf)) { struct iovec iov[2]; unsigned int iovcnt = rb_data_iovec(ios->sndbuf, iov, rb_size(ios->sndbuf)); size_t written; if (neo4j_ios_writev_all(ios->delegate, iov, iovcnt, &written)) { rb_discard(ios->sndbuf, written); return -1; } rb_clear(ios->sndbuf); } return neo4j_ios_flush(ios->delegate); }
/* edogaldo: Waits for the transmission of outgoing serial data to complete (Arduino 1.0 api specs) */ void HardwareSerial::flush(void) { while(!rb_is_empty(this->usart_device->wb)); // wait for TX buffer empty while(!((this->usart_device->regs->SR) & (1<<USART_SR_TC_BIT))); // wait for TC (Transmission Complete) flag set }
void at45_process_done_q() { while(!rb_is_empty(&at45_tx_done_q)) { at45_get_data(); } }