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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_threads.h>
32 #include <vlc_interrupt.h>
33 #include <vlc_network.h>
38 static void interrupt_callback(void *data
)
43 static void test_context_simple(vlc_interrupt_t
*ctx
)
45 vlc_interrupt_t
*octx
;
48 vlc_interrupt_set(ctx
);
49 octx
= vlc_interrupt_set(NULL
);
51 octx
= vlc_interrupt_set(ctx
);
53 octx
= vlc_interrupt_set(ctx
);
56 vlc_interrupt_register(interrupt_callback
, &sem
);
57 vlc_interrupt_raise(ctx
);
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
65 assert(vlc_sem_wait_i11e(&sem
) == 0);
67 vlc_interrupt_raise(ctx
);
68 assert(vlc_sem_wait_i11e(&sem
) == EINTR
);
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
);
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
);
90 assert(vlc_write_i11e(fds
[0], &c
, 1) == 1);
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
);
98 assert(vlc_sendto_i11e(fds
[0], &c
, 1, 0, NULL
, 0) == 1);
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
);
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);
123 test_context_simple(ctx
);
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
);
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
);
150 vlc_assert_unreachable();
153 static void unreachable_callback(void *data
)
161 vlc_interrupt_t
*ctx
;
166 ctx
= vlc_interrupt_create();
168 vlc_interrupt_destroy(ctx
);
170 vlc_sem_init(&sem
, 0);
171 ctx
= vlc_interrupt_create();
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
);
184 assert(!vlc_clone(&th
, test_thread_cleanup
, ctx
, VLC_THREAD_PRIORITY_LOW
));
187 assert(!vlc_clone(&th
, test_thread_cancel
, ctx
, VLC_THREAD_PRIORITY_LOW
));
191 vlc_interrupt_destroy(ctx
);
193 /* Tests without interrupt context */
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();
203 vlc_interrupt_forward_start(ctx
, data
);
204 assert(vlc_interrupt_forward_stop(data
) == 0);
208 vlc_sem_destroy(&sem
);