Example #1
0
static gpointer
_check_changes (BusRegistry *registry)
{
    g_assert (BUS_IS_REGISTRY (registry));

    g_mutex_lock (registry->mutex);
    while (registry->thread_running == TRUE && registry->changed == FALSE) {
        extern gint g_monitor_timeout;
        GTimeVal tv;
        g_get_current_time (&tv);
        g_time_val_add (&tv, g_monitor_timeout * G_USEC_PER_SEC);

        if (g_cond_timed_wait (registry->cond, registry->mutex, &tv) == FALSE) {
            /* timeout */
            if (bus_registry_check_modification (registry)) {
                registry->changed = TRUE;
                g_idle_add ((GSourceFunc) _emit_changed_signal_cb, registry);
                break;
            }
        }
        else
            g_warn_if_fail (registry->thread_running == FALSE);
    }
    g_mutex_unlock (registry->mutex);
    return NULL;
}
Example #2
0
JsonNode*
cometd_inbox_take(cometd_inbox* inbox)
{
  JsonNode* node = NULL;

  GTimeVal wait_timeout;
  GTimeVal now;

  GQueue* q = inbox->queue;
  GCond* c = inbox->c;
  GMutex* m = inbox->m;

  g_mutex_lock(m);

  g_get_current_time(&now);
  g_time_val_add(&now, 100000);  // 100ms

  while (g_queue_is_empty(q))
      if (!g_cond_timed_wait(c, m, &now))
        break;

  if (!g_queue_is_empty(q)) {
    node = (JsonNode*) g_queue_pop_head(q);
  }

  g_mutex_unlock(m);

  return node;
}
Example #3
0
/* Progress bar implementation */
static void* progress_bar(void *unused)
{
	GTimeVal	time;
	char		buf[PROGRESS_BUF_SIZE], A[20];
	int		i = 0, t = 0, len, mes_len;

	memset(buf, 0, PROGRESS_BUF_SIZE);
	mes_len = strlen(progress_mes);
	while (in_progress) {
		snprintf(A, 10, " %d.%d s ", t / 10, t % 10);
		len = strlen(A);
		memset(buf + mes_len, '.', i);
		strncpy(buf, progress_mes, mes_len);
		if (i > 8)
			strncpy(buf + mes_len + (i - len) / 2, A, len);
		printf("%s\r", buf);
		fflush(stdout);
		g_get_current_time(&time);
		g_time_val_add(&time, G_USEC_PER_SEC / 10);
		g_cond_timed_wait(thread_wait, thread_mutex, &time);
		if (i < (PROGRESS_BUF_SIZE - mes_len - 1)) i++;
		t++;
	};
        g_thread_exit(0);
	return (void *)1;
}
Example #4
0
/**
 * goo_semaphore_down:
 * @self: an #GooSemaphore structure
 * @timeout: TRUE for a timed semaphore, FALSE for an untimed semaphore
 *
 * While the counter is 0, the process will be stopped, waiting for
 * an up signal
 */
void
goo_semaphore_down (GooSemaphore *self, gboolean timeout)
{
        g_assert (self != NULL);

        g_mutex_lock (self->mutex);

        while (self->counter == 0)
        {
                self->waiting = TRUE;
                if (timeout)
                {
                        GTimeVal time;
                        g_get_current_time (&time);
                        g_time_val_add (&time, SEM_TMOUT);

                        g_assert (
                                g_cond_timed_wait (self->condition,
                                                   self->mutex, &time) == TRUE
                                );
                }
                else
                {
                        g_cond_wait (self->condition, self->mutex);
                }
        }

        self->waiting = FALSE;
        self->counter--;

        g_mutex_unlock (self->mutex);

        return;
}
static void
timed_wait_on_unlocked_mutex (void)
{
  GMutex* mutex = g_mutex_new ();
  GCond* cond = g_cond_new ();
  g_cond_timed_wait (cond, mutex, NULL);
}
Example #6
0
static gpointer oh_event_thread_loop(gpointer data)
{
        GTimeVal time;
        SaErrorT rv;

	g_mutex_lock(oh_thread_mutex);
        while(oh_run_threaded()) {
		trace("Thread Harvesting events");
                rv = oh_harvest_events();
                if(rv != SA_OK) {
                        trace("Error on harvest of events.");
                }
                
		trace("Thread processing events");
                rv = oh_process_events();
                if(rv != SA_OK) {
                        trace("Error on processing of events, aborting");
                }
                
                trace("Thread processing hotswap");
                process_hotswap_policy();
                
                g_get_current_time(&time);
                g_time_val_add(&time, OH_THREAD_SLEEP_TIME);

                trace("Going to sleep");
                if (g_cond_timed_wait(oh_thread_wait, oh_thread_mutex, &time))
                        trace("SIGNALED: Got signal from plugin");
                else
                        trace("TIMEDOUT: Woke up, am looping again");
        }
	g_mutex_unlock(oh_thread_mutex);
        g_thread_exit(0);
        return data;
}
Example #7
0
static gpointer oh_discovery_thread_loop(gpointer data)
{
        GTimeVal time;
        SaErrorT error = SA_OK;

        g_mutex_lock(oh_discovery_thread_mutex);
        while (1) {
                trace("Doing threaded discovery on all handlers");
                error = oh_domain_resource_discovery(0);
                if (error) {
                        trace("Got error on threaded discovery return.");
                }

                /* Let oh_wake_discovery_thread know this thread is done */
                g_cond_broadcast(oh_discovery_thread_wait);
                g_get_current_time(&time);
                g_time_val_add(&time, OH_DISCOVERY_THREAD_SLEEP_TIME);
                /* Go to sleep; let oh_wake_discovery_thread take the mutex */
                trace("Going to sleep");
                if (g_cond_timed_wait(oh_discovery_thread_wait,
                                      oh_discovery_thread_mutex, &time))
                        trace("SIGNALED: Got signal from saHpiDiscover()");
                else
                        trace("TIMEDOUT: Woke up, am doing discovery again");
        }
        g_mutex_unlock(oh_discovery_thread_mutex);
        g_thread_exit(0);

        return data;
}
Example #8
0
static gpointer
afmongodb_worker_thread (gpointer arg)
{
  MongoDBDestDriver *self = (MongoDBDestDriver *)arg;

  msg_debug ("Worker thread started",
	     evt_tag_str("driver", self->super.super.id),
	     NULL);

  afmongodb_dd_connect(self, FALSE);

  self->ns = g_strconcat (self->db, ".", self->coll, NULL);

  self->current_value = g_string_sized_new(256);

  self->bson = bson_new_sized(4096);

  while (!self->writer_thread_terminate)
    {
      g_mutex_lock(self->suspend_mutex);
      if (self->writer_thread_suspended)
	{
	  g_cond_timed_wait(self->writer_thread_wakeup_cond,
			    self->suspend_mutex,
			    &self->writer_thread_suspend_target);
	  self->writer_thread_suspended = FALSE;
	  g_mutex_unlock(self->suspend_mutex);
	}
      else if (!log_queue_check_items(self->queue, NULL, afmongodb_dd_message_became_available_in_the_queue, self, NULL))
	{
	  g_cond_wait(self->writer_thread_wakeup_cond, self->suspend_mutex);
	  g_mutex_unlock(self->suspend_mutex);
	}
      else
        g_mutex_unlock(self->suspend_mutex);

      if (self->writer_thread_terminate)
	break;

      if (!afmongodb_worker_insert (self))
	{
	  afmongodb_dd_disconnect(self);
	  afmongodb_dd_suspend(self);
	}
    }

  afmongodb_dd_disconnect(self);

  g_free (self->ns);
  g_string_free (self->current_value, TRUE);

  bson_free (self->bson);

  msg_debug ("Worker thread finished",
	     evt_tag_str("driver", self->super.super.id),
	     NULL);

  return NULL;
}
Example #9
0
/*
 * g_cond_wait_until:
 *
 * Just wrap g_cond_timed_wait.
 */
gboolean g_cond_wait_until(GCond *cond,GMutex *mutex,gint64 end_time)
{
	GTimeVal time;

	g_get_current_time(&time);
	g_time_val_add(&time,G_USEC_PER_SEC);
	return g_cond_timed_wait(cond,mutex,&time);
}
Example #10
0
static gpointer oh_event_thread_loop(gpointer data)
{
        GTimeVal time;
        SaErrorT error = SA_OK;
        static int first_loop = 1;

        g_mutex_lock(oh_event_thread_mutex);
        while (1) {
                /* Give the discovery time to start first -> FIXME */
                if (first_loop) {
                        struct timespec sleepytime =
                                { .tv_sec = 0, .tv_nsec = 500000000};
                        first_loop = 0;
                        nanosleep(&sleepytime, NULL);
                }

                trace("Thread Harvesting events");

                error = oh_harvest_events();
                if (error != SA_OK) dbg("Error on harvest of events.");

                trace("Thread processing events");
                error = oh_process_events();
                if (error != SA_OK) dbg("Error on processing of events.");

                /* Let oh_wake_event_thread know this thread is done */
                g_cond_broadcast(oh_event_thread_wait);
                g_get_current_time(&time);
                g_time_val_add(&time, OH_EVENT_THREAD_SLEEP_TIME);
                trace("Going to sleep");
                if (g_cond_timed_wait(oh_event_thread_wait, oh_event_thread_mutex, &time))
                        trace("SIGNALED: Got signal from plugin");
                else
                        trace("TIMEDOUT: Woke up, am looping again");
        }
        g_mutex_unlock(oh_event_thread_mutex);
        g_thread_exit(0);

        return data;
}

int oh_threaded_init()
{
        int error = 0;

        trace("Attempting to init event");
        if (!g_thread_supported()) {
                trace("Initializing thread support");
                g_thread_init(NULL);
        } else {
                trace("Already supporting threads");
        }

        error = oh_discovery_init();
        if (oh_event_init() || error) error = 1;

        return error;
}
Example #11
0
void
g_usleep (gulong microseconds)
{
#ifdef G_OS_WIN32
  Sleep (microseconds / 1000);
#else /* !G_OS_WIN32 */
# ifdef HAVE_NANOSLEEP
  struct timespec request, remaining;
  request.tv_sec = microseconds / G_USEC_PER_SEC;
  request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
  while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
    request = remaining;
# else /* !HAVE_NANOSLEEP */
#  ifdef HAVE_NSLEEP
  /* on AIX, nsleep is analogous to nanosleep */
  struct timespec request, remaining;
  request.tv_sec = microseconds / G_USEC_PER_SEC;
  request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
  while (nsleep (&request, &remaining) == -1 && errno == EINTR)
    request = remaining;
#  else /* !HAVE_NSLEEP */
  if (g_thread_supported ())
    {
      static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
      static GCond* cond = NULL;
      GTimeVal end_time;
      
      g_get_current_time (&end_time);
      if (microseconds > G_MAXLONG)
	{
	  microseconds -= G_MAXLONG;
	  g_time_val_add (&end_time, G_MAXLONG);
	}
      g_time_val_add (&end_time, microseconds);

      g_static_mutex_lock (&mutex);
      
      if (!cond)
	cond = g_cond_new ();
      
      while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), 
				&end_time))
	/* do nothing */;
      
      g_static_mutex_unlock (&mutex);
    }
  else
    {
      struct timeval tv;
      tv.tv_sec = microseconds / G_USEC_PER_SEC;
      tv.tv_usec = microseconds % G_USEC_PER_SEC;
      select(0, NULL, NULL, NULL, &tv);
    }
#  endif /* !HAVE_NSLEEP */
# endif /* !HAVE_NANOSLEEP */
#endif /* !G_OS_WIN32 */
}
Example #12
0
static gboolean
gst_hls_demux_update_thread (GstHLSDemux * demux)
{
  /* Loop for the updates. It's started when the first fragments are cached and
   * schedules the next update of the playlist (for lives sources) and the next
   * update of fragments. When a new fragment is downloaded, it compares the
   * download time with the next scheduled update to check if we can or should
   * switch to a different bitrate */

  g_mutex_lock (demux->thread_lock);
  while (TRUE) {
    /* block until the next scheduled update or the signal to quit this thread */
    if (g_cond_timed_wait (demux->thread_cond, demux->thread_lock,
            &demux->next_update)) {
      goto quit;
    }

    /* update the playlist for live sources */
    if (gst_m3u8_client_is_live (demux->client)) {
      if (!gst_hls_demux_update_playlist (demux, TRUE)) {
        GST_ERROR_OBJECT (demux, "Could not update the playlist");
        goto quit;
      }
    }

    /* schedule the next update */
    gst_hls_demux_schedule (demux);

    /* if it's a live source and the playlist couldn't be updated, there aren't
     * more fragments in the playlist, so we just wait for the next schedulled
     * update */
    if (gst_m3u8_client_is_live (demux->client) &&
        demux->client->update_failed_count > 0) {
      GST_WARNING_OBJECT (demux,
          "The playlist hasn't been updated, failed count is %d",
          demux->client->update_failed_count);
      continue;
    }

    /* fetch the next fragment */
    if (!gst_hls_demux_get_next_fragment (demux, TRUE)) {
      if (!demux->end_of_playlist && !demux->cancelled)
        GST_ERROR_OBJECT (demux, "Could not fetch the next fragment");
      goto quit;
    }

    /* try to switch to another bitrate if needed */
    gst_hls_demux_switch_playlist (demux);
  }

quit:
  {
    g_mutex_unlock (demux->thread_lock);
    return TRUE;
  }
}
static void
timed_wait_on_otherwise_locked_mutex (void)
{
  GMutex* mutex = g_mutex_new ();
  GCond* cond = g_cond_new ();
  GThread* thread = g_thread_create (locking_thread, mutex, TRUE, NULL);
  g_assert (thread != NULL);
  g_usleep (G_USEC_PER_SEC);
  g_cond_timed_wait (cond, mutex, NULL);
}
Example #14
0
static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex,
                                  gint64 end_time)
{
    gboolean ret = FALSE;
    end_time -= g_get_monotonic_time();
    GTimeVal time = { end_time / G_TIME_SPAN_SECOND,
                      end_time % G_TIME_SPAN_SECOND };
    ret = g_cond_timed_wait(cond, mutex, &time);
    return ret;
}
Example #15
0
/* assumes that db_thread_mutex is held */
static void
afsql_dd_wait_for_suspension_wakeup(AFSqlDestDriver *self)
{
  /* we got suspended, probably because of a connection error,
   * during this time we only get wakeups if we need to be
   * terminated. */
  if (!self->db_thread_terminate)
    g_cond_timed_wait(self->db_thread_wakeup_cond, self->db_thread_mutex, &self->db_thread_suspend_target);
  self->db_thread_suspended = FALSE;
}
Example #16
0
static gboolean _cond_wait_until(GCond *cond, GMutex *mutex, gint64 end_time)
{
    gboolean ret = FALSE;
#ifdef HAVE_COND_INIT
    ret = g_cond_wait_until(cond, mutex, end_time);
#else
    GTimeVal time = { end_time / G_TIME_SPAN_SECOND,
                      end_time % G_TIME_SPAN_SECOND };
    ret = g_cond_timed_wait(cond, mutex, &time);
#endif
    return ret;
}
Example #17
0
/* Wait until cond is signalled, or timeout us have passed. 
 *
 * You can get spurious wakeups, use this in a loop.
 */
void 
vips_g_cond_timed_wait( GCond *cond, GMutex *mutex, gint64 timeout )
{
#ifdef HAVE_COND_INIT
	g_cond_wait_until( cond, mutex, 
		g_get_monotonic_time() + timeout );
#else
	GTimeVal time;

	g_get_current_time( &time );
	g_time_val_add( &time, timeout );
	g_cond_timed_wait( cond, mutex, &time );
#endif
}
Example #18
0
/*
 * read one line from the file descriptor
 * timeout: msec unit, -1 for infinite
 * if CR comes then following LF is expected
 * returned string in line is always null terminated, maxlen-1 is maximum string length
 */
static int
read_line(struct rtspcl_data *rtspcld, char *line, int maxlen,
	  int timeout)
{
	g_mutex_lock(rtspcld->mutex);

	GTimeVal end_time;
	if (timeout >= 0) {
		g_get_current_time(&end_time);

		end_time.tv_sec += timeout / 1000;
		timeout %= 1000;
		end_time.tv_usec = timeout * 1000;
		if (end_time.tv_usec > 1000000) {
			end_time.tv_usec -= 1000000;
			++end_time.tv_sec;
		}
	}

	while (true) {
		if (!g_queue_is_empty(rtspcld->received_lines)) {
			/* success, copy to buffer */

			char *p = g_queue_pop_head(rtspcld->received_lines);
			g_mutex_unlock(rtspcld->mutex);

			g_strlcpy(line, p, maxlen);
			g_free(p);

			return strlen(line);
		}

		if (rtspcld->tcp_socket == NULL) {
			/* error */
			g_mutex_unlock(rtspcld->mutex);
			return -1;
		}

		if (timeout < 0) {
			g_cond_wait(rtspcld->cond, rtspcld->mutex);
		} else if (!g_cond_timed_wait(rtspcld->cond, rtspcld->mutex,
					      &end_time)) {
			g_mutex_unlock(rtspcld->mutex);
			return 0;
		}
	}
}
Example #19
0
void
egg_test_wait_stop (void)
{
	GTimeVal tv;

	g_get_current_time (&tv);
	g_time_val_add (&tv, 1000);

	g_assert (wait_mutex);
	g_assert (wait_condition);
	g_mutex_lock (wait_mutex);
		if (!wait_waiting)
			g_cond_timed_wait (wait_start, wait_mutex, &tv);
		g_assert (wait_waiting);
		g_cond_broadcast (wait_condition);
	g_mutex_unlock (wait_mutex);
}
Example #20
0
/**
 * e_flag_timed_wait:
 * @flag: an #EFlag
 * @abs_time: a #GTimeVal, determining the final time
 *
 * Blocks until @flag is set, or until the time specified by @abs_time.
 * If @flag is already set, the function returns immediately.  The return
 * value indicates the state of @flag after waiting.
 *
 * If @abs_time is %NULL, e_flag_timed_wait() acts like e_flag_wait().
 *
 * To easily calculate @abs_time, a combination of g_get_current_time() and
 * g_time_val_add() can be used.
 *
 * Returns: %TRUE if @flag is now set
 *
 * Since: 1.12
 **/
gboolean
e_flag_timed_wait (EFlag *flag,
                   GTimeVal *abs_time)
{
	gboolean is_set;

	g_return_val_if_fail (flag != NULL, FALSE);

	g_mutex_lock (flag->mutex);
	while (!flag->is_set)
		if (!g_cond_timed_wait (flag->cond, flag->mutex, abs_time))
			break;
	is_set = flag->is_set;
	g_mutex_unlock (flag->mutex);

	return is_set;
}
Example #21
0
/**
 * Wait until the output's delay reaches zero.
 *
 * @return true if playback should be continued, false if a command
 * was issued
 */
static bool
ao_wait(struct audio_output *ao)
{
    while (true) {
        unsigned delay = ao_plugin_delay(ao->plugin, ao->data);
        if (delay == 0)
            return true;

        GTimeVal tv;
        g_get_current_time(&tv);
        g_time_val_add(&tv, delay * 1000);
        (void)g_cond_timed_wait(ao->cond, ao->mutex, &tv);

        if (ao->command != AO_COMMAND_NONE)
            return false;
    }
}
Example #22
0
static gpointer
afamqp_worker_thread(gpointer arg)
{
  AMQPDestDriver *self = (AMQPDestDriver *) arg;

  msg_debug("Worker thread started",
            evt_tag_str("driver", self->super.super.id), NULL);

  afamqp_dd_connect(self, FALSE);

  while (!self->writer_thread_terminate)
    {
      g_mutex_lock(self->suspend_mutex);
      if (self->writer_thread_suspended)
        {
          g_cond_timed_wait(self->writer_thread_wakeup_cond,
                            self->suspend_mutex, &self->writer_thread_suspend_target);
          self->writer_thread_suspended = FALSE;
          g_mutex_unlock(self->suspend_mutex);
        }
      else if (!log_queue_check_items(self->queue, NULL, afamqp_dd_message_became_available_in_the_queue, self, NULL))
	{
	  g_cond_wait(self->writer_thread_wakeup_cond, self->suspend_mutex);
	  g_mutex_unlock(self->suspend_mutex);
	}
      else
        g_mutex_unlock(self->suspend_mutex);

      if (self->writer_thread_terminate)
        break;

      if (!afamqp_worker_insert(self))
        {
          afamqp_dd_disconnect(self);
          afamqp_dd_suspend(self);
        }
    }

  afamqp_dd_disconnect(self);

  msg_debug("Worker thread finished",
            evt_tag_str("driver", self->super.super.id), NULL);

  return NULL;
}
Example #23
0
gpointer LcmTunnel::sendThreadFunc(gpointer user_data)
{

  LcmTunnel *self = (LcmTunnel*) user_data;

  g_mutex_lock(self->sendQueueLock);
  int64_t nextFlushTime = 0;
  while (!self->stopSendThread) {
    if (self->sendQueue.empty()) {
      g_cond_wait(self->sendQueueCond, self->sendQueueLock);
      nextFlushTime = _timestamp_now() + self->tunnel_params->max_delay_ms * 1000;
      continue;
    }
    int64_t now = _timestamp_now();
    if (self->tunnel_params->max_delay_ms > 0 && self->bytesInQueue < NUM_BYTES_TO_SEND_IMMEDIATELY && nextFlushTime
        > now && !self->flushImmediately) {
      GTimeVal next_timeout;
      _timestamp_to_GTimeVal(nextFlushTime, &next_timeout);
      g_cond_timed_wait(self->sendQueueCond, self->sendQueueLock, &next_timeout);

      continue;
    }
    //there is stuff in the queue that we need to handle
    self->flushImmediately = false;

    //take current contents out of the queue
    std::deque<TunnelLcmMessage *> tmpQueue;
    tmpQueue.swap(self->sendQueue);
    uint32_t bytesInTmpQueue = self->bytesInQueue;
    self->bytesInQueue = 0;
    g_mutex_unlock(self->sendQueueLock);
    //release lock for sending

    //process whats in the queue
    bool success = self->send_lcm_messages(tmpQueue, bytesInTmpQueue);

    //reaquire lock to go around the loop
    g_mutex_lock(self->sendQueueLock);
    if (!success)
      break;
  }
  g_mutex_unlock(self->sendQueueLock);

  g_thread_exit(NULL);
}
CV_IMPL int cvWaitKey( int delay )
{
#ifdef HAVE_GTHREAD
	if(thread_started && g_thread_self()!=window_thread){
		gboolean expired;
		int my_last_key;

		// wait for signal or timeout if delay > 0
		if(delay>0){
			GTimeVal timer;
			g_get_current_time(&timer);
			g_time_val_add(&timer, delay*1000);
			expired = !g_cond_timed_wait(cond_have_key, last_key_mutex, &timer);
		}
		else{
			g_cond_wait(cond_have_key, last_key_mutex);
			expired=false;
		}
		my_last_key = last_key;
		g_mutex_unlock(last_key_mutex);
		if(expired || hg_windows==0){
			return -1;
		}
		return my_last_key;
	}
	else{
#endif
		int expired = 0;
		guint timer = 0;
		if( delay > 0 )
			timer = g_timeout_add( delay, icvAlarm, &expired );
		last_key = -1;
		while( gtk_main_iteration_do(TRUE) && last_key < 0 && !expired && hg_windows != 0 )
			;

		if( delay > 0 && !expired )
			g_source_remove(timer);
#ifdef HAVE_GTHREAD
	}
#endif
	return last_key;
}
Example #25
0
File: tee.c Project: zsx/ossbuild
static GstFlowReturn
final_sinkpad_bufferalloc (GstPad * pad, guint64 offset, guint size,
    GstCaps * caps, GstBuffer ** buf)
{
  BufferAllocHarness *h;
  GTimeVal deadline;

  h = g_object_get_qdata (G_OBJECT (pad),
      g_quark_from_static_string ("buffer-alloc-harness"));
  g_assert (h != NULL);

  if (--(h->countdown) == 0) {
    /* Time to make the app release the pad. */
    h->app_thread_prepped = FALSE;
    h->bufferalloc_blocked = TRUE;

    h->app_thread = g_thread_create (app_thread_func, h, TRUE, NULL);
    fail_if (h->app_thread == NULL);

    /* Wait for the app thread to get ready to call release_request_pad(). */
    g_mutex_lock (check_mutex);
    while (!h->app_thread_prepped)
      g_cond_wait (check_cond, check_mutex);
    g_mutex_unlock (check_mutex);

    /* Now wait for it to do that within a second, to avoid deadlocking
     * in the event of future changes to the locking semantics. */
    g_mutex_lock (check_mutex);
    g_get_current_time (&deadline);
    deadline.tv_sec += 1;
    while (h->bufferalloc_blocked) {
      if (!g_cond_timed_wait (check_cond, check_mutex, &deadline))
        break;
    }
    g_mutex_unlock (check_mutex);
  }

  *buf = gst_buffer_new_and_alloc (size);
  gst_buffer_set_caps (*buf, caps);

  return GST_FLOW_OK;
}
Example #26
0
static gpointer
g_async_queue_pop_intern_unlocked (GAsyncQueue *queue, 
				   gboolean     try_, 
				   GTimeVal    *end_time)
{
 gpointer retval;

	
 
  if (!g_queue_peek_tail_link (queue->queue))
    {
      if (try_)
	return NULL;
      
      if (!queue->cond)
	queue->cond = g_cond_new ();

      if (!end_time)
        {
          queue->waiting_threads++;
	  while (!g_queue_peek_tail_link (queue->queue))
            g_cond_wait (queue->cond, queue->mutex);
          queue->waiting_threads--;
        }
      else
        {
          queue->waiting_threads++;
          while (!g_queue_peek_tail_link (queue->queue))
            if (!g_cond_timed_wait (queue->cond, queue->mutex, end_time))
              break;
          queue->waiting_threads--;
          if (!g_queue_peek_tail_link (queue->queue))
	    return NULL;
        }
    }

  retval = g_queue_pop_tail (queue->queue);

  g_assert (retval);

  return retval;
}
bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
{
    // Time is in the past - return right away.
    if (absoluteTime < currentTime())
        return false;
    
    // Time is too far in the future for g_cond_timed_wait - wait forever.
    if (absoluteTime > INT_MAX) {
        wait(mutex);
        return true;
    }

    int timeSeconds = static_cast<int>(absoluteTime);
    int timeMicroseconds = static_cast<int>((absoluteTime - timeSeconds) * 1000000.0);
    
    GTimeVal targetTime;
    targetTime.tv_sec = timeSeconds;
    targetTime.tv_usec = timeMicroseconds;

    return g_cond_timed_wait(m_condition.get(), mutex.impl().get(), &targetTime);
}
Example #28
0
static gpointer oh_event_thread_loop(gpointer data)
{
        GTimeVal time;

        while(oh_run_threaded()) {
                dbg("About to run through the event loop");

                oh_get_events();

                g_get_current_time(&time);
                g_time_val_add(&time, OH_THREAD_SLEEP_TIME);
                dbg("Going to sleep");

                if (g_cond_timed_wait(oh_thread_wait, oh_thread_mutex, &time))
                        dbg("SIGNALED: Got signal from plugin");
                else
                        dbg("TIMEDOUT: Woke up, am looping again");
        }
        g_thread_exit(0);
        return 0;
}
Example #29
0
static inline void
wait_for_state (GOmxCore *core,
                OMX_STATETYPE state)
{
    GTimeVal tv;
    gboolean signaled;

    g_mutex_lock (core->omx_state_mutex);

    if (core->omx_error != OMX_ErrorNone)
        goto leave;

    g_get_current_time (&tv);
    g_time_val_add (&tv, 15 * G_USEC_PER_SEC);

    /* try once */
    if (core->omx_state != state)
    {
        signaled = g_cond_timed_wait (core->omx_state_condition, core->omx_state_mutex, &tv);

        if (!signaled)
        {
            GST_ERROR_OBJECT (core->object, "timed out switching from '%s' to '%s'",
                              omx_state_to_str(core->omx_state), omx_state_to_str(state));
        }
    }

    if (core->omx_error != OMX_ErrorNone)
        goto leave;

    if (core->omx_state != state)
    {
        GST_ERROR_OBJECT (core->object, "wrong state received: state=%d, expected=%d",
                          core->omx_state, state);
    }

leave:
    g_mutex_unlock (core->omx_state_mutex);
}
Example #30
0
/**
 * Wait until no collections have changed for 10 seconds, then sync.
 * @internal
 */
static gpointer
do_loop (gpointer udata)
{
	xmms_coll_dag_t *dag = udata;
	GTimeVal time;

	g_mutex_lock (mutex);

	while (keep_running) {
		if (!want_sync) {
			g_cond_wait (cond, mutex);
		}

		/* Wait until no requests have been filed for 10 seconds. */
		while (keep_running && want_sync) {
			want_sync = FALSE;

			g_get_current_time (&time);
			g_time_val_add (&time, 10000000);

			g_cond_timed_wait (cond, mutex, &time);
		}

		if (keep_running) {
			/* The dag might be locked when calling schedule_sync, so we need to
			 * unlock to avoid deadlocks */
			g_mutex_unlock (mutex);

			XMMS_DBG ("Syncing collections to database.");
			xmms_collection_sync (dag);

			g_mutex_lock (mutex);
		}
	}

	g_mutex_unlock (mutex);

	return NULL;
}