summaryrefslogtreecommitdiff
path: root/src/emerge.cpp
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2015-04-07 06:13:12 -0400
committerShadowNinja <shadowninja@minetest.net>2015-08-23 22:04:06 -0400
commite4bff8be94c0db4f94e63ad448d0eeb869ccdbbd (patch)
tree7935586e79da5c8c7144e345a8c0fc1cda53beed /src/emerge.cpp
parent6a1047d8c116f793890b63427d53f04ceca95d54 (diff)
downloadminetest-e4bff8be94c0db4f94e63ad448d0eeb869ccdbbd.tar.gz
minetest-e4bff8be94c0db4f94e63ad448d0eeb869ccdbbd.tar.bz2
minetest-e4bff8be94c0db4f94e63ad448d0eeb869ccdbbd.zip
Clean up threading
* Rename everything. * Strip J prefix. * Change UpperCamelCase functions to lowerCamelCase. * Remove global (!) semaphore count mutex on OSX. * Remove semaphore count getter (unused, unsafe, depended on internal API functions on Windows, and used a hack on OSX). * Add `Atomic<type>`. * Make `Thread` handle thread names. * Add support for C++11 multi-threading. * Combine pthread and win32 sources. * Remove `ThreadStarted` (unused, unneeded). * Move some includes from the headers to the sources. * Move all of `Event` into its header (allows inlining with no new includes). * Make `Event` use `Semaphore` (except on Windows). * Move some porting functions into `Thread`. * Integrate logging with `Thread`. * Add threading test.
Diffstat (limited to 'src/emerge.cpp')
-rw-r--r--src/emerge.cpp41
1 files changed, 18 insertions, 23 deletions
diff --git a/src/emerge.cpp b/src/emerge.cpp
index d6bda731a..084470783 100644
--- a/src/emerge.cpp
+++ b/src/emerge.cpp
@@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "server.h"
#include <iostream>
#include <queue>
-#include "jthread/jevent.h"
+#include "threading/event.h"
#include "map.h"
#include "environment.h"
#include "util/container.h"
@@ -59,7 +59,7 @@ MapgenDesc reg_mapgens[] = {
{"singlenode", new MapgenFactorySinglenode},
};
-class EmergeThread : public JThread
+class EmergeThread : public Thread
{
public:
Server *m_server;
@@ -73,7 +73,6 @@ public:
std::queue<v3s16> blockqueue;
EmergeThread(Server *server, int ethreadid):
- JThread(),
m_server(server),
map(NULL),
emerge(NULL),
@@ -81,9 +80,10 @@ public:
enable_mapgen_debug_info(false),
id(ethreadid)
{
+ name = "Emerge-" + itos(id);
}
- void *Thread();
+ void *run();
bool popBlockEmerge(v3s16 *pos, u8 *flags);
bool getBlockOrStartGen(v3s16 p, MapBlock **b,
BlockMakeData *data, bool allow_generate);
@@ -112,7 +112,7 @@ EmergeManager::EmergeManager(IGameDef *gamedef)
// some other misc thread
s16 nthreads = 0;
if (!g_settings->getS16NoEx("num_emerge_threads", nthreads))
- nthreads = porting::getNumberOfProcessors() - 2;
+ nthreads = Thread::getNumberOfProcessors() - 2;
if (nthreads < 1)
nthreads = 1;
@@ -141,9 +141,9 @@ EmergeManager::~EmergeManager()
{
for (u32 i = 0; i != emergethread.size(); i++) {
if (threads_active) {
- emergethread[i]->Stop();
+ emergethread[i]->stop();
emergethread[i]->qevent.signal();
- emergethread[i]->Wait();
+ emergethread[i]->wait();
}
delete emergethread[i];
delete mapgen[i];
@@ -196,7 +196,7 @@ void EmergeManager::initMapgens()
Mapgen *EmergeManager::getCurrentMapgen()
{
for (u32 i = 0; i != emergethread.size(); i++) {
- if (emergethread[i]->IsSameThread())
+ if (emergethread[i]->isSameThread())
return emergethread[i]->mapgen;
}
@@ -210,7 +210,7 @@ void EmergeManager::startThreads()
return;
for (u32 i = 0; i != emergethread.size(); i++)
- emergethread[i]->Start();
+ emergethread[i]->start();
threads_active = true;
}
@@ -223,13 +223,13 @@ void EmergeManager::stopThreads()
// Request thread stop in parallel
for (u32 i = 0; i != emergethread.size(); i++) {
- emergethread[i]->Stop();
+ emergethread[i]->stop();
emergethread[i]->qevent.signal();
}
// Then do the waiting for each
for (u32 i = 0; i != emergethread.size(); i++)
- emergethread[i]->Wait();
+ emergethread[i]->wait();
threads_active = false;
}
@@ -247,7 +247,7 @@ bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate
flags |= BLOCK_EMERGE_ALLOWGEN;
{
- JMutexAutoLock queuelock(queuemutex);
+ MutexAutoLock queuelock(queuemutex);
count = blocks_enqueued.size();
if (count >= qlimit_total)
@@ -360,7 +360,7 @@ MapgenSpecificParams *EmergeManager::createMapgenParams(const std::string &mgnam
bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags)
{
std::map<v3s16, BlockEmergeData *>::iterator iter;
- JMutexAutoLock queuelock(emerge->queuemutex);
+ MutexAutoLock queuelock(emerge->queuemutex);
if (blockqueue.empty())
return false;
@@ -390,7 +390,7 @@ bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b,
{
v2s16 p2d(p.X, p.Z);
//envlock: usually takes <=1ms, sometimes 90ms or ~400ms to acquire
- JMutexAutoLock envlock(m_server->m_env_mutex);
+ MutexAutoLock envlock(m_server->m_env_mutex);
// Load sector if it isn't loaded
if (map->getSectorNoGenerateNoEx(p2d) == NULL)
@@ -418,10 +418,8 @@ bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b,
}
-void *EmergeThread::Thread()
+void *EmergeThread::run()
{
- ThreadStarted();
- log_register_thread("EmergeThread" + itos(id));
DSTACK(__FUNCTION_NAME);
BEGIN_DEBUG_EXCEPTION_HANDLER
@@ -434,9 +432,7 @@ void *EmergeThread::Thread()
mapgen = emerge->mapgen[id];
enable_mapgen_debug_info = emerge->mapgen_debug_info;
- porting::setThreadName("EmergeThread");
-
- while (!StopRequested())
+ while (!stopRequested())
try {
if (!popBlockEmerge(&p, &flags)) {
qevent.wait();
@@ -471,7 +467,7 @@ void *EmergeThread::Thread()
{
//envlock: usually 0ms, but can take either 30 or 400ms to acquire
- JMutexAutoLock envlock(m_server->m_env_mutex);
+ MutexAutoLock envlock(m_server->m_env_mutex);
ScopeProfiler sp(g_profiler, "EmergeThread: after "
"Mapgen::makeChunk (envlock)", SPT_AVG);
@@ -538,7 +534,7 @@ void *EmergeThread::Thread()
}
{
- JMutexAutoLock queuelock(emerge->queuemutex);
+ MutexAutoLock queuelock(emerge->queuemutex);
while (!blockqueue.empty())
{
v3s16 p = blockqueue.front();
@@ -555,6 +551,5 @@ void *EmergeThread::Thread()
}
END_DEBUG_EXCEPTION_HANDLER(errorstream)
- log_deregister_thread();
return NULL;
}