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

/* ThreadManager.vala
 *
 * Copyright 2023 Diego Iván <diegoivan.mae@gmail.com>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <glib-object.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

typedef struct _ThreadManagerTask ThreadManagerTask;
typedef gpointer (*ThreadManagerThreadFunc) (gpointer user_data, GError** error);
typedef void (*ThreadManagerTaskFunc) (gpointer user_data);
#define _g_thread_pool_free0(var) ((var == NULL) ? NULL : (var = (g_thread_pool_free (var, FALSE, TRUE), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _thread_manager_task_free0(var) ((var == NULL) ? NULL : (var = (thread_manager_task_free (var), NULL)))
typedef struct _Block2Data Block2Data;
typedef struct _ThreadManagerRunInThreadData ThreadManagerRunInThreadData;

struct _ThreadManagerTask {
	ThreadManagerTaskFunc task_func;
	gpointer task_func_target;
	GDestroyNotify task_func_target_destroy_notify;
};

struct _Block2Data {
	int _ref_count_;
	GType g_type;
	GBoxedCopyFunc g_dup_func;
	GDestroyNotify g_destroy_func;
	GError* _error_;
	gpointer retval;
	ThreadManagerThreadFunc thread_func;
	gpointer thread_func_target;
	GDestroyNotify thread_func_target_destroy_notify;
	gpointer _async_data_;
};

struct _ThreadManagerRunInThreadData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ThreadManagerThreadFunc thread_func;
	gpointer thread_func_target;
	GDestroyNotify thread_func_target_destroy_notify;
	GType g_type;
	GBoxedCopyFunc g_dup_func;
	GDestroyNotify g_destroy_func;
	gpointer result;
	Block2Data* _data2_;
	gpointer _tmp0_;
	gpointer _tmp1_;
	gpointer _tmp2_;
	GThreadPool* _tmp3_;
	ThreadManagerTask* _tmp4_;
	GError* _tmp5_;
	GError* _tmp6_;
	GError* _tmp7_;
	gconstpointer _tmp8_;
	gpointer _tmp9_;
	GError* _inner_error0_;
};

VALA_EXTERN GThreadPool* thread_manager_thread_pool;
GThreadPool* thread_manager_thread_pool = NULL;
VALA_EXTERN gboolean thread_manager_initiated;
gboolean thread_manager_initiated = FALSE;
VALA_EXTERN gboolean thread_manager_supported;
gboolean thread_manager_supported = TRUE;

VALA_EXTERN void thread_manager_task_free (ThreadManagerTask * self);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ThreadManagerTask, thread_manager_task_free)
static void thread_manager_task_instance_init (ThreadManagerTask * self);
VALA_EXTERN ThreadManagerTask* thread_manager_task_new (ThreadManagerTaskFunc task_func_,
                                            gpointer task_func__target,
                                            GDestroyNotify task_func__target_destroy_notify);
VALA_EXTERN void thread_manager_task_run (ThreadManagerTask* self);
VALA_EXTERN void thread_manager_init (void);
VALA_EXTERN void thread_manager_run_task (ThreadManagerTask* task);
static void _thread_manager_run_task_gfunc (gpointer data,
                                     gpointer self);
static void thread_manager_run_in_thread_data_free (gpointer _data);
VALA_EXTERN void thread_manager_run_in_thread (GType g_type,
                                   GBoxedCopyFunc g_dup_func,
                                   GDestroyNotify g_destroy_func,
                                   ThreadManagerThreadFunc thread_func,
                                   gpointer thread_func_target,
                                   GDestroyNotify thread_func_target_destroy_notify,
                                   GAsyncReadyCallback _callback_,
                                   gpointer _user_data_);
VALA_EXTERN gpointer thread_manager_run_in_thread_finish (GAsyncResult* _res_,
                                              GError** error);
static gboolean thread_manager_run_in_thread_co (ThreadManagerRunInThreadData* _data_);
static Block2Data* block2_data_ref (Block2Data* _data2_);
static void block2_data_unref (void * _userdata_);
static void __lambda5_ (Block2Data* _data2_);
static gboolean _thread_manager_run_in_thread_co_gsource_func (gpointer self);
static void ___lambda5__thread_manager_task_func (gpointer self);

ThreadManagerTask*
thread_manager_task_new (ThreadManagerTaskFunc task_func_,
                         gpointer task_func__target,
                         GDestroyNotify task_func__target_destroy_notify)
{
	ThreadManagerTask* self;
	ThreadManagerTaskFunc _tmp0_;
	gpointer _tmp0__target;
	GDestroyNotify _tmp0__target_destroy_notify;
	self = g_slice_new0 (ThreadManagerTask);
	thread_manager_task_instance_init (self);
	_tmp0_ = task_func_;
	_tmp0__target = task_func__target;
	_tmp0__target_destroy_notify = task_func__target_destroy_notify;
	task_func_ = NULL;
	task_func__target = NULL;
	task_func__target_destroy_notify = NULL;
	(self->task_func_target_destroy_notify == NULL) ? NULL : (self->task_func_target_destroy_notify (self->task_func_target), NULL);
	self->task_func = NULL;
	self->task_func_target = NULL;
	self->task_func_target_destroy_notify = NULL;
	self->task_func = _tmp0_;
	self->task_func_target = _tmp0__target;
	self->task_func_target_destroy_notify = _tmp0__target_destroy_notify;
	(task_func__target_destroy_notify == NULL) ? NULL : (task_func__target_destroy_notify (task_func__target), NULL);
	task_func_ = NULL;
	task_func__target = NULL;
	task_func__target_destroy_notify = NULL;
	return self;
}

void
thread_manager_task_run (ThreadManagerTask* self)
{
	ThreadManagerTaskFunc _tmp0_;
	gpointer _tmp0__target;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->task_func;
	_tmp0__target = self->task_func_target;
	_tmp0_ (_tmp0__target);
}

static void
thread_manager_task_instance_init (ThreadManagerTask * self)
{
}

void
thread_manager_task_free (ThreadManagerTask * self)
{
	(self->task_func_target_destroy_notify == NULL) ? NULL : (self->task_func_target_destroy_notify (self->task_func_target), NULL);
	self->task_func = NULL;
	self->task_func_target = NULL;
	self->task_func_target_destroy_notify = NULL;
	g_slice_free (ThreadManagerTask, self);
}

static void
_thread_manager_run_task_gfunc (gpointer data,
                                gpointer self)
{
	thread_manager_run_task ((ThreadManagerTask*) data);
}

void
thread_manager_init (void)
{
	GError* _inner_error0_ = NULL;
	if (thread_manager_initiated) {
		return;
	}
	{
		GThreadPool* _tmp0_ = NULL;
		GThreadPool* _tmp1_;
		GThreadPool* _tmp2_;
		_tmp1_ = g_thread_pool_new (_thread_manager_run_task_gfunc, NULL, 1, FALSE, &_inner_error0_);
		_tmp0_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		_g_thread_pool_free0 (thread_manager_thread_pool);
		thread_manager_thread_pool = _tmp2_;
		_g_thread_pool_free0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		thread_manager_supported = FALSE;
		_tmp3_ = e;
		_tmp4_ = _tmp3_->message;
		g_critical ("ThreadManager.vala:39: %s", _tmp4_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	thread_manager_initiated = TRUE;
}

void
thread_manager_run_task (ThreadManagerTask* task)
{
	g_return_if_fail (task != NULL);
	thread_manager_task_run (task);
	_thread_manager_task_free0 (task);
}

static void
thread_manager_run_in_thread_data_free (gpointer _data)
{
	ThreadManagerRunInThreadData* _data_;
	_data_ = _data;
	((_data_->result == NULL) || (_data_->g_destroy_func == NULL)) ? NULL : (_data_->result = (_data_->g_destroy_func (_data_->result), NULL));
	g_slice_free (ThreadManagerRunInThreadData, _data_);
}

void
thread_manager_run_in_thread (GType g_type,
                              GBoxedCopyFunc g_dup_func,
                              GDestroyNotify g_destroy_func,
                              ThreadManagerThreadFunc thread_func,
                              gpointer thread_func_target,
                              GDestroyNotify thread_func_target_destroy_notify,
                              GAsyncReadyCallback _callback_,
                              gpointer _user_data_)
{
	ThreadManagerRunInThreadData* _data_;
	_data_ = g_slice_new0 (ThreadManagerRunInThreadData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, thread_manager_run_in_thread_data_free);
	(_data_->thread_func_target_destroy_notify == NULL) ? NULL : (_data_->thread_func_target_destroy_notify (_data_->thread_func_target), NULL);
	_data_->thread_func = NULL;
	_data_->thread_func_target = NULL;
	_data_->thread_func_target_destroy_notify = NULL;
	_data_->thread_func = thread_func;
	_data_->thread_func_target = thread_func_target;
	_data_->thread_func_target_destroy_notify = thread_func_target_destroy_notify;
	_data_->g_type = g_type;
	_data_->g_dup_func = g_dup_func;
	_data_->g_destroy_func = g_destroy_func;
	thread_manager_run_in_thread_co (_data_);
}

gpointer
thread_manager_run_in_thread_finish (GAsyncResult* _res_,
                                     GError** error)
{
	gpointer result;
	ThreadManagerRunInThreadData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return NULL;
	}
	result = _data_->result;
	_data_->result = NULL;
	return result;
}

static Block2Data*
block2_data_ref (Block2Data* _data2_)
{
	g_atomic_int_inc (&_data2_->_ref_count_);
	return _data2_;
}

static void
block2_data_unref (void * _userdata_)
{
	Block2Data* _data2_;
	_data2_ = (Block2Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data2_->_ref_count_)) {
		GType g_type;
		GBoxedCopyFunc g_dup_func;
		GDestroyNotify g_destroy_func;
		g_type = _data2_->g_type;
		g_dup_func = _data2_->g_dup_func;
		g_destroy_func = _data2_->g_destroy_func;
		((_data2_->retval == NULL) || (g_destroy_func == NULL)) ? NULL : (_data2_->retval = (g_destroy_func (_data2_->retval), NULL));
		_g_error_free0 (_data2_->_error_);
		(_data2_->thread_func_target_destroy_notify == NULL) ? NULL : (_data2_->thread_func_target_destroy_notify (_data2_->thread_func_target), NULL);
		_data2_->thread_func = NULL;
		_data2_->thread_func_target = NULL;
		_data2_->thread_func_target_destroy_notify = NULL;
		g_slice_free (Block2Data, _data2_);
	}
}

static gpointer
_g_error_copy0 (gpointer self)
{
	return self ? g_error_copy (self) : NULL;
}

static gboolean
_thread_manager_run_in_thread_co_gsource_func (gpointer self)
{
	gboolean result;
	result = thread_manager_run_in_thread_co (self);
	return result;
}

static void
__lambda5_ (Block2Data* _data2_)
{
	GType g_type;
	GBoxedCopyFunc g_dup_func;
	GDestroyNotify g_destroy_func;
	GError* _inner_error0_ = NULL;
	g_type = _data2_->g_type;
	g_dup_func = _data2_->g_dup_func;
	g_destroy_func = _data2_->g_destroy_func;
	{
		gpointer _tmp0_ = NULL;
		gpointer _tmp1_;
		gpointer _tmp2_;
		_tmp1_ = _data2_->thread_func (_data2_->thread_func_target, &_inner_error0_);
		_tmp0_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		((_data2_->retval == NULL) || (g_destroy_func == NULL)) ? NULL : (_data2_->retval = (g_destroy_func (_data2_->retval), NULL));
		_data2_->retval = _tmp2_;
		((_tmp0_ == NULL) || (g_destroy_func == NULL)) ? NULL : (_tmp0_ = (g_destroy_func (_tmp0_), NULL));
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp3_;
		GError* _tmp4_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp3_ = e;
		_tmp4_ = _g_error_copy0 (_tmp3_);
		_g_error_free0 (_data2_->_error_);
		_data2_->_error_ = _tmp4_;
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, _thread_manager_run_in_thread_co_gsource_func, _data2_->_async_data_, NULL);
}

static void
___lambda5__thread_manager_task_func (gpointer self)
{
	__lambda5_ (self);
}

static gboolean
thread_manager_run_in_thread_co (ThreadManagerRunInThreadData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_data2_ = g_slice_new0 (Block2Data);
	_data_->_data2_->_ref_count_ = 1;
	_data_->_data2_->g_type = _data_->g_type;
	_data_->_data2_->g_dup_func = _data_->g_dup_func;
	_data_->_data2_->g_destroy_func = _data_->g_destroy_func;
	(_data_->_data2_->thread_func_target_destroy_notify == NULL) ? NULL : (_data_->_data2_->thread_func_target_destroy_notify (_data_->_data2_->thread_func_target), NULL);
	_data_->_data2_->thread_func = NULL;
	_data_->_data2_->thread_func_target = NULL;
	_data_->_data2_->thread_func_target_destroy_notify = NULL;
	_data_->_data2_->thread_func = _data_->thread_func;
	_data_->_data2_->thread_func_target = _data_->thread_func_target;
	_data_->_data2_->thread_func_target_destroy_notify = _data_->thread_func_target_destroy_notify;
	_data_->_data2_->_async_data_ = _data_;
	if (!thread_manager_initiated) {
		thread_manager_init ();
	}
	if (!thread_manager_supported) {
		g_critical ("ThreadManager.vala:55: Thread Creation not supported in this context. " \
"Running synchronously...");
		_data_->_tmp1_ = _data_->_data2_->thread_func (_data_->_data2_->thread_func_target, &_data_->_inner_error0_);
		_data_->_tmp0_ = _data_->_tmp1_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
			block2_data_unref (_data_->_data2_);
			_data_->_data2_ = NULL;
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
		_data_->_tmp2_ = _data_->_tmp0_;
		_data_->_tmp0_ = NULL;
		_data_->result = _data_->_tmp2_;
		((_data_->_tmp0_ == NULL) || (_data_->g_destroy_func == NULL)) ? NULL : (_data_->_tmp0_ = (_data_->g_destroy_func (_data_->_tmp0_), NULL));
		block2_data_unref (_data_->_data2_);
		_data_->_data2_ = NULL;
		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;
	}
	_data_->_data2_->_error_ = NULL;
	_data_->_data2_->retval = NULL;
	_data_->_tmp3_ = thread_manager_thread_pool;
	_data_->_tmp4_ = thread_manager_task_new (___lambda5__thread_manager_task_func, block2_data_ref (_data_->_data2_), block2_data_unref);
	g_thread_pool_push (_data_->_tmp3_, _data_->_tmp4_, &_data_->_inner_error0_);
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		block2_data_unref (_data_->_data2_);
		_data_->_data2_ = NULL;
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_state_ = 1;
	return FALSE;
	_state_1:
	;
	_data_->_tmp5_ = _data_->_data2_->_error_;
	if (_data_->_tmp5_ != NULL) {
		_data_->_tmp6_ = _data_->_data2_->_error_;
		_data_->_tmp7_ = _g_error_copy0 (_data_->_tmp6_);
		_data_->_inner_error0_ = _data_->_tmp7_;
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		block2_data_unref (_data_->_data2_);
		_data_->_data2_ = NULL;
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp8_ = _data_->_data2_->retval;
	_data_->_tmp9_ = ((_data_->_tmp8_ != NULL) && (_data_->g_dup_func != NULL)) ? _data_->g_dup_func ((gpointer) _data_->_tmp8_) : ((gpointer) _data_->_tmp8_);
	_data_->result = _data_->_tmp9_;
	block2_data_unref (_data_->_data2_);
	_data_->_data2_ = NULL;
	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;
}

