void rtp_recorder_destroy(struct rtp_recorder_t* rr) { mutex_lock(rr->timer_mutex); if (rr->synchronization_thread != NULL) { condition_signal(rr->timer_cond); mutex_unlock(rr->timer_mutex); thread_join(rr->synchronization_thread); mutex_lock(rr->timer_mutex); thread_destroy(rr->synchronization_thread); rr->synchronization_thread = NULL; } mutex_unlock(rr->timer_mutex); rtp_socket_destroy(rr->streaming_socket); rtp_socket_destroy(rr->control_socket); rtp_socket_destroy(rr->timing_socket); sockaddr_destroy(rr->remote_control_end_point); sockaddr_destroy(rr->remote_timing_end_point); mutex_destroy(rr->timer_mutex); condition_destroy(rr->timer_cond); free(rr); }
//push消息并执行同步操作 static inline void msgque_sync_push(ptq_t ptq) { assert(ptq->mode == MSGQ_WRITE); if(ptq->mode != MSGQ_WRITE) return; msgque_t que = ptq->que; mutex_lock(que->mtx); uint8_t empty = llist_is_empty(&que->share_que); llist_swap(&que->share_que,&ptq->local_que); if(empty){ struct dnode *l = dlist_pop(&que->blocks); if(l){ //if there is a block per_thread_struct wake it up ptq_t block_ptq = (ptq_t)l; mutex_unlock(que->mtx); condition_signal(block_ptq->cond); } } //对所有在can_interrupt中的元素调用回调 while(!dlist_empty(&que->can_interrupt)) { ptq_t ptq = (ptq_t)dlist_pop(&que->can_interrupt); ptq->read_que.notify_function(ptq->read_que.ud); } mutex_unlock(que->mtx); }
//加任务 void threadpool_add_task(threadpool_t *pool, void *(*run)(void *arg),void *arg) { task_t *newstask = (task_t *)malloc(sizeof(task_t)); newstask->run = run; newstask->arg = arg; newstask -> next = NULL; condition_lock(&pool -> ready); //将任务添加到对列中 if(pool -> first ==NULL) { pool -> first = newstask; } else pool -> last -> next = newstask; pool -> last = newstask; //如果有等待线程,则唤醒其中一个 if(pool -> idle > 0) { condition_signal(&pool -> ready); } else if(pool -> counter < pool -> max_threads) { pthread_t tid; pthread_create(&tid,NULL,thread_routine,pool); pool -> counter++; } condition_unlock(&pool -> ready); }
void osfmach3_setrun( struct task_struct *task) { ASSERT(holding_uniproc()); ASSERT(task->state != TASK_ZOMBIE); ASSERT(task->state >= TASK_RUNNING && task->state <= TASK_SWAPPING); condition_signal(&task->osfmach3.thread->mach_wait_channel); }
/* increase available_resources by count */ int increase_count(int count) { sem_wait(&lock); printf("i curr:%d +%d\n", available_resources, count); available_resources += count; condition_signal(); if (next_count > 0) sem_post(&next); else sem_post(&lock); return 0; }
static error_t hurdio_assert_dtr () { if (ioport == MACH_PORT_NULL) { assert_dtr = 1; condition_signal (&hurdio_assert_dtr_condition); } return 0; }
void thread_resume(thread_t t) { pthread_t self = pthread_self(); if(self == t->threadid) return;//只能有其它线程resume mutex_lock(t->mtx); if(t->is_suspend) { t->is_suspend = 0; condition_signal(t->cond); } mutex_unlock(t->mtx); }
static void* start_routine(void *_) { struct start_arg *starg = (struct start_arg*)_; void *arg = starg->arg; void*(*routine)(void*) = starg->routine; mutex_lock(starg->mtx); if(!starg->running){ starg->running = 1; mutex_unlock(starg->mtx); condition_signal(starg->cond); } void *ret = routine(arg); clear_thdmailbox(); return ret; }
/** * @brief Get an event * * Wait for an event from the standard input. * * @param ui User Interface. */ static void ui_read_input(UI *ui) { char *buffer; char cmd[8]; Event *event = ui->event; Play *play = ui->play; buffer = string_read_line(stdin); if (buffer == NULL) { if (ferror(stdin)) { if (errno == EINTR) clearerr(stdin); else fatal_error("stdin is broken\n"); } else if (feof(stdin)) { buffer = string_duplicate("eof"); } event->loop = false; } else { if (ui->type == UI_GTP) gtp_preprocess(buffer); parse_word(buffer, cmd, 5); string_to_lowercase(cmd); if (strcmp(cmd, "stop") == 0) { event_clear_messages(event); info("<stop>\n"); play_stop(play); if (ui->type == UI_GGS) play_stop(play + 1); } else if (ui->type == UI_NBOARD && strcmp(cmd, "ping") == 0) { play_stop(play); } else if (ui->type == UI_XBOARD && strcmp(cmd, "?") == 0) { play_stop(play); } else { if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0) { event_clear_messages(event); play_stop(play); if (ui->type == UI_GGS) play_stop(play + 1); event->loop = false; } } } if (buffer) { lock(event); event_add_message(event, buffer); condition_signal(event); unlock(event); } }
void WA_unlock(WA_recursiveLock _lock) #endif { CThreadRecursiveLock *lock = (CThreadRecursiveLock *)_lock; #ifdef EXTRA_DEBUGGING_LOGS if (_lock != logMutex) WOLog(WO_DBG, "thread unlocking %s from %s:%d", cthread_self(), lock->name, file, line); #endif mutex_lock(&lock->m); lock->lockCount--; if (lock->lockCount == 0) { lock->lockingThread = NULL; condition_signal(&lock->c); } mutex_unlock(&lock->m); }
void thread_resume(thread_t t) { pthread_t self = pthread_self(); #ifdef _MINGW_ if(self.p != t->threadid.p || self.x != t->threadid.x) return; #else if(self == t->threadid) return;//只能有其它线程resume #endif mutex_lock(t->mtx); if(t->is_suspend) { t->is_suspend = 0; condition_signal(t->cond); } mutex_unlock(t->mtx); }
static inline mq_sync_push(mq_t m,struct per_thread_struct *pts) { mutex_lock(m->mtx); uint8_t empty = link_list_is_empty(m->share_list); link_list_swap(m->share_list,pts->local_push_q); if(empty) { struct double_link_node *l = double_link_pop(&m->blocks); if(l) { //if there is block per_thread_struct wake it up struct per_thread_struct *block_pts = (struct per_thread_struct *)((uint8_t*)l - sizeof(struct list_node)); mutex_unlock(m->mtx); condition_signal(block_pts->cond); return; } } mutex_unlock(m->mtx); }
/* 向线程池中添加线程 */ void threadpool_add_task( threadpool_t* pool, void* (*run)( void* arg ), void* arg ) { task_t* newtask = ( task_t* ) malloc ( sizeof( task_t ) ) ; // 创建线程控制块 newtask->run = run ; // 设置线程的回调函数 newtask->arg = arg ; // 设置回调函数的参数 newtask->next = NULL ; // 新加入的线程会被添加到链表尾部 /************************** 进入临界区 ***********************/ condition_lock( &pool->ready ) ; // 拿到互斥锁 // 把新创建的 TCB 添加到线程链表中 if ( pool->first == NULL ) { // 如果线程链表为空,则 TCB 作为链表头部 pool->first = newtask ; } else { // 如果线程链表不为空,加入到链表尾部 pool->last->next = newtask ; } pool->last = newtask ; // 修改链表尾指针 // 如果有空闲线程,那么就唤醒空闲线程 if ( pool->idle > 0 ) { condition_signal( &pool->ready ) ; // 通知等待在条件变量上的空闲线程 } else if ( pool->counter < pool->max_threads ) { // 如果没有空闲线程可用,而且当前线程数量小于线程池的容量,我们就创建一个线程 pthread_t tid ; pthread_create( &tid, NULL, thread_runtime, pool ) ; // 指定新线程的起始函数为 thread_runtime,把线程池传递给 thread_runtime ++ pool->counter ; } condition_unlock( &pool->ready ) ; // 释放互斥锁 /************************** 退出临界区 ***********************/ }
/* Insert 'm' into the mailbox 'q' */ int mbox_send(int q, msg_t * m) { int msgSize = MSG_SIZE(m); lock_acquire(&Q[q].l); print_trace("Send", q, msgSize); /* Wait until there is enough space */ while (space_available(&Q[q]) < msgSize) { condition_wait(&Q[q].l, &Q[q].moreSpace); } /* copy from message m (header and body) to Q[q].buffer */ msg_to_buffer((char *) m, msgSize, Q[q].buffer, Q[q].head); Q[q].head = (Q[q].head + msgSize) % BUFFER_SIZE; /* Send of one message can only satisfy one reader. */ condition_signal(&Q[q].moreData); Q[q].count++; lock_release(&Q[q].l); return 1; }
void _rtp_recorder_process_timing_packet(struct rtp_recorder_t* rr, struct rtp_packet_t* packet) { assert(packet != NULL); double current_time = hardware_get_time(); double reference_time = _ntp_time_to_hardware_time(*(struct ntp_time*)(packet->packet_data + 4)); double received_time = _ntp_time_to_hardware_time(*(struct ntp_time*)(packet->packet_data + 12)); double send_time = _ntp_time_to_hardware_time(*(struct ntp_time*)(packet->packet_data + 20)); double delay = ((current_time - reference_time) - (send_time - received_time)) / 2; double client_time = received_time + (send_time - received_time) + delay; log_message(LOG_INFO, "Client time is %1.6f (peer delay: %1.6f)", client_time, delay); audio_queue_set_remote_time(rr->audio_queue, client_time); rr->initial_time_response_count++; if (rr->initial_time_response_count == 2) condition_signal(rr->timer_cond); }
void *thread_routine(void *arg) { struct timespec abstime; int timeout; printf("thread 0x%0x is starting\n",(int)pthread_self()); threadpool_t *pool = (threadpool_t *)arg; while(1) { timeout = 0; condition_lock(&pool -> ready); pool -> idle++; //等待队列有任务到来或者线程池销毁的通知 while(pool -> first == NULL && !pool -> quit) { printf("thread 0x%0x is waiting\n",(int)pthread_self()); clock_gettime(CLOCK_REALTIME,&abstime); abstime.tv_sec += 2; int status=condition_timewait(&pool -> ready,&abstime); if(status == ETIMEDOUT) { printf("thread 0x%0x is wait timed out\n",(int)pthread_self()); timeout = 1; break; } } //等到到条件,处于工作状态 pool -> idle--; if(pool -> first != NULL) { task_t *t = pool -> first; pool -> first = t -> next; //需要先解锁,以便添加新任务。其他消费者线程能够进入等待任务。 condition_unlock(&pool -> ready); t -> run(t->arg); free(t); condition_lock(&pool -> ready); } //等待线程池销毁的通知 if(pool -> quit && pool ->first == NULL) { pool -> counter--; if(pool->counter == 0) { condition_signal(&pool -> ready); } condition_unlock(&pool->ready); //跳出循环之前要记得解锁 break; } if(timeout &&pool -> first ==NULL) { pool -> counter--; condition_unlock(&pool->ready); //跳出循环之前要记得解锁 break; } condition_unlock(&pool -> ready); } printf("thread 0x%0x is exiting\n",(int)pthread_self()); return NULL; }
void thread_wrap(){ current_thread->initial_function(current_thread->initial_argument); current_thread->state = DONE; condition_signal(current_thread->condList); yield(); };
void root_update_schedule () { condition_signal (&update_wakeup); }
int ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond ) { condition_signal( cond ); return( 0 ); }
void syscall_condition_signal(cond_t* cond, lock_t* lock) { condition_signal(cond, lock); }
void* thread_routine(void* arg) { struct timespec abstime; int timeout; printf("thread 0x%x is starting ...\n", (int)pthread_self()); threadpool_t* pool = (threadpool_t*)arg; while(1) { timeout = 0; condition_lock(&pool->ready); pool->idle++; while(pool->first == NULL && !pool->quit) { printf("thread 0x%x is waiting ...\n", (int)pthread_self()); clock_gettime(CLOCK_REALTIME, &abstime); abstime.tv_sec += 2; int status = condition_timewait(&pool->ready, &abstime); if(status == ETIMEDOUT) { printf("thread 0x%x is wait timed out ...\n", (int)pthread_self()); timeout = 1; break; } } pool->idle--; if(pool->first != NULL) { task_t* t = pool->first; pool->first = t->next; condition_unlock(&pool->ready); t->run(t->arg); free(t); condition_lock(&pool->ready); } if(pool->quit && pool->first == NULL) { if(--pool->counter == 0) condition_signal(&pool->ready); condition_unlock(&pool->ready); break; } if(timeout && pool->first == NULL) { pool->counter--; condition_unlock(&pool->ready); break; } condition_unlock(&pool->ready); } printf("thread 0x%x is exiting ...\n", (int)pthread_self()); return NULL; }
/* 线程入口函数 */ void* thread_runtime( void* arg ) { struct timespec abstime ; int timeout ; // 拿到线程池对象 threadpool_t* pool = ( threadpool_t* ) arg ; while ( 1 ) { timeout = 0 ; /************************** 进入临界区 ***********************/ condition_lock( &pool->ready ) ; // 空闲线程数加 1 ++ pool->idle ; // 如果线程链表为空,而且线程池处于运行状态,那么线程就该等待任务的到来 while ( pool->first == NULL && pool->quit == 0 ) { printf( "thread 0x%x is waiting\n", (int)pthread_self() ) ; clock_gettime( CLOCK_REALTIME, &abstime ) ; abstime.tv_sec += 2 ; int status = condition_timedwait( &pool->ready, &abstime ) ; if ( status == ETIMEDOUT ) { printf( "thread 0x%x is wait timed out\n", (int)pthread_self() ) ; timeout = 1 ; break ; } } // 如果线程等待超时 if ( timeout && pool->first == NULL ) { -- pool->counter ; // 那么线程数量减 1 condition_unlock( &pool->ready ) ; // 释放互斥锁 break ; // 跳出 while,注意,break 之后,线程入口函数执行完毕,线程将不复存在 } // 线程获得任务 -- pool->idle ; // 线程池空闲数减 1 if ( pool->first != NULL ) { task_t* t = pool->first ; // 从链表头部取出 TCB pool->first = t->next ; // 指向下一个 TCB // 执行任务需要一定时间,所以要先解锁 // 以便生产者可以往链表中加入任务 // 以及其他消费者可以等待任务 condition_unlock( &pool->ready ) ; t->run( t->arg ) ; // 执行任务的回调函数 free( t ) ; // 任务执行完毕,销毁 TCB condition_lock( &pool->ready ) ; } // quit == 1 说明要销毁线程池 if ( pool->quit && pool->first == NULL ) { -- pool->counter ; if ( pool->counter == 0 ) { condition_signal( &pool->ready ) ; // 唤醒等待在条件变量上的主线程 } condition_unlock( &pool->ready ) ; break ; } condition_unlock( &pool->ready ) ; /************************** 退出临界区 ***********************/ } return NULL ; }