efl

Форк
0
/
eina_test_thread.c 
124 строки · 2.9 Кб
1
/* EINA - EFL data type library
2
 * Copyright (C) 2020 Expertise Solutions Cons em Inf
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library;
16
 * if not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
#include <check.h>
20
#ifdef HAVE_CONFIG_H
21
# include "config.h"
22
#endif
23

24
#ifdef HAVE_UNISTD
25
# include <unistd.h>
26
#endif
27

28
#ifdef _WIN32
29
# include <evil_private.h> /* mkdir */
30
#endif
31

32
#include <Eina.h>
33
#include "eina_suite.h"
34

35
static void
36
thread_cleanup_fn(void *arg)
37
{
38
   *(int *) arg  = 1;
39
}
40

41
static void *
42
thread_fn_execute(void *arg, Eina_Thread t EINA_UNUSED)
43
{
44
   EINA_THREAD_CLEANUP_PUSH(thread_cleanup_fn, arg);
45
   EINA_THREAD_CLEANUP_POP(1);
46
   return NULL;
47
}
48

49
static void *
50
thread_fn_skip(void *arg, Eina_Thread t EINA_UNUSED)
51
{
52
   EINA_THREAD_CLEANUP_PUSH(thread_cleanup_fn, arg);
53
   EINA_THREAD_CLEANUP_POP(0);
54
   return NULL;
55
}
56

57
static void *
58
thread_fn_cancel(void *arg, Eina_Thread t EINA_UNUSED)
59
{
60
   Eina_Condition *cond = arg;
61

62
   ck_assert(eina_thread_cancellable_set(EINA_TRUE, NULL));
63
   ck_assert(eina_condition_signal(cond));
64

65
   for (size_t i = 0; i < 100; ++i)
66
     {
67
        eina_thread_cancel_checkpoint();
68
#ifdef _WIN32
69
        Sleep(100);
70
#else
71
        usleep(100 * 1000);
72
#endif
73
     }
74

75
   return NULL;
76
}
77

78
EFL_START_TEST(eina_thread_test_cleanup_execute)
79
{
80
   Eina_Thread t;
81
   int flag = 0;
82
   ck_assert(eina_thread_create(&t, EINA_THREAD_NORMAL, -1, thread_fn_execute, &flag));
83
   eina_thread_join(t);
84
   ck_assert_uint_eq(flag, 1);
85
}
86
EFL_END_TEST
87

88
EFL_START_TEST(eina_thread_test_cleanup_skip)
89
{
90
   Eina_Thread t;
91
   int flag = 2;
92
   ck_assert(eina_thread_create(&t, EINA_THREAD_NORMAL, -1, thread_fn_skip, &flag));
93
   eina_thread_join(t);
94
   ck_assert_uint_eq(flag, 2);
95
}
96
EFL_END_TEST
97

98
EFL_START_TEST(eina_thread_test_cancel)
99
{
100
   Eina_Thread t;
101
   Eina_Lock mutex;
102
   Eina_Condition cond;
103

104
   ck_assert(eina_lock_new(&mutex));
105
   ck_assert(eina_condition_new(&cond, &mutex));
106

107
   ck_assert(eina_thread_create(&t, EINA_THREAD_NORMAL, -1, thread_fn_cancel, &cond));
108
   ck_assert(eina_lock_take(&mutex));
109
   ck_assert(eina_condition_wait(&cond));
110
   ck_assert(eina_thread_cancel(t));
111
   ck_assert_ptr_eq(eina_thread_join(t), EINA_THREAD_JOIN_CANCELED);
112

113
   eina_condition_free(&cond);
114
   eina_lock_free(&mutex);
115
}
116
EFL_END_TEST
117

118
void
119
eina_test_thread(TCase *tc)
120
{
121
   tcase_add_test(tc, eina_thread_test_cleanup_skip);
122
   tcase_add_test(tc, eina_thread_test_cleanup_execute);
123
   tcase_add_test(tc, eina_thread_test_cancel);
124
}
125

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.