vout: ios: remove useless var creation
[vlc.git] / src / test / interrupt.c
blob6b1bffbdf01b1138cfeef71cbbe77157f83e7f9f
1 /*****************************************************************************
2 * interrupt.c: Test for interrupt context API
3 *****************************************************************************
4 * Copyright (C) 2015 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #undef NDEBUG
26 #include <assert.h>
27 #include <errno.h>
28 #include <unistd.h>
30 #include <vlc_common.h>
31 #include <vlc_threads.h>
32 #include <vlc_interrupt.h>
33 #include <vlc_network.h>
35 static vlc_sem_t sem;
36 static int fds[2];
38 static void interrupt_callback(void *data)
40 vlc_sem_post(data);
43 static void test_context_simple(vlc_interrupt_t *ctx)
45 vlc_interrupt_t *octx;
46 char c;
48 vlc_interrupt_set(ctx);
49 octx = vlc_interrupt_set(NULL);
50 assert(octx == ctx);
51 octx = vlc_interrupt_set(ctx);
52 assert(octx == NULL);
53 octx = vlc_interrupt_set(ctx);
54 assert(octx == ctx);
56 vlc_interrupt_register(interrupt_callback, &sem);
57 vlc_interrupt_raise(ctx);
58 vlc_sem_wait(&sem);
59 vlc_interrupt_unregister();
61 /* BIG FAT WARNING: This is only meant to test the vlc_cond_wait_i11e()
62 * function. This is NOT a good example of how to use the function in
63 * normal code. */
64 vlc_sem_post(&sem);
65 assert(vlc_sem_wait_i11e(&sem) == 0);
67 vlc_interrupt_raise(ctx);
68 assert(vlc_sem_wait_i11e(&sem) == EINTR);
70 vlc_sem_post(&sem);
71 vlc_interrupt_raise(ctx);
72 assert(vlc_sem_wait_i11e(&sem) == EINTR);
73 assert(vlc_sem_wait_i11e(&sem) == 0);
75 vlc_interrupt_raise(ctx);
76 vlc_sem_post(&sem);
77 assert(vlc_sem_wait_i11e(&sem) == EINTR);
78 assert(vlc_sem_wait_i11e(&sem) == 0);
80 assert(vlc_mwait_i11e(1) == 0);
81 vlc_interrupt_raise(ctx);
82 assert(vlc_mwait_i11e(CLOCK_FREQ * 10000000) == EINTR);
84 assert(vlc_poll_i11e(NULL, 0, 1) == 0);
85 vlc_interrupt_raise(ctx);
86 assert(vlc_poll_i11e(NULL, 0, 1000000000) == -1);
87 assert(errno == EINTR);
89 c = 12;
90 assert(vlc_write_i11e(fds[0], &c, 1) == 1);
91 c = 0;
92 assert(vlc_read_i11e(fds[1], &c, 1) == 1 && c == 12);
93 vlc_interrupt_raise(ctx);
94 assert(vlc_read_i11e(fds[1], &c, 1) == -1);
95 assert(errno == EINTR);
97 c = 42;
98 assert(vlc_sendto_i11e(fds[0], &c, 1, 0, NULL, 0) == 1);
99 c = 0;
100 assert(vlc_recvfrom_i11e(fds[1], &c, 1, 0, NULL, 0) == 1 && c == 42);
101 vlc_interrupt_raise(ctx);
102 assert(vlc_recvfrom_i11e(fds[1], &c, 1, 0, NULL, 0) == -1);
103 assert(errno == EINTR);
105 vlc_interrupt_raise(ctx);
106 assert(vlc_accept_i11e(fds[1], NULL, NULL, true) < 0);
108 octx = vlc_interrupt_set(NULL);
109 assert(octx == ctx);
110 octx = vlc_interrupt_set(NULL);
111 assert(octx == NULL);
114 static void *test_thread_simple(void *data)
116 vlc_interrupt_t *ctx = data;
118 vlc_interrupt_set(ctx);
119 assert(vlc_sem_wait_i11e(&sem) == EINTR);
120 assert(vlc_sem_wait_i11e(&sem) == 0);
121 vlc_sem_wait(&sem);
123 test_context_simple(ctx);
124 return NULL;
127 static void *test_thread_cleanup(void *data)
129 vlc_interrupt_t *ctx = data;
131 test_context_simple(ctx);
132 /* Test context clearing on exit */
133 vlc_interrupt_set(ctx);
134 return NULL;
137 static void *test_thread_cancel(void *data)
139 vlc_interrupt_t *ctx = data;
141 int canc = vlc_savecancel();
142 test_context_simple(ctx);
143 vlc_restorecancel(canc);
145 /* Test context clearing on cancellation */
146 vlc_interrupt_set(ctx);
147 for (;;)
148 pause();
150 vlc_assert_unreachable();
153 static void unreachable_callback(void *data)
155 (void) data;
156 abort();
159 int main (void)
161 vlc_interrupt_t *ctx;
162 vlc_thread_t th;
164 alarm(2);
166 ctx = vlc_interrupt_create();
167 assert(ctx != NULL);
168 vlc_interrupt_destroy(ctx);
170 vlc_sem_init(&sem, 0);
171 ctx = vlc_interrupt_create();
172 assert(ctx != NULL);
174 assert(vlc_socketpair(PF_LOCAL, SOCK_STREAM, 0, fds, false) == 0);
176 test_context_simple(ctx);
178 assert(!vlc_clone(&th, test_thread_simple, ctx, VLC_THREAD_PRIORITY_LOW));
179 vlc_interrupt_raise(ctx);
180 vlc_sem_post(&sem);
181 vlc_sem_post(&sem);
182 vlc_join(th, NULL);
184 assert(!vlc_clone(&th, test_thread_cleanup, ctx, VLC_THREAD_PRIORITY_LOW));
185 vlc_join(th, NULL);
187 assert(!vlc_clone(&th, test_thread_cancel, ctx, VLC_THREAD_PRIORITY_LOW));
188 vlc_cancel(th);
189 vlc_join(th, NULL);
191 vlc_interrupt_destroy(ctx);
193 /* Tests without interrupt context */
194 vlc_sem_post(&sem);
195 assert(vlc_sem_wait_i11e(&sem) == 0);
196 assert(vlc_mwait_i11e(1) == 0);
197 assert(vlc_poll_i11e(NULL, 0, 1) == 0);
199 vlc_interrupt_register(unreachable_callback, NULL);
200 vlc_interrupt_unregister();
202 void *data[2];
203 vlc_interrupt_forward_start(ctx, data);
204 assert(vlc_interrupt_forward_stop(data) == 0);
206 vlc_close(fds[1]);
207 vlc_close(fds[0]);
208 vlc_sem_destroy(&sem);
209 return 0;