/* AsyncMutex.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from AsyncMutex.vala, do not modify */

/*
* Copyright 2019 elementary, Inc. (https://elementary.io)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
*/

#include <glib-object.h>
#include <gee.h>
#include <glib.h>
#include <gio/gio.h>
#include <gobject/gvaluecollector.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TYPE_ASYNC_MUTEX (async_mutex_get_type ())
#define ASYNC_MUTEX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_ASYNC_MUTEX, AsyncMutex))
#define ASYNC_MUTEX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_ASYNC_MUTEX, AsyncMutexClass))
#define IS_ASYNC_MUTEX(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_ASYNC_MUTEX))
#define IS_ASYNC_MUTEX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_ASYNC_MUTEX))
#define ASYNC_MUTEX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_ASYNC_MUTEX, AsyncMutexClass))

typedef struct _AsyncMutex AsyncMutex;
typedef struct _AsyncMutexClass AsyncMutexClass;
typedef struct _AsyncMutexPrivate AsyncMutexPrivate;

#define ASYNC_MUTEX_TYPE_CALLBACK (async_mutex_callback_get_type ())
#define ASYNC_MUTEX_CALLBACK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ASYNC_MUTEX_TYPE_CALLBACK, AsyncMutexCallback))
#define ASYNC_MUTEX_CALLBACK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ASYNC_MUTEX_TYPE_CALLBACK, AsyncMutexCallbackClass))
#define ASYNC_MUTEX_IS_CALLBACK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ASYNC_MUTEX_TYPE_CALLBACK))
#define ASYNC_MUTEX_IS_CALLBACK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ASYNC_MUTEX_TYPE_CALLBACK))
#define ASYNC_MUTEX_CALLBACK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ASYNC_MUTEX_TYPE_CALLBACK, AsyncMutexCallbackClass))

typedef struct _AsyncMutexCallback AsyncMutexCallback;
typedef struct _AsyncMutexCallbackClass AsyncMutexCallbackClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _async_mutex_unref0(var) ((var == NULL) ? NULL : (var = (async_mutex_unref (var), NULL)))
#define _async_mutex_callback_unref0(var) ((var == NULL) ? NULL : (var = (async_mutex_callback_unref (var), NULL)))
typedef struct _AsyncMutexLockData AsyncMutexLockData;
typedef struct _AsyncMutexCallbackPrivate AsyncMutexCallbackPrivate;
typedef struct _AsyncMutexParamSpecCallback AsyncMutexParamSpecCallback;
typedef struct _ParamSpecAsyncMutex ParamSpecAsyncMutex;

struct _AsyncMutex {
	GTypeInstance parent_instance;
	volatile int ref_count;
	AsyncMutexPrivate * priv;
};

struct _AsyncMutexClass {
	GTypeClass parent_class;
	void (*finalize) (AsyncMutex *self);
};

struct _AsyncMutexPrivate {
	GeeArrayQueue* callbacks;
	gboolean locked;
};

struct _AsyncMutexLockData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	AsyncMutex* self;
	GSourceFunc cb;
	gpointer cb_target;
	GDestroyNotify cb_target_destroy_notify;
	GeeArrayQueue* _tmp0_;
	GSourceFunc _tmp1_;
	gpointer _tmp1__target;
	GDestroyNotify _tmp1__target_destroy_notify;
	AsyncMutexCallback* _tmp2_;
	AsyncMutexCallback* _tmp3_;
};

struct _AsyncMutexCallback {
	GTypeInstance parent_instance;
	volatile int ref_count;
	AsyncMutexCallbackPrivate * priv;
	GSourceFunc callback;
	gpointer callback_target;
	GDestroyNotify callback_target_destroy_notify;
};

struct _AsyncMutexCallbackClass {
	GTypeClass parent_class;
	void (*finalize) (AsyncMutexCallback *self);
};

struct _AsyncMutexParamSpecCallback {
	GParamSpec parent_instance;
};

struct _ParamSpecAsyncMutex {
	GParamSpec parent_instance;
};

static gint AsyncMutex_private_offset;
static gpointer async_mutex_parent_class = NULL;
static gpointer async_mutex_callback_parent_class = NULL;

VALA_EXTERN gpointer async_mutex_ref (gpointer instance);
VALA_EXTERN void async_mutex_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_async_mutex (const gchar* name,
                                    const gchar* nick,
                                    const gchar* blurb,
                                    GType object_type,
                                    GParamFlags flags);
VALA_EXTERN void value_set_async_mutex (GValue* value,
                            gpointer v_object);
VALA_EXTERN void value_take_async_mutex (GValue* value,
                             gpointer v_object);
VALA_EXTERN gpointer value_get_async_mutex (const GValue* value);
VALA_EXTERN GType async_mutex_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (AsyncMutex, async_mutex_unref)
static gpointer async_mutex_callback_ref (gpointer instance);
static void async_mutex_callback_unref (gpointer instance);
static GParamSpec* async_mutex_param_spec_callback (const gchar* name,
                                             const gchar* nick,
                                             const gchar* blurb,
                                             GType object_type,
                                             GParamFlags flags) G_GNUC_UNUSED ;
static void async_mutex_value_set_callback (GValue* value,
                                     gpointer v_object) G_GNUC_UNUSED ;
static void async_mutex_value_take_callback (GValue* value,
                                      gpointer v_object) G_GNUC_UNUSED ;
static gpointer async_mutex_value_get_callback (const GValue* value) G_GNUC_UNUSED ;
static GType async_mutex_callback_get_type (void) G_GNUC_CONST  G_GNUC_UNUSED ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (AsyncMutexCallback, async_mutex_callback_unref)
VALA_EXTERN AsyncMutex* async_mutex_new (void);
VALA_EXTERN AsyncMutex* async_mutex_construct (GType object_type);
static void async_mutex_lock_data_free (gpointer _data);
VALA_EXTERN void async_mutex_lock (AsyncMutex* self,
                       GAsyncReadyCallback _callback_,
                       gpointer _user_data_);
VALA_EXTERN void async_mutex_lock_finish (AsyncMutex* self,
                              GAsyncResult* _res_);
static gboolean async_mutex_lock_co (AsyncMutexLockData* _data_);
static gboolean _async_mutex_lock_co_gsource_func (gpointer self);
static AsyncMutexCallback* async_mutex_callback_new (GSourceFunc cb,
                                              gpointer cb_target,
                                              GDestroyNotify cb_target_destroy_notify);
static AsyncMutexCallback* async_mutex_callback_construct (GType object_type,
                                                    GSourceFunc cb,
                                                    gpointer cb_target,
                                                    GDestroyNotify cb_target_destroy_notify);
VALA_EXTERN void async_mutex_unlock (AsyncMutex* self);
static void async_mutex_callback_finalize (AsyncMutexCallback * obj);
static GType async_mutex_callback_get_type_once (void);
static void async_mutex_finalize (AsyncMutex * obj);
static GType async_mutex_get_type_once (void);

static inline gpointer
async_mutex_get_instance_private (AsyncMutex* self)
{
	return G_STRUCT_MEMBER_P (self, AsyncMutex_private_offset);
}

AsyncMutex*
async_mutex_construct (GType object_type)
{
	AsyncMutex* self = NULL;
	GeeArrayQueue* _tmp0_;
	self = (AsyncMutex*) g_type_create_instance (object_type);
	self->priv->locked = FALSE;
	_tmp0_ = gee_array_queue_new (ASYNC_MUTEX_TYPE_CALLBACK, (GBoxedCopyFunc) async_mutex_callback_ref, (GDestroyNotify) async_mutex_callback_unref, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->callbacks);
	self->priv->callbacks = _tmp0_;
	return self;
}

AsyncMutex*
async_mutex_new (void)
{
	return async_mutex_construct (TYPE_ASYNC_MUTEX);
}

static void
async_mutex_lock_data_free (gpointer _data)
{
	AsyncMutexLockData* _data_;
	_data_ = _data;
	_async_mutex_unref0 (_data_->self);
	g_slice_free (AsyncMutexLockData, _data_);
}

static gpointer
_async_mutex_ref0 (gpointer self)
{
	return self ? async_mutex_ref (self) : NULL;
}

void
async_mutex_lock (AsyncMutex* self,
                  GAsyncReadyCallback _callback_,
                  gpointer _user_data_)
{
	AsyncMutexLockData* _data_;
	AsyncMutex* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (AsyncMutexLockData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, async_mutex_lock_data_free);
	_tmp0_ = _async_mutex_ref0 (self);
	_data_->self = _tmp0_;
	async_mutex_lock_co (_data_);
}

void
async_mutex_lock_finish (AsyncMutex* self,
                         GAsyncResult* _res_)
{
	AsyncMutexLockData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static gboolean
_async_mutex_lock_co_gsource_func (gpointer self)
{
	gboolean result;
	result = async_mutex_lock_co (self);
	return result;
}

static gboolean
async_mutex_lock_co (AsyncMutexLockData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	while (TRUE) {
		if (!_data_->self->priv->locked) {
			break;
		}
		_data_->cb = _async_mutex_lock_co_gsource_func;
		_data_->cb_target = _data_;
		_data_->cb_target_destroy_notify = NULL;
		_data_->_tmp0_ = _data_->self->priv->callbacks;
		_data_->_tmp1_ = _data_->cb;
		_data_->_tmp1__target = _data_->cb_target;
		_data_->_tmp1__target_destroy_notify = _data_->cb_target_destroy_notify;
		_data_->cb = NULL;
		_data_->cb_target = NULL;
		_data_->cb_target_destroy_notify = NULL;
		_data_->_tmp2_ = async_mutex_callback_new (_data_->_tmp1_, _data_->_tmp1__target, _data_->_tmp1__target_destroy_notify);
		_data_->_tmp3_ = _data_->_tmp2_;
		gee_deque_offer_head ((GeeDeque*) _data_->_tmp0_, _data_->_tmp3_);
		_async_mutex_callback_unref0 (_data_->_tmp3_);
		_data_->_state_ = 1;
		return FALSE;
		_state_1:
		;
		(_data_->cb_target_destroy_notify == NULL) ? NULL : (_data_->cb_target_destroy_notify (_data_->cb_target), NULL);
		_data_->cb = NULL;
		_data_->cb_target = NULL;
		_data_->cb_target_destroy_notify = NULL;
	}
	_data_->self->priv->locked = TRUE;
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

void
async_mutex_unlock (AsyncMutex* self)
{
	AsyncMutexCallback* callback = NULL;
	GeeArrayQueue* _tmp0_;
	gpointer _tmp1_;
	AsyncMutexCallback* _tmp2_;
	g_return_if_fail (self != NULL);
	self->priv->locked = FALSE;
	_tmp0_ = self->priv->callbacks;
	_tmp1_ = gee_deque_poll_head ((GeeDeque*) _tmp0_);
	callback = (AsyncMutexCallback*) _tmp1_;
	_tmp2_ = callback;
	if (_tmp2_ != NULL) {
		AsyncMutexCallback* _tmp3_;
		GSourceFunc _tmp4_;
		gpointer _tmp4__target;
		GDestroyNotify _tmp4__target_destroy_notify;
		_tmp3_ = callback;
		_tmp4_ = _tmp3_->callback;
		_tmp4__target = _tmp3_->callback_target;
		_tmp4__target_destroy_notify = _tmp3_->callback_target_destroy_notify;
		_tmp3_->callback = NULL;
		_tmp3_->callback_target = NULL;
		_tmp3_->callback_target_destroy_notify = NULL;
		g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, _tmp4_, _tmp4__target, _tmp4__target_destroy_notify);
	}
	_async_mutex_callback_unref0 (callback);
}

static AsyncMutexCallback*
async_mutex_callback_construct (GType object_type,
                                GSourceFunc cb,
                                gpointer cb_target,
                                GDestroyNotify cb_target_destroy_notify)
{
	AsyncMutexCallback* self = NULL;
	GSourceFunc _tmp0_;
	gpointer _tmp0__target;
	GDestroyNotify _tmp0__target_destroy_notify;
	self = (AsyncMutexCallback*) g_type_create_instance (object_type);
	_tmp0_ = cb;
	_tmp0__target = cb_target;
	_tmp0__target_destroy_notify = cb_target_destroy_notify;
	cb = NULL;
	cb_target = NULL;
	cb_target_destroy_notify = NULL;
	(self->callback_target_destroy_notify == NULL) ? NULL : (self->callback_target_destroy_notify (self->callback_target), NULL);
	self->callback = NULL;
	self->callback_target = NULL;
	self->callback_target_destroy_notify = NULL;
	self->callback = _tmp0_;
	self->callback_target = _tmp0__target;
	self->callback_target_destroy_notify = _tmp0__target_destroy_notify;
	(cb_target_destroy_notify == NULL) ? NULL : (cb_target_destroy_notify (cb_target), NULL);
	cb = NULL;
	cb_target = NULL;
	cb_target_destroy_notify = NULL;
	return self;
}

static AsyncMutexCallback*
async_mutex_callback_new (GSourceFunc cb,
                          gpointer cb_target,
                          GDestroyNotify cb_target_destroy_notify)
{
	return async_mutex_callback_construct (ASYNC_MUTEX_TYPE_CALLBACK, cb, cb_target, cb_target_destroy_notify);
}

static void
async_mutex_value_callback_init (GValue* value)
{
	value->data[0].v_pointer = NULL;
}

static void
async_mutex_value_callback_free_value (GValue* value)
{
	if (value->data[0].v_pointer) {
		async_mutex_callback_unref (value->data[0].v_pointer);
	}
}

static void
async_mutex_value_callback_copy_value (const GValue* src_value,
                                       GValue* dest_value)
{
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = async_mutex_callback_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}

static gpointer
async_mutex_value_callback_peek_pointer (const GValue* value)
{
	return value->data[0].v_pointer;
}

static gchar*
async_mutex_value_callback_collect_value (GValue* value,
                                          guint n_collect_values,
                                          GTypeCValue* collect_values,
                                          guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		AsyncMutexCallback * object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = async_mutex_callback_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
async_mutex_value_callback_lcopy_value (const GValue* value,
                                        guint n_collect_values,
                                        GTypeCValue* collect_values,
                                        guint collect_flags)
{
	AsyncMutexCallback ** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = async_mutex_callback_ref (value->data[0].v_pointer);
	}
	return NULL;
}

static GParamSpec*
async_mutex_param_spec_callback (const gchar* name,
                                 const gchar* nick,
                                 const gchar* blurb,
                                 GType object_type,
                                 GParamFlags flags)
{
	AsyncMutexParamSpecCallback* spec;
	g_return_val_if_fail (g_type_is_a (object_type, ASYNC_MUTEX_TYPE_CALLBACK), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}

static gpointer
async_mutex_value_get_callback (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, ASYNC_MUTEX_TYPE_CALLBACK), NULL);
	return value->data[0].v_pointer;
}

static void
async_mutex_value_set_callback (GValue* value,
                                gpointer v_object)
{
	AsyncMutexCallback * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, ASYNC_MUTEX_TYPE_CALLBACK));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, ASYNC_MUTEX_TYPE_CALLBACK));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		async_mutex_callback_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		async_mutex_callback_unref (old);
	}
}

static void
async_mutex_value_take_callback (GValue* value,
                                 gpointer v_object)
{
	AsyncMutexCallback * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, ASYNC_MUTEX_TYPE_CALLBACK));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, ASYNC_MUTEX_TYPE_CALLBACK));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		async_mutex_callback_unref (old);
	}
}

static void
async_mutex_callback_class_init (AsyncMutexCallbackClass * klass,
                                 gpointer klass_data)
{
	async_mutex_callback_parent_class = g_type_class_peek_parent (klass);
	((AsyncMutexCallbackClass *) klass)->finalize = async_mutex_callback_finalize;
}

static void
async_mutex_callback_instance_init (AsyncMutexCallback * self,
                                    gpointer klass)
{
	self->ref_count = 1;
}

static void
async_mutex_callback_finalize (AsyncMutexCallback * obj)
{
	AsyncMutexCallback * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ASYNC_MUTEX_TYPE_CALLBACK, AsyncMutexCallback);
	g_signal_handlers_destroy (self);
	(self->callback_target_destroy_notify == NULL) ? NULL : (self->callback_target_destroy_notify (self->callback_target), NULL);
	self->callback = NULL;
	self->callback_target = NULL;
	self->callback_target_destroy_notify = NULL;
}

static GType
async_mutex_callback_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { async_mutex_value_callback_init, async_mutex_value_callback_free_value, async_mutex_value_callback_copy_value, async_mutex_value_callback_peek_pointer, "p", async_mutex_value_callback_collect_value, "p", async_mutex_value_callback_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (AsyncMutexCallbackClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) async_mutex_callback_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (AsyncMutexCallback), 0, (GInstanceInitFunc) async_mutex_callback_instance_init, &g_define_type_value_table };
	static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
	GType async_mutex_callback_type_id;
	async_mutex_callback_type_id = g_type_register_fundamental (g_type_fundamental_next (), "AsyncMutexCallback", &g_define_type_info, &g_define_type_fundamental_info, 0);
	return async_mutex_callback_type_id;
}

static GType
async_mutex_callback_get_type (void)
{
	static volatile gsize async_mutex_callback_type_id__once = 0;
	if (g_once_init_enter (&async_mutex_callback_type_id__once)) {
		GType async_mutex_callback_type_id;
		async_mutex_callback_type_id = async_mutex_callback_get_type_once ();
		g_once_init_leave (&async_mutex_callback_type_id__once, async_mutex_callback_type_id);
	}
	return async_mutex_callback_type_id__once;
}

static gpointer
async_mutex_callback_ref (gpointer instance)
{
	AsyncMutexCallback * self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}

static void
async_mutex_callback_unref (gpointer instance)
{
	AsyncMutexCallback * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		ASYNC_MUTEX_CALLBACK_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

static void
value_async_mutex_init (GValue* value)
{
	value->data[0].v_pointer = NULL;
}

static void
value_async_mutex_free_value (GValue* value)
{
	if (value->data[0].v_pointer) {
		async_mutex_unref (value->data[0].v_pointer);
	}
}

static void
value_async_mutex_copy_value (const GValue* src_value,
                              GValue* dest_value)
{
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = async_mutex_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}

static gpointer
value_async_mutex_peek_pointer (const GValue* value)
{
	return value->data[0].v_pointer;
}

static gchar*
value_async_mutex_collect_value (GValue* value,
                                 guint n_collect_values,
                                 GTypeCValue* collect_values,
                                 guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		AsyncMutex * object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = async_mutex_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
value_async_mutex_lcopy_value (const GValue* value,
                               guint n_collect_values,
                               GTypeCValue* collect_values,
                               guint collect_flags)
{
	AsyncMutex ** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = async_mutex_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
param_spec_async_mutex (const gchar* name,
                        const gchar* nick,
                        const gchar* blurb,
                        GType object_type,
                        GParamFlags flags)
{
	ParamSpecAsyncMutex* spec;
	g_return_val_if_fail (g_type_is_a (object_type, TYPE_ASYNC_MUTEX), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}

gpointer
value_get_async_mutex (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_ASYNC_MUTEX), NULL);
	return value->data[0].v_pointer;
}

void
value_set_async_mutex (GValue* value,
                       gpointer v_object)
{
	AsyncMutex * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_ASYNC_MUTEX));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_ASYNC_MUTEX));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		async_mutex_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		async_mutex_unref (old);
	}
}

void
value_take_async_mutex (GValue* value,
                        gpointer v_object)
{
	AsyncMutex * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_ASYNC_MUTEX));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_ASYNC_MUTEX));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		async_mutex_unref (old);
	}
}

static void
async_mutex_class_init (AsyncMutexClass * klass,
                        gpointer klass_data)
{
	async_mutex_parent_class = g_type_class_peek_parent (klass);
	((AsyncMutexClass *) klass)->finalize = async_mutex_finalize;
	g_type_class_adjust_private_offset (klass, &AsyncMutex_private_offset);
}

static void
async_mutex_instance_init (AsyncMutex * self,
                           gpointer klass)
{
	self->priv = async_mutex_get_instance_private (self);
	self->ref_count = 1;
}

static void
async_mutex_finalize (AsyncMutex * obj)
{
	AsyncMutex * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_ASYNC_MUTEX, AsyncMutex);
	g_signal_handlers_destroy (self);
	_g_object_unref0 (self->priv->callbacks);
}

static GType
async_mutex_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { value_async_mutex_init, value_async_mutex_free_value, value_async_mutex_copy_value, value_async_mutex_peek_pointer, "p", value_async_mutex_collect_value, "p", value_async_mutex_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (AsyncMutexClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) async_mutex_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (AsyncMutex), 0, (GInstanceInitFunc) async_mutex_instance_init, &g_define_type_value_table };
	static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
	GType async_mutex_type_id;
	async_mutex_type_id = g_type_register_fundamental (g_type_fundamental_next (), "AsyncMutex", &g_define_type_info, &g_define_type_fundamental_info, 0);
	AsyncMutex_private_offset = g_type_add_instance_private (async_mutex_type_id, sizeof (AsyncMutexPrivate));
	return async_mutex_type_id;
}

GType
async_mutex_get_type (void)
{
	static volatile gsize async_mutex_type_id__once = 0;
	if (g_once_init_enter (&async_mutex_type_id__once)) {
		GType async_mutex_type_id;
		async_mutex_type_id = async_mutex_get_type_once ();
		g_once_init_leave (&async_mutex_type_id__once, async_mutex_type_id);
	}
	return async_mutex_type_id__once;
}

gpointer
async_mutex_ref (gpointer instance)
{
	AsyncMutex * self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}

void
async_mutex_unref (gpointer instance)
{
	AsyncMutex * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		ASYNC_MUTEX_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

