Merge branch 'master' into develop
[jack2.git] / common / JackMidiAsyncWaitQueue.cpp
blob016737cbe595e628efc2db8dfd157f3d8b31bcce
1 /*
2 Copyright (C) 2010 Devin Anderson
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <new>
22 #include "JackMidiAsyncWaitQueue.h"
23 #include "JackMidiUtil.h"
24 #include "JackTime.h"
26 using Jack::JackMidiAsyncWaitQueue;
28 JackMidiAsyncWaitQueue::JackMidiAsyncWaitQueue(size_t max_bytes,
29 size_t max_messages):
30 JackMidiAsyncQueue(max_bytes, max_messages)
32 if (semaphore.Allocate("JackMidiAsyncWaitQueue", "midi-thread", 0)) {
33 throw std::bad_alloc();
37 JackMidiAsyncWaitQueue::~JackMidiAsyncWaitQueue()
39 semaphore.Destroy();
42 jack_midi_event_t *
43 JackMidiAsyncWaitQueue::DequeueEvent()
45 return DequeueEvent((long) 0);
48 jack_midi_event_t *
49 JackMidiAsyncWaitQueue::DequeueEvent(jack_nframes_t frame)
52 // XXX: I worry about timer resolution on Solaris and Windows. When the
53 // resolution for the `JackSynchro` object is milliseconds, the worst-case
54 // scenario for processor objects is that the wait time becomes less than a
55 // millisecond, and the processor object continually calls this method,
56 // expecting to wait a certain amount of microseconds, and ends up not
57 // waiting at all each time, essentially busy-waiting until the current
58 // frame is reached. Perhaps there should be a #define that indicates the
59 // wait time resolution for `JackSynchro` objects so that we can wait a
60 // little longer if necessary.
62 jack_time_t frame_time = GetTimeFromFrames(frame);
63 jack_time_t current_time = GetMicroSeconds();
64 return DequeueEvent((frame_time < current_time) ? 0 :
65 (long) (frame_time - current_time));
68 jack_midi_event_t *
69 JackMidiAsyncWaitQueue::DequeueEvent(long usec)
71 return ((usec < 0) ? semaphore.Wait() : semaphore.TimedWait(usec)) ?
72 JackMidiAsyncQueue::DequeueEvent() : 0;
75 Jack::JackMidiWriteQueue::EnqueueResult
76 JackMidiAsyncWaitQueue::EnqueueEvent(jack_nframes_t time, size_t size,
77 jack_midi_data_t *buffer)
79 EnqueueResult result = JackMidiAsyncQueue::EnqueueEvent(time, size,
80 buffer);
81 if (result == OK) {
82 semaphore.Signal();
84 return result;