mirror of
https://github.com/mumble-voip/mumble.git
synced 2025-10-26 11:19:16 +00:00
DeferInit -- avoids thread races for global initializers
git-svn-id: https://mumble.svn.sourceforge.net/svnroot/mumble/trunk@865 05730e5d-ab1b-0410-a4ac-84af385074fa
This commit is contained in:
parent
d47fa08d7c
commit
4447a28d95
@ -45,7 +45,15 @@ class ALSAEnumerator {
|
||||
QHash<QString,QString> qhOutput;
|
||||
ALSAEnumerator();
|
||||
};
|
||||
static ALSAEnumerator cards;
|
||||
|
||||
static ALSAEnumerator *cards = NULL;
|
||||
|
||||
class ALSAAudioInit : public DeferInit {
|
||||
void initialize() { cards = new ALSAEnumerator(); };
|
||||
void destroy() { delete cards; cards = NULL; };
|
||||
};
|
||||
|
||||
static ALSAAudioInit aai;
|
||||
|
||||
class ALSAAudioInputRegistrar : public AudioInputRegistrar {
|
||||
public:
|
||||
@ -77,7 +85,7 @@ AudioInput *ALSAAudioInputRegistrar::create() {
|
||||
const QList<audioDevice> ALSAAudioInputRegistrar::getDeviceChoices() {
|
||||
QList<audioDevice> qlReturn;
|
||||
|
||||
QStringList qlInputDevs = cards.qhInput.keys();
|
||||
QStringList qlInputDevs = cards->qhInput.keys();
|
||||
qSort(qlInputDevs);
|
||||
|
||||
if (qlInputDevs.contains(g.s.qsALSAInput)) {
|
||||
@ -86,7 +94,7 @@ const QList<audioDevice> ALSAAudioInputRegistrar::getDeviceChoices() {
|
||||
}
|
||||
|
||||
foreach(const QString &dev, qlInputDevs) {
|
||||
QString t=QString::fromLatin1("[%1] %2").arg(dev).arg(cards.qhInput[dev]);
|
||||
QString t=QString::fromLatin1("[%1] %2").arg(dev).arg(cards->qhInput[dev]);
|
||||
qlReturn << audioDevice(t, dev);
|
||||
}
|
||||
|
||||
@ -108,7 +116,7 @@ AudioOutput *ALSAAudioOutputRegistrar::create() {
|
||||
const QList<audioDevice> ALSAAudioOutputRegistrar::getDeviceChoices() {
|
||||
QList<audioDevice> qlReturn;
|
||||
|
||||
QStringList qlOutputDevs = cards.qhOutput.keys();
|
||||
QStringList qlOutputDevs = cards->qhOutput.keys();
|
||||
qSort(qlOutputDevs);
|
||||
|
||||
if (qlOutputDevs.contains(g.s.qsALSAOutput)) {
|
||||
@ -117,7 +125,7 @@ const QList<audioDevice> ALSAAudioOutputRegistrar::getDeviceChoices() {
|
||||
}
|
||||
|
||||
foreach(const QString &dev, qlOutputDevs) {
|
||||
QString t=QString::fromLatin1("[%1] %2").arg(dev).arg(cards.qhInput[dev]);
|
||||
QString t=QString::fromLatin1("[%1] %2").arg(dev).arg(cards->qhInput[dev]);
|
||||
qlReturn << audioDevice(t, dev);
|
||||
}
|
||||
|
||||
@ -196,16 +204,16 @@ ALSAEnumerator::ALSAEnumerator() {
|
||||
ALSAConfig::ALSAConfig(Settings &st) : ConfigWidget(st) {
|
||||
setupUi(this);
|
||||
|
||||
QList<QString> qlOutputDevs = cards.qhOutput.keys();
|
||||
QList<QString> qlOutputDevs = cards->qhOutput.keys();
|
||||
qSort(qlOutputDevs);
|
||||
QList<QString> qlInputDevs = cards.qhInput.keys();
|
||||
QList<QString> qlInputDevs = cards->qhInput.keys();
|
||||
qSort(qlInputDevs);
|
||||
|
||||
bool found;
|
||||
|
||||
found = false;
|
||||
foreach(QString dev, qlInputDevs) {
|
||||
QString t=QString::fromLatin1("[%1] %2").arg(dev).arg(cards.qhInput[dev]);
|
||||
QString t=QString::fromLatin1("[%1] %2").arg(dev).arg(cards->qhInput[dev]);
|
||||
qcbInputDevice->addItem(t, dev);
|
||||
if (dev == g.s.qsALSAInput) {
|
||||
found = true;
|
||||
@ -219,7 +227,7 @@ ALSAConfig::ALSAConfig(Settings &st) : ConfigWidget(st) {
|
||||
|
||||
found = false;
|
||||
foreach(QString dev, qlOutputDevs) {
|
||||
QString t=QString::fromLatin1("[%1] %2").arg(dev).arg(cards.qhOutput[dev]);
|
||||
QString t=QString::fromLatin1("[%1] %2").arg(dev).arg(cards->qhOutput[dev]);
|
||||
qcbOutputDevice->addItem(t, dev);
|
||||
if (dev == g.s.qsALSAOutput) {
|
||||
found = true;
|
||||
|
||||
@ -44,3 +44,31 @@ Global::Global() {
|
||||
bPushToMute = false;
|
||||
bCenterPosition = false;
|
||||
}
|
||||
|
||||
QMultiMap<int, DeferInit *> *DeferInit::qmDeferers = NULL;
|
||||
|
||||
void DeferInit::add(int priority) {
|
||||
if (qmDeferers == NULL) {
|
||||
qmDeferers = new QMultiMap<int, DeferInit *>();
|
||||
}
|
||||
qmDeferers->insert(priority, this);
|
||||
}
|
||||
|
||||
DeferInit::~DeferInit() {
|
||||
}
|
||||
|
||||
void DeferInit::run_initializers() {
|
||||
if (! qmDeferers)
|
||||
return;
|
||||
foreach(DeferInit *d, *qmDeferers) {
|
||||
d->initialize();
|
||||
}
|
||||
}
|
||||
|
||||
void DeferInit::run_destroyers() {
|
||||
if (! qmDeferers)
|
||||
return;
|
||||
foreach(DeferInit *d, *qmDeferers) {
|
||||
d->destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,6 +84,25 @@ struct Global {
|
||||
Global();
|
||||
};
|
||||
|
||||
// Class to handle ordered initialization of globals.
|
||||
// This allows the same link-time magic as used everywhere else
|
||||
// for globals that need an init before the GUI starts, but
|
||||
// after we reach main().
|
||||
|
||||
class DeferInit {
|
||||
protected:
|
||||
static QMultiMap<int, DeferInit *> *qmDeferers;
|
||||
void add(int priority);
|
||||
public:
|
||||
DeferInit(int priority) { add(priority); };
|
||||
DeferInit() { add(0); };
|
||||
virtual ~DeferInit();
|
||||
virtual void initialize() { };
|
||||
virtual void destroy() { };
|
||||
static void run_initializers();
|
||||
static void run_destroyers();
|
||||
};
|
||||
|
||||
// -Wshadow is bugged. If an inline function of a class uses a variable or
|
||||
// parameter named 'g', that will generate a warning even if the class header
|
||||
// is included long before this definition.
|
||||
|
||||
@ -50,7 +50,14 @@ class OSSEnumerator {
|
||||
OSSEnumerator();
|
||||
};
|
||||
|
||||
static OSSEnumerator cards;
|
||||
static OSSEnumerator *cards = NULL;
|
||||
|
||||
class OSSInit : public DeferInit {
|
||||
void initialize() { cards = new OSSEnumerator(); };
|
||||
void destroy() { delete cards; cards = NULL; };
|
||||
};
|
||||
|
||||
static OSSInit ossi;
|
||||
|
||||
class OSSInputRegistrar : public AudioInputRegistrar {
|
||||
public:
|
||||
@ -82,7 +89,7 @@ AudioInput *OSSInputRegistrar::create() {
|
||||
const QList<audioDevice> OSSInputRegistrar::getDeviceChoices() {
|
||||
QList<audioDevice> qlReturn;
|
||||
|
||||
QStringList qlInputDevs = cards.qhInput.keys();
|
||||
QStringList qlInputDevs = cards->qhInput.keys();
|
||||
qSort(qlInputDevs);
|
||||
|
||||
if (qlInputDevs.contains(g.s.qsOSSInput)) {
|
||||
@ -91,7 +98,7 @@ const QList<audioDevice> OSSInputRegistrar::getDeviceChoices() {
|
||||
}
|
||||
|
||||
foreach(const QString &dev, qlInputDevs) {
|
||||
qlReturn << audioDevice(cards.qhInput.value(dev), dev);
|
||||
qlReturn << audioDevice(cards->qhInput.value(dev), dev);
|
||||
}
|
||||
|
||||
return qlReturn;
|
||||
@ -111,7 +118,7 @@ AudioOutput *OSSOutputRegistrar::create() {
|
||||
const QList<audioDevice> OSSOutputRegistrar::getDeviceChoices() {
|
||||
QList<audioDevice> qlReturn;
|
||||
|
||||
QStringList qlOutputDevs = cards.qhOutput.keys();
|
||||
QStringList qlOutputDevs = cards->qhOutput.keys();
|
||||
qSort(qlOutputDevs);
|
||||
|
||||
if (qlOutputDevs.contains(g.s.qsOSSOutput)) {
|
||||
@ -120,7 +127,7 @@ const QList<audioDevice> OSSOutputRegistrar::getDeviceChoices() {
|
||||
}
|
||||
|
||||
foreach(const QString &dev, qlOutputDevs) {
|
||||
qlReturn << audioDevice(cards.qhOutput.value(dev), dev);
|
||||
qlReturn << audioDevice(cards->qhOutput.value(dev), dev);
|
||||
}
|
||||
|
||||
return qlReturn;
|
||||
@ -185,16 +192,16 @@ OSSEnumerator::OSSEnumerator() {
|
||||
OSSConfig::OSSConfig(Settings &st) : ConfigWidget(st) {
|
||||
setupUi(this);
|
||||
|
||||
QList<QString> qlOutputDevs = cards.qhOutput.keys();
|
||||
QList<QString> qlOutputDevs = cards->qhOutput.keys();
|
||||
qSort(qlOutputDevs);
|
||||
QList<QString> qlInputDevs = cards.qhInput.keys();
|
||||
QList<QString> qlInputDevs = cards->qhInput.keys();
|
||||
qSort(qlInputDevs);
|
||||
|
||||
bool found;
|
||||
|
||||
found = false;
|
||||
foreach(QString dev, qlInputDevs) {
|
||||
qcbInputDevice->addItem(cards.qhInput.value(dev), dev);
|
||||
qcbInputDevice->addItem(cards->qhInput.value(dev), dev);
|
||||
if (dev == g.s.qsOSSInput) {
|
||||
found = true;
|
||||
qcbInputDevice->setCurrentIndex(qcbInputDevice->count() - 1);
|
||||
@ -203,7 +210,7 @@ OSSConfig::OSSConfig(Settings &st) : ConfigWidget(st) {
|
||||
|
||||
found = false;
|
||||
foreach(QString dev, qlOutputDevs) {
|
||||
qcbOutputDevice->addItem(cards.qhOutput.value(dev), dev);
|
||||
qcbOutputDevice->addItem(cards->qhOutput.value(dev), dev);
|
||||
if (dev == g.s.qsOSSOutput) {
|
||||
found = true;
|
||||
qcbOutputDevice->setCurrentIndex(qcbOutputDevice->count() - 1);
|
||||
@ -267,10 +274,10 @@ OSSInput::~OSSInput() {
|
||||
void OSSInput::run() {
|
||||
bRunning = true;
|
||||
|
||||
QByteArray device = cards.qhDevices.value(g.s.qsOSSInput).toLatin1();
|
||||
QByteArray device = cards->qhDevices.value(g.s.qsOSSInput).toLatin1();
|
||||
if (device.isEmpty()) {
|
||||
qWarning("OSSInput: Stored device not found, falling back to default");
|
||||
device = cards.qhDevices.value(QString()).toLatin1();
|
||||
device = cards->qhDevices.value(QString()).toLatin1();
|
||||
}
|
||||
|
||||
int fd = open(device.constData(), O_RDONLY, 0);
|
||||
@ -331,10 +338,10 @@ OSSOutput::~OSSOutput() {
|
||||
void OSSOutput::run() {
|
||||
bRunning = true;
|
||||
|
||||
QByteArray device = cards.qhDevices.value(g.s.qsOSSOutput).toLatin1();
|
||||
QByteArray device = cards->qhDevices.value(g.s.qsOSSOutput).toLatin1();
|
||||
if (device.isEmpty()) {
|
||||
qWarning("OSSOutput: Stored device not found, falling back to default");
|
||||
device = cards.qhDevices.value(QString()).toLatin1();
|
||||
device = cards->qhDevices.value(QString()).toLatin1();
|
||||
}
|
||||
|
||||
int fd = open(device.constData(), O_WRONLY, 0);
|
||||
|
||||
@ -38,7 +38,7 @@
|
||||
#include <errno.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
static PulseAudioSystem pasys;
|
||||
static PulseAudioSystem *pasys = NULL;
|
||||
|
||||
static const pa_sample_spec ss = { PA_SAMPLE_S16LE, SAMPLE_RATE, 1 };
|
||||
|
||||
@ -67,6 +67,14 @@ class PulseAudioOutputRegistrar : public AudioOutputRegistrar {
|
||||
static PulseAudioInputRegistrar airPulseAudio;
|
||||
static PulseAudioOutputRegistrar aorPulseAudio;
|
||||
|
||||
class PulseAudioInit : public DeferInit {
|
||||
public:
|
||||
void initialize() { pasys = new PulseAudioSystem(); };
|
||||
void destroy() { delete pasys; pasys = NULL; };
|
||||
};
|
||||
|
||||
static PulseAudioInit pulseinit;
|
||||
|
||||
PulseAudioSystem::PulseAudioSystem() {
|
||||
pasInput = pasOutput = pasSpeaker = NULL;
|
||||
iDelayCache = 0;
|
||||
@ -90,7 +98,6 @@ PulseAudioSystem::PulseAudioSystem() {
|
||||
jbJitter = jitter_buffer_init();
|
||||
int margin = 320;
|
||||
jitter_buffer_ctl(jbJitter, JITTER_BUFFER_SET_MARGIN, &margin);
|
||||
|
||||
start(QThread::TimeCriticalPriority);
|
||||
}
|
||||
|
||||
@ -472,7 +479,7 @@ AudioInput *PulseAudioInputRegistrar::create() {
|
||||
const QList<audioDevice> PulseAudioInputRegistrar::getDeviceChoices() {
|
||||
QList<audioDevice> qlReturn;
|
||||
|
||||
QStringList qlInputDevs = pasys.qhInput.keys();
|
||||
QStringList qlInputDevs = pasys->qhInput.keys();
|
||||
qSort(qlInputDevs);
|
||||
|
||||
if (qlInputDevs.contains(g.s.qsPulseAudioInput)) {
|
||||
@ -481,7 +488,7 @@ const QList<audioDevice> PulseAudioInputRegistrar::getDeviceChoices() {
|
||||
}
|
||||
|
||||
foreach(const QString &dev, qlInputDevs) {
|
||||
qlReturn << audioDevice(pasys.qhInput.value(dev), dev);
|
||||
qlReturn << audioDevice(pasys->qhInput.value(dev), dev);
|
||||
}
|
||||
|
||||
return qlReturn;
|
||||
@ -501,7 +508,7 @@ AudioOutput *PulseAudioOutputRegistrar::create() {
|
||||
const QList<audioDevice> PulseAudioOutputRegistrar::getDeviceChoices() {
|
||||
QList<audioDevice> qlReturn;
|
||||
|
||||
QStringList qlOutputDevs = pasys.qhOutput.keys();
|
||||
QStringList qlOutputDevs = pasys->qhOutput.keys();
|
||||
qSort(qlOutputDevs);
|
||||
|
||||
if (qlOutputDevs.contains(g.s.qsPulseAudioOutput)) {
|
||||
@ -510,7 +517,7 @@ const QList<audioDevice> PulseAudioOutputRegistrar::getDeviceChoices() {
|
||||
}
|
||||
|
||||
foreach(const QString &dev, qlOutputDevs) {
|
||||
qlReturn << audioDevice(pasys.qhOutput.value(dev), dev);
|
||||
qlReturn << audioDevice(pasys->qhOutput.value(dev), dev);
|
||||
}
|
||||
|
||||
return qlReturn;
|
||||
@ -529,17 +536,17 @@ static ConfigRegistrar registrar(22, PulseAudioConfigDialogNew);
|
||||
PulseAudioConfig::PulseAudioConfig(Settings &st) : ConfigWidget(st) {
|
||||
setupUi(this);
|
||||
|
||||
QStringList qlOutputDevs = pasys.qhOutput.keys();
|
||||
QStringList qlOutputDevs = pasys->qhOutput.keys();
|
||||
qSort(qlOutputDevs);
|
||||
QStringList qlInputDevs = pasys.qhInput.keys();
|
||||
QStringList qlInputDevs = pasys->qhInput.keys();
|
||||
qSort(qlInputDevs);
|
||||
|
||||
foreach(QString dev, qlInputDevs) {
|
||||
qcbInputDevice->addItem(pasys.qhInput.value(dev), dev);
|
||||
qcbInputDevice->addItem(pasys->qhInput.value(dev), dev);
|
||||
}
|
||||
|
||||
foreach(QString dev, qlOutputDevs) {
|
||||
qcbOutputDevice->addItem(pasys.qhOutput.value(dev), dev);
|
||||
qcbOutputDevice->addItem(pasys->qhOutput.value(dev), dev);
|
||||
}
|
||||
}
|
||||
|
||||
@ -588,20 +595,24 @@ void PulseAudioConfig::on_qsOutputDelay_valueChanged(int v) {
|
||||
PulseAudioInput::PulseAudioInput() {
|
||||
bRunning = true;
|
||||
bHasSpeaker = g.s.bPulseAudioEcho;
|
||||
pasys.wakeup();
|
||||
if (pasys)
|
||||
pasys->wakeup();
|
||||
};
|
||||
|
||||
PulseAudioInput::~PulseAudioInput() {
|
||||
bRunning = false;
|
||||
pasys.wakeup();
|
||||
if (pasys)
|
||||
pasys->wakeup();
|
||||
}
|
||||
|
||||
PulseAudioOutput::PulseAudioOutput() {
|
||||
bRunning = true;
|
||||
pasys.wakeup();
|
||||
if (pasys)
|
||||
pasys->wakeup();
|
||||
}
|
||||
|
||||
PulseAudioOutput::~PulseAudioOutput() {
|
||||
bRunning = false;
|
||||
pasys.wakeup();
|
||||
if (pasys)
|
||||
pasys->wakeup();
|
||||
}
|
||||
|
||||
@ -96,6 +96,8 @@ int main(int argc, char **argv) {
|
||||
// Load preferences
|
||||
g.s.load();
|
||||
|
||||
DeferInit::run_initializers();
|
||||
|
||||
if (! g.s.qsStyle.isEmpty()) {
|
||||
a.setStyle(g.s.qsStyle);
|
||||
}
|
||||
@ -210,6 +212,8 @@ int main(int argc, char **argv) {
|
||||
delete g.o;
|
||||
|
||||
delete g.qs;
|
||||
|
||||
DeferInit::run_destroyers();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user