Revision | 9c9331905a9899c0df8b4c1cac0ee4c4b2916ca4 (tree) |
---|---|
Time | 2014-08-11 17:00:00 |
Author | alucky4416 <alucky4416@user...> |
Commiter | alucky4416 |
change: add mutexlockert to ThSchManager and PlayMusicThread.
@@ -5,6 +5,7 @@ | ||
5 | 5 | |
6 | 6 | PlayMusicThread::PlayMusicThread(WId winId) |
7 | 7 | { |
8 | + QMutexLocker locker(&mutex); | |
8 | 9 | stopped = false; |
9 | 10 | |
10 | 11 | #ifdef __WIN32__ |
@@ -31,28 +32,41 @@ PlayMusicThread::~PlayMusicThread() | ||
31 | 32 | |
32 | 33 | void PlayMusicThread::stop() |
33 | 34 | { |
35 | + QMutexLocker locker(&mutex); | |
34 | 36 | stopped = true; |
35 | 37 | } |
36 | 38 | |
37 | 39 | void PlayMusicThread::PlayMusic(const QString &filepath) { |
38 | 40 | if (state == IDLE) { |
41 | + QMutexLocker locker(&mutex); | |
39 | 42 | FilePath = filepath; |
40 | 43 | } |
41 | 44 | } |
42 | 45 | |
43 | 46 | void PlayMusicThread::StopMusic() { |
44 | 47 | if (state == PLAYING) { |
48 | + QMutexLocker locker(&mutex); | |
45 | 49 | playstopped = true; |
46 | 50 | } |
47 | 51 | } |
48 | 52 | int PlayMusicThread::GetPlayStatus() { |
53 | + QMutexLocker locker(&mutex); | |
49 | 54 | return state; |
50 | 55 | } |
51 | 56 | |
52 | 57 | void PlayMusicThread::run() |
53 | 58 | { |
54 | - while (!stopped) { | |
59 | + | |
60 | + forever { | |
61 | + { | |
62 | + QMutexLocker locker(&mutex); | |
63 | + if (stopped) { | |
64 | + stopped = false; | |
65 | + break; | |
66 | + } | |
67 | + } | |
55 | 68 | if (state == IDLE) { |
69 | + QMutexLocker locker(&mutex); | |
56 | 70 | if (FilePath.isEmpty()) { |
57 | 71 | ; |
58 | 72 | } else { |
@@ -69,6 +83,7 @@ void PlayMusicThread::run() | ||
69 | 83 | emit MusicStarted(PlayFileName); |
70 | 84 | } |
71 | 85 | } else if (state == PLAYING){ |
86 | + QMutexLocker locker(&mutex); | |
72 | 87 | if (!IsMusicPlaying() || playstopped) { |
73 | 88 | // Music Stop |
74 | 89 | MusicStop(); |
@@ -2,8 +2,10 @@ | ||
2 | 2 | #define PLAYMUSICTHREAD_H |
3 | 3 | |
4 | 4 | #include <QWidget> // WId |
5 | -#include <QThread> | |
6 | 5 | #include <QString> |
6 | +#include <QThread> | |
7 | +#include <QMutexLocker> | |
8 | + | |
7 | 9 | |
8 | 10 | #include <bass.h> |
9 | 11 |
@@ -34,6 +36,7 @@ protected: | ||
34 | 36 | int IsMusicPlaying(); |
35 | 37 | |
36 | 38 | private: |
39 | + QMutex mutex; | |
37 | 40 | volatile bool stopped; |
38 | 41 | volatile bool playstopped; |
39 | 42 |
@@ -2,6 +2,7 @@ | ||
2 | 2 | |
3 | 3 | #include <QStringList> |
4 | 4 | #include <QDateTime> |
5 | +#include <QMutexLocker> | |
5 | 6 | |
6 | 7 | #include <QDebug> |
7 | 8 |
@@ -20,23 +21,39 @@ ThSchManager::~ThSchManager() | ||
20 | 21 | |
21 | 22 | void ThSchManager::stop() |
22 | 23 | { |
24 | + QMutexLocker locker(&mutex); | |
23 | 25 | stopped = true; |
24 | 26 | } |
25 | 27 | |
26 | 28 | void ThSchManager::run() |
27 | 29 | { |
30 | + bool EventEmpty; | |
28 | 31 | EventData event; |
29 | 32 | int event_id; |
30 | 33 | int status = 0; // 0 = idle, 1 = exec |
31 | 34 | int index; |
32 | 35 | QString nexttime; |
33 | 36 | |
34 | - while (!stopped) { | |
35 | - if (EvtQue->isEmpty()) { | |
37 | + forever { | |
38 | + { | |
39 | + QMutexLocker locker(&mutex); | |
40 | + if (stopped) { | |
41 | + stopped = false; | |
42 | + break; | |
43 | + } else { | |
44 | + if (EventEmpty = EvtQue->isEmpty()) { | |
45 | + ; | |
46 | + } else{ | |
47 | + event = EvtQue->dequeue(); | |
48 | + } | |
49 | + } | |
50 | + } | |
51 | + if (EventEmpty) { | |
36 | 52 | if (status == 1) { // EXEC (supervise sch time) |
53 | + QMutexLocker locker(&mutex); | |
37 | 54 | // Is Saturday and Sunday |
38 | 55 | if ((ExcludeSS && isSaturdayOrSunday()) || (ExcludeHolidays && isHolidays())) { |
39 | - sleep(1); // skip | |
56 | + ; // skip | |
40 | 57 | } else { |
41 | 58 | // Time Check |
42 | 59 | if (isMatchSchTimeVsCurrentTime(&nexttime)) { |
@@ -45,50 +62,48 @@ void ThSchManager::run() | ||
45 | 62 | ThPlayMusic->PlayMusic(ChimeFilepath); |
46 | 63 | |
47 | 64 | // next |
48 | - if ((index + 1) < SchTimeList.count()) | |
65 | + if ((index + 1) < SchTimeList.count()) { | |
49 | 66 | index++; |
50 | - else | |
67 | + } else { | |
51 | 68 | index = 0; |
69 | + } | |
52 | 70 | nexttime = SchTimeList.at(index); |
53 | 71 | emit sig_notify_nexttime(nexttime); |
54 | 72 | // qDebug() << "### next time : " << nexttime; |
55 | - } else { | |
56 | - sleep(1); | |
57 | 73 | } |
58 | 74 | } |
59 | - } else { | |
60 | - sleep(1); | |
61 | 75 | } |
76 | + sleep(1); | |
62 | 77 | } else { |
63 | - event = EvtQue->dequeue(); | |
64 | - event_id = event.id; | |
65 | - switch(event_id) { | |
66 | - case Ev_Start: | |
67 | - if (status == 0) { // idle | |
68 | - // Start; | |
69 | - // qDebug() << "### Start!"; | |
70 | - index = nextSchTimeIndex(&SchTimeList); | |
71 | - nexttime = SchTimeList.at(index); | |
72 | - emit sig_notify_nexttime(nexttime); | |
73 | - // qDebug() << "### next time : " << nexttime; | |
74 | - status = 1; // goto EXEC | |
75 | - } | |
76 | - break; | |
77 | - case Ev_Stop: | |
78 | - if (status == 1) { | |
79 | - // Stop; | |
80 | - emit sig_notify_nexttime("idle..."); | |
81 | - // qDebug() << "### Stop!"; | |
82 | - status = 0; // goto IDLE | |
78 | + // event = EvtQue->dequeue(); | |
79 | + event_id = event.id; | |
80 | + switch(event_id) { | |
81 | + case Ev_Start: | |
82 | + if (status == 0) { // idle | |
83 | + QMutexLocker locker(&mutex); | |
84 | + // Start; | |
85 | + // qDebug() << "### Start!"; | |
86 | + index = nextSchTimeIndex(&SchTimeList); | |
87 | + nexttime = SchTimeList.at(index); | |
88 | + emit sig_notify_nexttime(nexttime); | |
89 | + // qDebug() << "### next time : " << nexttime; | |
90 | + status = 1; // goto EXEC | |
91 | + } | |
92 | + break; | |
93 | + case Ev_Stop: | |
94 | + if (status == 1) { | |
95 | + QMutexLocker locker(&mutex); | |
96 | + // Stop; | |
97 | + emit sig_notify_nexttime("idle..."); | |
98 | + // qDebug() << "### Stop!"; | |
99 | + status = 0; // goto IDLE | |
100 | + } | |
101 | + break; | |
102 | + default: | |
103 | + break; | |
83 | 104 | } |
84 | - break; | |
85 | - default: | |
86 | - break; | |
87 | - } | |
88 | - } | |
89 | - | |
90 | - } // while(!stopped) | |
91 | - | |
105 | + } // if(EventEmpty) | |
106 | + } // forever | |
92 | 107 | } |
93 | 108 | |
94 | 109 | void ThSchManager::sendEvent_Start() |
@@ -96,6 +111,7 @@ void ThSchManager::sendEvent_Start() | ||
96 | 111 | EventData evt; |
97 | 112 | |
98 | 113 | evt.id = Ev_Start; |
114 | + QMutexLocker locker(&mutex); | |
99 | 115 | EvtQue->enqueue(evt); |
100 | 116 | } |
101 | 117 |
@@ -104,6 +120,7 @@ void ThSchManager::sendEvent_Stop() | ||
104 | 120 | EventData evt; |
105 | 121 | |
106 | 122 | evt.id = Ev_Stop; |
123 | + QMutexLocker locker(&mutex); | |
107 | 124 | EvtQue->enqueue(evt); |
108 | 125 | } |
109 | 126 |
@@ -121,6 +138,7 @@ void ThSchManager::setSchTimeList(QStringList list) | ||
121 | 138 | { |
122 | 139 | if (list.isEmpty()) return; |
123 | 140 | |
141 | + QMutexLocker locker(&mutex); | |
124 | 142 | SchTimeList.clear(); |
125 | 143 | SchTimeList = list; |
126 | 144 | // qDebug() << "ThSchManager::SchTimeList:" << SchTimeList; |
@@ -130,6 +148,7 @@ void ThSchManager::setHolidaysList(QStringList list) | ||
130 | 148 | { |
131 | 149 | if (list.isEmpty()) return; |
132 | 150 | |
151 | + QMutexLocker locker(&mutex); | |
133 | 152 | HolidaysList.clear(); |
134 | 153 | HolidaysList = list; |
135 | 154 | // qDebug() << "ThSchManager::HolidaysList:" << HolidaysList; |
@@ -137,11 +156,13 @@ void ThSchManager::setHolidaysList(QStringList list) | ||
137 | 156 | |
138 | 157 | void ThSchManager::setExcludeSS(bool flag) |
139 | 158 | { |
159 | + QMutexLocker locker(&mutex); | |
140 | 160 | ExcludeSS = flag; |
141 | 161 | } |
142 | 162 | |
143 | 163 | void ThSchManager::setExcludeHolidays(bool flag) |
144 | 164 | { |
165 | + QMutexLocker locker(&mutex); | |
145 | 166 | ExcludeHolidays = flag; |
146 | 167 | } |
147 | 168 |
@@ -38,6 +38,7 @@ protected: | ||
38 | 38 | void run(); |
39 | 39 | |
40 | 40 | private: |
41 | + QMutex mutex; | |
41 | 42 | volatile bool stopped; |
42 | 43 | |
43 | 44 | // Event ID |