summaryrefslogtreecommitdiff
path: root/src/tests/eina/eina_test_barrier.c
blob: 81b257a2ce627e14df70b6982cbdff4bf37727ad (plain)
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
/* EINA - EFL data type library
 * Copyright (C) 2013 ProFUSION embedded systems
 *
 * This library 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 library 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 library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <unistd.h>

#include <Eina.h>

#include "eina_suite.h"

static Eina_Thread wk1, wk2, wk3, wk4, wk5;
static Eina_Barrier barrier;

static void *
wk_func(void *data EINA_UNUSED, Eina_Thread thread EINA_UNUSED)
{
    eina_barrier_wait(&barrier);
    return NULL;
}

static void *
wk1_func(void *data EINA_UNUSED, Eina_Thread thread EINA_UNUSED)
{
    sleep(1);
    eina_barrier_wait(&barrier);
    return NULL;
}

static void *
wk2_func(void *data EINA_UNUSED, Eina_Thread thread EINA_UNUSED)
{
    sleep(2);
    eina_barrier_wait(&barrier);
    return NULL;
}

static void *
wk3_func(void *data EINA_UNUSED, Eina_Thread thread EINA_UNUSED)
{
    sleep(3);
    eina_barrier_wait(&barrier);
    return NULL;
}

START_TEST(eina_barrier_test_simple)
{
    Eina_Bool r;
    int i;

    i = eina_init();
    _ck_assert_int(i, >=, 1);

    i = eina_threads_init();
    _ck_assert_int(i, >=, 1);

    r = eina_barrier_new(&barrier, 6);
    fail_unless(r);

    r = eina_thread_create(&wk1, EINA_THREAD_NORMAL, -1, wk_func, NULL);
    fail_unless(r);

    r = eina_thread_create(&wk2, EINA_THREAD_NORMAL, -1, wk_func, NULL);
    fail_unless(r);

    r = eina_thread_create(&wk3, EINA_THREAD_NORMAL, -1, wk1_func, NULL);
    fail_unless(r);

    r = eina_thread_create(&wk4, EINA_THREAD_NORMAL, -1, wk2_func, NULL);
    fail_unless(r);

    r = eina_thread_create(&wk5, EINA_THREAD_NORMAL, -1, wk3_func, NULL);
    fail_unless(r);

    eina_barrier_wait(&barrier);

    eina_thread_join(wk1);

    eina_thread_join(wk2);

    eina_thread_join(wk3);

    eina_thread_join(wk4);

    eina_thread_join(wk5);

    eina_barrier_free(&barrier);

    eina_threads_shutdown();
    eina_shutdown();
}
END_TEST

void
eina_test_barrier(TCase *tc)
{
#ifndef _WIN32
   tcase_set_timeout(tc, 6);
#endif
   tcase_add_test(tc, eina_barrier_test_simple);
}