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