Skip to content

Commit

Permalink
trace: add glib 2.32+ static GMutex support
Browse files Browse the repository at this point in the history
The GStaticMutex API was deprecated in glib 2.32.  We cannot switch over
to GMutex unconditionally since we would drop support for older glib
versions.  But the deprecated API warnings during build are annoying so
use static GMutex when possible.

Signed-off-by: Stefan Hajnoczi <[email protected]>
  • Loading branch information
stefanhaRH committed Jan 27, 2014
1 parent b618c28 commit 05735a2
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions trace/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@
* Trace records are written out by a dedicated thread. The thread waits for
* records to become available, writes them out, and then waits again.
*/
#if GLIB_CHECK_VERSION(2, 32, 0)
static GMutex trace_lock;
#define lock_trace_lock() g_mutex_lock(&trace_lock)
#define unlock_trace_lock() g_mutex_unlock(&trace_lock)
#define get_trace_lock_mutex() (&trace_lock)
#else
static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT;
#define lock_trace_lock() g_static_mutex_lock(&trace_lock)
#define unlock_trace_lock() g_static_mutex_unlock(&trace_lock)
#define get_trace_lock_mutex() g_static_mutex_get_mutex(&trace_lock)
#endif

/* g_cond_new() was deprecated in glib 2.31 but we still need to support it */
#if GLIB_CHECK_VERSION(2, 31, 0)
Expand Down Expand Up @@ -140,27 +150,26 @@ static bool get_trace_record(unsigned int idx, TraceRecord **recordptr)
*/
static void flush_trace_file(bool wait)
{
g_static_mutex_lock(&trace_lock);
lock_trace_lock();
trace_available = true;
g_cond_signal(trace_available_cond);

if (wait) {
g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock));
g_cond_wait(trace_empty_cond, get_trace_lock_mutex());
}

g_static_mutex_unlock(&trace_lock);
unlock_trace_lock();
}

static void wait_for_trace_records_available(void)
{
g_static_mutex_lock(&trace_lock);
lock_trace_lock();
while (!(trace_available && trace_writeout_enabled)) {
g_cond_signal(trace_empty_cond);
g_cond_wait(trace_available_cond,
g_static_mutex_get_mutex(&trace_lock));
g_cond_wait(trace_available_cond, get_trace_lock_mutex());
}
trace_available = false;
g_static_mutex_unlock(&trace_lock);
unlock_trace_lock();
}

static gpointer writeout_thread(gpointer opaque)
Expand Down

0 comments on commit 05735a2

Please sign in to comment.