static void _dispatch_queue(struct harbor *h, struct skynet_context * context, struct msg_queue * queue, uint32_t handle, const char name[GLOBALNAME_LENGTH] ) { int harbor_id = handle >> HANDLE_REMOTE_SHIFT; assert(harbor_id != 0); int fd = h->remote_fd[harbor_id]; if (fd < 0) { char tmp [GLOBALNAME_LENGTH+1]; memcpy(tmp, name , GLOBALNAME_LENGTH); tmp[GLOBALNAME_LENGTH] = '\0'; skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id); return; } struct msg * m = _pop_queue(queue); while (m) { struct remote_message_header * cookie = (struct remote_message_header *)(m->buffer + m->size - sizeof(*cookie)); cookie->destination |= (handle & HANDLE_MASK); _header_to_message(cookie, (uint32_t *)cookie); int err = _send_package(fd, m->buffer, m->size); if (err) { close(fd); h->remote_fd[harbor_id] = _connect_to(context, h->remote_addr[harbor_id]); if (h->remote_fd[harbor_id] < 0) { skynet_error(context, "Reconnect to harbor %d %s failed",harbor_id, h->remote_addr[harbor_id]); return; } } free(m->buffer); m = _pop_queue(queue); } }
static void _release_queue(struct msg_queue *queue) { if (queue == NULL) return; struct msg * m = _pop_queue(queue); while (m) { free(m->buffer); m = _pop_queue(queue); } free(queue->data); free(queue); }
void *_thread_work(void *thread_params) { if(thread_params == NULL) exit(-1); struct thread_params_t *params = (struct thread_params_t*)thread_params; int idx = params->idx; threadpool_t *pool = params->pool; thread_t *thread = &pool->threads[idx]; pthread_mutex_lock(&(thread->lock)); thread->running = 1; pthread_mutex_unlock(&(thread->lock)); pthread_mutex_lock(&(pool->lock)); pool->started++; pthread_mutex_unlock(&(pool->lock)); while(1) { /* Lock must be taken to wait on conditional variable */ pthread_mutex_lock(&(thread->lock)); while(thread->running == 1 && thread->task_num == 0) { pthread_cond_wait(&(thread->notify), &(thread->lock)); } //terminate this thread if(thread->running == 0) break; //no task to do if(thread->task_num == 0) { pthread_mutex_unlock(&(thread->lock)); continue; } /* Unlock */ pthread_mutex_unlock(&(thread->lock)); /* Grab our task */ thread_task_t *task = _pop_queue(thread->task_queue); if(task != NULL && task->function != NULL) { (*(task->function))(task->argument); } thread->task_num--; } fprintf(stderr, "thread :%u terminated\n", (unsigned)pthread_self()); pthread_mutex_unlock(&(thread->lock)); pthread_mutex_lock(&(pool->lock)); pool->started--; pthread_mutex_unlock(&(pool->lock)); pthread_exit(NULL); return(NULL); }
static void _dispatch_queue(struct harbor *h, struct msg_queue * queue, uint32_t handle, const char name[GLOBALNAME_LENGTH] ) { int harbor_id = handle >> HANDLE_REMOTE_SHIFT; assert(harbor_id != 0); struct skynet_context * context = h->ctx; int fd = h->remote_fd[harbor_id]; if (fd < 0) { char tmp [GLOBALNAME_LENGTH+1]; memcpy(tmp, name , GLOBALNAME_LENGTH); tmp[GLOBALNAME_LENGTH] = '\0'; skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id); return; } struct msg * m = _pop_queue(queue); while (m) { struct remote_message_header cookie; uint8_t *ptr = m->buffer + m->size - sizeof(cookie); memcpy(&cookie, ptr, sizeof(cookie)); cookie.destination |= (handle & HANDLE_MASK); _header_to_message(&cookie, ptr); _send_package(context, fd, m->buffer, m->size); m = _pop_queue(queue); } }
int tty_read(char *buf, int cnt) { int i = 0; if (_is_empty_queue(&console_tty.td_read_q)) { console_tty.td_read_q.tq_wait_task = current_task(); sleep(console_tty.td_read_q.tq_wait_task); } while(i < cnt) { if (_is_empty_queue(&console_tty.td_read_q)) { console_tty.td_read_q.tq_wait_task = current_task(); sleep(console_tty.td_read_q.tq_wait_task); } buf[i] = (char)_pop_queue(&console_tty.td_read_q); if (buf[i] == '\r') break; ++i; } return i + 1; }