Jack2  1.9.9
JackMidiAsyncWaitQueue.cpp
1 /*
2 Copyright (C) 2010 Devin Anderson
3 
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.
8 
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.
13 
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.
17 
18 */
19 
20 #include <new>
21 
22 #include "JackMidiAsyncWaitQueue.h"
23 #include "JackMidiUtil.h"
24 #include "JackTime.h"
25 
27 
28 JackMidiAsyncWaitQueue::JackMidiAsyncWaitQueue(size_t max_bytes,
29  size_t max_messages):
30  JackMidiAsyncQueue(max_bytes, max_messages)
31 {
32  if (semaphore.Allocate("JackMidiAsyncWaitQueue", "midi-thread", 0)) {
33  throw std::bad_alloc();
34  }
35 }
36 
37 JackMidiAsyncWaitQueue::~JackMidiAsyncWaitQueue()
38 {
39  semaphore.Destroy();
40 }
41 
44 {
45  return DequeueEvent((long) 0);
46 }
47 
50 {
51 
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.
61 
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));
66 }
67 
70 {
71  return ((usec < 0) ? semaphore.Wait() : semaphore.TimedWait(usec)) ?
73 }
74 
75 Jack::JackMidiWriteQueue::EnqueueResult
76 JackMidiAsyncWaitQueue::EnqueueEvent(jack_nframes_t time, size_t size,
77  jack_midi_data_t *buffer)
78 {
79  EnqueueResult result = JackMidiAsyncQueue::EnqueueEvent(time, size,
80  buffer);
81  if (result == OK) {
82  semaphore.Signal();
83  }
84  return result;
85 }
EnqueueResult EnqueueEvent(jack_nframes_t time, size_t size, jack_midi_data_t *buffer)
virtual jack_midi_event_t * DequeueEvent()
virtual EnqueueResult EnqueueEvent(jack_nframes_t time, size_t size, jack_midi_data_t *buffer)