/* testthreading.c generated by valac 0.16.1, the Vala compiler
 * generated from testthreading.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <fsobasics.h>
#include <stdlib.h>
#include <string.h>

#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))


extern GMainLoop* loop;
GMainLoop* loop = NULL;
extern gint counter;
gint counter = 0;

void* thread_func_async (void);
void someDelegate (void* data);
static void _someDelegate_fso_framework_threading_void_func_with_void_star_param (void* param, gpointer self);
void* thread_func_sync (void);
static gboolean __lambda2_ (void);
static gboolean ___lambda2__gsource_func (gpointer self);
void anotherDelegate (void* data);
void test_threading_call_delegate_on_main_thread_async (void);
static gpointer _thread_func_async_gthread_func (gpointer self);
void test_threading_call_delegate_on_main_thread_sync (void);
static gpointer _thread_func_sync_gthread_func (gpointer self);
void test_threading_call_delegate_on_new_thread (void);
static void _anotherDelegate_fso_framework_threading_void_func_with_void_star_param (void* param, gpointer self);
void _vala_main (gchar** args, int args_length1);
static void _test_threading_call_delegate_on_main_thread_async_gcallback (void);
static void _test_threading_call_delegate_on_main_thread_sync_gcallback (void);
static void _test_threading_call_delegate_on_new_thread_gcallback (void);


static void _someDelegate_fso_framework_threading_void_func_with_void_star_param (void* param, gpointer self) {
	someDelegate (param);
}


void* thread_func_async (void) {
	void* result = NULL;
	gboolean _tmp0_ = FALSE;
	_tmp0_ = fso_framework_threading_isMainThread ();
	g_assert (!_tmp0_);
	g_usleep ((gulong) 10);
	fso_framework_threading_callDelegateOnMainThread (_someDelegate_fso_framework_threading_void_func_with_void_star_param, NULL, FALSE, NULL);
	result = NULL;
	return result;
}


void* thread_func_sync (void) {
	void* result = NULL;
	gboolean _tmp0_ = FALSE;
	_tmp0_ = fso_framework_threading_isMainThread ();
	g_assert (!_tmp0_);
	g_usleep ((gulong) 10);
	fso_framework_threading_callDelegateOnMainThread (_someDelegate_fso_framework_threading_void_func_with_void_star_param, NULL, TRUE, NULL);
	result = NULL;
	return result;
}


static gboolean __lambda2_ (void) {
	gboolean result = FALSE;
	GMainLoop* _tmp0_;
	_tmp0_ = loop;
	g_main_loop_quit (_tmp0_);
	result = FALSE;
	return result;
}


static gboolean ___lambda2__gsource_func (gpointer self) {
	gboolean result;
	result = __lambda2_ ();
	return result;
}


void someDelegate (void* data) {
	g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, (guint) 2, ___lambda2__gsource_func, NULL, NULL);
	g_usleep ((gulong) 500);
}


void anotherDelegate (void* data) {
	guint32 _tmp0_ = 0U;
	gint _tmp1_;
	gint _tmp2_;
	_tmp0_ = g_random_int ();
	g_usleep ((gulong) (_tmp0_ % (1000 * 1000)));
	_tmp1_ = counter;
	counter = _tmp1_ - 1;
	_tmp2_ = counter;
	if (_tmp2_ == 0) {
		GMainLoop* _tmp3_;
		_tmp3_ = loop;
		g_main_loop_quit (_tmp3_);
	}
}


static gpointer _thread_func_async_gthread_func (gpointer self) {
	gpointer result;
	result = thread_func_async ();
	return result;
}


void test_threading_call_delegate_on_main_thread_async (void) {
	GMainLoop* _tmp0_;
	GMainLoop* _tmp1_;
	GError * _inner_error_ = NULL;
	_tmp0_ = g_main_loop_new (NULL, FALSE);
	_g_main_loop_unref0 (loop);
	loop = _tmp0_;
	g_thread_create (_thread_func_async_gthread_func, NULL, FALSE, &_inner_error_);
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	_tmp1_ = loop;
	g_main_loop_run (_tmp1_);
}


static gpointer _thread_func_sync_gthread_func (gpointer self) {
	gpointer result;
	result = thread_func_sync ();
	return result;
}


void test_threading_call_delegate_on_main_thread_sync (void) {
	GMainLoop* _tmp0_;
	GMainLoop* _tmp1_;
	GError * _inner_error_ = NULL;
	_tmp0_ = g_main_loop_new (NULL, FALSE);
	_g_main_loop_unref0 (loop);
	loop = _tmp0_;
	g_thread_create (_thread_func_sync_gthread_func, NULL, FALSE, &_inner_error_);
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	_tmp1_ = loop;
	g_main_loop_run (_tmp1_);
}


static void _anotherDelegate_fso_framework_threading_void_func_with_void_star_param (void* param, gpointer self) {
	anotherDelegate (param);
}


void test_threading_call_delegate_on_new_thread (void) {
	GMainLoop* _tmp0_;
	GMainLoop* _tmp7_;
	_tmp0_ = g_main_loop_new (NULL, FALSE);
	_g_main_loop_unref0 (loop);
	loop = _tmp0_;
	{
		gint i;
		i = 1;
		{
			gboolean _tmp1_;
			_tmp1_ = TRUE;
			while (TRUE) {
				gboolean _tmp2_;
				gint _tmp4_;
				gint _tmp5_;
				gint _tmp6_;
				_tmp2_ = _tmp1_;
				if (!_tmp2_) {
					gint _tmp3_;
					_tmp3_ = i;
					i = _tmp3_ + 1;
				}
				_tmp1_ = FALSE;
				_tmp4_ = i;
				if (!(_tmp4_ <= 50)) {
					break;
				}
				_tmp5_ = counter;
				counter = _tmp5_ + 1;
				_tmp6_ = i;
				fso_framework_threading_callDelegateOnNewThread (_anotherDelegate_fso_framework_threading_void_func_with_void_star_param, NULL, (void*) _tmp6_);
				g_usleep ((gulong) 500);
			}
		}
	}
	_tmp7_ = loop;
	g_main_loop_run (_tmp7_);
}


static void _test_threading_call_delegate_on_main_thread_async_gcallback (void) {
	test_threading_call_delegate_on_main_thread_async ();
}


static void _test_threading_call_delegate_on_main_thread_sync_gcallback (void) {
	test_threading_call_delegate_on_main_thread_sync ();
}


static void _test_threading_call_delegate_on_new_thread_gcallback (void) {
	test_threading_call_delegate_on_new_thread ();
}


void _vala_main (gchar** args, int args_length1) {
	g_test_init (&args_length1, &args, NULL);
	g_test_add_func ("/Threading/callDelegateOnMainThread/ASync", _test_threading_call_delegate_on_main_thread_async_gcallback);
	g_test_add_func ("/Threading/callDelegateOnMainThread/Sync", _test_threading_call_delegate_on_main_thread_sync_gcallback);
	g_test_add_func ("/Threading/callDelegateOnNewThread", _test_threading_call_delegate_on_new_thread_gcallback);
	g_test_run ();
}


int main (int argc, char ** argv) {
	g_thread_init (NULL);
	g_type_init ();
	_vala_main (argv, argc);
	return 0;
}



