svn update: 48958 (latest:48959)
[framework/uifw/ecore.git] / src / lib / ecore / ecore_job.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include <stdlib.h>
10
11 #include "Ecore.h"
12 #include "ecore_private.h"
13
14 static int _ecore_job_event_handler(void *data, int type, void *ev);
15 static void _ecore_job_event_free(void *data, void *ev);
16
17 static int ecore_event_job_type = 0;
18 static Ecore_Event_Handler* _ecore_job_handler = NULL;
19
20 struct _Ecore_Job
21 {
22    ECORE_MAGIC;
23    Ecore_Event  *event;
24    void        (*func) (void *data);
25    void         *data;
26 };
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  * Add a job to the event queue.
44  * @param   func The function to call when the job gets handled.
45  * @param   data Data pointer to be passed to the job function when the job is
46  *               handled.
47  * @return  The handle of the job.  @c NULL is returned if the job could not be
48  *          added to the queue.
49  * @ingroup Ecore_Job_Group
50  * @note    Once the job has been executed, the job handle is invalid.
51  */
52 EAPI Ecore_Job *
53 ecore_job_add(void (*func) (void *data), const void *data)
54 {
55    Ecore_Job *job;
56    
57    if (!func) return NULL;
58
59    job = calloc(1, sizeof(Ecore_Job));
60    if (!job) return NULL;
61    ECORE_MAGIC_SET(job, ECORE_MAGIC_JOB);
62    job->event = ecore_event_add(ecore_event_job_type, job, _ecore_job_event_free, NULL);
63    if (!job->event)
64      {
65         free(job);
66         return NULL;
67      }
68    job->func = func;
69    job->data = (void *)data;
70    return job;
71 }
72
73 /**
74  * Delete a queued job that has not yet been executed.
75  * @param   job  Handle of the job to delete.
76  * @return  The data pointer that was to be passed to the job.
77  * @ingroup Ecore_Job_Group
78  */
79 EAPI void *
80 ecore_job_del(Ecore_Job *job)
81 {
82    void *data;
83    
84    if (!ECORE_MAGIC_CHECK(job, ECORE_MAGIC_JOB))
85      {
86         ECORE_MAGIC_FAIL(job, ECORE_MAGIC_JOB,
87                          "ecore_job_del");
88         return NULL;
89      }
90    data = job->data;
91    ECORE_MAGIC_SET(job, ECORE_MAGIC_NONE);
92    ecore_event_del(job->event);
93    return data;
94 }
95
96 static int
97 _ecore_job_event_handler(void *data __UNUSED__, int type __UNUSED__, void *ev)
98 {
99    Ecore_Job *job;
100    
101    job = ev;
102    job->func(job->data);
103    return 0;
104 }
105
106 static void
107 _ecore_job_event_free(void *data __UNUSED__, void *ev)
108 {
109    free(ev);
110 }