Debian Codegraph @

libretro-common/rthreads/async_job.c

raw symbols SCIP
1 /* Copyright (C) 2010-2015 The RetroArch team
2 *
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (async_job.c).
5 * ---------------------------------------------------------------------------------------
6 *
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdlib.h>
24
25 #include <rthreads/rthreads.h>
26 #include <rthreads/rsemaphore.h>
27 #include <rthreads/async_job.h>
28
29 typedef struct async_job_node async_job_node_t;
30
31 struct async_job_node
32 {
33 async_task_t task;
34 void *payload;
35 async_job_node_t *next;
36 };
37
38 struct async_job
39 {
40 async_job_node_t *first;
41 async_job_node_t *last;
42 volatile int finish;
43 slock_t *lock;
44 ssem_t *sem;
45 sthread_t* thread;
46 };
47
48 static void async_job_processor(void *userdata)
49 {
50 async_job_node_t *node = NULL;
51 async_job_t *ajob = (async_job_t*)userdata;
52
53 for (;;)
54 {
55 ssem_wait(ajob->sem);
56
57 if (ajob->finish)
58 return;
59
60 slock_lock(ajob->lock);
61
62 node = ajob->first;
63 ajob->first = node->next;
64
65 slock_unlock(ajob->lock);
66
67 node->task(node->payload);
68 free((void*)node);
69 }
70 }
71
72 async_job_t *async_job_new(void)
73 {
74 async_job_t *ajob = (async_job_t*)calloc(1, sizeof(*ajob));
75
76 if (!ajob)
77 return NULL;
78
79 ajob->lock = slock_new();
80
81 if (!ajob->lock)
82 goto error;
83
84 ajob->sem = ssem_new(0);
85
86 if (!ajob->sem)
87 goto error;
88
89 ajob->thread = sthread_create(async_job_processor, (void*)ajob);
90
91 if (!ajob->thread)
92 goto error;
93
94 return ajob;
95
96 error:
97 if (ajob->lock)
98 slock_free(ajob->lock);
99 ajob->lock = NULL;
100 if (ajob->sem)
101 ssem_free(ajob->sem);
102 if (ajob)
103 free((void*)ajob);
104 return NULL;
105 }
106
107 void async_job_free(async_job_t *ajob)
108 {
109 if (!ajob)
110 return;
111
112 ajob->finish = 1;
113 ssem_signal(ajob->sem);
114 sthread_join(ajob->thread);
115 ssem_free(ajob->sem);
116
117 free((void*)ajob);
118 }
119
120 int async_job_add(async_job_t *ajob, async_task_t task, void *payload)
121 {
122 async_job_node_t *node;
123
124 if (!ajob)
125 return -1;
126
127 node = (async_job_node_t*)calloc(1, sizeof(*node));
128
129 if (!node)
130 return -1;
131
132 node->task = task;
133 node->payload = payload;
134
135 slock_lock(ajob->lock);
136
137 if (ajob->first)
138 {
139 ajob->last->next = node;
140 ajob->last = node;
141 }
142 else
143 ajob->first = ajob->last = node;
144
145 slock_unlock(ajob->lock);
146 ssem_signal(ajob->sem);
147
148 return 0;
149 }