Skip to content

Commit 420125d

Browse files
committedJun 27, 2015
Remove busy polling inside minimap thread
1 parent 36163d9 commit 420125d

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed
 

Diff for: ‎src/minimap.cpp

+32-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#include "minimap.h"
2121
#include "logoutputbuffer.h"
2222
#include "jthread/jmutexautolock.h"
23+
#include "jthread/jsemaphore.h"
2324
#include "clientmap.h"
2425
#include "settings.h"
2526
#include "nodedef.h"
@@ -47,16 +48,15 @@ MinimapUpdateQueue::~MinimapUpdateQueue()
4748
{
4849
JMutexAutoLock lock(m_mutex);
4950

50-
for (std::vector<QueuedMinimapUpdate*>::iterator
51+
for (std::list<QueuedMinimapUpdate*>::iterator
5152
i = m_queue.begin();
52-
i != m_queue.end(); i++)
53-
{
53+
i != m_queue.end(); ++i) {
5454
QueuedMinimapUpdate *q = *i;
5555
delete q;
5656
}
5757
}
5858

59-
void MinimapUpdateQueue::addBlock(v3s16 pos, MinimapMapblock *data)
59+
bool MinimapUpdateQueue::addBlock(v3s16 pos, MinimapMapblock *data)
6060
{
6161
DSTACK(__FUNCTION_NAME);
6262

@@ -66,15 +66,14 @@ void MinimapUpdateQueue::addBlock(v3s16 pos, MinimapMapblock *data)
6666
Find if block is already in queue.
6767
If it is, update the data and quit.
6868
*/
69-
for (std::vector<QueuedMinimapUpdate*>::iterator
69+
for (std::list<QueuedMinimapUpdate*>::iterator
7070
i = m_queue.begin();
71-
i != m_queue.end(); i++)
72-
{
71+
i != m_queue.end(); ++i) {
7372
QueuedMinimapUpdate *q = *i;
7473
if (q->pos == pos) {
7574
delete q->data;
7675
q->data = data;
77-
return;
76+
return false;
7877
}
7978
}
8079

@@ -85,16 +84,16 @@ void MinimapUpdateQueue::addBlock(v3s16 pos, MinimapMapblock *data)
8584
q->pos = pos;
8685
q->data = data;
8786
m_queue.push_back(q);
87+
return true;
8888
}
8989

9090
QueuedMinimapUpdate * MinimapUpdateQueue::pop()
9191
{
9292
JMutexAutoLock lock(m_mutex);
9393

94-
for (std::vector<QueuedMinimapUpdate*>::iterator
94+
for (std::list<QueuedMinimapUpdate*>::iterator
9595
i = m_queue.begin();
96-
i != m_queue.end(); i++)
97-
{
96+
i != m_queue.end(); i++) {
9897
QueuedMinimapUpdate *q = *i;
9998
m_queue.erase(i);
10099
return q;
@@ -106,6 +105,22 @@ QueuedMinimapUpdate * MinimapUpdateQueue::pop()
106105
Minimap update thread
107106
*/
108107

108+
void MinimapUpdateThread::Stop()
109+
{
110+
JThread::Stop();
111+
112+
// give us a nudge
113+
m_queue_sem.Post();
114+
}
115+
116+
void MinimapUpdateThread::enqueue_Block(v3s16 pos, MinimapMapblock *data)
117+
{
118+
if (m_queue.addBlock(pos, data))
119+
// we had to allocate a new block
120+
m_queue_sem.Post();
121+
}
122+
123+
109124
void *MinimapUpdateThread::Thread()
110125
{
111126
ThreadStarted();
@@ -120,8 +135,13 @@ void *MinimapUpdateThread::Thread()
120135

121136
while (!StopRequested()) {
122137

138+
m_queue_sem.Wait();
139+
if (StopRequested()) break;
140+
123141
while (m_queue.size()) {
124142
QueuedMinimapUpdate *q = m_queue.pop();
143+
if (!q)
144+
break;
125145
std::map<v3s16, MinimapMapblock *>::iterator it;
126146
it = m_blocks_cache.find(q->pos);
127147
if (q->data) {
@@ -138,7 +158,6 @@ void *MinimapUpdateThread::Thread()
138158
data->map_invalidated = false;
139159
}
140160
}
141-
// sleep_ms(10);
142161
}
143162
END_DEBUG_EXCEPTION_HANDLER(errorstream)
144163

@@ -266,7 +285,7 @@ Mapper::~Mapper()
266285

267286
void Mapper::addBlock (v3s16 pos, MinimapMapblock *data)
268287
{
269-
m_minimap_update_thread->m_queue.addBlock(pos, data);
288+
m_minimap_update_thread->enqueue_Block(pos, data);
270289
}
271290

272291
MinimapMode Mapper::getMinimapMode()

Diff for: ‎src/minimap.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#include "client.h"
2525
#include "voxel.h"
2626
#include "jthread/jmutex.h"
27+
#include "jthread/jsemaphore.h"
2728
#include <map>
2829
#include <string>
2930
#include <vector>
@@ -93,8 +94,9 @@ class MinimapUpdateQueue
9394

9495
~MinimapUpdateQueue();
9596

96-
void addBlock(v3s16 pos, MinimapMapblock *data);
97+
bool addBlock(v3s16 pos, MinimapMapblock *data);
9798

99+
// blocking!!
98100
QueuedMinimapUpdate *pop();
99101

100102
u32 size()
@@ -104,13 +106,15 @@ class MinimapUpdateQueue
104106
}
105107

106108
private:
107-
std::vector<QueuedMinimapUpdate*> m_queue;
109+
std::list<QueuedMinimapUpdate*> m_queue;
108110
JMutex m_mutex;
109111
};
110112

111113
class MinimapUpdateThread : public JThread
112114
{
113115
private:
116+
JSemaphore m_queue_sem;
117+
MinimapUpdateQueue m_queue;
114118

115119
public:
116120
MinimapUpdateThread(IrrlichtDevice *device, Client *client)
@@ -124,13 +128,16 @@ class MinimapUpdateThread : public JThread
124128
MinimapPixel *getMinimapPixel (v3s16 pos, s16 height, s16 &pixel_height);
125129
s16 getAirCount (v3s16 pos, s16 height);
126130
video::SColor getColorFromId(u16 id);
131+
132+
void enqueue_Block(v3s16 pos, MinimapMapblock *data);
133+
127134
IrrlichtDevice *device;
128135
Client *client;
129136
video::IVideoDriver *driver;
130137
ITextureSource *tsrc;
138+
void Stop();
131139
void *Thread();
132140
MinimapData *data;
133-
MinimapUpdateQueue m_queue;
134141
std::map<v3s16, MinimapMapblock *> m_blocks_cache;
135142
};
136143

0 commit comments

Comments
 (0)
Please sign in to comment.