2
* This file is part of hildon-home
4
* Copyright (C) 2009 Nokia Corporation.
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public License
8
* version 2.1 as published by the Free Software Foundation.
10
* This library is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28
#include "hd-command-thread-pool.h"
30
#define HD_COMMAND_THREAD_POOL_GET_PRIVATE(object) \
31
(G_TYPE_INSTANCE_GET_PRIVATE ((object), HD_TYPE_COMMAND_THREAD_POOL, HDCommandThreadPoolPrivate))
35
HDCommandCallback command;
37
GDestroyNotify destroy_data;
40
static void thread_command_execute (ThreadCommand *thread_command,
42
static ThreadCommand *thread_command_new (HDCommandCallback command,
44
GDestroyNotify destroy_data);
45
static void thread_command_free (ThreadCommand *thread_command);
52
GDestroyNotify destroy_data;
55
static IdleCommandData *idle_command_data_new (gint priority,
58
GDestroyNotify destroy_data);
59
static void idle_command_execute (IdleCommandData *command_data);
60
static void idle_command_data_free (IdleCommandData *command_data);
62
struct _HDCommandThreadPoolPrivate
64
GThreadPool *thread_pool;
67
G_DEFINE_TYPE (HDCommandThreadPool, hd_command_thread_pool, G_TYPE_OBJECT);
70
hd_command_thread_pool_dipose (GObject *object)
72
HDCommandThreadPoolPrivate *priv = HD_COMMAND_THREAD_POOL (object)->priv;
74
if (priv->thread_pool)
76
g_thread_pool_free (priv->thread_pool,
79
priv->thread_pool = NULL;
82
G_OBJECT_CLASS (hd_command_thread_pool_parent_class)->dispose (object);
86
hd_command_thread_pool_class_init (HDCommandThreadPoolClass *klass)
88
GObjectClass *object_class = G_OBJECT_CLASS (klass);
90
object_class->dispose = hd_command_thread_pool_dipose;
92
g_type_class_add_private (klass, sizeof (HDCommandThreadPoolPrivate));
96
hd_command_thread_pool_init (HDCommandThreadPool *command_thread_pool)
98
HDCommandThreadPoolPrivate *priv;
100
priv = HD_COMMAND_THREAD_POOL_GET_PRIVATE (command_thread_pool);
101
command_thread_pool->priv = priv;
103
priv->thread_pool = g_thread_pool_new ((GFunc) thread_command_execute,
111
thread_command_execute (ThreadCommand *thread_command,
114
thread_command->command (thread_command->data);
115
thread_command_free (thread_command);
118
static ThreadCommand *
119
thread_command_new (HDCommandCallback command,
121
GDestroyNotify destroy_data)
123
ThreadCommand *thread_command = g_slice_new (ThreadCommand);
125
thread_command->command = command;
126
thread_command->data = data;
127
thread_command->destroy_data = destroy_data;
129
return thread_command;
133
thread_command_free (ThreadCommand *thread_command)
138
if (thread_command->destroy_data)
139
thread_command->destroy_data (thread_command->data);
141
g_slice_free (ThreadCommand, thread_command);
144
HDCommandThreadPool *
145
hd_command_thread_pool_new (void)
147
return g_object_new (HD_TYPE_COMMAND_THREAD_POOL, NULL);
151
hd_command_thread_pool_push (HDCommandThreadPool *pool,
152
HDCommandCallback command,
154
GDestroyNotify destroy_data)
156
HDCommandThreadPoolPrivate *priv;
157
GError *error = NULL;
159
g_return_if_fail (HD_IS_COMMAND_THREAD_POOL (pool));
163
ThreadCommand *thread_command = thread_command_new (command,
166
g_thread_pool_push (priv->thread_pool,
172
g_debug ("%s. Error: %s", __FUNCTION__, error->message);
173
g_error_free (error);
178
hd_command_thread_pool_push_idle (HDCommandThreadPool *pool,
180
GSourceFunc function,
182
GDestroyNotify destroy_data)
184
IdleCommandData *command_data;
186
g_return_if_fail (HD_IS_COMMAND_THREAD_POOL (pool));
188
command_data = idle_command_data_new (priority,
193
hd_command_thread_pool_push (pool,
194
(HDCommandCallback) idle_command_execute,
196
(GDestroyNotify) idle_command_data_free);
199
static IdleCommandData *
200
idle_command_data_new (gint priority,
201
GSourceFunc function,
203
GDestroyNotify destroy_data)
205
IdleCommandData *command_data = g_slice_new (IdleCommandData);
207
command_data->priority = priority;
208
command_data->function = function;
209
command_data->data = data;
210
command_data->destroy_data = destroy_data;
216
idle_command_execute (IdleCommandData *command_data)
218
gdk_threads_add_idle_full (command_data->priority,
219
command_data->function,
221
command_data->destroy_data);
225
idle_command_data_free (IdleCommandData *command_data)
230
g_slice_free (IdleCommandData, command_data);