Skip to content

Commit 1daf644

Browse files
committed
Store list of actions in order, except for CANCEL actions
1 parent cb6c5ea commit 1daf644

1 file changed

Lines changed: 35 additions & 15 deletions

File tree

src/rtmixer.c

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,38 @@ void get_stats(PaStreamCallbackFlags flags, struct stats* stats)
4141
if (flags & paOutputOverflow) { stats->output_overflows++; }
4242
}
4343

44+
// CANCEL actions are inserted in the front, others in the back of the list.
45+
// Linked lists of actions are supported.
46+
void get_new_actions(struct state* state)
47+
{
48+
struct action** last_action_addr = &(state->actions);
49+
for (struct action* new_action = NULL
50+
; PaUtil_ReadRingBuffer(state->action_q, &new_action, 1)
51+
;)
52+
{
53+
do
54+
{
55+
struct action* next = new_action->next;
56+
if (new_action->type == CANCEL)
57+
{
58+
new_action->next = state->actions;
59+
state->actions = new_action;
60+
}
61+
else
62+
{
63+
new_action->next = NULL;
64+
while (*last_action_addr)
65+
{
66+
last_action_addr = &((*last_action_addr)->next);
67+
}
68+
*last_action_addr = new_action;
69+
}
70+
new_action = next;
71+
}
72+
while (new_action);
73+
}
74+
}
75+
4476
frame_t seconds2samples(PaTime time, double samplerate)
4577
{
4678
return (frame_t) llround(time * samplerate);
@@ -57,20 +89,7 @@ int callback(const void* input, void* output, frame_t frameCount
5789

5890
get_stats(statusFlags, &(state->stats));
5991

60-
for (struct action* action = NULL
61-
; PaUtil_ReadRingBuffer(state->action_q, &action, 1)
62-
;)
63-
{
64-
// Actions are added at the beginning of the list, because CANCEL actions
65-
// must come before the action they are cancelling. Also, it's easier.
66-
struct action* i = action;
67-
while (i->next)
68-
{
69-
i = i->next;
70-
}
71-
i->next = state->actions;
72-
state->actions = action;
73-
}
92+
get_new_actions(state);
7493

7594
struct action** actionaddr = &(state->actions);
7695
while (*actionaddr)
@@ -129,7 +148,8 @@ int callback(const void* input, void* output, frame_t frameCount
129148

130149
if (action->type == CANCEL)
131150
{
132-
// Search the following list items, not the preceding ones!
151+
// Since CANCEL actions are inserted in the beginning,
152+
// we need to search only the following list items
133153
for (struct action** i = &(action->next); *i; i = &((*i)->next))
134154
{
135155
if (*i == action->action)

0 commit comments

Comments
 (0)