Tizen 2.1 base
[framework/uifw/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,
11                                           int   type,
12                                           void *ev);
13 static void _ecore_job_event_free(void *data,
14                                   void *ev);
15
16 static int ecore_event_job_type = 0;
17 static Ecore_Event_Handler *_ecore_job_handler = NULL;
18
19 struct _Ecore_Job
20 {
21    ECORE_MAGIC;
22    Ecore_Event *event;
23    Ecore_Cb     func;
24    void        *data;
25 };
26 GENERIC_ALLOC_SIZE_DECLARE(Ecore_Job);
27
28 void
29 _ecore_job_init(void)
30 {
31    ecore_event_job_type = ecore_event_type_new();
32    _ecore_job_handler = ecore_event_handler_add(ecore_event_job_type, _ecore_job_event_handler, NULL);
33 }
34
35 void
36 _ecore_job_shutdown(void)
37 {
38    _ecore_event_handler_del(_ecore_job_handler);
39    _ecore_job_handler = NULL;
40 }
41
42 /**
43  * @addtogroup Ecore_Job_Group
44  *
45  * @{
46  */
47
48 /**
49  * Add a job to the event queue.
50  * @param   func The function to call when the job gets handled.
51  * @param   data Data pointer to be passed to the job function when the job is
52  *               handled.
53  * @return  The handle of the job.  @c NULL is returned if the job could not be
54  *          added to the queue.
55  * @note    Once the job has been executed, the job handle is invalid.
56  */
57 EAPI Ecore_Job *
58 ecore_job_add(Ecore_Cb    func,
59               const void *data)
60 {
61    Ecore_Job *job;
62
63    EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
64    if (!func) return NULL;
65
66    job = ecore_job_calloc(1);
67    if (!job) return NULL;
68    ECORE_MAGIC_SET(job, ECORE_MAGIC_JOB);
69    job->event = ecore_event_add(ecore_event_job_type, job, _ecore_job_event_free, NULL);
70    if (!job->event)
71      {
72         ecore_job_mp_free(job);
73         return NULL;
74      }
75    job->func = func;
76    job->data = (void *)data;
77    return job;
78 }
79
80 /**
81  * Delete a queued job that has not yet been executed.
82  * @param   job  Handle of the job to delete.
83  * @return  The data pointer that was to be passed to the job.
84  */
85 EAPI void *
86 ecore_job_del(Ecore_Job *job)
87 {
88    void *data;
89
90    EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
91    if (!ECORE_MAGIC_CHECK(job, ECORE_MAGIC_JOB))
92      {
93         ECORE_MAGIC_FAIL(job, ECORE_MAGIC_JOB,
94                          "ecore_job_del");
95         return NULL;
96      }
97    data = job->data;
98    ECORE_MAGIC_SET(job, ECORE_MAGIC_NONE);
99    ecore_event_del(job->event);
100    return data;
101 }
102
103 /**
104  * @}
105  */
106
107 static Eina_Bool
108 _ecore_job_event_handler(void *data __UNUSED__,
109                          int   type __UNUSED__,
110                          void *ev)
111 {
112    Ecore_Job *job;
113
114    job = ev;
115    job->func(job->data);
116    return ECORE_CALLBACK_CANCEL;
117 }
118
119 static void
120 _ecore_job_event_free(void *data __UNUSED__,
121                       void *job)
122 {
123    ecore_job_mp_free(job);
124 }
125