forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterrupt.c
142 lines (114 loc) · 3.73 KB
/
interrupt.c
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*****************************************************************************
* interrupt.c: Test for interrupt context API
*****************************************************************************
* Copyright (C) 2015 Rémi Denis-Courmont
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#undef NDEBUG
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <vlc_common.h>
#include <vlc_threads.h>
#include <vlc_interrupt.h>
static vlc_sem_t sem;
static void test_context_simple(vlc_interrupt_t *ctx)
{
vlc_interrupt_t *octx;
vlc_interrupt_set(ctx);
octx = vlc_interrupt_set(NULL);
assert(octx == ctx);
octx = vlc_interrupt_set(ctx);
assert(octx == NULL);
octx = vlc_interrupt_set(ctx);
assert(octx == ctx);
/* BIG FAT WARNING: This is only meant to test the vlc_cond_wait_i11e()
* function. This is NOT a good example of how to use the function in
* normal code. */
vlc_sem_post(&sem);
assert(vlc_sem_wait_i11e(&sem) == 0);
vlc_interrupt_raise(ctx);
assert(vlc_sem_wait_i11e(&sem) == EINTR);
vlc_sem_post(&sem);
vlc_interrupt_raise(ctx);
assert(vlc_sem_wait_i11e(&sem) == EINTR);
assert(vlc_sem_wait_i11e(&sem) == 0);
vlc_interrupt_raise(ctx);
vlc_sem_post(&sem);
assert(vlc_sem_wait_i11e(&sem) == EINTR);
assert(vlc_sem_wait_i11e(&sem) == 0);
octx = vlc_interrupt_set(NULL);
assert(octx == ctx);
octx = vlc_interrupt_set(NULL);
assert(octx == NULL);
}
static void *test_thread_simple(void *data)
{
vlc_interrupt_t *ctx = data;
vlc_interrupt_set(ctx);
assert(vlc_sem_wait_i11e(&sem) == EINTR);
assert(vlc_sem_wait_i11e(&sem) == 0);
vlc_sem_wait(&sem);
test_context_simple(ctx);
return NULL;
}
static void *test_thread_cleanup(void *data)
{
vlc_interrupt_t *ctx = data;
test_context_simple(ctx);
/* Test context clearing on exit */
vlc_interrupt_set(ctx);
return NULL;
}
static void *test_thread_cancel(void *data)
{
vlc_interrupt_t *ctx = data;
test_context_simple(ctx);
/* Test context clearing on cancellation */
vlc_interrupt_set(ctx);
for (;;)
pause();
vlc_assert_unreachable();
}
int main (void)
{
vlc_interrupt_t *ctx;
vlc_thread_t th;
alarm(2);
ctx = vlc_interrupt_create();
assert(ctx != NULL);
vlc_interrupt_destroy(ctx);
vlc_sem_init(&sem, 0);
ctx = vlc_interrupt_create();
assert(ctx != NULL);
test_context_simple(ctx);
assert(!vlc_clone(&th, test_thread_simple, ctx, VLC_THREAD_PRIORITY_LOW));
vlc_interrupt_raise(ctx);
vlc_sem_post(&sem);
vlc_sem_post(&sem);
vlc_join(th, NULL);
assert(!vlc_clone(&th, test_thread_cleanup, ctx, VLC_THREAD_PRIORITY_LOW));
vlc_join(th, NULL);
assert(!vlc_clone(&th, test_thread_cancel, ctx, VLC_THREAD_PRIORITY_LOW));
vlc_cancel(th);
vlc_join(th, NULL);
vlc_interrupt_destroy(ctx);
vlc_sem_destroy(&sem);
return 0;
}