~behda/+junk/udisks2.original

« back to all changes in this revision

Viewing changes to src/udisksthreadedjob.c

  • Committer: behda
  • Date: 2014-05-24 15:15:11 UTC
  • Revision ID: pauvitk@gmail.com-20140524151511-3vtr0uubjewx3z2j
Initial commit of source code and Debian packaging.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 
2
 *
 
3
 * Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include <glib/gi18n-lib.h>
 
23
 
 
24
#include <sys/types.h>
 
25
#include <sys/wait.h>
 
26
 
 
27
#include "udisksbasejob.h"
 
28
#include "udisksthreadedjob.h"
 
29
#include "udisks-daemon-marshal.h"
 
30
#include "udisksdaemon.h"
 
31
 
 
32
/**
 
33
 * SECTION:udisksthreadedjob
 
34
 * @title: UDisksThreadedJob
 
35
 * @short_description: Job that runs in a thread
 
36
 *
 
37
 * This type provides an implementation of the #UDisksJob interface
 
38
 * for jobs that run in a thread.
 
39
 */
 
40
 
 
41
typedef struct _UDisksThreadedJobClass   UDisksThreadedJobClass;
 
42
 
 
43
/**
 
44
 * UDisksThreadedJob:
 
45
 *
 
46
 * The #UDisksThreadedJob structure contains only private data and should
 
47
 * only be accessed using the provided API.
 
48
 */
 
49
struct _UDisksThreadedJob
 
50
{
 
51
  UDisksBaseJob parent_instance;
 
52
 
 
53
  UDisksThreadedJobFunc job_func;
 
54
  gpointer user_data;
 
55
  GDestroyNotify user_data_free_func;
 
56
 
 
57
  gboolean job_result;
 
58
  GError *job_error;
 
59
};
 
60
 
 
61
struct _UDisksThreadedJobClass
 
62
{
 
63
  UDisksBaseJobClass parent_class;
 
64
 
 
65
  gboolean (*threaded_job_completed) (UDisksThreadedJob  *job,
 
66
                                      gboolean            result,
 
67
                                      GError             *error);
 
68
};
 
69
 
 
70
static void job_iface_init (UDisksJobIface *iface);
 
71
 
 
72
enum
 
73
{
 
74
  PROP_0,
 
75
  PROP_JOB_FUNC,
 
76
  PROP_USER_DATA,
 
77
  PROP_USER_DATA_FREE_FUNC
 
78
};
 
79
 
 
80
enum
 
81
{
 
82
  THREADED_JOB_COMPLETED_SIGNAL,
 
83
  LAST_SIGNAL
 
84
};
 
85
 
 
86
static gulong signals[LAST_SIGNAL] = { 0 };
 
87
 
 
88
static gboolean udisks_threaded_job_threaded_job_completed_default (UDisksThreadedJob  *job,
 
89
                                                                    gboolean            result,
 
90
                                                                    GError             *error);
 
91
 
 
92
G_DEFINE_TYPE_WITH_CODE (UDisksThreadedJob, udisks_threaded_job, UDISKS_TYPE_BASE_JOB,
 
93
                         G_IMPLEMENT_INTERFACE (UDISKS_TYPE_JOB, job_iface_init));
 
94
 
 
95
static void
 
96
udisks_threaded_job_finalize (GObject *object)
 
97
{
 
98
  UDisksThreadedJob *job = UDISKS_THREADED_JOB (object);
 
99
 
 
100
  if (job->job_error != NULL)
 
101
    g_error_free (job->job_error);
 
102
 
 
103
  if (job->user_data_free_func != NULL)
 
104
    job->user_data_free_func (job->user_data);
 
105
 
 
106
  if (G_OBJECT_CLASS (udisks_threaded_job_parent_class)->finalize != NULL)
 
107
    G_OBJECT_CLASS (udisks_threaded_job_parent_class)->finalize (object);
 
108
}
 
109
 
 
110
static void
 
111
udisks_threaded_job_get_property (GObject    *object,
 
112
                                  guint       prop_id,
 
113
                                  GValue     *value,
 
114
                                  GParamSpec *pspec)
 
115
{
 
116
  UDisksThreadedJob *job = UDISKS_THREADED_JOB (object);
 
117
 
 
118
  switch (prop_id)
 
119
    {
 
120
    case PROP_JOB_FUNC:
 
121
      g_value_set_pointer (value, job->job_func);
 
122
      break;
 
123
 
 
124
    case PROP_USER_DATA:
 
125
      g_value_set_pointer (value, job->user_data);
 
126
      break;
 
127
 
 
128
    case PROP_USER_DATA_FREE_FUNC:
 
129
      g_value_set_pointer (value, job->user_data_free_func);
 
130
      break;
 
131
 
 
132
    default:
 
133
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
134
      break;
 
135
    }
 
136
}
 
137
 
 
138
static void
 
139
udisks_threaded_job_set_property (GObject      *object,
 
140
                                  guint         prop_id,
 
141
                                  const GValue *value,
 
142
                                  GParamSpec   *pspec)
 
143
{
 
144
  UDisksThreadedJob *job = UDISKS_THREADED_JOB (object);
 
145
 
 
146
  switch (prop_id)
 
147
    {
 
148
    case PROP_JOB_FUNC:
 
149
      g_assert (job->job_func == NULL);
 
150
      job->job_func = g_value_get_pointer (value);
 
151
      break;
 
152
 
 
153
    case PROP_USER_DATA:
 
154
      g_assert (job->user_data == NULL);
 
155
      job->user_data = g_value_get_pointer (value);
 
156
      break;
 
157
 
 
158
    case PROP_USER_DATA_FREE_FUNC:
 
159
      g_assert (job->user_data_free_func == NULL);
 
160
      job->user_data_free_func = g_value_get_pointer (value);
 
161
      break;
 
162
 
 
163
    default:
 
164
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
165
      break;
 
166
    }
 
167
}
 
168
 
 
169
/* ---------------------------------------------------------------------------------------------------- */
 
170
 
 
171
static gboolean
 
172
job_complete (gpointer user_data)
 
173
{
 
174
  UDisksThreadedJob *job = UDISKS_THREADED_JOB (user_data);
 
175
  gboolean ret;
 
176
 
 
177
  /* take a reference so it's safe for a signal-handler to release the last one */
 
178
  g_object_ref (job);
 
179
  g_signal_emit (job,
 
180
                 signals[THREADED_JOB_COMPLETED_SIGNAL],
 
181
                 0,
 
182
                 job->job_result,
 
183
                 job->job_error,
 
184
                 &ret);
 
185
  g_object_unref (job);
 
186
  return FALSE;
 
187
}
 
188
 
 
189
static gboolean
 
190
run_io_scheduler_job (GIOSchedulerJob  *io_scheduler_job,
 
191
                      GCancellable     *cancellable,
 
192
                      gpointer          user_data)
 
193
{
 
194
  UDisksThreadedJob *job = UDISKS_THREADED_JOB (user_data);
 
195
 
 
196
  /* TODO: probably want to create a GMainContext dedicated to the thread */
 
197
 
 
198
  g_assert (!job->job_result);
 
199
  g_assert_no_error (job->job_error);
 
200
 
 
201
  if (!g_cancellable_set_error_if_cancelled (cancellable, &job->job_error))
 
202
    {
 
203
      job->job_result = job->job_func (job,
 
204
                                       cancellable,
 
205
                                       job->user_data,
 
206
                                       &job->job_error);
 
207
    }
 
208
 
 
209
  g_io_scheduler_job_send_to_mainloop (io_scheduler_job,
 
210
                                       job_complete,
 
211
                                       job,
 
212
                                       NULL);
 
213
 
 
214
  return FALSE; /* job is complete (or cancelled) */
 
215
}
 
216
 
 
217
static void
 
218
udisks_threaded_job_constructed (GObject *object)
 
219
{
 
220
  UDisksThreadedJob *job = UDISKS_THREADED_JOB (object);
 
221
 
 
222
  if (G_OBJECT_CLASS (udisks_threaded_job_parent_class)->constructed != NULL)
 
223
    G_OBJECT_CLASS (udisks_threaded_job_parent_class)->constructed (object);
 
224
 
 
225
  g_assert (g_thread_supported ());
 
226
  g_io_scheduler_push_job (run_io_scheduler_job,
 
227
                           job,
 
228
                           NULL,
 
229
                           G_PRIORITY_DEFAULT,
 
230
                           udisks_base_job_get_cancellable (UDISKS_BASE_JOB (job)));
 
231
}
 
232
 
 
233
/* ---------------------------------------------------------------------------------------------------- */
 
234
 
 
235
static void
 
236
udisks_threaded_job_init (UDisksThreadedJob *job)
 
237
{
 
238
}
 
239
 
 
240
static void
 
241
udisks_threaded_job_class_init (UDisksThreadedJobClass *klass)
 
242
{
 
243
  GObjectClass *gobject_class;
 
244
 
 
245
  klass->threaded_job_completed = udisks_threaded_job_threaded_job_completed_default;
 
246
 
 
247
  gobject_class = G_OBJECT_CLASS (klass);
 
248
  gobject_class->finalize     = udisks_threaded_job_finalize;
 
249
  gobject_class->constructed  = udisks_threaded_job_constructed;
 
250
  gobject_class->set_property = udisks_threaded_job_set_property;
 
251
  gobject_class->get_property = udisks_threaded_job_get_property;
 
252
 
 
253
  /**
 
254
   * UDisksThreadedJob:job-func:
 
255
   *
 
256
   * The #UDisksThreadedJobFunc to use.
 
257
   */
 
258
  g_object_class_install_property (gobject_class,
 
259
                                   PROP_JOB_FUNC,
 
260
                                   g_param_spec_pointer ("job-func",
 
261
                                                         "Job Function",
 
262
                                                         "The Job Function",
 
263
                                                         G_PARAM_READABLE |
 
264
                                                         G_PARAM_WRITABLE |
 
265
                                                         G_PARAM_CONSTRUCT_ONLY |
 
266
                                                         G_PARAM_STATIC_STRINGS));
 
267
 
 
268
  /**
 
269
   * UDisksThreadedJob:user-data:
 
270
   *
 
271
   * User data for the #UDisksThreadedJobFunc.
 
272
   */
 
273
  g_object_class_install_property (gobject_class,
 
274
                                   PROP_USER_DATA,
 
275
                                   g_param_spec_pointer ("user-data",
 
276
                                                         "Job Function's user data",
 
277
                                                         "The Job Function user data",
 
278
                                                         G_PARAM_READABLE |
 
279
                                                         G_PARAM_WRITABLE |
 
280
                                                         G_PARAM_CONSTRUCT_ONLY |
 
281
                                                         G_PARAM_STATIC_STRINGS));
 
282
 
 
283
  /**
 
284
   * UDisksThreadedJob:user-data-free-func:
 
285
   *
 
286
   * Free function for user data for the #UDisksThreadedJobFunc.
 
287
   */
 
288
  g_object_class_install_property (gobject_class,
 
289
                                   PROP_USER_DATA_FREE_FUNC,
 
290
                                   g_param_spec_pointer ("user-data-free-func",
 
291
                                                         "Job Function's user data free function",
 
292
                                                         "The Job Function user data free function",
 
293
                                                         G_PARAM_READABLE |
 
294
                                                         G_PARAM_WRITABLE |
 
295
                                                         G_PARAM_CONSTRUCT_ONLY |
 
296
                                                         G_PARAM_STATIC_STRINGS));
 
297
 
 
298
  /**
 
299
   * UDisksThreadedJob::threaded-job-completed:
 
300
   * @job: The #UDisksThreadedJob emitting the signal.
 
301
   * @result: The #gboolean returned by the #UDisksThreadedJobFunc.
 
302
   * @error: The #GError set by the #UDisksThreadedJobFunc.
 
303
   *
 
304
   * Emitted when the threaded job is complete.
 
305
   *
 
306
   * The default implementation simply emits the #UDisksJob::completed
 
307
   * signal with @success set to %TRUE if, and only if, @error is
 
308
   * %NULL. Otherwise, @message on that signal is set to a string
 
309
   * describing @error. You can avoid the default implementation by
 
310
   * returning %TRUE from your signal handler.
 
311
   *
 
312
   * This signal is emitted in the
 
313
   * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
 
314
   * of the thread that @job was created in.
 
315
   *
 
316
   * Returns: %TRUE if the signal was handled, %FALSE to let other
 
317
   * handlers run.
 
318
   */
 
319
  signals[THREADED_JOB_COMPLETED_SIGNAL] =
 
320
    g_signal_new ("threaded-job-completed",
 
321
                  UDISKS_TYPE_THREADED_JOB,
 
322
                  G_SIGNAL_RUN_LAST,
 
323
                  G_STRUCT_OFFSET (UDisksThreadedJobClass, threaded_job_completed),
 
324
                  g_signal_accumulator_true_handled,
 
325
                  NULL,
 
326
                  udisks_daemon_marshal_BOOLEAN__BOOLEAN_BOXED,
 
327
                  G_TYPE_BOOLEAN,
 
328
                  2,
 
329
                  G_TYPE_BOOLEAN,
 
330
                  G_TYPE_ERROR);
 
331
}
 
332
 
 
333
/**
 
334
 * udisks_threaded_job_new:
 
335
 * @job_func: The function to run in another thread.
 
336
 * @user_data: User data to pass to @job_func.
 
337
 * @user_data_free_func: Function to free @user_data with or %NULL.
 
338
 * @daemon: A #UDisksDaemon.
 
339
 * @cancellable: A #GCancellable or %NULL.
 
340
 *
 
341
 * Creates a new #UDisksThreadedJob instance.
 
342
 *
 
343
 * The job is started immediately - connect to the
 
344
 * #UDisksThreadedJob::threaded-job-completed or #UDisksJob::completed
 
345
 * signals to get notified when the job is done.
 
346
 *
 
347
 * Returns: A new #UDisksThreadedJob. Free with g_object_unref().
 
348
 */
 
349
UDisksThreadedJob *
 
350
udisks_threaded_job_new (UDisksThreadedJobFunc  job_func,
 
351
                         gpointer               user_data,
 
352
                         GDestroyNotify         user_data_free_func,
 
353
                         UDisksDaemon          *daemon,
 
354
                         GCancellable          *cancellable)
 
355
{
 
356
  /* g_return_val_if_fail (UDISKS_IS_DAEMON (daemon), NULL); */
 
357
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
 
358
  return UDISKS_THREADED_JOB (g_object_new (UDISKS_TYPE_THREADED_JOB,
 
359
                                            "job-func", job_func,
 
360
                                            "user-data", user_data,
 
361
                                            "user-data-free-func", user_data_free_func,
 
362
                                            "daemon", daemon,
 
363
                                            "cancellable", cancellable,
 
364
                                            NULL));
 
365
}
 
366
 
 
367
/**
 
368
 * udisks_threaded_job_get_user_data:
 
369
 * @job: A #UDisksThreadedJob.
 
370
 *
 
371
 * Gets the @user_data parameter that @job was constructed with.
 
372
 *
 
373
 * Returns: A #gpointer owned by @job.
 
374
 */
 
375
gpointer
 
376
udisks_threaded_job_get_user_data (UDisksThreadedJob *job)
 
377
{
 
378
  g_return_val_if_fail (UDISKS_IS_THREADED_JOB (job), NULL);
 
379
  return job->user_data;
 
380
}
 
381
 
 
382
/* ---------------------------------------------------------------------------------------------------- */
 
383
 
 
384
static void
 
385
job_iface_init (UDisksJobIface *iface)
 
386
{
 
387
  /* For Cancel(), just use the implementation from our super class (UDisksBaseJob) */
 
388
  /* iface->handle_cancel   = handle_cancel; */
 
389
}
 
390
 
 
391
/* ---------------------------------------------------------------------------------------------------- */
 
392
 
 
393
static gboolean
 
394
udisks_threaded_job_threaded_job_completed_default (UDisksThreadedJob  *job,
 
395
                                                    gboolean            result,
 
396
                                                    GError            *error)
 
397
{
 
398
  if (result)
 
399
    {
 
400
      udisks_job_emit_completed (UDISKS_JOB (job),
 
401
                                 TRUE,
 
402
                                 "");
 
403
    }
 
404
  else
 
405
    {
 
406
      GString *message;
 
407
 
 
408
      g_assert (error != NULL);
 
409
 
 
410
      message = g_string_new (NULL);
 
411
      g_string_append_printf (message,
 
412
                              "Threaded job failed with error: %s (%s, %d)",
 
413
                              error->message,
 
414
                              g_quark_to_string (error->domain),
 
415
                              error->code);
 
416
      udisks_job_emit_completed (UDISKS_JOB (job),
 
417
                                 FALSE,
 
418
                                 message->str);
 
419
      g_string_free (message, TRUE);
 
420
    }
 
421
 
 
422
  return TRUE;
 
423
}
 
424