Exemplo n.º 1
0
 static void WakeWaiters(BuildQueue* queue, int count)
 {
   if (count > 1)
     CondBroadcast(&queue->m_WorkAvailable);
   else
     CondSignal(&queue->m_WorkAvailable);
 }
Exemplo n.º 2
0
// Function to fill the table
void fillThread(int c) {

	// Transform the lock
	CondId cond = (int)c;

	tab[0] = 0;
	tab[1] = 1;
	
	int i;
	int j;
	for (i = 2; i < NUM; i++) {
		tab[i] = 0;

		for (j = (i-1); j > 0; j--) {
			tab[i] += tab[j];
		}
	}

	n_printf("Table just filled\n");

	// Signal the cond
	if (CondSignal(cond) < 0)
		n_printf("Error signal condition");

	Exit(0);
}
Exemplo n.º 3
0
void test2()
    {
    	CondSignal(cond); /*Signalling the waiting thread */
	y+=10;
   	n_printf("\n value of x inside Test2 is %d", y);
	Join(tid1);
    }
Exemplo n.º 4
0
int func1(void* arg)
	{
	int retval=0;
	ThreadData* pData = (ThreadData*) arg;
	
	retval = MutexLock(pData);
	retval = CondSignal(pData);
	retval = MutexUnlock(pData);
	StopThread(pData);	
	
	return retval;
	}
Exemplo n.º 5
0
 static void UnparkExpensiveNode(BuildQueue* queue)
 {
   if (queue->m_ExpensiveWaitCount > 0)
   {
     NodeState* node = queue->m_ExpensiveWaitList[--queue->m_ExpensiveWaitCount];
     CHECK(NodeStateIsQueued(node));
     // Really only to avoid tripping up checks in Enqueue()
     NodeStateFlagUnqueued(node);
     NodeStateFlagInactive(node);
     Enqueue(queue, node);
     CondSignal(&queue->m_WorkAvailable);
   }
 }
Exemplo n.º 6
0
//---------------------------------------------------------------------------
//	CondHandleBroadcast
//
//	This function is very similar to CondHandleSignal. But instead of
//	waking only one process, it wakes up all the processes waiting on the
//	condition variable. For this call to succeed, the calling process must
//	have acquired the lock associated with the condition variable. This
//	function should be written in such a way that the calling process
//	should retain the lock even after call completion.
//
//	Note that the process woken up by this call tries to acquire the lock
//	associated with the condition variable as soon as it wakes up. Thus,
//	for such a process to run, the process invoking CondHandleBroadcast
//	must explicitly release the lock after the call completion.
//---------------------------------------------------------------------------
int CondBroadcast(Cond *c) {
  if (!c) return SYNC_FAIL;
 
  if (c->lock->pid != GetCurrentPid()) {
    dbprintf('s', "CondBroadcast: Proc %d tried to broadcast, but it doesn't own cond %d\n", GetCurrentPid(), (int)(c-conds));
    return SYNC_FAIL;
  }

  while (!AQueueEmpty(&c->waiting)) {
    if (CondSignal(c) != SYNC_SUCCESS) {
      dbprintf('s', "CondBroadcast: Proc %d failed in signalling cond %d\n", GetCurrentPid(), (int)(c-conds));
      return SYNC_FAIL;
    }
  }
  dbprintf('s', "CondBroadcast: Proc %d successful broadcast on cond %d, still needs to release lock\n", 
                 GetCurrentPid(), (int)(c-conds));
  return SYNC_SUCCESS;
}
Exemplo n.º 7
0
static void *testLoggingThread(void *args)
{
	unsigned int i = 0;
	char  msg[256];
	int *threadNum = (int*)(args);

	printf("Thread %d fired off\n", threadNum);

	MutexLock(&threadCountLock);
	++threadCount;
	MutexUnlock(&threadCountLock);

        MutexLock(&cond_mutex);
        while (start_flag == 0) {
            CondWait(&start_cond, &cond_mutex);
        }
        MutexUnlock(&cond_mutex);

	printf("Thread %d actually going\n", *threadNum);

	for (i=0; i<numLogsPerThread; ++i) {
		int bytes = snprintf(msg, sizeof(msg),
		                     "Thread %d, operation %d: reporting for work, SIR!",
		                     *threadNum, i);
		append_log_message(msg, bytes);
	}

	printf("Thread %d finished logging\n", threadNum);

	MutexLock(&threadCountLock);
	--threadCount;
	CondSignal(&threadCountCond);
	MutexUnlock(&threadCountLock);

	printf("Thread %d exiting\n", threadNum);

	free(threadNum);

	return 0;
}
Exemplo n.º 8
0
TInt CTestCondwait::TestCond413()
	{
	int errsum=0, err = 0;
	int retval = 0;
	ThreadData lThreadData;

	sem_t lSignalSemaphore;
	sem_t lSuspendSemaphore;

	sem_t           lTestSemaphore;
	pthread_mutex_t lTestMutex;
	pthread_cond_t  lTestCondVar;
	pthread_condattr_t  lCondAttr;
	pthread_mutexattr_t lTestMutexAttr;

	pthread_mutexattr_t defaultattr;
	pthread_mutexattr_t errorcheckattr;
	pthread_mutexattr_t recursiveattr;

	pthread_mutexattr_init(&defaultattr);
	pthread_mutexattr_init(&errorcheckattr);
	pthread_mutexattr_init(&recursiveattr);

	pthread_mutexattr_settype(&errorcheckattr,PTHREAD_MUTEX_ERRORCHECK);
	pthread_mutexattr_settype(&recursiveattr,PTHREAD_MUTEX_RECURSIVE);


	pthread_mutex_t l_staticmutex = PTHREAD_MUTEX_INITIALIZER;
	pthread_mutex_t l_errorcheckmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
	pthread_mutex_t l_recursivemutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
    pthread_cond_t  l_staticcondvar = PTHREAD_COND_INITIALIZER;


    CommonData lCommonData;
    lCommonData.iStaticMutex = &l_staticmutex;
	lCommonData.iErrorCheckMutex = &l_errorcheckmutex;
	lCommonData.iRecursiveMutex = &l_recursivemutex;
	lCommonData.iStaticCondVar = &l_staticcondvar;

	retval = sem_init(&lSignalSemaphore,0,0);
	if(retval != 0)
		{
		return retval;
		}

	retval = sem_init(&lSuspendSemaphore,0,0);
	if(retval != 0)
		{
		return retval;
		}

	lThreadData.iSignalSemaphore = &lSignalSemaphore;
	lThreadData.iSuspendSemaphore = &lSuspendSemaphore;
	lThreadData.iTestSemaphore   = &lTestSemaphore;
	lThreadData.iTestMutex       = &lTestMutex;
	lThreadData.iTestMutexAttr   = &lTestMutexAttr;
	lThreadData.iTestCondVar     = &lTestCondVar;
	lThreadData.iDefaultAttr     = &defaultattr;
	lThreadData.iErrorcheckAttr = &errorcheckattr;
	lThreadData.iRecursiveAttr   = &recursiveattr;

	lThreadData.iCondAttr        = &lCondAttr;
	for (int loop = 0; loop < EThreadMain; loop++)
		{
	    g_spinFlag[loop] = true;
		}
	lThreadData.iSuspending      = false;
	lThreadData.iSpinCounter     = 0;
	lThreadData.iCurrentCommand  = -1;
	lThreadData.iSelf            = EThreadMain;
	lThreadData.iValue           = 0;
	lThreadData.iRetValue        = 0;
	lThreadData.ierrno           = 0;
	lThreadData.iExpectederrno   = 0;
	lThreadData.iTimes           = 0;
	lThreadData.iStopped         = false;
	lThreadData.iCommonData      = &lCommonData;
	
	retval = CondInit(&lThreadData);
	retval = MutexInitNULL(&lThreadData);
	
	fp=func;
	retval = ThreadCreate(&lThreadData, (void*) EThread1);
	
	WaitTillSuspended(&lThreadData, (void*) EThread1);
	
	fp=func1;
	retval = ThreadCreate(&lThreadData, (void*) EThread2);
	
	retval = MutexLock(&lThreadData);
	retval = CondSignal(&lThreadData);
	retval = MutexUnlock(&lThreadData);
	retval = ThreadDestroy(&lThreadData, (void*) EThread1);
	retval = ThreadDestroy(&lThreadData, (void*) EThread2);
	retval = MutexDestroy(&lThreadData);
	retval = CondDestroy(&lThreadData);
	StopThread(&lThreadData);

	err = pthread_cond_destroy(&l_staticcondvar);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutex_destroy(&l_recursivemutex);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutex_destroy(&l_errorcheckmutex);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutex_destroy(&l_staticmutex);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutexattr_destroy(&recursiveattr);
	if(err != EINVAL)
		{
		errsum += err;
		}
	err = pthread_mutexattr_destroy(&errorcheckattr);
	if(err != EINVAL)
		{
		errsum += err;
		}
	err = pthread_mutexattr_destroy(&defaultattr);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = sem_destroy(&lSignalSemaphore);
	if(err != EINVAL)
		{	
		errsum += err;
		}
	err = sem_destroy(&lSuspendSemaphore);
	if(err != EINVAL)
		{
		errsum += err;
		}

	return retval+errsum;
	}
Exemplo n.º 9
0
int Decoder::decodeFrame(AVFrame *frame, AVSubtitle *sub) {
    int got_frame = 0;

    do {
        int ret = -1;
        // 如果处于舍弃状态,则直接返回
        if (queue->isAbort()) {
            return -1;
        }
        // 如果当前没有包在等待,获取队列的序列不相同,取出下一阵
        if (!packet_pending || queue->serial != pkt_serial) {
            AVPacket pkt;
            do {
                // 队列为空
                if (queue->isEmpty()) {
                    CondSignal(empty_queue_cond);
                }
                // 获取裸数据
                if (queue->get(&pkt, 1, &pkt_serial) < 0) {
                    return -1;
                }
                // 刷新数据
                if (pkt.data == flush_pkt->data) {
                    avcodec_flush_buffers(avctx);
                    finished = 0;
                    next_pts = start_pts;
                    next_pts_tb = start_pts_tb;
                }
            } while (pkt.data == flush_pkt->data || queue->serial != pkt_serial);
            av_packet_unref(&this->pkt);
            this->pkt_temp = this->pkt_temp = pkt;
            packet_pending = 1;
        }

        // 根据解码器类型判断是音频还是视频
        switch (avctx->codec_type) {
            case AVMEDIA_TYPE_VIDEO:
                // 视频解码
                ret = avcodec_decode_video2(avctx, frame, &got_frame, &pkt_temp);
                // 解码成功,更新时间戳
                if (got_frame) {
                    if (reorderPts == -1) {
                        frame->pts = av_frame_get_best_effort_timestamp(frame);
                    } else if (!reorderPts) {
                        frame->pts = frame->pkt_dts;
                    }
                }
                break;

            case AVMEDIA_TYPE_AUDIO:
                // 音频解码
                ret = avcodec_decode_audio4(avctx, frame, &got_frame, &pkt_temp);
                if (got_frame) {
                    AVRational tb = (AVRational) {1, frame->sample_rate};
                    // 更新帧时间戳
                    if (frame->pts != AV_NOPTS_VALUE) {
                        frame->pts = av_rescale_q(frame->pts, av_codec_get_pkt_timebase(avctx), tb);
                    } else if (next_pts != AV_NOPTS_VALUE) {
                        frame->pts = av_rescale_q(next_pts, next_pts_tb, tb);
                    }
                    // 更新下一帧时间戳
                    if (frame->pts != AV_NOPTS_VALUE) {
                        next_pts = frame->pts + frame->nb_samples;
                        next_pts_tb = tb;
                    }
                }
                break;
        }

        // 判断是否解码成功
        if (ret < 0) {
            packet_pending = 0;
        } else {
            pkt_temp.dts = pkt_temp.pts = AV_NOPTS_VALUE;
            if (pkt_temp.data) {
                if (avctx->codec_type != AVMEDIA_TYPE_AUDIO) {
                    ret = pkt_temp.size;
                }
                pkt_temp.data += ret;
                pkt_temp.size -= ret;

                if (pkt_temp.size <= 0) {
                    packet_pending = 0;
                }
            } else {
                if (!got_frame) {
                    packet_pending = 0;
                    finished = pkt_serial;
                }
            }
        }
    } while (!got_frame && !finished);

    return got_frame;
}
Exemplo n.º 10
0
int CondHandleBroadcast(cond_t cond) {
  if (cond < 0) return SYNC_FAIL;
  if (cond >= MAX_CONDS) return SYNC_FAIL;
  if (!conds[cond].inuse)  return SYNC_FAIL;
  return CondSignal(&conds[cond]);
}