コード例 #1
0
ファイル: testsem.c プロジェクト: Evengard/UniMod
static void
TestWaitTimeout(void)
{
    Uint32 start_ticks;
    Uint32 end_ticks;
    Uint32 duration;
    int retval;

    sem = SDL_CreateSemaphore(0);
    SDL_Log("Waiting 2 seconds on semaphore\n");

    start_ticks = SDL_GetTicks();
    retval = SDL_SemWaitTimeout(sem, 2000);
    end_ticks = SDL_GetTicks();

    duration = end_ticks - start_ticks;

    /* Accept a little offset in the effective wait */
    if (duration > 1900 && duration < 2050)
        SDL_Log("Wait done.\n");
    else
        SDL_Log("Wait took %d milliseconds\n", duration);

    /* Check to make sure the return value indicates timed out */
    if (retval != SDL_MUTEX_TIMEDOUT)
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
}
コード例 #2
0
ファイル: sync.cpp プロジェクト: dazzle-multimedia/mpeg4ip
/*
 * sync_thread_pause - wait for messages to exit, pretty much.
 */
int CPlayerSession::sync_thread_done (void)
{
  int state;
  state = process_msg_queue(SYNC_STATE_DONE);
  if (state == SYNC_STATE_DONE) {
    SDL_SemWaitTimeout(m_sync_sem, 10);
  } 
  return (state);
}
コード例 #3
0
ファイル: mutex.cpp プロジェクト: Hellzed/xoreos
bool Semaphore::lock(uint32 timeout) {
	int ret;

	if (timeout == 0)
		ret = SDL_SemWait(_semaphore);
	else
		ret = SDL_SemWaitTimeout(_semaphore, timeout);

	return ret == 0;
}
コード例 #4
0
ファイル: Thread.cpp プロジェクト: AliSayed/MoSync
bool MoSyncSemaphore::tryWait(int timeout) {
	_ASSERT(timeout > 0);
	if(SDL_SemWaitTimeout(mSem, timeout) == 0) {
		return true;
	}
	else {
		return false;
	}

}
コード例 #5
0
ファイル: mq.c プロジェクト: sharpglasses/ServerSkeleton
rt_err_t rt_mq_recv (rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeout)
{
	rt_err_t r;
	struct rt_mq_message *msg;

	SDL_SemWait(hmq->mutex);

	/* mq is empty */
	if (mq->entry == 0)
	{
		SDL_SemPost(hmq->mutex);

		if (timeout == RT_WAITING_FOREVER)
		{
			r = SDL_SemWait(hmq->msg);
		}
		else
		{
			r = SDL_SemWaitTimeout(hmq->msg, timeout * 10);
		}

		if (r != 0) return -RT_ERROR;

		SDL_SemWait(hmq->mutex);
	}
	else
	{
		/* take one message */
		SDL_SemWait(hmq->msg);
	}

	/* get message from queue */
	msg = (struct rt_mq_message*) mq->msg_queue_head;

	/* move message queue head */
	mq->msg_queue_head = msg->next;

	/* reach queue tail, set to NULL */
	if (mq->msg_queue_tail == msg) mq->msg_queue_tail = RT_NULL;

	/* copy message */
	rt_memcpy(buffer, msg + 1,
		size > mq->msg_size? (unsigned short)(mq->msg_size) : (unsigned short)size);

	/* put message to free list */
	msg->next = (struct rt_mq_message*)mq->msg_queue_free;
	mq->msg_queue_free = msg;

	/* decrease message entry */
	mq->entry --;

	SDL_SemPost(hmq->mutex);

	return RT_EOK;
}
コード例 #6
0
ファイル: sem.c プロジェクト: amsl/RTGUI
rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time)
{
	int r;

	r = SDL_SemWaitTimeout((SDL_sem *)(sem->host_sem), time);

	if (r == 0) return RT_EOK;
	else if (r == SDL_MUTEX_TIMEDOUT) return -RT_ETIMEOUT;

	return -RT_ERROR;
}
コード例 #7
0
/* Wait on the condition variable for at most 'ms' milliseconds.
   The mutex must be locked before entering this function!
   The mutex is unlocked during the wait, and locked again after the wait.

Typical use:

Thread A:
	SDL_LockMutex(lock);
	while ( ! condition ) {
		SDL_CondWait(cond);
	}
	SDL_UnlockMutex(lock);

Thread B:
	SDL_LockMutex(lock);
	...
	condition = true;
	...
	SDL_UnlockMutex(lock);
 */
int
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
{
    int retval;

    if (!cond) {
        SDL_SetError("Passed a NULL condition variable");
        return -1;
    }

    /* Obtain the protection mutex, and increment the number of waiters.
       This allows the signal mechanism to only perform a signal if there
       are waiting threads.
     */
    SDL_LockMutex(cond->lock);
    ++cond->waiting;
    SDL_UnlockMutex(cond->lock);

    /* Unlock the mutex, as is required by condition variable semantics */
    SDL_UnlockMutex(mutex);

    /* Wait for a signal */
    if (ms == SDL_MUTEX_MAXWAIT) {
        retval = SDL_SemWait(cond->wait_sem);
    } else {
        retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
    }

    /* Let the signaler know we have completed the wait, otherwise
       the signaler can race ahead and get the condition semaphore
       if we are stopped between the mutex unlock and semaphore wait,
       giving a deadlock.  See the following URL for details:
       http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
     */
    SDL_LockMutex(cond->lock);
    if (cond->signals > 0) {
        /* If we timed out, we need to eat a condition signal */
        if (retval > 0) {
            SDL_SemWait(cond->wait_sem);
        }
        /* We always notify the signal thread that we are done */
        SDL_SemPost(cond->wait_done);

        /* Signal handshake complete */
        --cond->signals;
    }
    --cond->waiting;
    SDL_UnlockMutex(cond->lock);

    /* Lock the mutex, as is required by condition variable semantics */
    SDL_LockMutex(mutex);

    return retval;
}
コード例 #8
0
ファイル: threads.cpp プロジェクト: PaulForey/lovepd
	bool Semaphore::wait(int timeout)
	{
		if (timeout < 0)
			return SDL_SemWait(semaphore) ? false : true;
		else if (timeout == 0)
			return SDL_SemTryWait(semaphore) ? false : true;
		else
		{
			int ret = SDL_SemWaitTimeout(semaphore, timeout);
			return (ret == 0);
		}
	}
コード例 #9
0
void myGlutIdle( )
{
//   SDL_LockMutex(commandMutex);
//   if (!SDL_CondWaitTimeout(commandCond, commandMutex, 1)) {
  if (!SDL_SemWaitTimeout(commandSem, 1)) {
  //if (!SDL_SemWait(commandSem)) {
    //printf("receive a command\n");
    if ( glutGetWindow() && glutGetWindow() != main_window ) 
      glutSetWindow(main_window);
    nextCommand = command;
    command = 0;
    glutPostRedisplay();
  }
//   SDL_UnlockMutex(commandMutex);
}
コード例 #10
0
ファイル: sync.cpp プロジェクト: dazzle-multimedia/mpeg4ip
/*
 * sync_thread_wait_audio - wait until the audio thread starts and signals
 * us.
 */
int CPlayerSession::sync_thread_wait_audio (void)
{
  int state;

  state = process_msg_queue(SYNC_STATE_WAIT_AUDIO);
  if (state == SYNC_STATE_WAIT_AUDIO) {
    if (m_waiting_for_audio != 0) {
      SDL_SemWaitTimeout(m_sync_sem, 10);
    } else {
      // make sure we set the current time
      get_current_time();
      sync_message(LOG_DEBUG, "Current time is "U64, m_current_time);
      return (SYNC_STATE_PLAYING);
    }
  }
  return (state);
}
コード例 #11
0
ファイル: sdl2_mutex.c プロジェクト: Moon4u/mruby-sdl2
static mrb_value
mrb_sdl2_semaphore_wait_timeout(mrb_state *mrb, mrb_value self)
{
  mrb_int ms;
  int status;
  mrb_sdl2_semaphore_data_t *data =
    (mrb_sdl2_semaphore_data_t*)mrb_data_get_ptr(mrb, self, &mrb_sdl2_semaphore_data_type);
  mrb_get_args(mrb, "i", &ms);
  if (NULL == data->semaphore) {
    return mrb_nil_value();
  }
  status = SDL_SemWaitTimeout(data->semaphore, (Uint32)ms);
  if (0 > status) {
    mruby_sdl2_raise_error(mrb);
  }
  if (0 == status) {
    return mrb_true_value();
  }
  return mrb_false_value();
}
コード例 #12
0
static void TestWaitTimeout(void)
{
	Uint32 start_ticks;
	Uint32 end_ticks;
	Uint32 duration;

	sem = SDL_CreateSemaphore(0);
	printf("Waiting 2 seconds on semaphore\n");

	start_ticks = SDL_GetTicks();
	SDL_SemWaitTimeout(sem, 2000);
	end_ticks = SDL_GetTicks();

	duration = end_ticks - start_ticks;
	
	/* Accept a little offset in the effective wait */
	if (duration > 1900 && duration < 2050)
		printf("Wait done.\n");
	else
		fprintf(stderr, "Wait took %d milliseconds\n", duration);
}
コード例 #13
0
ファイル: SDL_syssem.c プロジェクト: 3bu1/crossbridge
DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem)
{
        return SDL_SemWaitTimeout(sem, 0);
}
コード例 #14
0
static int
SDL_TimerThread(void *_data)
{
    SDL_TimerData *data = (SDL_TimerData *)_data;
    SDL_Timer *pending;
    SDL_Timer *current;
    SDL_Timer *freelist_head = NULL;
    SDL_Timer *freelist_tail = NULL;
    Uint32 tick, now, interval, delay;

    /* Threaded timer loop:
     *  1. Queue timers added by other threads
     *  2. Handle any timers that should dispatch this cycle
     *  3. Wait until next dispatch time or new timer arrives
     */
    for ( ; ; ) {
        /* Pending and freelist maintenance */
        SDL_AtomicLock(&data->lock);
        {
            /* Get any timers ready to be queued */
            pending = data->pending;
            data->pending = NULL;

            /* Make any unused timer structures available */
            if (freelist_head) {
                freelist_tail->next = data->freelist;
                data->freelist = freelist_head;
            }
        }
        SDL_AtomicUnlock(&data->lock);

        /* Sort the pending timers into our list */
        while (pending) {
            current = pending;
            pending = pending->next;
            SDL_AddTimerInternal(data, current);
        }
        freelist_head = NULL;
        freelist_tail = NULL;

        /* Check to see if we're still running, after maintenance */
        if (!data->active) {
            break;
        }

        /* Initial delay if there are no timers */
        delay = SDL_MUTEX_MAXWAIT;

        tick = SDL_GetTicks();

        /* Process all the pending timers for this tick */
        while (data->timers) {
            current = data->timers;

            if ((Sint32)(tick-current->scheduled) < 0) {
                /* Scheduled for the future, wait a bit */
                delay = (current->scheduled - tick);
                break;
            }

            /* We're going to do something with this timer */
            data->timers = current->next;

            if (current->canceled) {
                interval = 0;
            } else {
                interval = current->callback(current->interval, current->param);
            }

            if (interval > 0) {
                /* Reschedule this timer */
                current->scheduled = tick + interval;
                SDL_AddTimerInternal(data, current);
            } else {
                if (!freelist_head) {
                    freelist_head = current;
                }
                if (freelist_tail) {
                    freelist_tail->next = current;
                }
                freelist_tail = current;

                current->canceled = SDL_TRUE;
            }
        }

        /* Adjust the delay based on processing time */
        now = SDL_GetTicks();
        interval = (now - tick);
        if (interval > delay) {
            delay = 0;
        } else {
            delay -= interval;
        }

        /* Note that each time a timer is added, this will return
           immediately, but we process the timers added all at once.
           That's okay, it just means we run through the loop a few
           extra times.
         */
        SDL_SemWaitTimeout(data->sem, delay);
    }
    return 0;
}
コード例 #15
0
ファイル: threadpool.c プロジェクト: Dinth/naev
/**
 * @brief Handles assigning jobs to the workers and killing them if necessary.
 * Stopping the threeadpool_handler is not yet implemented.
 *
 * @param data Not used. SDL threading requires functions to take a void
 * pointer as argument.
 *
 * @return Not really implemented yet.
 */
static int threadpool_handler( void *data )
{
   (void) data;
   int i, nrunning, newthread;
   ThreadData *threadargs, *threadarg;
   /* Queues for idle workers and stopped workers */
   ThreadQueue *idle, *stopped;
   ThreadQueueData *node;

   /* Initialize the idle and stopped queues. */
   idle     = tq_create();
   stopped  = tq_create();

   /* Allocate threadargs to communicate with workers */
   threadargs = calloc( MAXTHREADS, sizeof(ThreadData) );

   /* Initialize threadargs */
   for (i=0; i<MAXTHREADS; i++) {
      threadargs[i].function  = NULL;
      threadargs[i].data      = NULL;
      threadargs[i].semaphore = SDL_CreateSemaphore( 0 );
      threadargs[i].idle      = idle;
      threadargs[i].stopped   = stopped;
      threadargs[i].signal    = THREADSIG_RUN;
      /* 'Workers' that do not have a thread are considered stopped */
      tq_enqueue( stopped, &threadargs[i] );
   }

   /* Set the number of running threads to 0 */
   nrunning = 0;

   /*
    * Thread handler main loop.
    */
   while (1) {
      /*
       * We must now wait, this shall be done on each active thread. However they will
       * be put to sleep as time passes. When we receive a command we'll proceed to process
       * it.
       */
      if (nrunning > 0) {
         /*
          * Here we'll wait until thread gets work to do. If it doesn't it will
          * just stop a worker thread and wait until it gets something to do.
          */
         if (SDL_SemWaitTimeout( global_queue->semaphore, THREADPOOL_TIMEOUT ) != 0) {
            /* There weren't any new jobs so we'll start killing threads ;) */
            if (SDL_SemTryWait( idle->semaphore ) == 0) {
               threadarg         = tq_dequeue( idle );
               /* Set signal to stop worker thread */
               threadarg->signal = THREADSIG_STOP;
               /* Signal thread and decrement running threads counter */
               SDL_SemPost( threadarg->semaphore );
               nrunning -= 1;
            }

            /* We just go back to waiting on a thread. */
            continue;
         }

         /* We got work. Continue to handle work. */
      }
      else {
         /*
          * Here we wait for a new job. No threads are alive at this point and the
          * threadpool is just patiently waiting for work to arrive.
          */
         if (SDL_SemWait( global_queue->semaphore ) == -1) {
             WARN("L%d: SDL_SemWait failed! Error: %s", __LINE__, SDL_GetError());
             continue;
         }

         /* We got work. Continue to handle work. */
      }

      /*
       * We assume there's work available. We now have to choose who does the work.
       * We'll try to wake up a sleeping thread, if none are left new threads will
       * be created.
       */

      /*
       * Get a new job from the queue. This should be safe as we have received
       * a permission from the global_queue->semaphore.
       */
      node        = tq_dequeue( global_queue );
      newthread   = 0;

      /*
       * Choose where to get the thread. Either idle, revive stopped or block until
       * another thread becomes idle.
       */
      /* Idle thread available */
      if (SDL_SemTryWait(idle->semaphore) == 0)
         threadarg         = tq_dequeue( idle );
      /* Make a new thread */
      else if (SDL_SemTryWait(stopped->semaphore) == 0) {
         threadarg         = tq_dequeue( stopped );
         threadarg->signal = THREADSIG_RUN;
         newthread         = 1;
      }
      /* Wait for idle thread */
      else {
         while (SDL_SemWait(idle->semaphore) == -1) {
             /* Bad idea */
             WARN("L%d: SDL_SemWait failed! Error: %s", __LINE__, SDL_GetError());
         }
         threadarg         = tq_dequeue( idle );
      }

      /* Assign arguments for the thread */
      threadarg->function  = node->function;
      threadarg->data      = node->data;
      /* Signal the thread that there's a new job */
      SDL_SemPost( threadarg->semaphore );
      /* Start a new thread and increment the thread counter */
      if (newthread) {
         SDL_CreateThread( threadpool_worker, threadarg );
         nrunning += 1;
      }

      /* Free the now unused job from the global_queue */
      free(node);
   }
   /* TODO: cleanup and a way to stop the threadpool */

   return 0;
}
コード例 #16
0
bool ZL_Semaphore::WaitTimeout(int ms) { return (SDL_SemWaitTimeout(impl->sem, (Uint32)ms)==0); }
コード例 #17
0
ファイル: sync.cpp プロジェクト: dazzle-multimedia/mpeg4ip
int CPlayerSession::sync_thread_audio_resync (void) 
{
  int state;
  uint64_t audio_resync_time = 0;
  int64_t wait_audio_time;
  bool video_have_next_time = false;
  uint64_t video_next_time;
  bool have_audio_eof = false;
  bool need_audio_resync = false;
  bool have_video_eof = false;
  bool need_audio_restart = false;

  state = process_msg_queue(SYNC_STATE_AUDIO_RESYNC);
  if (state == SYNC_STATE_AUDIO_RESYNC) {
    get_current_time();
    need_audio_resync = m_audio_sync->check_audio_sync(m_current_time, 
						       audio_resync_time,
						       wait_audio_time,
						       have_audio_eof,
						       need_audio_restart);
    if (need_audio_restart) {
      // this occurs when the time is in the past, but we're continuing
      // video
      return SYNC_STATE_WAIT_SYNC;
    }
    if (need_audio_resync == false) {
      // video continued, so, restart audio
      sync_message(LOG_ERR, "resync but no audio resync");
      return SYNC_STATE_PLAYING;
    }
    have_video_eof = true;
    video_next_time = MAX_UINT64;
    for (CTimedSync *ts = m_timed_sync_list;
	 ts != NULL;
	 ts = ts->GetNext()) {
      uint64_t cmp;
      bool have_eof;
      if (ts->play_at(m_current_time, true, cmp, have_eof)) {
	video_have_next_time = true;
	video_next_time = MIN(cmp, video_next_time);
	have_video_eof &= have_eof;
      }
    }

    sync_message(LOG_DEBUG, "audio resync - "U64" have_next %d "U64,
		 m_current_time, video_have_next_time, 
		 video_next_time);
    sync_message(LOG_DEBUG, "audio time "U64, audio_resync_time);

    if (have_audio_eof && have_video_eof) {
      return SYNC_STATE_DONE;
    }

    if (video_have_next_time == false) {
      SDL_SemWaitTimeout(m_sync_sem, 10);
    } else {
      int64_t diff = video_next_time - audio_resync_time;
      if (diff >= TO_D64(-5000) && diff <= TO_D64(5000)) {
	return SYNC_STATE_WAIT_SYNC;
      }
      diff = video_next_time - m_current_time;
      if (diff < 0 || diff > TO_D64(1000)) {
	// drop this frame
	return SYNC_STATE_WAIT_SYNC;
      }
      if (diff > 9) {
	diff = 9;
      }
      if (diff >= 9) {
	int delay = (int)diff;
	SDL_SemWaitTimeout(m_sync_sem, delay);
      }
    }
  }

  return state;
}
コード例 #18
0
ファイル: sdlthread.hpp プロジェクト: SDLGUI/sdlgui
def_dll int sdl_semaphore::wait_timeout(Uint32 ms)
{
	return SDL_SemWaitTimeout(_semaphore,ms);
}
コード例 #19
0
ファイル: sync.cpp プロジェクト: dazzle-multimedia/mpeg4ip
/*
 * sync_thread_playing - do the work of displaying the video and making
 * sure we're in sync.
 */
int CPlayerSession::sync_thread_playing (void) 
{
  int state;
  uint64_t audio_resync_time = 0;
  int64_t wait_audio_time;
  bool video_have_next_time = false;
  uint64_t video_next_time;
  bool have_audio_eof = false;
  bool need_audio_restart = false;
  bool need_audio_resync = false;
  bool have_video_eof = false;

  state = process_msg_queue(SYNC_STATE_PLAYING);
  if (state == SYNC_STATE_PLAYING) {
    get_current_time();
    if (m_audio_sync) {
      need_audio_resync = m_audio_sync->check_audio_sync(m_current_time, 
							 audio_resync_time,
							 wait_audio_time,
							 have_audio_eof,
							 need_audio_restart);
      if (need_audio_resync) {
	if (m_timed_sync_list) {
	  // wait for video to play out
	  if (config.GetBoolValue(CONFIG_SHORT_VIDEO_RENDER)) {
	    // flush timed sync
	    for (CTimedSync *ts = m_timed_sync_list;
		 ts != NULL;
		 ts = ts->GetNext()) {
	      ts->flush_sync_buffers();
	      ts->flush_decode_buffers();
	    }
	    return SYNC_STATE_WAIT_AUDIO_READY;
	  } 
	  return SYNC_STATE_AUDIO_RESYNC;
	} else {
	  // skip right to the audio
	  return SYNC_STATE_WAIT_SYNC;
	}
      }
    }
    video_next_time = MAX_UINT64;
    if (m_timed_sync_list != NULL) {
      have_video_eof = true;
      for (CTimedSync *ts = m_timed_sync_list;
	   ts != NULL;
	   ts = ts->GetNext()) {
	uint64_t cmp;
	bool have_eof = false;
	if (ts->play_at(m_current_time, false, cmp, have_eof)) {
	  video_have_next_time = true;
#if 0
	  sync_message(LOG_DEBUG, "return "U64 " "U64 " %u", cmp, m_current_time,
		       have_eof);
#endif
	  video_next_time = MIN(cmp, video_next_time);
	}
	have_video_eof &= have_eof;
      }
    } 

    int delay = 0;
    bool have_delay = false;

    if (m_timed_sync_list && m_audio_sync) {
      if (have_video_eof && have_audio_eof) {
	return (SYNC_STATE_DONE);
      }
      if (video_have_next_time) {
	have_delay = true;
	int64_t video_wait_time = video_next_time - m_current_time;
	delay = (int)video_wait_time;
      }
    } else if (m_timed_sync_list) {
      if (have_video_eof) {
	sync_message(LOG_DEBUG, "have video eof %d", have_video_eof);
	return (SYNC_STATE_DONE);
      } 
      if (video_have_next_time) {
	have_delay = true;
	delay = (int)(video_next_time - m_current_time);
      }
    } else {
      // audio only
      if (have_audio_eof) {
	return (SYNC_STATE_DONE);
      }
      if (audio_resync_time != 0) {
	have_delay = true;
	delay = (int)(audio_resync_time - m_current_time);
      }
    }
      
    if (have_delay) {
#if 0
      if (delay >= 9) {
	delay = 9;
	SDL_SemWaitTimeout(m_sync_sem, delay);
      }
#else
      //sync_message(LOG_DEBUG, "delay %d", delay);
      if (delay >= 10) {
	delay = 10;
      }
      SDL_SemWaitTimeout(m_sync_sem, delay);
#endif
	
    } else {
      SDL_SemWaitTimeout(m_sync_sem, 10);
    }
  }
  return (state);
}
コード例 #20
0
ファイル: SDL_syssem.c プロジェクト: bohwaz/ozex
int SDL_SemWait(SDL_sem *sem)
{
	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
}
コード例 #21
0
ファイル: SDL_syssem.c プロジェクト: esxgx/sc9800-epos-sdlx
int SDL_SemTryWait(SDL_sem *sem)
{
	SDL_SemWaitTimeout(sem, 0);
	return 0;
}
コード例 #22
0
ファイル: SDL_syssem.c プロジェクト: GWRon/sdl.mod
int SDL_SemTryWait(SDL_sem *sem)
{
    return SDL_SemWaitTimeout(sem, 0);
}
コード例 #23
0
ファイル: text_encoder.cpp プロジェクト: BluePandaLi/mpeg4ip
int CTextEncoder::ThreadMain (void)
{
  CMsg* pMsg;
  bool stop = false;
  
  Init();

  m_textDstType = GetFrameType();
  double temp = Profile()->GetFloatValue(CFG_TEXT_REPEAT_TIME_SECS);
  temp *= 1000.0;
  uint32_t wait_time = (uint32_t)temp;

  while (stop == false) {
    int rc = SDL_SemWaitTimeout(m_myMsgQueueSemaphore, wait_time);
    if (rc == 0) {
      pMsg = m_myMsgQueue.get_message();
      if (pMsg != NULL) {
	switch (pMsg->get_value()) {
	case MSG_NODE_STOP_THREAD:
	  debug_message("text %s stop received", 
			Profile()->GetName());
	  DoStopText();
	  stop = true;
	  break;
	case MSG_NODE_START:
	  // DoStartTransmit();  Anything ?
	  break;
	case MSG_NODE_STOP:
	  DoStopText();
	  break;
	case MSG_SINK_FRAME: {
	  uint32_t dontcare;
	  CMediaFrame *mf = (CMediaFrame*)pMsg->get_message(dontcare);
	  if (m_stop_thread == false)
	    ProcessTextFrame(mf);
	  if (mf->RemoveReference()) {
	    delete mf;
	  }
	  break;
	}
	}
      
	delete pMsg;
      } 
    } else if (rc == SDL_MUTEX_TIMEDOUT) {
      SendFrame(GetTimestamp());
    }
  }
  while ((pMsg = m_myMsgQueue.get_message()) != NULL) {
    if (pMsg->get_value() == MSG_SINK_FRAME) {
      uint32_t dontcare;
      CMediaFrame *mf = (CMediaFrame*)pMsg->get_message(dontcare);
      if (mf->RemoveReference()) {
	delete mf;
      }
    }
    delete pMsg;
  }
  debug_message("text encoder %s exit", Profile()->GetName());
  return 0;
}
コード例 #24
0
		int SemWaitTimeout(Sem *sem, Uint32 timeout)
		{
			return SDL_SemWaitTimeout(sem, timeout);
		}
コード例 #25
0
ファイル: SDL_syssem.c プロジェクト: esxgx/sc9800-epos-sdlx
int SDL_SemWait(SDL_sem *sem)
{
	return SDL_SemWaitTimeout(sem, 1*1000);
}
コード例 #26
0
ファイル: sync.cpp プロジェクト: dazzle-multimedia/mpeg4ip
/*
 * sync_thread_init - wait until all the sync routines are initialized.
 */
int CPlayerSession::sync_thread_init (void)
{
  int ret = 1;
  uint failed = 0;
  CTimedSync *ts;
  bool all_timed_inited, audio_inited = false, any_timed_inited;
  if (m_audio_sync != NULL) {
    ret = m_audio_sync->initialize_audio(m_timed_sync_list != NULL);
    if (ret <= 0) {
      //sync_message(LOG_DEBUG, "audio sync failed init");
      audio_inited = false;
      failed |= 1;
    } else {
      audio_inited = true;
      //sync_message(LOG_DEBUG, "audio sync init");
      if (config.GetBoolValue(CONFIG_SHORT_VIDEO_RENDER))
	return (SYNC_STATE_WAIT_AUDIO_READY);
    }
  }

  all_timed_inited = initialize_timed_sync(failed, any_timed_inited);
      

  if ((m_audio_sync == NULL || audio_inited) && 
      all_timed_inited) {
    return (SYNC_STATE_WAIT_SYNC); 
  } 

  if ((m_audio_sync != NULL && audio_inited) || any_timed_inited) {
    m_init_tries_made_with_no_media++;
    if (m_init_tries_made_with_no_media > 5 * 100) {
      sync_message(LOG_CRIT, "One media is not initializing; it might not be receiving correctly");
      if ((failed & 0x2) != 0 ) {
	sync_message(LOG_INFO, "video failed");
      } 
      if ((failed & 0x4) != 0 ) {
	sync_message(LOG_INFO, "timed text failed");
      }
      if ((failed & 0x1) != 0 ) {
	sync_message(LOG_INFO, "audio failed");
      }

      ret = -1;
    }
  }  else {
    m_init_tries_made_with_no_media++;
    if (m_init_tries_made_with_no_media > 30 * 100) {
      sync_message(LOG_CRIT, "No media has been initialized or received");
      ret = -1;
    }
  }
		 
  if (ret == -1) {
    sync_message(LOG_CRIT, "Fatal error while initializing hardware");
    ts = m_timed_sync_list;
    while (ts != NULL) {
      ts->flush_sync_buffers();
      ts = ts->GetNext();
    }
    if (m_audio_sync != NULL) {
      m_audio_sync->flush_sync_buffers();
    }
    m_master_msg_queue->send_message(MSG_RECEIVED_QUIT, 
				     m_master_msg_queue_sem);
    m_hardware_error = 1;
    return (SYNC_STATE_DONE);
  }
	

  CMsg *newmsg;

  newmsg = m_sync_thread_msg_queue.get_message();
  if (newmsg != NULL) {
    int value = newmsg->get_value();
    delete newmsg;

    if (value == MSG_STOP_THREAD) {
      return (SYNC_STATE_EXIT);
    }
    if (value == MSG_PAUSE_SESSION) {
      m_sync_pause_done = 1;
    }
  }

  SDL_SemWaitTimeout(m_sync_sem, 10);
	
  return (SYNC_STATE_INIT);
}
コード例 #27
0
ファイル: SDL_syssem.c プロジェクト: 3bu1/crossbridge
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
	int retval;
	DWORD dwMilliseconds;

	if ( ! sem ) {
		SDL_SetError("Passed a NULL sem");
		return -1;
	}

	if ( timeout == SDL_MUTEX_MAXWAIT ) {
		dwMilliseconds = INFINITE;
	} else {
		dwMilliseconds = (DWORD)timeout;
	}
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
	switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
#else
	switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
#endif
	    case WAIT_OBJECT_0:
		InterlockedDecrement(&sem->count);
		retval = 0;
		break;
	    case WAIT_TIMEOUT:
		retval = SDL_MUTEX_TIMEDOUT;
		break;
	    default:
		SDL_SetError("WaitForSingleObject() failed");
		retval = -1;
		break;
	}
	return retval;
}

int SDL_SemTryWait(SDL_sem *sem)
{
	return SDL_SemWaitTimeout(sem, 0);
}

int SDL_SemWait(SDL_sem *sem)
{
	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
}

/* Returns the current count of the semaphore */
Uint32 SDL_SemValue(SDL_sem *sem)
{
	if ( ! sem ) {
		SDL_SetError("Passed a NULL sem");
		return 0;
	}
	return sem->count;
}

int SDL_SemPost(SDL_sem *sem)
{
	if ( ! sem ) {
		SDL_SetError("Passed a NULL sem");
		return -1;
	}
	/* Increase the counter in the first place, because
	 * after a successful release the semaphore may
	 * immediately get destroyed by another thread which
	 * is waiting for this semaphore.
	 */
	InterlockedIncrement(&sem->count);
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
	if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) {
#else
	if ( ReleaseSemaphore(sem->id, 1, NULL) == FALSE ) {
#endif
		InterlockedDecrement(&sem->count);	/* restore */
		SDL_SetError("ReleaseSemaphore() failed");
		return -1;
	}
	return 0;
}
コード例 #28
0
ファイル: thread-sdl.c プロジェクト: ntj/rockbox
void switch_thread(void)
{
    struct thread_entry *current = cores[CURRENT_CORE].running;

    enable_irq();

    switch (current->state)
    {
    case STATE_RUNNING:
    {
        SDL_UnlockMutex(m);
        /* Any other thread waiting already will get it first */
        SDL_LockMutex(m);
        break;
        } /* STATE_RUNNING: */

    case STATE_BLOCKED:
    {
        int oldlevel;

        SDL_UnlockMutex(m);
        SDL_SemWait(current->context.s);
        SDL_LockMutex(m);

        oldlevel = disable_irq_save();
        current->state = STATE_RUNNING;
        restore_irq(oldlevel);
        break;
        } /* STATE_BLOCKED: */

    case STATE_BLOCKED_W_TMO:
    {
        int result, oldlevel;

        SDL_UnlockMutex(m);
        result = SDL_SemWaitTimeout(current->context.s, current->tmo_tick);
        SDL_LockMutex(m);

        oldlevel = disable_irq_save();

        if (current->state == STATE_BLOCKED_W_TMO)
        {
            /* Timed out */
            remove_from_list_l(current->bqp, current);

#ifdef HAVE_WAKEUP_EXT_CB
            if (current->wakeup_ext_cb != NULL)
                current->wakeup_ext_cb(current);
#endif
            current->state = STATE_RUNNING;
        }

        if (result == SDL_MUTEX_TIMEDOUT)
        {
            /* Other signals from an explicit wake could have been made before
             * arriving here if we timed out waiting for the semaphore. Make
             * sure the count is reset. */
            while (SDL_SemValue(current->context.s) > 0)
                SDL_SemTryWait(current->context.s);
        }

        restore_irq(oldlevel);
        break;
        } /* STATE_BLOCKED_W_TMO: */

    case STATE_SLEEPING:
    {
        SDL_UnlockMutex(m);
        SDL_SemWaitTimeout(current->context.s, current->tmo_tick);
        SDL_LockMutex(m);
        current->state = STATE_RUNNING;
        break;
        } /* STATE_SLEEPING: */
    }

    cores[CURRENT_CORE].running = current;

    if (threads_status != THREADS_RUN)
        thread_exit();
}