summaryrefslogtreecommitdiff
path: root/src/libaudcore/timer.cc
blob: 4de6abf175cc3a4c1d2fdefdc460c782a359a689 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * timer.c
 * Copyright 2015 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "hook.h"

#include <pthread.h>

#include "index.h"
#include "internal.h"
#include "mainloop.h"
#include "runtime.h"

static const aud::array<TimerRate, int> rate_to_ms = {1000, 250, 100, 33};

struct TimerItem {
    TimerFunc func;
    void * data;
};

struct TimerList
{
    QueuedFunc source;
    Index<TimerItem> items;
    int use_count = 0;

    bool contains (TimerFunc func, void * data) const
    {
        for (auto & item : items)
        {
            if (item.func == func && item.data == data)
                return true;
        }

        return false;
    }

    void check_stop ()
    {
        if (! use_count)
        {
            auto is_empty = [] (const TimerItem & item)
                { return ! item.func; };

            items.remove_if (is_empty, true);

            if (! items.len () && source.running ())
                source.stop ();
        }
    }
};

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static aud::array<TimerRate, TimerList> lists;

static void timer_run (void * list_)
{
    auto & list = * (TimerList *) list_;
    pthread_mutex_lock (& mutex);

    list.use_count ++;

    /* note: the list may grow (but not shrink) during the call */
    for (int i = 0; i < list.items.len (); i ++)
    {
        /* copy locally to prevent race condition */
        TimerItem item = list.items[i];

        if (item.func)
        {
            pthread_mutex_unlock (& mutex);
            item.func (item.data);
            pthread_mutex_lock (& mutex);
        }
    }

    list.use_count --;
    list.check_stop ();

    pthread_mutex_unlock (& mutex);
}

EXPORT void timer_add (TimerRate rate, TimerFunc func, void * data)
{
    auto & list = lists[rate];
    pthread_mutex_lock (& mutex);

    if (! list.contains (func, data))
    {
        list.items.append (func, data);

        if (! list.source.running ())
            list.source.start (rate_to_ms[rate], timer_run, & list);
    }

    pthread_mutex_unlock (& mutex);
}

EXPORT void timer_remove (TimerRate rate, TimerFunc func, void * data)
{
    auto & list = lists[rate];
    pthread_mutex_lock (& mutex);

    for (TimerItem & item : list.items)
    {
        if (item.func == func && (! data || item.data == data))
            item.func = nullptr;
    }

    list.check_stop ();

    pthread_mutex_unlock (& mutex);
}

void timer_cleanup ()
{
    pthread_mutex_lock (& mutex);

    int timers_running = 0;
    for (TimerList & list : lists)
        timers_running += list.items.len ();

    if (timers_running)
        AUDWARN ("%d timers still registered at exit\n", timers_running);

    pthread_mutex_unlock (& mutex);
}