Debian Codegraph @

src/job/fm-job.c

raw
1 /*
2 * fm-job.c
3 *
4 * Copyright 2009 PCMan <[email protected]>
5 * Copyright 2012-2013 Andriy Grytsenko (LStranger) <[email protected]>
6 *
7 * This file is a part of the Libfm library.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #define FM_DISABLE_SEAL
29
30 #include "fm-job.h"
31 #include "fm-marshal.h"
32 #include "glib-compat.h"
33 #include "fm-utils.h"
34
35 /**
36 * SECTION:fm-job
37 * @short_description: Base class of all kinds of asynchronous jobs.
38 * @title: FmJob
39 *
40 * @include: libfm/fm.h
41 *
42 * The #FmJob can be used to create asynchronous jobs performing some
43 * time-consuming tasks in another worker thread.
44 * To run a #FmJob in another thread you simply call
45 * fm_job_run_async(), and then the task will be done in another
46 * worker thread. Later, when the job is finished, #FmJob::finished signal is
47 * emitted. When the job is still running, it's possible to cancel it
48 * from main thread by calling fm_job_cancel(). Then, #FmJob::cancelled signal
49 * will be emitted before emitting #FmJob::finished signal. You can also run
50 * the job in blocking fashion instead of running it asynchronously by
51 * calling fm_job_run_sync().
52 */
53
54 enum {
55 FINISHED,
56 ERROR,
57 CANCELLED,
58 ASK,
59 N_SIGNALS
60 };
61
62 typedef struct _FmIdleCall
63 {
64 FmJob* job;
65 FmJobCallMainThreadFunc func;
66 gpointer user_data;
67 gpointer ret;
68 }FmIdleCall;
69
70 static void fm_job_finalize (GObject *object);
71 /*
72 static gboolean fm_job_error_accumulator(GSignalInvocationHint *ihint, GValue *return_accu,
73 const GValue *handler_return, gpointer data);
74 */
75 static void on_cancellable_cancelled(GCancellable* cancellable, FmJob* job);
76
77 G_DEFINE_ABSTRACT_TYPE(FmJob, fm_job, G_TYPE_OBJECT);
78
79 static gboolean fm_job_real_run_async(FmJob* job);
80 static gboolean on_idle_cleanup(gpointer unused);
81 static void job_thread(FmJob* job, gpointer unused);
82
83 static guint idle_handler = 0;
84 static GSList* finished = NULL;
85 G_LOCK_DEFINE_STATIC(idle_handler);
86
87 static GThreadPool* thread_pool = NULL;
88 static guint n_jobs = 0;
89
90 static guint signals[N_SIGNALS];
91
92 static void fm_job_emit_finished(FmJob* job)
93 {
94 g_signal_emit(job, signals[FINISHED], 0);
95 }
96
97 static void fm_job_emit_cancelled(FmJob* job)
98 {
99 g_signal_emit(job, signals[CANCELLED], 0);
100 }
101
102 static void fm_job_dispose(GObject *object)
103 {
104 FmJob *self;
105
106 g_return_if_fail(object != NULL);
107 g_return_if_fail(FM_IS_JOB(object));
108
109 self = (FmJob*)object;
110
111 if(self->cancellable)
112 {
113 g_signal_handlers_disconnect_by_func(self->cancellable, on_cancellable_cancelled, self);
114 g_object_unref(self->cancellable);
115 self->cancellable = NULL;
116 #if GLIB_CHECK_VERSION(2, 32, 0)
117 g_rec_mutex_clear(&self->stop);
118 #else
119 g_static_rec_mutex_free(&self->stop);
120 #endif
121 }
122
123 G_OBJECT_CLASS(fm_job_parent_class)->dispose(object);
124 }
125
126 static void fm_job_class_init(FmJobClass *klass)
127 {
128 GObjectClass *g_object_class;
129
130 g_object_class = G_OBJECT_CLASS(klass);
131 g_object_class->dispose = fm_job_dispose;
132 g_object_class->finalize = fm_job_finalize;
133
134 klass->run_async = fm_job_real_run_async;
135
136 fm_job_parent_class = (GObjectClass*)g_type_class_peek(G_TYPE_OBJECT);
137
138 /**
139 * FmJob::finished:
140 * @job: a job that emitted the signal
141 *
142 * The #FmJob::finished signal is emitted after the job is finished.
143 * The signal is never emitted if the fm_job_run_XXX function
144 * returned %FALSE, in that case the #FmJob::cancelled signal will be
145 * emitted instead.
146 *
147 * Since: 0.1.0
148 */
149 signals[FINISHED] =
150 g_signal_new( "finished",
151 G_TYPE_FROM_CLASS ( klass ),
152 G_SIGNAL_RUN_FIRST,
153 G_STRUCT_OFFSET ( FmJobClass, finished ),
154 NULL, NULL,
155 g_cclosure_marshal_VOID__VOID,
156 G_TYPE_NONE, 0 );
157
158 /**
159 * FmJob::error:
160 * @job: a job that emitted the signal
161 * @error: an error descriptor
162 * @severity: #FmJobErrorSeverity of the error
163 *
164 * The #FmJob::error signal is emitted when errors happen. A case if
165 * more than one handler is connected to this signal is ambiguous.
166 *
167 * Return value: #FmJobErrorAction that should be performed on that error.
168 *
169 * Since: 0.1.0
170 */
171 signals[ERROR] =
172 g_signal_new( "error",
173 G_TYPE_FROM_CLASS ( klass ),
174 G_SIGNAL_RUN_LAST,
175 G_STRUCT_OFFSET ( FmJobClass, error ),
176 NULL /*fm_job_error_accumulator*/, NULL,
177 #if GLIB_CHECK_VERSION(2,26,0)
178 fm_marshal_UINT__BOXED_UINT,
179 G_TYPE_UINT, 2, G_TYPE_ERROR, G_TYPE_UINT );
180 #else
181 fm_marshal_INT__POINTER_INT,
182 G_TYPE_INT, 2, G_TYPE_POINTER, G_TYPE_INT );
183 #endif
184
185 /**
186 * FmJob::cancelled:
187 * @job: a job that emitted the signal
188 *
189 * The #FmJob::cancelled signal is emitted when the job is cancelled
190 * or aborted due to critical errors.
191 *
192 * Since: 0.1.0
193 */
194 signals[CANCELLED] =
195 g_signal_new( "cancelled",
196 G_TYPE_FROM_CLASS ( klass ),
197 G_SIGNAL_RUN_FIRST,
198 G_STRUCT_OFFSET ( FmJobClass, cancelled ),
199 NULL, NULL,
200 g_cclosure_marshal_VOID__VOID,
201 G_TYPE_NONE, 0 );
202
203 /**
204 * FmJob::ask:
205 * @job: a job that emitted the signal
206 * @question: (const gchar *) a question to ask user
207 * @options: (gchar* const *) list of choices to ask user
208 *
209 * The #FmJob::ask signal is emitted when the job asks for some
210 * user interactions. The user then will have a list of available
211 * @options. If there is more than one handler connected to the
212 * signal then only one of them will receive it.
213 *
214 * Return value: user's choice.
215 *
216 * Since: 0.1.0
217 */
218 signals[ASK] =
219 g_signal_new( "ask",
220 G_TYPE_FROM_CLASS ( klass ),
221 G_SIGNAL_RUN_LAST,
222 G_STRUCT_OFFSET ( FmJobClass, ask ),
223 g_signal_accumulator_first_wins, NULL,
224 fm_marshal_INT__POINTER_POINTER,
225 G_TYPE_INT, 2, G_TYPE_POINTER, G_TYPE_POINTER );
226 }
227
228 static void fm_job_init(FmJob *self)
229 {
230 /* create the thread pool if it doesn't exist. */
231 if( G_UNLIKELY(!thread_pool) )
232 thread_pool = g_thread_pool_new((GFunc)job_thread, NULL, -1, FALSE, NULL);
233 ++n_jobs;
234 }
235
236 /**
237 * fm_job_new
238 *
239 * Creates a new #FmJob object.
240 *
241 * Returns: a new #FmJob object.
242 *
243 * Since: 0.1.0
244 */
245 FmJob* fm_job_new(void)
246 {
247 return (FmJob*)g_object_new(FM_TYPE_JOB, NULL);
248 }
249
250
251 static void fm_job_finalize(GObject *object)
252 {
253 g_return_if_fail(object != NULL);
254 g_return_if_fail(FM_IS_JOB(object));
255
256 if (G_OBJECT_CLASS(fm_job_parent_class)->finalize)
257 (* G_OBJECT_CLASS(fm_job_parent_class)->finalize)(object);
258
259 --n_jobs;
260 if(0 == n_jobs)
261 {
262 g_thread_pool_free(thread_pool, TRUE, FALSE);
263 thread_pool = NULL;
264 }
265 }
266
267 static gboolean fm_job_real_run_async(FmJob* job)
268 {
269 g_thread_pool_push(thread_pool, job, NULL);
270 return TRUE;
271 }
272
273 /**
274 * fm_job_run_async
275 * @job: a job to run
276 *
277 * Starts the @job asyncronously creating new thread. If job starts
278 * successfully then the #FmJob::finished signal will be emitted when
279 * @job is either succeeded or was cancelled. If @job could not be
280 * started then #FmJob::cancelled signal is emitted before return from
281 * this function.
282 *
283 * Returns: %TRUE if job started successfully.
284 *
285 * Since: 0.1.0
286 */
287 gboolean fm_job_run_async(FmJob* job)
288 {
289 FmJobClass* klass = FM_JOB_CLASS(G_OBJECT_GET_CLASS(job));
290 gboolean ret;
291 job->running = TRUE;
292 g_object_ref(job); /* acquire a ref, it will be unrefed by on_idle_cleanup() */
293 ret = klass->run_async(job);
294 if(G_UNLIKELY(!ret)) /* failed? */
295 {
296 fm_job_emit_cancelled(job);
297 g_object_unref(job);
298 }
299 return ret;
300 }
301
302 /**
303 * fm_job_run_sync
304 * @job: a job to run
305 *
306 * Runs the @job in current thread in a blocking fashion. The job will
307 * emit either #FmJob::cancelled signal if job was cancelled
308 * or #FmJob::finished signal if it finished successfully.
309 *
310 * Returns: %TRUE if @job ran successfully.
311 *
312 * Since: 0.1.0
313 */
314 /* run a job in current thread in a blocking fashion. */
315 gboolean fm_job_run_sync(FmJob* job)
316 {
317 FmJobClass* klass = FM_JOB_CLASS(G_OBJECT_GET_CLASS(job));
318 gboolean ret;
319 job->running = TRUE;
320 ret = klass->run(job);
321 job->running = FALSE;
322 if(job->cancel)
323 fm_job_emit_cancelled(job);
324 else
325 fm_job_emit_finished(job);
326 return ret;
327 }
328
329 static void on_sync_job_finished(FmJob* job, GMainLoop* mainloop)
330 {
331 g_main_loop_quit(mainloop);
332 job->running = FALSE;
333 }
334
335 /**
336 * fm_job_run_sync_with_mainloop
337 * @job: a job to run
338 *
339 * Runs the @job in current thread in a blocking fashion and an additional
340 * mainloop being created to prevent blocking of user interface. If @job
341 * started successfully then #FmJob::finished signal is emitted when @job
342 * is either succeeded or was cancelled.
343 * Note: using this API from within GTK main loop will lead to deadlock
344 * therefore if it is a GTK application then caller should unlock GDK
345 * threads before calling this API and lock them back after return from
346 * it. This statement is valid for any GTK application that uses locks.
347 *
348 * Returns: %TRUE if job started successfully.
349 *
350 * Since: 0.1.1
351 */
352 gboolean fm_job_run_sync_with_mainloop(FmJob* job)
353 {
354 GMainLoop* mainloop = g_main_loop_new(NULL, FALSE);
355 gboolean ret;
356 g_signal_connect(job, "finished", G_CALLBACK(on_sync_job_finished), mainloop);
357 ret = fm_job_run_async(job);
358 if(G_LIKELY(ret))
359 {
360 g_main_loop_run(mainloop);
361 }
362 g_signal_handlers_disconnect_by_func(job, on_sync_job_finished, mainloop);
363 g_main_loop_unref(mainloop);
364 return ret;
365 }
366
367 /* this is called from working thread */
368 static void job_thread(FmJob* job, gpointer unused)
369 {
370 FmJobClass* klass = FM_JOB_CLASS(G_OBJECT_GET_CLASS(job));
371 klass->run(job);
372
373 /* let the main thread know that we're done, and free the job
374 * in idle handler if neede. */
375 fm_job_finish(job);
376 }
377
378 /**
379 * fm_job_cancel
380 * @job: a job to cancel
381 *
382 * Cancels the @job.
383 *
384 * Since: 0.1.0
385 */
386 /* cancel the job */
387 void fm_job_cancel(FmJob* job)
388 {
389 FmJobClass* klass = FM_JOB_CLASS(G_OBJECT_GET_CLASS(job));
390 job->cancel = TRUE;
391 if(job->cancellable)
392 g_cancellable_cancel(job->cancellable);
393 if(klass->cancel)
394 klass->cancel(job);
395 }
396
397 static gboolean on_idle_call(gpointer input_data)
398 {
399 FmIdleCall* data = (FmIdleCall*)input_data;
400 data->ret = data->func(data->job, data->user_data);
401 return FALSE;
402 }
403
404 /**
405 * fm_job_call_main_thread
406 * @job: the job that calls main thread
407 * @func: callback to run from main thread
408 * @user_data: user data for the callback
409 *
410 * Stops calling thread, waits main thread for idle, passes @user_data
411 * to callback @func in main thread, gathers result of callback, and
412 * returns it to caller.
413 *
414 * This APIs is private to #FmJob and should only be used in the
415 * implementation of classes derived from #FmJob.
416 *
417 * This function should be called from working thread only.
418 *
419 * Returns: return value from running @func.
420 *
421 * Since: 0.1.0
422 */
423 gpointer fm_job_call_main_thread(FmJob* job,
424 FmJobCallMainThreadFunc func, gpointer user_data)
425 {
426 FmIdleCall data;
427 data.job = job;
428 data.func = func;
429 data.user_data = user_data;
430 fm_run_in_default_main_context(on_idle_call, &data);
431 return data.ret;
432 }
433
434 /**
435 * fm_job_finish
436 * @job: the job that was finished
437 *
438 * Schedules the finishing of job. Once this function is called the
439 * @job becomes invalid for the caller and should be not used anymore.
440 *
441 * This APIs is private to #FmJob and should only be used in the
442 * implementation of classes derived from #FmJob.
443 *
444 * This function should be called from working thread only.
445 *
446 * Since: 0.1.0
447 */
448 void fm_job_finish(FmJob* job)
449 {
450 G_LOCK(idle_handler);
451 if(0 == idle_handler)
452 idle_handler = g_idle_add(on_idle_cleanup, NULL);
453 finished = g_slist_append(finished, job);
454 job->running = FALSE;
455 G_UNLOCK(idle_handler);
456 }
457
458 struct AskData
459 {
460 const char* question;
461 gchar* const *options;
462 };
463
464 static gpointer ask_in_main_thread(FmJob* job, gpointer input_data)
465 {
466 gint ret;
467 #define data ((struct AskData*)input_data)
468 g_signal_emit(job, signals[ASK], 0, data->question, data->options, &ret);
469 #undef data
470 return GINT_TO_POINTER(ret);
471 }
472
473 /**
474 * fm_job_ask
475 * @job: the job that calls main thread
476 * @question: the text to ask the user
477 * @...: list of choices to give the user
478 *
479 * Asks the user for some interactions. The user will have a list of
480 * available options and should make a choice.
481 *
482 * This APIs is private to #FmJob and should only be used in the
483 * implementation of classes derived from #FmJob.
484 *
485 * This function should be called from working thread only.
486 *
487 * Returns: user's choice.
488 *
489 * Since: 0.1.0
490 */
491 gint fm_job_ask(FmJob* job, const char* question, ...)
492 {
493 gint ret;
494 va_list args;
495 va_start (args, question);
496 ret = fm_job_ask_valist(job, question, args);
497 va_end (args);
498 return ret;
499 }
500
501 /**
502 * fm_job_askv
503 * @job: the job that calls main thread
504 * @question: the text to ask the user
505 * @options: list of choices to give the user
506 *
507 * Asks the user for some interactions. The user will have a list of
508 * available options and should make a choice.
509 *
510 * This APIs is private to #FmJob and should only be used in the
511 * implementation of classes derived from #FmJob.
512 *
513 * This function should be called from working thread only.
514 *
515 * Returns: user's choice.
516 *
517 * Since: 0.1.0
518 */
519 gint fm_job_askv(FmJob* job, const char* question, gchar* const *options)
520 {
521 struct AskData data;
522 data.question = question;
523 data.options = options;
524 return GPOINTER_TO_INT(fm_job_call_main_thread(job, ask_in_main_thread, &data));
525 }
526
527 /**
528 * fm_job_ask_valist
529 * @job: the job that calls main thread
530 * @question: the text to ask the user
531 * @options: list of choices to give the user
532 *
533 * Asks the user for some interactions. The user will have a list of
534 * available options and should make a choice.
535 *
536 * This APIs is private to #FmJob and should only be used in the
537 * implementation of classes derived from #FmJob.
538 *
539 * This function should be called from working thread only.
540 *
541 * Returns: user's choice.
542 *
543 * Since: 0.1.0
544 */
545 gint fm_job_ask_valist(FmJob* job, const char* question, va_list options)
546 {
547 GArray* opts = g_array_sized_new(TRUE, TRUE, sizeof(char*), 6);
548 gint ret;
549 const char* opt = va_arg(options, const char*);
550 while(opt)
551 {
552 g_array_append_val(opts, opt);
553 opt = va_arg (options, const char *);
554 }
555 ret = fm_job_askv(job, question, &opts->data);
556 g_array_free(opts, TRUE);
557 return ret;
558 }
559
560
561 /* unref finished job objects in main thread on idle */
562 static gboolean on_idle_cleanup(gpointer unused)
563 {
564 GSList* jobs;
565 GSList* l;
566
567 G_LOCK(idle_handler);
568 jobs = finished;
569 finished = NULL;
570 idle_handler = 0;
571 G_UNLOCK(idle_handler);
572
573 for(l = jobs; l; l=l->next)
574 {
575 FmJob* job = FM_JOB(l->data);
576 if(job->cancel)
577 fm_job_emit_cancelled(job);
578 fm_job_emit_finished(job);
579 g_object_unref(job);
580 }
581 g_slist_free(jobs);
582 return FALSE;
583 }
584
585 /**
586 * fm_job_init_cancellable
587 * @job: the job to init
588 *
589 * Used by derived classes to implement #FmJobClass:run() using gio inside.
590 * This API tries to initialize a #GCancellable object for use with gio and
591 * should only be called once in the constructor of derived classes which
592 * require the use of #GCancellable.
593 *
594 * This APIs is private to #FmJob and should only be used in the
595 * implementation of classes derived from #FmJob.
596 *
597 * Since: 0.1.0
598 */
599 void fm_job_init_cancellable(FmJob* job)
600 {
601 #if GLIB_CHECK_VERSION(2, 32, 0)
602 g_rec_mutex_init(&job->stop);
603 #else
604 g_static_rec_mutex_init(&job->stop);
605 #endif
606 job->cancellable = g_cancellable_new();
607 g_signal_connect(job->cancellable, "cancelled", G_CALLBACK(on_cancellable_cancelled), job);
608 }
609
610 /**
611 * fm_job_get_cancellable
612 * @job: the job to inspect
613 *
614 * Get an existing #GCancellable object from @job for use with gio in
615 * another job by calling fm_job_set_cancellable().
616 * This can be used when you wish to share a cancellable object
617 * among different jobs.
618 *
619 * This APIs is private to #FmJob and should only be used in the
620 * implementation of classes derived from #FmJob.
621 *
622 * Returns: a #GCancellable object if it was initialized for @job.
623 *
624 * Since: 0.1.9
625 */
626 GCancellable* fm_job_get_cancellable(FmJob* job)
627 {
628 return job->cancellable;
629 }
630
631 /**
632 * fm_job_set_cancellable
633 * @job: the job to set
634 * @cancellable: (allow-none): a shared cancellable object
635 *
636 * Lets the job to use an existing @cancellable object.
637 * This can be used when you wish to share a cancellable object
638 * among different jobs.
639 * This should only be called before the @job is launched.
640 *
641 * This APIs is private to #FmJob and should only be used in the
642 * implementation of classes derived from #FmJob.
643 *
644 * Since: 0.1.0
645 */
646 void fm_job_set_cancellable(FmJob* job, GCancellable* cancellable)
647 {
648 if(G_UNLIKELY(job->cancellable))
649 {
650 g_signal_handlers_disconnect_by_func(job->cancellable, on_cancellable_cancelled, job);
651 g_object_unref(job->cancellable);
652 }
653 if(G_LIKELY(cancellable))
654 {
655 job->cancellable = (GCancellable*)g_object_ref(cancellable);
656 g_signal_connect(job->cancellable, "cancelled", G_CALLBACK(on_cancellable_cancelled), job);
657 }
658 else
659 job->cancellable = NULL;
660 }
661
662 static void on_cancellable_cancelled(GCancellable* cancellable, FmJob* job)
663 {
664 job->cancel = TRUE;
665 }
666
667 struct ErrData
668 {
669 GError* err;
670 FmJobErrorSeverity severity;
671 };
672
673 static gpointer error_in_main_thread(FmJob* job, gpointer input_data)
674 {
675 guint ret;
676 #define data ((struct ErrData*)input_data)
677 g_debug("FmJob error: %s", data->err->message);
678 g_signal_emit(job, signals[ERROR], 0, data->err, (guint)data->severity, &ret);
679 #undef data
680 return GUINT_TO_POINTER(ret);
681 }
682
683 /**
684 * fm_job_emit_error
685 * @job: a job that emitted the signal
686 * @err: an error descriptor
687 * @severity: severity of the error
688 *
689 * Emits an #FmJob::error signal in the main thread to notify it when an
690 * error occurs.
691 * The return value of this function is the return value returned by
692 * the connected signal handlers.
693 * If @severity is FM_JOB_ERROR_CRITICAL, the returned value is ignored and
694 * fm_job_cancel() is called to abort the job. Otherwise, the signal
695 * handler of this error can return FM_JOB_RETRY to ask for retrying the
696 * failed operation, return FM_JOB_CONTINUE to ignore the error and
697 * continue the remaining job, or return FM_JOB_ABORT to abort the job.
698 * If FM_JOB_ABORT is returned by the signal handler, fm_job_cancel()
699 * will be called in fm_job_emit_error().
700 *
701 * This APIs is private to #FmJob and should only be used in the
702 * implementation of classes derived from #FmJob.
703 *
704 * This function should be called from working thread only.
705 *
706 * Returns: action that should be performed on that error.
707 *
708 * Since: 0.1.0
709 */
710 FmJobErrorAction fm_job_emit_error(FmJob* job, GError* err, FmJobErrorSeverity severity)
711 {
712 FmJobErrorAction ret;
713 struct ErrData data;
714 g_return_val_if_fail(err, FM_JOB_ABORT);
715 data.err = err;
716 data.severity = severity;
717 ret = GPOINTER_TO_UINT(fm_job_call_main_thread(job, error_in_main_thread, &data));
718 if(severity == FM_JOB_ERROR_CRITICAL || ret == FM_JOB_ABORT)
719 {
720 ret = FM_JOB_ABORT;
721 fm_job_cancel(job);
722 }
723
724 /* If the job is already cancelled, retry is not allowed. */
725 if(ret == FM_JOB_RETRY )
726 {
727 if(job->cancel || (err->domain == G_IO_ERROR && err->code == G_IO_ERROR_CANCELLED))
728 ret = FM_JOB_CONTINUE;
729 }
730
731 return ret;
732 }
733
734 /* FIXME: need to re-think how to do this in a correct way. */
735 /*
736 gboolean fm_job_error_accumulator(GSignalInvocationHint *ihint, GValue *return_accu,
737 const GValue *handler_return, gpointer data)
738 {
739 int val = g_value_get_int(handler_return);
740 g_debug("accumulate: %d, %d", g_value_get_int(return_accu), val);
741 g_value_set_int(return_accu, val);
742 return val != FM_JOB_CONTINUE;
743 }
744 */
745
746 #if !GLIB_CHECK_VERSION(2, 32, 0)
747 #define g_rec_mutex_lock g_static_rec_mutex_lock
748 #define g_rec_mutex_unlock g_static_rec_mutex_unlock
749 #endif
750
751 /* lock the job but be sure lock is single */
752 static void _ensure_job_locked(FmJob *job)
753 {
754 g_rec_mutex_lock(&job->stop);
755 if (job->suspended)
756 g_rec_mutex_unlock(&job->stop); /* drop extra lock */
757 }
758
759 /**
760 * fm_job_is_cancelled
761 * @job: the job to inspect
762 *
763 * Checks if the job is already cancelled.
764 *
765 * Returns: %TRUE if the job is already cancelled.
766 *
767 * Since: 0.1.9
768 */
769 gboolean fm_job_is_cancelled(FmJob* job)
770 {
771 g_rec_mutex_lock(&job->stop);
772 /* wait on lock */
773 g_rec_mutex_unlock(&job->stop);
774 return job->cancel;
775 }
776
777 /**
778 * fm_job_is_running
779 * @job: the job to inspect
780 *
781 * Checks if the job is still running.
782 *
783 * Returns: %TRUE if the job is still running.
784 *
785 * Since: 0.1.9
786 */
787 gboolean fm_job_is_running(FmJob* job)
788 {
789 return job->running;
790 }
791
792 /**
793 * fm_job_pause
794 * @job: a job to apply
795 *
796 * Locks execution of job until next call to fm_job_resume(). This call
797 * may be used from thread different from the thread where the job runs
798 * in. This call may be done again (but have no extra effect) from the
799 * same thread. Any other usage may lead to deadlock.
800 *
801 * Returns: %FALSE if job cannot be locked.
802 *
803 * Since: 1.2.0
804 */
805 gboolean fm_job_pause(FmJob *job)
806 {
807 g_return_val_if_fail(job != NULL && FM_IS_JOB(job), FALSE);
808 if (!job->cancellable)
809 return FALSE;
810 _ensure_job_locked(job); /* acquire lock */
811 job->suspended = TRUE; /* mark appropriately */
812 return TRUE;
813 }
814
815 /**
816 * fm_job_resume
817 * @job: a job to apply
818 *
819 * Unlocks execution of @job that was made by previous call to
820 * fm_job_pause(). This call may be used only from the same thread
821 * where previous fm_job_pause() was made. Any other usage may lead to
822 * deadlock.
823 *
824 * Since: 1.2.0
825 */
826 void fm_job_resume(FmJob *job)
827 {
828 g_return_if_fail(job != NULL && FM_IS_JOB(job));
829 if (!job->cancellable)
830 return;
831 _ensure_job_locked(job); /* acquire lock... */
832 job->suspended = FALSE;
833 g_rec_mutex_unlock(&job->stop); /* ...and drop it */
834 }