bd50232f83129b5abe32cb5cce3d4d7764f8af1c
[profile/ivi/ecore.git] / src / lib / ecore / ecore_job.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdlib.h>
6
7 #include "Ecore.h"
8 #include "ecore_private.h"
9
10 static Eina_Bool _ecore_job_event_handler(void *data, int type, void *ev);
11 static void _ecore_job_event_free(void *data, void *ev);
12
13 static int ecore_event_job_type = 0;
14 static Ecore_Event_Handler* _ecore_job_handler = NULL;
15
16 struct _Ecore_Job
17 {
18    ECORE_MAGIC;
19    Ecore_Event  *event;
20    Ecore_Cb func;
21    void         *data;
22 };
23
24 void
25 _ecore_job_init(void)
26 {
27    ecore_event_job_type = ecore_event_type_new();
28    _ecore_job_handler = ecore_event_handler_add(ecore_event_job_type, _ecore_job_event_handler, NULL);
29 }
30
31 void
32 _ecore_job_shutdown(void)
33 {
34    ecore_event_handler_del(_ecore_job_handler);
35    _ecore_job_handler = NULL;
36 }
37
38 /**
39  * Add a job to the event queue.
40  * @param   func The function to call when the job gets handled.
41  * @param   data Data pointer to be passed to the job function when the job is
42  *               handled.
43  * @return  The handle of the job.  @c NULL is returned if the job could not be
44  *          added to the queue.
45  * @ingroup Ecore_Job_Group
46  * @note    Once the job has been executed, the job handle is invalid.
47  */
48 EAPI Ecore_Job *
49 ecore_job_add(Ecore_Cb func, const void *data)
50 {
51    Ecore_Job *job;
52    
53    if (!func) return NULL;
54
55    job = calloc(1, sizeof(Ecore_Job));
56    if (!job) return NULL;
57    ECORE_MAGIC_SET(job, ECORE_MAGIC_JOB);
58    job->event = ecore_event_add(ecore_event_job_type, job, _ecore_job_event_free, NULL);
59    if (!job->event)
60      {
61         free(job);
62         return NULL;
63      }
64    job->func = func;
65    job->data = (void *)data;
66    return job;
67 }
68
69 /**
70  * Delete a queued job that has not yet been executed.
71  * @param   job  Handle of the job to delete.
72  * @return  The data pointer that was to be passed to the job.
73  * @ingroup Ecore_Job_Group
74  */
75 EAPI void *
76 ecore_job_del(Ecore_Job *job)
77 {
78    void *data;
79    
80    if (!ECORE_MAGIC_CHECK(job, ECORE_MAGIC_JOB))
81      {
82         ECORE_MAGIC_FAIL(job, ECORE_MAGIC_JOB,
83                          "ecore_job_del");
84         return NULL;
85      }
86    data = job->data;
87    ECORE_MAGIC_SET(job, ECORE_MAGIC_NONE);
88    ecore_event_del(job->event);
89    return data;
90 }
91
92 static Eina_Bool
93 _ecore_job_event_handler(void *data __UNUSED__, int type __UNUSED__, void *ev)
94 {
95    Ecore_Job *job;
96    
97    job = ev;
98    job->func(job->data);
99    return ECORE_CALLBACK_CANCEL;
100 }
101
102 static void
103 _ecore_job_event_free(void *data __UNUSED__, void *ev)
104 {
105    free(ev);
106 }