summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/lib/ecore_job/ecore_job.c
blob: 990ad8734e921dfd0537af517c2386cb02a325d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "Ecore.h"
#include "ecore_private.h"
#include "ecore_job_private.h"
#include "Ecore_Job.h"

static int _ecore_job_event_handler(void *data, int type, void *ev);
static void _ecore_job_event_free(void *data, void *ev);
    
static int ecore_event_job_type = 0;
static int _ecore_init_job_count = 0;
static Ecore_Event_Handler* _ecore_job_handler = NULL;

EAPI int
ecore_job_init()
{
   if (++_ecore_init_job_count == 1)
     {
        ecore_init();
	ecore_event_job_type = ecore_event_type_new();
	_ecore_job_handler = ecore_event_handler_add(ecore_event_job_type, _ecore_job_event_handler, NULL);
     }

   return _ecore_init_job_count;
}

EAPI int
ecore_job_shutdown()
{
   if (--_ecore_init_job_count)
     return _ecore_init_job_count;

   ecore_event_handler_del(_ecore_job_handler);
   _ecore_job_handler = NULL;
   ecore_shutdown();

   return _ecore_init_job_count;
}

/**
 * Add a job to the event queue.
 * @param   func The function to call when the job gets handled.
 * @param   data Data pointer to be passed to the job function when the job is
 *               handled.
 * @return  The handle of the job.  @c NULL is returned if the job could not be
 *          added to the queue.
 * @ingroup Ecore_Job_Group
 * @note    Once the job has been executed, the job handle is invalid.
 */
EAPI Ecore_Job *
ecore_job_add(void (*func) (void *data), const void *data)
{
   Ecore_Job *job;
   
   if (!func) return NULL;

   job = calloc(1, sizeof(Ecore_Job));
   if (!job) return NULL;
   ECORE_MAGIC_SET(job, ECORE_MAGIC_JOB);
   job->event = ecore_event_add(ecore_event_job_type, job, _ecore_job_event_free, NULL);
   if (!job->event)
     {
	free(job);
	return NULL;
     }
   job->func = func;
   job->data = (void *)data;
   return job;
}

/**
 * Delete a queued job that has not yet been executed.
 * @param   job  Handle of the job to delete.
 * @return  The data pointer that was to be passed to the job.
 * @ingroup Ecore_Job_Group
 */
EAPI void *
ecore_job_del(Ecore_Job *job)
{
   void *data;
   
   if (!ECORE_MAGIC_CHECK(job, ECORE_MAGIC_JOB))
     {
	ECORE_MAGIC_FAIL(job, ECORE_MAGIC_JOB,
			 "ecore_job_del");
	return NULL;
     }
   data = job->data;
   ECORE_MAGIC_SET(job, ECORE_MAGIC_NONE);
   ecore_event_del(job->event);
   return data;
}

static int
_ecore_job_event_handler(void *data __UNUSED__, int type __UNUSED__, void *ev)
{
   Ecore_Job *job;
   
   job = ev;
   job->func(job->data);
   return 0;
}

static void
_ecore_job_event_free(void *data __UNUSED__, void *ev)
{
   Ecore_Job *job;
   
   job = ev;
   free(ev);
}