efl
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_H21# include "config.h"22#endif23
24#ifdef HAVE_UNISTD25# include <unistd.h>26#endif27
28#ifdef _WIN3229# include <evil_private.h> /* mkdir */30#endif31
32#include <Eina.h>33#include "eina_suite.h"34
35static void36thread_cleanup_fn(void *arg)37{
38*(int *) arg = 1;39}
40
41static void *42thread_fn_execute(void *arg, Eina_Thread t EINA_UNUSED)43{
44EINA_THREAD_CLEANUP_PUSH(thread_cleanup_fn, arg);45EINA_THREAD_CLEANUP_POP(1);46return NULL;47}
48
49static void *50thread_fn_skip(void *arg, Eina_Thread t EINA_UNUSED)51{
52EINA_THREAD_CLEANUP_PUSH(thread_cleanup_fn, arg);53EINA_THREAD_CLEANUP_POP(0);54return NULL;55}
56
57static void *58thread_fn_cancel(void *arg, Eina_Thread t EINA_UNUSED)59{
60Eina_Condition *cond = arg;61
62ck_assert(eina_thread_cancellable_set(EINA_TRUE, NULL));63ck_assert(eina_condition_signal(cond));64
65for (size_t i = 0; i < 100; ++i)66{67eina_thread_cancel_checkpoint();68#ifdef _WIN3269Sleep(100);70#else71usleep(100 * 1000);72#endif73}74
75return NULL;76}
77
78EFL_START_TEST(eina_thread_test_cleanup_execute)79{
80Eina_Thread t;81int flag = 0;82ck_assert(eina_thread_create(&t, EINA_THREAD_NORMAL, -1, thread_fn_execute, &flag));83eina_thread_join(t);84ck_assert_uint_eq(flag, 1);85}
86EFL_END_TEST
87
88EFL_START_TEST(eina_thread_test_cleanup_skip)89{
90Eina_Thread t;91int flag = 2;92ck_assert(eina_thread_create(&t, EINA_THREAD_NORMAL, -1, thread_fn_skip, &flag));93eina_thread_join(t);94ck_assert_uint_eq(flag, 2);95}
96EFL_END_TEST
97
98EFL_START_TEST(eina_thread_test_cancel)99{
100Eina_Thread t;101Eina_Lock mutex;102Eina_Condition cond;103
104ck_assert(eina_lock_new(&mutex));105ck_assert(eina_condition_new(&cond, &mutex));106
107ck_assert(eina_thread_create(&t, EINA_THREAD_NORMAL, -1, thread_fn_cancel, &cond));108ck_assert(eina_lock_take(&mutex));109ck_assert(eina_condition_wait(&cond));110ck_assert(eina_thread_cancel(t));111ck_assert_ptr_eq(eina_thread_join(t), EINA_THREAD_JOIN_CANCELED);112
113eina_condition_free(&cond);114eina_lock_free(&mutex);115}
116EFL_END_TEST
117
118void
119eina_test_thread(TCase *tc)120{
121tcase_add_test(tc, eina_thread_test_cleanup_skip);122tcase_add_test(tc, eina_thread_test_cleanup_execute);123tcase_add_test(tc, eina_thread_test_cancel);124}
125