![]() |
Mixxx
|
00001 /*************************************************************************** 00002 controlobjectthread.cpp - description 00003 ------------------- 00004 begin : Thu Sep 23 2004 00005 copyright : (C) 2004 by Tue Haste Andersen 00006 email : haste@diku.dk 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include <qapplication.h> 00019 #include "controlobjectthread.h" 00020 #include "controlobject.h" 00021 00022 00023 QWaitCondition ControlObjectThread::m_sqWait; 00024 QMutex ControlObjectThread::m_sqMutex; 00025 QQueue<ControlObjectThread*> ControlObjectThread::m_sqQueue; 00026 00027 ControlObjectThread::ControlObjectThread(ControlObject* pControlObject, QObject* pParent) 00028 : QObject(pParent), 00029 m_dValue(0.0) 00030 , m_pControlObject(pControlObject) { 00031 // Update associated ControlObject 00032 if (m_pControlObject == NULL) return; 00033 Q_ASSERT(m_pControlObject); 00034 m_pControlObject->addProxy(this); 00035 00036 connect(m_pControlObject, SIGNAL(destroyed()), this, SLOT(slotParentDead())); 00037 00038 // Initialize value 00039 m_dValue = m_pControlObject->get(); 00040 emitValueChanged(); 00041 } 00042 00043 ControlObjectThread::~ControlObjectThread() 00044 { 00045 if (m_pControlObject) 00046 { 00047 // Our parent is still around, make sure it doesn't send us any more events 00048 m_pControlObject->removeProxy(this); 00049 } 00050 } 00051 00052 double ControlObjectThread::get() 00053 { 00054 m_dataMutex.lock(); 00055 double v = m_dValue; 00056 m_dataMutex.unlock(); 00057 00058 return v; 00059 } 00060 00061 void ControlObjectThread::slotSet(double v) 00062 { 00063 m_dataMutex.lock(); 00064 m_dValue = v; 00065 m_dataMutex.unlock(); 00066 00067 updateControlObject(); 00068 } 00069 00070 bool ControlObjectThread::setExtern(double v) 00071 { 00072 bool result = false; 00073 00074 if (m_dataMutex.tryLock()) { 00075 m_dValue = v; 00076 result = true; 00077 m_dataMutex.unlock(); 00078 } 00079 00080 // if ( m_sqMutex.tryLock() ) 00081 // { 00082 // if( m_dataMutex.tryLock() ) 00083 // { 00084 // m_sqQueue.enqueue(this); 00085 // m_dValue = v; 00086 00087 // m_dataMutex.unlock(); 00088 // m_sqMutex.unlock(); 00089 00090 // result = true; 00091 // } 00092 // else 00093 // { 00094 // m_sqMutex.unlock(); 00095 // } 00096 // } 00097 00098 return result; 00099 } 00100 00101 bool ControlObjectThread::update() 00102 { 00103 bool result = false; 00104 00105 m_sqMutex.lock(); 00106 ControlObjectThread* p = NULL; 00107 if (!m_sqQueue.isEmpty()) 00108 p = m_sqQueue.dequeue(); 00109 m_sqMutex.unlock(); 00110 00111 if (p) 00112 { 00113 p->emitValueChanged(); 00114 result = true; 00115 } 00116 00117 return result; 00118 } 00119 00120 void ControlObjectThread::emitValueChanged() 00121 { 00122 emit(valueChanged(get())); 00123 } 00124 00125 void ControlObjectThread::add(double v) 00126 { 00127 m_dataMutex.lock(); 00128 m_dValue += v; 00129 m_dataMutex.unlock(); 00130 00131 updateControlObject(); 00132 } 00133 00134 void ControlObjectThread::sub(double v) 00135 { 00136 m_dataMutex.lock(); 00137 m_dValue -= v; 00138 m_dataMutex.unlock(); 00139 00140 updateControlObject(); 00141 } 00142 00143 void ControlObjectThread::updateControlObject() 00144 { 00145 m_pControlObject->queueFromThread(get(), this); 00146 } 00147 00148 void ControlObjectThread::slotParentDead() 00149 { 00150 // Now we've got a chance of avoiding segfaults with judicious 00151 // use of if(m_pControlObject) 00152 m_pControlObject = 0; 00153 } 00154 00155 ControlObject* ControlObjectThread::getControlObject() 00156 { 00157 return m_pControlObject; 00158 }