-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessagequeuewriter.cpp
69 lines (58 loc) · 1.43 KB
/
messagequeuewriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "messagequeuewriter.h"
#include "imessagequeue.h"
#include "logging.h"
#include <QDateTime>
#include <QAtomicInt>
#include <QMutex>
MessageType MessageQueueWriter::generate(quint64 threadId, int& priority)
{
static QAtomicInt INDEX = 0;
priority = qrand() % 10;
return QString("index: %1; priority: %2; thread: %3")
.arg(++INDEX)
.arg(priority)
.arg(threadId).toStdString();
}
MessageQueueWriter::MessageQueueWriter(IMessageQueue& queue)
: mQueue(queue)
, mLwmOrStopped()
{
LOG;
queue.set_events(*this);
}
MessageQueueWriter::~MessageQueueWriter()
{
LOG;
}
void MessageQueueWriter::run()
{
LOG;
QMutex mutex;
qsrand(QTime::currentTime().msecsSinceStartOfDay());
const quint64 threadId = (quint64)QThread::currentThreadId();
int priority = 0;
RetCodes result = this->mQueue.put(generate(threadId, priority), priority);
while (result != STOPPED) {
switch (result) {
case HWM:
case NO_SPACE:
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
this->mLwmOrStopped.wait(&mutex);
break;
}
default:
break;
}
result = this->mQueue.put(generate(threadId, priority), priority);
}
}
void MessageQueueWriter::on_lwm()
{
this->mLwmOrStopped.wakeAll();
}
void MessageQueueWriter::on_stop()
{
this->mLwmOrStopped.wakeAll();
}