1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
4 |
* * |
* * |
5 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
20 |
|
|
21 |
#include "InstrumentManagerThread.h" |
#include "InstrumentManagerThread.h" |
22 |
|
|
23 |
|
#include "../common/global_private.h" |
24 |
|
|
25 |
namespace LinuxSampler { |
namespace LinuxSampler { |
26 |
|
|
27 |
InstrumentManagerThread::InstrumentManagerThread() : Thread(true, false, 0, -4) { |
InstrumentManagerThread::InstrumentManagerThread() : Thread(true, false, 0, -4) { |
|
pQueue = new RingBuffer<command_t,true>(INSTRUMENT_LOADER_QUEUE_SIZE); |
|
28 |
} |
} |
29 |
|
|
30 |
InstrumentManagerThread::~InstrumentManagerThread() { |
InstrumentManagerThread::~InstrumentManagerThread() { |
|
if (pQueue) delete pQueue; |
|
31 |
} |
} |
32 |
|
|
33 |
/** |
/** |
42 |
* @param pEngineChannel - engine channel on which the instrument should be loaded |
* @param pEngineChannel - engine channel on which the instrument should be loaded |
43 |
*/ |
*/ |
44 |
void InstrumentManagerThread::StartNewLoad(String Filename, uint uiInstrumentIndex, EngineChannel* pEngineChannel) { |
void InstrumentManagerThread::StartNewLoad(String Filename, uint uiInstrumentIndex, EngineChannel* pEngineChannel) { |
45 |
dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).",Filename.c_str(),uiInstrumentIndex)); |
dmsg(1,("Scheduling '%s' (Index=%d) to be loaded in background (if not loaded yet).\n",Filename.c_str(),uiInstrumentIndex)); |
46 |
// already tell the engine which instrument to load |
// already tell the engine which instrument to load |
47 |
pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrumentIndex); |
pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrumentIndex); |
48 |
|
|
49 |
command_t cmd; |
command_t cmd; |
50 |
cmd.type = command_t::DIRECT_LOAD; |
cmd.type = command_t::DIRECT_LOAD; |
51 |
cmd.pEngineChannel = pEngineChannel; |
cmd.pEngineChannel = pEngineChannel; |
52 |
pQueue->push(&cmd); |
|
53 |
|
mutex.Lock(); |
54 |
|
queue.push_back(cmd); |
55 |
|
mutex.Unlock(); |
56 |
|
|
57 |
StartThread(); // ensure thread is running |
StartThread(); // ensure thread is running |
58 |
conditionJobsLeft.Set(true); // wake up thread |
conditionJobsLeft.Set(true); // wake up thread |
59 |
} |
} |
76 |
cmd.pManager = pManager; |
cmd.pManager = pManager; |
77 |
cmd.instrumentId = ID; |
cmd.instrumentId = ID; |
78 |
cmd.mode = Mode; |
cmd.mode = Mode; |
79 |
pQueue->push(&cmd); |
|
80 |
|
mutex.Lock(); |
81 |
|
queue.push_back(cmd); |
82 |
|
mutex.Unlock(); |
83 |
|
|
84 |
StartThread(); // ensure thread is running |
StartThread(); // ensure thread is running |
85 |
conditionJobsLeft.Set(true); // wake up thread |
conditionJobsLeft.Set(true); // wake up thread |
86 |
} |
} |
88 |
// Entry point for the task thread. |
// Entry point for the task thread. |
89 |
int InstrumentManagerThread::Main() { |
int InstrumentManagerThread::Main() { |
90 |
while (true) { |
while (true) { |
91 |
while (pQueue->read_space()) { |
while (!queue.empty()) { |
92 |
command_t cmd; |
command_t cmd; |
93 |
pQueue->pop(&cmd); |
|
94 |
|
// grab a new command from the queue |
95 |
|
mutex.Lock(); |
96 |
|
cmd = queue.front(); |
97 |
|
mutex.Unlock(); |
98 |
|
|
99 |
try { |
try { |
100 |
switch (cmd.type) { |
switch (cmd.type) { |
101 |
case command_t::DIRECT_LOAD: |
case command_t::DIRECT_LOAD: |
107 |
default: |
default: |
108 |
std::cerr << "InstrumentManagerThread: unknown command - BUG!\n" << std::flush; |
std::cerr << "InstrumentManagerThread: unknown command - BUG!\n" << std::flush; |
109 |
} |
} |
110 |
} |
} catch (Exception e) { |
|
catch (Exception e) { |
|
111 |
e.PrintMessage(); |
e.PrintMessage(); |
112 |
|
} catch (...) { |
113 |
|
std::cerr << "InstrumentManagerThread: some exception occured, could not finish task\n" << std::flush; |
114 |
} |
} |
115 |
|
|
116 |
|
// remove processed command from queue |
117 |
|
mutex.Lock(); |
118 |
|
queue.pop_front(); |
119 |
|
mutex.Unlock(); |
120 |
} |
} |
121 |
|
|
122 |
// nothing left to do, sleep until new jobs arrive |
// nothing left to do, sleep until new jobs arrive |