<commit_before>/* 
 * Copyright (C) 2012 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "midgard_execution_pool.h"
#include "midgard_executable.h"
#include "midgard_validable.h"
#include "midgard_core_object.h"

struct _MidgardExecutionPoolPrivate {
	GThreadPool *gpool;
};

/**
 * midgard_execution_pool_new:
 * @max_n_threads: maximum number of threads
 * 
 * Returns: new #MidgardExecutionPool
 *
 * Since: 10.05.8
 */ 
MidgardExecutionPool *             
midgard_execution_pool_new (guint max_n_threads)
{
	MidgardExecutionPool *self = g_object_new (MIDGARD_TYPE_EXECUTION_POOL, "max_n_threads", max_n_threads, NULL);
	return self;
}

guint
_midgard_execution_pool_get_max_n_resources (MidgardPool *iface)
{
	MidgardExecutionPool *self = MIDGARD_EXECUTION_POOL(iface);
	return (guint) g_thread_pool_get_max_threads (self->priv->gpool);
}

guint
_midgard_execution_pool_get_n_resources (MidgardPool *iface)
{
	MidgardExecutionPool *self = MIDGARD_EXECUTION_POOL(iface);
	return (guint) g_thread_pool_get_num_threads (self->priv->gpool);
}

MidgardPool*
_midgard_execution_pool_set_max_n_resources (MidgardPool *iface, guint n_max, GError **error)
{
	MidgardExecutionPool *self = MIDGARD_EXECUTION_POOL(iface);
	GError *err = NULL;
	gboolean is_set = g_thread_pool_set_max_threads (self->priv->gpool, (gint)n_max, &err);
	if (err)
		g_propagate_error (error, err);
	return iface;
}

MidgardPool*
_midgard_execution_pool_push (MidgardPool *iface, GObject *object, GError **error)
{
	g_return_if_fail (MIDGARD_IS_EXECUTION_POOL (iface));

	MidgardExecutionPool *self = MIDGARD_EXECUTION_POOL(iface);

	if (!MIDGARD_IS_EXECUTABLE (object)) {
		g_set_error (error, MIDGARD_VALIDATION_ERROR, MIDGARD_VALIDATION_ERROR_TYPE_INVALID,
				"Invalid type. Expected Executable, got '%s'", G_OBJECT_TYPE_NAME (object));
	}

	GError *err = NULL;
	/* Add new reference to executor, pool will unref it after real execution is done */<commit_msg>GLib <2.32 compatibility. Refs gh-168<commit_after>/* 
 * Copyright (C) 2012 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "midgard_execution_pool.h"
#include "midgard_executable.h"
#include "midgard_validable.h"
#include "midgard_core_object.h"

struct _MidgardExecutionPoolPrivate {
	GThreadPool *gpool;
};

/**
 * midgard_execution_pool_new:
 * @max_n_threads: maximum number of threads
 * 
 * Returns: new #MidgardExecutionPool
 *
 * Since: 10.05.8
 */ 
MidgardExecutionPool *             
midgard_execution_pool_new (guint max_n_threads)
{
	MidgardExecutionPool *self = g_object_new (MIDGARD_TYPE_EXECUTION_POOL, "max_n_threads", max_n_threads, NULL);
	return self;
}

guint
_midgard_execution_pool_get_max_n_resources (MidgardPool *iface)
{
	MidgardExecutionPool *self = MIDGARD_EXECUTION_POOL(iface);
	return (guint) g_thread_pool_get_max_threads (self->priv->gpool);
}

guint
_midgard_execution_pool_get_n_resources (MidgardPool *iface)
{
	MidgardExecutionPool *self = MIDGARD_EXECUTION_POOL(iface);
	return (guint) g_thread_pool_get_num_threads (self->priv->gpool);
}

MidgardPool*
_midgard_execution_pool_set_max_n_resources (MidgardPool *iface, guint n_max, GError **error)
{
	MidgardExecutionPool *self = MIDGARD_EXECUTION_POOL(iface);
	GError *err = NULL;
#if GLIB_CHECK_VERSION(2,32,0)
	gboolean is_set = g_thread_pool_set_max_threads (self->priv->gpool, (gint)n_max, &err);
#else 
	g_thread_pool_set_max_threads (self->priv->gpool, (gint)n_max, &err);
#endif
	if (err)
		g_propagate_error (error, err);
	return iface;
}

MidgardPool*
_midgard_execution_pool_push (MidgardPool *iface, GObject *object, GError **error)
{
	g_return_if_fail (MIDGARD_IS_EXECUTION_POOL (iface));

	MidgardExecutionPool *self = MIDGARD_EXECUTION_POOL(iface);

	if (!MIDGARD_IS_EXECUTABLE (object)) {
		g_set_error (error, MIDGARD_VALIDATION_ERROR, MIDGARD_VALIDATION_ERROR_TYPE_INVALID,
				"Invalid type. Expected Executable, got '%s'", G_OBJECT_TYPE_NAME (object));
	}

	GError *err = NULL;
	/* Add new reference to executor, pool will unref it after real execution is done */
{'fraction_non_alphanumeric': 0.09028803545051699, 'fraction_numerical': 0.01070901033973412, 'mean_word_length': 5.590024330900243, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4012502', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1835, 'n_words': 514}