1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <pulse/xmalloc.h>
25
26 #include <pulsecore/macro.h>
27
28 #include "hook-list.h"
29
pa_hook_init(pa_hook * hook,void * data)30 void pa_hook_init(pa_hook *hook, void *data) {
31 pa_assert(hook);
32
33 PA_LLIST_HEAD_INIT(pa_hook_slot, hook->slots);
34 hook->n_dead = hook->n_firing = 0;
35 hook->data = data;
36 }
37
slot_free(pa_hook * hook,pa_hook_slot * slot)38 static void slot_free(pa_hook *hook, pa_hook_slot *slot) {
39 pa_assert(hook);
40 pa_assert(slot);
41
42 PA_LLIST_REMOVE(pa_hook_slot, hook->slots, slot);
43
44 pa_xfree(slot);
45 }
46
pa_hook_done(pa_hook * hook)47 void pa_hook_done(pa_hook *hook) {
48 pa_assert(hook);
49 pa_assert(hook->n_firing == 0);
50
51 while (hook->slots)
52 slot_free(hook, hook->slots);
53
54 pa_hook_init(hook, NULL);
55 }
56
pa_hook_connect(pa_hook * hook,pa_hook_priority_t prio,pa_hook_cb_t cb,void * data)57 pa_hook_slot* pa_hook_connect(pa_hook *hook, pa_hook_priority_t prio, pa_hook_cb_t cb, void *data) {
58 pa_hook_slot *slot, *where, *prev;
59
60 pa_assert(cb);
61
62 slot = pa_xnew(pa_hook_slot, 1);
63 slot->hook = hook;
64 slot->dead = false;
65 slot->callback = cb;
66 slot->data = data;
67 slot->priority = prio;
68
69 prev = NULL;
70 for (where = hook->slots; where; where = where->next) {
71 if (prio < where->priority)
72 break;
73 prev = where;
74 }
75
76 PA_LLIST_INSERT_AFTER(pa_hook_slot, hook->slots, prev, slot);
77
78 return slot;
79 }
80
pa_hook_slot_free(pa_hook_slot * slot)81 void pa_hook_slot_free(pa_hook_slot *slot) {
82 pa_assert(slot);
83 pa_assert(!slot->dead);
84
85 if (slot->hook->n_firing > 0) {
86 slot->dead = true;
87 slot->hook->n_dead++;
88 } else
89 slot_free(slot->hook, slot);
90 }
91
pa_hook_fire(pa_hook * hook,void * data)92 pa_hook_result_t pa_hook_fire(pa_hook *hook, void *data) {
93 pa_hook_slot *slot, *next;
94 pa_hook_result_t result = PA_HOOK_OK;
95
96 pa_assert(hook);
97
98 hook->n_firing ++;
99
100 PA_LLIST_FOREACH(slot, hook->slots) {
101 if (slot->dead)
102 continue;
103
104 if ((result = slot->callback(hook->data, data, slot->data)) != PA_HOOK_OK)
105 break;
106 }
107
108 hook->n_firing --;
109 pa_assert(hook->n_firing >= 0);
110
111 for (slot = hook->slots; hook->n_dead > 0 && slot; slot = next) {
112 next = slot->next;
113
114 if (slot->dead) {
115 slot_free(hook, slot);
116 hook->n_dead--;
117 }
118 }
119
120 pa_assert(hook->n_dead == 0);
121
122 return result;
123 }
124
pa_hook_is_firing(pa_hook * hook)125 bool pa_hook_is_firing(pa_hook *hook) {
126 pa_assert(hook);
127
128 return hook->n_firing > 0;
129 }
130