summaryrefslogtreecommitdiffstats
path: root/tinySAK/test/test_timer.h
blob: 8f28b0c2569dc1b06b90595c9c5e803aeb6f2f83 (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
/*
* Copyright (C) 2009 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/
#ifndef _TEST_TIMER_H_
#define _TEST_TIMER_H_
typedef struct test_timer_s {
    tsk_timer_id_t id;
    uint64_t timeout;
    const char *arg;
}
test_timer_t;

test_timer_t timers[] = {
    {0, 2000,	"3"},
    {1, 2500,	"4"},
    {2, 500,	"1"},
    {3, 1000,	"2"},
    {4, 1000,	"2"},
    {5, 0,		"0"},
    {6, 10000,	"6"},
    {7, 3000,	"5"},
    {8, 2500,	"4"},
};

static int test_timer_callback(const void* arg, tsk_timer_id_t timer_id)
{
    // Do quick job
    printf("test_timer - id=%llu and arg=%s//\n", timer_id, arg);
    return 0;
}

void test_global_timer()
{
    size_t i;
    tsk_timer_mgr_global_ref();

    // for test: start it two times
    tsk_timer_mgr_global_start();
    tsk_timer_mgr_global_start();

    for(i=0; i<sizeof(timers)/sizeof(test_timer_t); ++i) {
        timers[i].id = tsk_timer_mgr_global_schedule(timers[i].timeout, test_timer_callback, timers[i].arg);
    }

    tsk_timer_mgr_global_cancel(timers[6].id);
    tsk_timer_mgr_global_cancel(timers[2].id);

    tsk_thread_sleep(4000);

    // for test: stop it only one time
    tsk_timer_mgr_global_stop();
    tsk_timer_mgr_global_stop();

    tsk_timer_mgr_global_unref();
}

void test_single_timer()
{
    size_t i;
    tsk_timer_manager_handle_t *handle = tsk_timer_manager_create();
    printf("test_timer//\n");

    tsk_timer_manager_start(handle);

    for(i=0; i<sizeof(timers)/sizeof(test_timer_t); ++i) {
        timers[i].id = tsk_timer_manager_schedule(handle, timers[i].timeout, test_timer_callback, timers[i].arg);
    }

    tsk_timer_manager_cancel(handle, timers[6].id);
    tsk_timer_manager_cancel(handle, timers[2].id);

    tsk_thread_sleep(4000);

    /* Stops and frees the timer manager */
    TSK_OBJECT_SAFE_FREE(handle);
}

void test_timer()
{
    //test_single_timer();
    test_global_timer();
}

#endif /* _TEST_TIMER_H_ */
OpenPOWER on IntegriCloud