Explicitly initialize the deleted flag for the timer, idler and job objects.
[ruby-ecore.git] / src / ecore_job / rb_job.c
1 /*
2  * $Id: rb_job.c 67 2004-08-12 20:08:13Z tilman $
3  *
4  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
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
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22
23 #include <Ecore.h>
24 #include <Ecore_Job.h>
25 #include <stdbool.h>
26
27 #include "rb_ecore_job.h"
28
29 typedef struct {
30         Ecore_Job *real;
31         VALUE callback;
32         bool deleted;
33 } RbJob;
34
35 static void on_job (void *data)
36 {
37         RbJob *job = data;
38
39         rb_funcall (job->callback, rb_intern ("call"), 0);
40         job->deleted = true;
41 }
42
43 static void c_mark (RbJob *job)
44 {
45         rb_gc_mark (job->callback);
46 }
47
48 static void c_free (RbJob *job)
49 {
50         if (job->real && !job->deleted)
51                 ecore_job_del (job->real);
52
53         ecore_shutdown ();
54
55         free (job);
56 }
57
58 /*
59  * call-seq:
60  *  Ecore::Job::Job.new { block } => job
61  *
62  * Creates an Ecore::Job::Job object.
63  * After execution, the object will be deleted.
64  */
65 static VALUE c_new (VALUE klass)
66 {
67         VALUE self;
68         RbJob *job;
69
70         if (!rb_block_given_p ())
71                 return Qnil;
72
73         self = Data_Make_Struct (klass, RbJob, c_mark, c_free, job);
74
75         ecore_init ();
76
77         job->callback = rb_block_proc ();
78         job->deleted = false;
79         job->real = ecore_job_add (on_job, job);
80
81         rb_obj_call_init (self, 0, NULL);
82
83         return self;
84 }
85
86 /*
87  * call-seq:
88  *  job.delete => nil
89  *
90  * Deletes <i>job</i>.
91  */
92 static VALUE c_delete (VALUE self)
93 {
94         VALUE ret = Qfalse;
95         RbJob *job = NULL;
96
97         Data_Get_Struct (self, RbJob, job);
98
99         if (job->real && !job->deleted) {
100                 ecore_job_del (job->real);
101                 job->deleted = true;
102                 job->real = NULL;
103                 ret = Qtrue;
104         }
105
106         return ret;
107 }
108
109 void Init_Job (void)
110 {
111         VALUE c = rb_define_class_under (mJob, "Job", rb_cObject);
112
113         rb_define_singleton_method (c, "new", c_new, 0);
114         rb_define_method (c, "delete", c_delete, 0);
115 }