~cordia-team/+junk/hildon-home

« back to all changes in this revision

Viewing changes to src/hd-command-thread-pool.c

  • Committer: Thomas-Karl Pietrowski
  • Date: 2011-10-09 16:56:50 UTC
  • Revision ID: thopiekar@googlemail.com-20111009165650-4c3oct3pk33c2fb3
first release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of hildon-home
 
3
 *
 
4
 * Copyright (C) 2009 Nokia Corporation.
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 * 02110-1301 USA
 
19
 *
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#include <gdk/gdk.h>
 
27
 
 
28
#include "hd-command-thread-pool.h"
 
29
 
 
30
#define HD_COMMAND_THREAD_POOL_GET_PRIVATE(object) \
 
31
  (G_TYPE_INSTANCE_GET_PRIVATE ((object), HD_TYPE_COMMAND_THREAD_POOL, HDCommandThreadPoolPrivate))
 
32
 
 
33
typedef struct
 
34
{
 
35
  HDCommandCallback command;
 
36
  gpointer data;
 
37
  GDestroyNotify destroy_data;
 
38
} ThreadCommand;
 
39
 
 
40
static void           thread_command_execute (ThreadCommand *thread_command,
 
41
                                              gpointer       data);
 
42
static ThreadCommand *thread_command_new     (HDCommandCallback command,
 
43
                                              gpointer          data,
 
44
                                              GDestroyNotify    destroy_data);
 
45
static void           thread_command_free    (ThreadCommand *thread_command);
 
46
 
 
47
typedef struct
 
48
{
 
49
  gint priority;
 
50
  GSourceFunc function;
 
51
  gpointer data;
 
52
  GDestroyNotify destroy_data;
 
53
} IdleCommandData;
 
54
 
 
55
static IdleCommandData *idle_command_data_new  (gint           priority,
 
56
                                                GSourceFunc    function,
 
57
                                                gpointer       data,
 
58
                                                GDestroyNotify destroy_data);
 
59
static void             idle_command_execute   (IdleCommandData *command_data);
 
60
static void             idle_command_data_free (IdleCommandData *command_data);
 
61
 
 
62
struct _HDCommandThreadPoolPrivate
 
63
{
 
64
  GThreadPool *thread_pool;
 
65
};
 
66
 
 
67
G_DEFINE_TYPE (HDCommandThreadPool, hd_command_thread_pool, G_TYPE_OBJECT);
 
68
 
 
69
static void
 
70
hd_command_thread_pool_dipose (GObject *object)
 
71
{
 
72
  HDCommandThreadPoolPrivate *priv = HD_COMMAND_THREAD_POOL (object)->priv;
 
73
 
 
74
  if (priv->thread_pool)
 
75
    {
 
76
      g_thread_pool_free (priv->thread_pool,
 
77
                          FALSE,
 
78
                          TRUE);
 
79
      priv->thread_pool = NULL;
 
80
    }
 
81
 
 
82
  G_OBJECT_CLASS (hd_command_thread_pool_parent_class)->dispose (object);
 
83
}
 
84
 
 
85
static void
 
86
hd_command_thread_pool_class_init (HDCommandThreadPoolClass *klass)
 
87
{
 
88
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
89
 
 
90
  object_class->dispose = hd_command_thread_pool_dipose;
 
91
 
 
92
  g_type_class_add_private (klass, sizeof (HDCommandThreadPoolPrivate));
 
93
}
 
94
 
 
95
static void
 
96
hd_command_thread_pool_init (HDCommandThreadPool *command_thread_pool)
 
97
{
 
98
  HDCommandThreadPoolPrivate *priv;
 
99
 
 
100
  priv = HD_COMMAND_THREAD_POOL_GET_PRIVATE (command_thread_pool);
 
101
  command_thread_pool->priv = priv;
 
102
 
 
103
  priv->thread_pool = g_thread_pool_new ((GFunc) thread_command_execute,
 
104
                                         NULL,
 
105
                                         1,
 
106
                                         FALSE,
 
107
                                         NULL);
 
108
}
 
109
 
 
110
static void
 
111
thread_command_execute (ThreadCommand *thread_command,
 
112
                        gpointer       data)
 
113
{
 
114
  thread_command->command (thread_command->data);
 
115
  thread_command_free (thread_command);
 
116
}
 
117
 
 
118
static ThreadCommand *
 
119
thread_command_new (HDCommandCallback command,
 
120
                    gpointer          data,
 
121
                    GDestroyNotify    destroy_data)
 
122
{
 
123
  ThreadCommand *thread_command = g_slice_new (ThreadCommand);
 
124
 
 
125
  thread_command->command = command;
 
126
  thread_command->data = data;
 
127
  thread_command->destroy_data = destroy_data;
 
128
 
 
129
  return thread_command;
 
130
}
 
131
 
 
132
static void
 
133
thread_command_free (ThreadCommand *thread_command)
 
134
{
 
135
  if (!thread_command)
 
136
    return;
 
137
 
 
138
  if (thread_command->destroy_data)
 
139
    thread_command->destroy_data (thread_command->data);
 
140
 
 
141
  g_slice_free (ThreadCommand, thread_command);
 
142
}
 
143
 
 
144
HDCommandThreadPool *
 
145
hd_command_thread_pool_new (void)
 
146
{
 
147
  return g_object_new (HD_TYPE_COMMAND_THREAD_POOL, NULL);
 
148
}
 
149
 
 
150
void
 
151
hd_command_thread_pool_push (HDCommandThreadPool *pool,
 
152
                             HDCommandCallback    command,
 
153
                             gpointer             data,
 
154
                             GDestroyNotify       destroy_data)
 
155
{
 
156
  HDCommandThreadPoolPrivate *priv;
 
157
  GError *error = NULL;
 
158
 
 
159
  g_return_if_fail (HD_IS_COMMAND_THREAD_POOL (pool));
 
160
 
 
161
  priv = pool->priv;
 
162
 
 
163
  ThreadCommand *thread_command = thread_command_new (command,
 
164
                                                      data,
 
165
                                                      destroy_data);
 
166
  g_thread_pool_push (priv->thread_pool,
 
167
                      thread_command,
 
168
                      &error);
 
169
 
 
170
  if (error)
 
171
    {
 
172
      g_debug ("%s. Error: %s", __FUNCTION__, error->message);
 
173
      g_error_free (error);
 
174
    }
 
175
}
 
176
 
 
177
void
 
178
hd_command_thread_pool_push_idle (HDCommandThreadPool *pool,
 
179
                                  gint                 priority,
 
180
                                  GSourceFunc          function,
 
181
                                  gpointer             data,
 
182
                                  GDestroyNotify       destroy_data)
 
183
{
 
184
  IdleCommandData *command_data;
 
185
 
 
186
  g_return_if_fail (HD_IS_COMMAND_THREAD_POOL (pool));
 
187
 
 
188
  command_data = idle_command_data_new (priority,
 
189
                                        function,
 
190
                                        data,
 
191
                                        destroy_data);
 
192
 
 
193
  hd_command_thread_pool_push (pool,
 
194
                               (HDCommandCallback) idle_command_execute,
 
195
                               command_data,
 
196
                               (GDestroyNotify) idle_command_data_free);
 
197
}
 
198
 
 
199
static IdleCommandData *
 
200
idle_command_data_new (gint           priority,
 
201
                       GSourceFunc    function,
 
202
                       gpointer       data,
 
203
                       GDestroyNotify destroy_data)
 
204
{
 
205
  IdleCommandData *command_data = g_slice_new (IdleCommandData);
 
206
 
 
207
  command_data->priority = priority;
 
208
  command_data->function = function;
 
209
  command_data->data = data;
 
210
  command_data->destroy_data = destroy_data;
 
211
 
 
212
  return command_data;
 
213
}
 
214
 
 
215
static void
 
216
idle_command_execute (IdleCommandData *command_data)
 
217
{
 
218
  gdk_threads_add_idle_full (command_data->priority,
 
219
                             command_data->function,
 
220
                             command_data->data,
 
221
                             command_data->destroy_data);
 
222
}
 
223
 
 
224
static void
 
225
idle_command_data_free (IdleCommandData *command_data)
 
226
{
 
227
  if (!command_data)
 
228
    return;
 
229
 
 
230
  g_slice_free (IdleCommandData, command_data);
 
231
}
 
232