END_TEST START_TEST (test_clear_corrupted) { callbackStatus = false; QUEUE_PUSH(uint8_t, &queue, (uint8_t) 128); QUEUE_PUSH(uint8_t, &queue, (uint8_t) '\0'); processQueue(&queue, callback); fail_unless(called); fail_unless(QUEUE_EMPTY(uint8_t, &queue)); }
int32_t msgqueue_push( struct msgqueue * self, struct task * task, uint8_t isnotify ) { int32_t rc = -1; uint32_t isbc = 0; pthread_mutex_lock( &self->lock ); rc = QUEUE_PUSH(taskqueue)(&self->queue, task); if ( isnotify != 0 ) { isbc = QUEUE_COUNT(taskqueue)(&self->queue); } pthread_mutex_unlock( &self->lock ); if ( rc == 0 && isbc == 1 ) { char buf[1] = {0}; if ( write( self->pushfd, buf, 1 ) != 1 ) { // } } return rc; }
static gboolean _unresponsive_timeout (GstClock * clock, GstClockTime time, GstClockID id, gpointer user_data) { GstAggregatorPad *aggpad; GstAggregator *self; if (user_data == NULL) return FALSE; aggpad = GST_AGGREGATOR_PAD (user_data); /* avoid holding the last reference to the parent element here */ PAD_LOCK_EVENT (aggpad); self = GST_AGGREGATOR (gst_pad_get_parent (GST_PAD (aggpad))); GST_DEBUG_OBJECT (aggpad, "marked unresponsive"); g_atomic_int_set (&aggpad->unresponsive, TRUE); if (self) { QUEUE_PUSH (self); gst_object_unref (self); } PAD_UNLOCK_EVENT (aggpad); return TRUE; }
word FindShortestPath(void) { #define PROCEED(n) if(!path_prev[n] && TilePassable(n)){path_prev[n] = m; QUEUE_PUSH(n)} word n = TILE_ENCODE(player->tilex, player->tiley); word m; byte i; _fmemset(path_prev,0,sizeof(path_prev)); QUEUE_RESET QUEUE_PUSH(n) path_prev[n] = WORD_MAX; while(!QUEUE_EMPTY) { m = QueuePop(); if(ObjectOfInterest(m)) { return m; } // add neighbours. don't worry about bounds checking, maps // should be guarded anyway n = m + 1; PROCEED(n) n = m - 1; PROCEED(n) n = m + MAPSIZE; PROCEED(n) n = m - MAPSIZE; PROCEED(n) } return 0; #undef PROCEED }
} END_TEST START_TEST(test_queue_push) { struct msg *msgs; struct msg m1, m2, m3; QUEUE_INIT(struct msg, msgs); m1.content = "abc"; m2.content = "def"; m3.content = "ghi"; QUEUE_PUSH(msgs, &m1); ck_assert_ptr_eq(msgs->qh.qc, m1.qh.qc); ck_assert_ptr_eq(msgs->qh.qc->front, &m1); ck_assert_ptr_eq(msgs->qh.qc->back, &m1); ck_assert_ptr_eq(msgs->qh.qc->backqh, &m1.qh); ck_assert_uint_eq(msgs->qh.qc->size, 1); ck_assert_ptr_eq(msgs->qh.next, NULL); QUEUE_PUSH(msgs, &m2); ck_assert_ptr_eq(msgs->qh.qc, m2.qh.qc); ck_assert_ptr_eq(msgs->qh.qc->front, &m1); ck_assert_ptr_eq(msgs->qh.qc->back, &m2); ck_assert_ptr_eq(msgs->qh.qc->backqh, &m2.qh); ck_assert_uint_eq(msgs->qh.qc->size, 2); ck_assert_ptr_eq(m1.qh.next, &m2); ck_assert_ptr_eq(m2.qh.next, NULL); QUEUE_PUSH(msgs, &m3); ck_assert_ptr_eq(msgs->qh.qc, m3.qh.qc); ck_assert_ptr_eq(msgs->qh.qc->front, &m1); ck_assert_ptr_eq(msgs->qh.qc->back, &m3); ck_assert_ptr_eq(msgs->qh.qc->backqh, &m3.qh); ck_assert_uint_eq(msgs->qh.qc->size, 3); ck_assert_ptr_eq(m1.qh.next, &m2); ck_assert_ptr_eq(m2.qh.next, &m3); ck_assert_ptr_eq(m3.qh.next, NULL); QUEUE_FREE(msgs); } END_TEST
END_TEST START_TEST (test_missing_callback) { QUEUE_PUSH(uint8_t, &queue, (uint8_t) 128); processQueue(&queue, NULL); fail_if(called); fail_if(QUEUE_EMPTY(uint8_t, &queue)); }
END_TEST START_TEST (test_success_clears) { callbackStatus = true; QUEUE_PUSH(uint8_t, &queue, (uint8_t) 128); processQueue(&queue, callback); fail_unless(called); fail_unless(QUEUE_EMPTY(uint8_t, &queue)); }
END_TEST START_TEST (test_failure_preserves) { callbackStatus = false; QUEUE_PUSH(uint8_t, &queue, (uint8_t) 128); processQueue(&queue, callback); fail_unless(called); fail_if(QUEUE_EMPTY(uint8_t, &queue)); }
END_TEST START_TEST (test_enqueue_no_room_for_crlf) { for(int i = 0; i < 503; i++) { QUEUE_PUSH(uint8_t, &queue, (uint8_t) 128); } char* message = "a message"; bool result = conditionalEnqueue(&queue, (uint8_t*)message, 9); fail_if(result); }
END_TEST START_TEST (test_enqueue_just_enough_room) { for(int i = 0; i < 501; i++) { QUEUE_PUSH(uint8_t, &queue, (uint8_t) 128); } char* message = "a message"; bool result = conditionalEnqueue(&queue, (uint8_t*)message, 9); fail_unless(result); }
// TODO see if we can do this with interrupts on the chipKIT // http://www.chipkit.org/forum/viewtopic.php?f=7&t=1088 void readFromSerial(SerialDevice* device, bool (*callback)(uint8_t*)) { if(device != NULL) { int bytesAvailable = ((HardwareSerial*)device->controller)->available(); if(bytesAvailable > 0) { for(int i = 0; i < bytesAvailable && !QUEUE_FULL(uint8_t, &device->receiveQueue); i++) { char byte = ((HardwareSerial*)device->controller)->read(); QUEUE_PUSH(uint8_t, &device->receiveQueue, (uint8_t) byte); } processQueue(&device->receiveQueue, callback); } } }
END_TEST START_TEST (test_enqueue_full) { for(int i = 0; i < 512; i++) { QUEUE_PUSH(uint8_t, &queue, (uint8_t) 128); } fail_unless(QUEUE_FULL(uint8_t, &queue)); char* message = "a message"; bool result = conditionalEnqueue(&queue, (uint8_t*)message, 10); fail_if(result); }
/* stop procthread */ void procthread_stop(PROCTHREAD *pth) { MESSAGE msg = {0}; if(pth && pth->message_queue) { msg.msg_id = MESSAGE_STOP; msg.parent = (void *)pth; QUEUE_PUSH(pth->message_queue, MESSAGE, &msg); DEBUG_LOGGER(pth->logger, "Ready for stopping procthread[%d]", pth->index); } return ; }
END_TEST START_TEST (test_full_clears) { for(int i = 0; i < 512; i++) { QUEUE_PUSH(uint8_t, &queue, (uint8_t) 128); } fail_unless(QUEUE_FULL(uint8_t, &queue)); callbackStatus = false; processQueue(&queue, callback); fail_unless(called); fail_unless(QUEUE_EMPTY(uint8_t, &queue)); }
void handleReceiveInterrupt() { while(!QUEUE_FULL(uint8_t, &listener.serial->receiveQueue)) { uint8_t byte; uint32_t received = UART_Receive(UART1_DEVICE, &byte, 1, NONE_BLOCKING); if(received > 0) { QUEUE_PUSH(uint8_t, &listener.serial->receiveQueue, byte); if(QUEUE_FULL(uint8_t, &listener.serial->receiveQueue)) { pauseReceive(); } } else { break; } } }
} END_TEST START_TEST(test_queue_size) { struct msg *msgs; struct msg m1, m2, m3; QUEUE_INIT(struct msg, msgs); m1.content = "abc"; m2.content = "def"; m3.content = "ghi"; ck_assert_uint_eq(QUEUE_SIZE(msgs), 0); QUEUE_PUSH(msgs, &m1); ck_assert_uint_eq(QUEUE_SIZE(msgs), 1); QUEUE_PUSH(msgs, &m2); ck_assert_uint_eq(QUEUE_SIZE(msgs), 2); QUEUE_PUSH(msgs, &m3); ck_assert_uint_eq(QUEUE_SIZE(msgs), 3); } END_TEST
void readFromHost(UsbDevice* usbDevice, bool (*callback)(uint8_t*)) { if(!usbDevice->device.HandleBusy(usbDevice->hostToDeviceHandle)) { if(usbDevice->receiveBuffer[0] != NULL) { for(int i = 0; i < usbDevice->outEndpointSize; i++) { if(!QUEUE_PUSH(uint8_t, &usbDevice->receiveQueue, usbDevice->receiveBuffer[i])) { debug("Dropped write from host -- queue is full\r\n"); } } processQueue(&usbDevice->receiveQueue, callback); } armForRead(usbDevice, usbDevice->receiveBuffer); } }
void readFromHost(UsbDevice* usbDevice, bool (*callback)(uint8_t*)) { uint8_t previousEndpoint = Endpoint_GetCurrentEndpoint(); Endpoint_SelectEndpoint(OUT_ENDPOINT_NUMBER); while(Endpoint_IsOUTReceived()) { for(int i = 0; i < usbDevice->outEndpointSize; i++) { if(!QUEUE_PUSH(uint8_t, &usbDevice->receiveQueue, Endpoint_Read_8())) { debug("Dropped write from host -- queue is full"); } } processQueue(&usbDevice->receiveQueue, callback); Endpoint_ClearOUT(); } Endpoint_SelectEndpoint(previousEndpoint); }
/* add new task */ int procthread_newtask(PROCTHREAD *pth, CALLBACK *task_handler, void *arg) { MESSAGE msg = {0}; int ret = -1; if(pth && pth->message_queue && task_handler) { msg.msg_id = MESSAGE_TASK; msg.parent = pth; msg.handler = task_handler; msg.arg = arg; QUEUE_PUSH(pth->message_queue, MESSAGE, &msg); DEBUG_LOGGER(pth->logger, "Added message task to procthreads[%d]", pth->index); ret = 0; } return ret; }
unsigned int _write(struct Process *sender, struct Process *receiver) { int bytes = (int)sender->stackptr[2+1]; char *buf = (char*)sender->stackptr[2+2]; if(pipe_push_message(&(receiver->msgs),bytes,buf)) { /* the queue is full */ sender->blocked = (unsigned int)&_write; QUEUE_PUSH(receiver->writers,sender); } else { sender->stackptr[2+0] = 0; /* not using return value yet */ /* unblock reader */ if(receiver->blocked == (unsigned int)&_read) { receiver->blocked = 0; _read(receiver); } } return sender->blocked; }
static void _release_pad (GstElement * element, GstPad * pad) { GstBuffer *tmpbuf; GstAggregator *self = GST_AGGREGATOR (element); GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad); GST_INFO_OBJECT (pad, "Removing pad"); g_atomic_int_set (&aggpad->priv->flushing, TRUE); tmpbuf = gst_aggregator_pad_steal_buffer (aggpad); gst_buffer_replace (&tmpbuf, NULL); gst_element_remove_pad (element, pad); /* Something changed make sure we try to aggregate */ QUEUE_PUSH (self); }
int32_t session_append( struct session * self, struct message * message ) { int32_t rc = -1; ioservice_t * service = &self->service; char * buf = message_get_buffer( message ); uint32_t nbytes = message_get_length( message ); // 数据改造(加密 or 压缩) char * buffer = service->transform( self->context, (const char *)buf, &nbytes ); if ( buffer == buf ) { // 消息未进行改造 // 添加到会话的发送列表中 rc = QUEUE_PUSH(sendqueue)(&self->sendqueue, &message); if ( rc == 0 ) { // 注册写事件, 处理发送队列 session_add_event( self, EV_WRITE ); } } else if ( buffer != NULL ) { // 消息改造成功 rc = _send( self, buffer, nbytes ); if ( rc >= 0 ) { // 改造后的消息已经单独发送 message_add_success( message ); } free( buffer ); } if ( rc < 0 ) { message_add_failure( message, self->id ); } return rc; }
/* add new transaction */ int procthread_newtransaction(PROCTHREAD *pth, CONN *conn, int tid) { MESSAGE msg = {0}; int ret = -1; if(pth && pth->message_queue && conn) { msg.msg_id = MESSAGE_TRANSACTION; msg.fd = conn->fd; msg.tid = tid; msg.handler = conn; msg.parent = (void *)pth; QUEUE_PUSH(pth->message_queue, MESSAGE, &msg); DEBUG_LOGGER(pth->logger, "Added message transaction[%d] to %s:%d via %d total %d", tid, conn->ip, conn->port, conn->fd, QTOTAL(pth->message_queue)); ret = 0; } return ret; }
/* Add connection message */ int procthread_addconn(PROCTHREAD *pth, CONN *conn) { MESSAGE msg = {0}; int ret = -1; if(pth && pth->message_queue && conn) { msg.msg_id = MESSAGE_NEW_SESSION; msg.fd = conn->fd; msg.handler = (void *)conn; msg.parent = (void *)pth; QUEUE_PUSH(pth->message_queue, MESSAGE, &msg); DEBUG_LOGGER(pth->logger, "Ready for adding msg[%s] connection[%s:%d] via %d total %d", MESSAGE_DESC(MESSAGE_NEW_SESSION), conn->ip, conn->port, conn->fd, QTOTAL(pth->message_queue)); ret = 0; } return ret; }
static gboolean _stop_srcpad_task (GstAggregator * self, GstEvent * flush_start) { gboolean res = TRUE; GST_INFO_OBJECT (self, "%s srcpad task", flush_start ? "Pausing" : "Stopping"); self->priv->running = FALSE; QUEUE_PUSH (self); if (flush_start) { res = gst_pad_push_event (self->srcpad, flush_start); } gst_pad_stop_task (self->srcpad); QUEUE_FLUSH (self); return res; }
/* push message to message queue */ int conn_push_message(CONN *conn, int message_id) { MESSAGE msg = {0}; int ret = -1; if(conn && (message_id & MESSAGE_ALL) ) { msg.msg_id = message_id; msg.fd = conn->fd; msg.handler = conn; msg.parent = conn->parent; QUEUE_PUSH(conn->message_queue, MESSAGE, &msg); DEBUG_LOGGER(conn->logger, "Pushed message[%s] to message_queue[%08x] " "on %s:%d via %d total %d handler[%08x] parent[%08x]", MESSAGE_DESC(message_id), conn->message_queue, conn->ip, conn->port, conn->fd, QTOTAL(conn->message_queue), conn, conn->parent); ret = 0; } return ret; }
int32_t _send( struct session * self, char * buf, uint32_t nbytes ) { int32_t rc = 0; if ( self->status&SESSION_EXITING ) { // 等待关闭的连接 return -1; } // 判断session是否繁忙 if ( !(self->status&SESSION_WRITING) && session_sendqueue_count(self) == 0 ) { // 直接发送 rc = channel_send( self, buf, nbytes ); if ( rc == nbytes ) { // 全部发送出去 return rc; } // 为什么发送错误没有直接终止会话呢? // 该接口有可能在ioservice_t中调用, 直接终止会话后, 会引发后续对会话的操作崩溃 } // 创建message, 添加到发送队列中 struct message * message = message_create(); if ( message == NULL ) { return -2; } message_add_buffer( message, buf+rc, nbytes-rc ); message_add_receiver( message, self->id ); QUEUE_PUSH(sendqueue)(&self->sendqueue, &message); session_add_event( self, EV_WRITE ); return rc; }
/* push chunk file */ int conn_push_file(CONN *conn, char *filename, long long offset, long long size) { int ret = -1; CHUNK *cp = NULL; CONN_CHECK_RET(conn, ret); if(conn && conn->send_queue && filename && offset >= 0 && size > 0) { CK_INIT(cp); if(cp) { CK_FILE(cp, filename, offset, size); QUEUE_PUSH(conn->send_queue, PCHUNK, &cp); if((QTOTAL(conn->send_queue)) > 0 ) conn->event->add(conn->event, E_WRITE); DEBUG_LOGGER(conn->logger, "Pushed file[%s] [%lld][%lld] to " "send_queue total %d on %s:%d via %d ", filename, offset, size, QTOTAL(conn->send_queue), conn->ip, conn->port, conn->fd); ret = 0; } } return ret; }
/* push chunk */ int conn_push_chunk(CONN *conn, void *data, int size) { int ret = -1; CHUNK *cp = NULL; CONN_CHECK_RET(conn, ret); if(conn && conn->send_queue && data && size > 0) { CK_INIT(cp); if(cp) { CK_MEM(cp, size); CK_MEM_COPY(cp, data, size); QUEUE_PUSH(conn->send_queue, PCHUNK, &cp); } if(QTOTAL(conn->send_queue) > 0 ) conn->event->add(conn->event, E_WRITE); DEBUG_LOGGER(conn->logger, "Pushed chunk size[%d] to send_queue " "total %d on %s:%d via %d ", size, QTOTAL(conn->send_queue), conn->ip, conn->port, conn->fd); ret = 0; } return ret; }
} END_TEST START_TEST(test_queue_pop) { struct msg *msgs; struct msg src1, src2, src3, *dst1, *dst2, *dst3; QUEUE_INIT(struct msg, msgs); src1.content = "abc"; src2.content = "def"; src3.content = "ghi"; QUEUE_PUSH(msgs, &src1); QUEUE_POP(msgs, dst1); ck_assert_ptr_eq(dst1, &src1); ck_assert_ptr_eq(msgs->qh.qc->front, NULL); ck_assert_ptr_eq(msgs->qh.qc->back, &src1); // unchanged w/ pop ck_assert_ptr_eq(msgs->qh.qc->backqh, &src1.qh); // unchanged w/ pop ck_assert_uint_eq(msgs->qh.qc->size, 0); ck_assert_ptr_eq(dst1->qh.next, msgs->qh.qc->front); ck_assert_str_eq(src1.content, dst1->content); QUEUE_PUSH(msgs, &src1); QUEUE_PUSH(msgs, &src2); QUEUE_POP(msgs, dst1); ck_assert_ptr_eq(dst1, &src1); ck_assert_ptr_eq(msgs->qh.qc->front, &src2); ck_assert_ptr_eq(msgs->qh.qc->back, &src2); // unchanged w/ pop ck_assert_ptr_eq(msgs->qh.qc->backqh, &src2.qh); // unchanged w/ pop ck_assert_uint_eq(msgs->qh.qc->size, 1); ck_assert_ptr_eq(dst1->qh.next, msgs->qh.qc->front); ck_assert_str_eq(src1.content, dst1->content); QUEUE_POP(msgs, dst2); ck_assert_ptr_eq(dst2, &src2); ck_assert_ptr_eq(msgs->qh.qc->front, NULL); ck_assert_ptr_eq(msgs->qh.qc->back, &src2); // unchanged w/ pop ck_assert_ptr_eq(msgs->qh.qc->backqh, &src2.qh); // unchanged w/ pop ck_assert_uint_eq(msgs->qh.qc->size, 0); ck_assert_ptr_eq(dst2->qh.next, msgs->qh.qc->front); ck_assert_str_eq(src2.content, dst2->content); QUEUE_PUSH(msgs, &src1); QUEUE_PUSH(msgs, &src2); QUEUE_PUSH(msgs, &src3); QUEUE_POP(msgs, dst1); ck_assert_ptr_eq(dst1, &src1); ck_assert_ptr_eq(msgs->qh.qc->front, &src2); ck_assert_ptr_eq(msgs->qh.qc->back, &src3); // unchanged w/ pop ck_assert_ptr_eq(msgs->qh.qc->backqh, &src3.qh); // unchanged w/ pop ck_assert_uint_eq(msgs->qh.qc->size, 2); ck_assert_ptr_eq(dst1->qh.next, msgs->qh.qc->front); ck_assert_str_eq(src1.content, dst1->content); QUEUE_POP(msgs, dst2); ck_assert_ptr_eq(dst2, &src2); ck_assert_ptr_eq(msgs->qh.qc->front, &src3); ck_assert_ptr_eq(msgs->qh.qc->back, &src3); // unchanged w/ pop ck_assert_ptr_eq(msgs->qh.qc->backqh, &src3.qh); // unchanged w/ pop ck_assert_uint_eq(msgs->qh.qc->size, 1); ck_assert_ptr_eq(dst2->qh.next, msgs->qh.qc->front); ck_assert_str_eq(src2.content, dst2->content); QUEUE_POP(msgs, dst3); ck_assert_ptr_eq(dst3, &src3); ck_assert_ptr_eq(msgs->qh.qc->front, NULL); ck_assert_ptr_eq(msgs->qh.qc->back, &src3); // unchanged w/ pop ck_assert_ptr_eq(msgs->qh.qc->backqh, &src3.qh); // unchanged w/ pop ck_assert_uint_eq(msgs->qh.qc->size, 0); ck_assert_ptr_eq(dst3->qh.next, msgs->qh.qc->front); ck_assert_str_eq(src3.content, dst3->content); QUEUE_FREE(msgs); } END_TEST