void skynet_mq_push(struct message_queue *q, struct skynet_message *message) { assert(message); LOCK(q) if (q->lock_session !=0 && message->session == q->lock_session) { _pushhead(q,message); } else { q->queue[q->tail] = *message; if (++ q->tail >= q->cap) { q->tail = 0; } if (q->head == q->tail) { expand_queue(q); } if (q->lock_session == 0) { if (q->in_global == 0) { q->in_global = MQ_IN_GLOBAL; skynet_globalmq_push(q); } } } UNLOCK(q) }
static void _pushhead(struct message_queue *q, struct skynet_message *message) { int head = q->head - 1; if (head < 0) { head = q->cap - 1; } if (head == q->tail) { expand_queue(q); --q->tail; head = q->cap - 1; } q->queue[head] = *message; q->head = head; // this api use in push a unlock message, so the in_global flags must not be 0 , // but the q is not exist in global queue. if (q->in_global == MQ_LOCKED) { skynet_globalmq_push(q); q->in_global = MQ_IN_GLOBAL; } else { assert(q->in_global == MQ_DISPATCHING); } q->lock_session = 0; }
void bidirection_breadth_search(bi_node **s, int m, int n, bi_node beg, bi_node end) {//矩阵s有m行n列,行下标从0到m-1,列下标从0到n-1 //visit_beg记录beg队列访问过的点 //visit_end记录end队列访问过的点 int **visit_beg = new int*[MAX]; int **visit_end = new int*[MAX]; for(int i = 0; i < m; ++ i){ visit_beg[i] = new int[MAX]; memset(visit_beg[i], 0, MAX * sizeof(int)); visit_end[i] = new int[MAX]; memset(visit_end[i], 0, MAX * sizeof(int)); } //q_beg是从起点bfs的队列 //q_end是从终点bfs的队列 deque<bi_node> q_beg, q_end; //起点终点分别进入两个队列 q_beg.push_back(beg), q_end.push_back(end); visit_beg[beg.b_y][beg.b_x] = 1, visit_end[end.b_y][end.b_x] = 1; //meet_pos返回q_beg和q_end中相遇的点 //meet_pos.first是q_beg队列中的相遇点 //meet_pos.second是q_end队列中的相遇点 //这两个点是相邻的,即这两个点相遇 pair<bi_node, bi_node> meet_pos; while(1){ if(q_beg.size() > q_end.size()){ //扩展q_end,检查要加入q_end的点是否已存在于q_beg中 //最后一个参数 2 标志被扩展的队列是q_end meet_pos = expand_queue(q_end, q_beg, s, m, n, visit_end, 2); if(meet_pos != pair<bi_node, bi_node>(*q_end.end(), *q_end.end())) //找到了相遇点 break; } else{ //扩展q_beg,检查要加入q_beg的点是否已存在于q_end中 //最后一个参数 1 标志被扩展的队列是q_beg meet_pos = expand_queue(q_beg, q_end, s, m, n, visit_beg, 1); if(meet_pos != pair<bi_node, bi_node>(*q_beg.end(), *q_beg.end())) break; } } //若找到相遇的两点,则传入print_road函数输出路径 print_road(meet_pos, s); }
static void _pushhead(struct socket_buffer_pool* pool, struct socket_buffer* sb) { int head = pool->head - 1; if (head < 0) { head = pool->cap - 1; } if (head == pool->tail) { expand_queue(pool); --pool->tail; head = pool->cap - 1; } pool->queue[head] = sb; pool->head = head; }
static void _pushhead(struct message_queue *q, struct skynet_message *message) { int head = q->head - 1; if (head < 0) { head = q->cap - 1; } if (head == q->tail) { expand_queue(q); --q->tail; head = q->cap - 1; } q->queue[head] = *message; q->head = head; _unlock(q); }
/* 向位于虚拟机栈位置一的包队列中插入一个完整的数据包. 当队列不够容纳此包时将扩张队列. clone 表示是否需要复制消息. * 参数: L 是 Lua 虚拟机栈; fd 是套接字 id; buffer 是数据缓冲; size 是数据的大小; clone 表示是否需要复制消息. */ static void push_data(lua_State *L, int fd, void *buffer, int size, int clone) { if (clone) { void * tmp = skynet_malloc(size); memcpy(tmp, buffer, size); buffer = tmp; } struct queue *q = get_queue(L); struct netpack *np = &q->queue[q->tail]; if (++q->tail >= q->cap) q->tail -= q->cap; np->id = fd; np->buffer = buffer; np->size = size; if (q->head == q->tail) { expand_queue(L, q); } }
static void _pushhead(struct message_queue *q, struct skynet_message *message) { int head = q->head - 1; if (head < 0) { head = q->cap - 1; } if (head == q->tail) { expand_queue(q); --q->tail; head = q->cap - 1; } q->queue[head] = *message; q->head = head; // this api use in push a unlock message, so the in_global flags must be 1 , but the q is not exist in global queue. assert(q->in_global); skynet_globalmq_push(q); }
static void build_tree(TreeElement *tree, size_t tree_len, uint8_t *code_lengths, unsigned int num_code_lengths) { TreeBuildData build; unsigned int code_len; build.tree = tree; build.tree_len = tree_len; // Start with a single entry in the queue - the root node // pointer. build.next_entry = 0; // We always have the root ... build.tree_allocated = 1; // Iterate over each possible code length. // Note: code_len == 0 is deliberately skipped over, as 0 // indicates "not used". code_len = 0; do { // Advance to the next code length by allocating extra // nodes to the tree - the slots waiting in the queue // will now be one level deeper in the tree (and the // codes 1 bit longer). expand_queue(&build); ++code_len; // Add all codes that have this length. } while (add_codes_with_length(&build, code_lengths, num_code_lengths, code_len)); }
/// 将数据压入 queue.queue 中 static void push_data(lua_State *L, int fd, void *buffer, int size, int clone) { // 如果需要复制, 则分配新的内存空间, 复制数据 if (clone) { void * tmp = skynet_malloc(size); memcpy(tmp, buffer, size); buffer = tmp; } // 将数据压入到 queue.queue 中 struct queue *q = get_queue(L); struct netpack *np = &q->queue[q->tail]; if (++q->tail >= q->cap) q->tail -= q->cap; np->id = fd; np->buffer = buffer; np->size = size; // 扩展队列空间 if (q->head == q->tail) { expand_queue(L, q); } }
void socket_buffer_pool_put(struct socket_buffer_pool* pool, struct socket_buffer* sb) { assert(sb); LOCK(pool) // 队列尾部插入 pool->queue[pool->tail] = sb; if (++ pool->tail >= pool->cap) { pool->tail = 0; } // 队列满 if (pool->head == pool->tail) { expand_queue(pool); } //printf("%d\n", socket_buffer_pool_length(pool)); if (socket_buffer_pool_length(pool)>=MAX_POOL_SIZE) { while (socket_buffer_pool_length(pool) > DEFAULT_POOL_SIZE) { struct socket_buffer* sb = pool->queue[pool->head]; if ( ++pool->head >= pool->cap) { pool->head = 0; } free_buffer(sb); } } UNLOCK(pool) }
void tqueue_push(struct tqueue *tq, int session, double time) { assert(session !=0); ++ tq->n; if (tq->head == tq->tail) { // queue is empty; tq->head = 0; tq->tail = 1; tq->q[0].session = session; tq->q[0].time = time; return; } struct session_time * st = &tq->q[tq->tail++]; if (tq->tail >= tq->cap) { tq->tail = 0; } if (tq->tail == tq->head) { st = expand_queue(tq); } st->session = session; st->time = time; // session must great than last one int i; for (i=1;i<tq->n;i++) { struct session_time *last = last_one(tq, st); if (session > last->session) return; // swap st, last struct session_time temp = *last; *last = *st; *st = temp; st = last; } }