Skip to content

Commit

Permalink
Clean up threading
Browse files Browse the repository at this point in the history
  * 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.
  • Loading branch information
ShadowNinja committed Aug 24, 2015
1 parent 6a1047d commit e4bff8b
Show file tree
Hide file tree
Showing 77 changed files with 1,593 additions and 2,045 deletions.
44 changes: 9 additions & 35 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,52 +36,26 @@ doc/html/
doc/doxygen_*

## Build files
CMakeFiles/*
src/CMakeFiles/*
src/Makefile
src/android_version_githash.h
CMakeFiles
Makefile
!build/android/Makefile
cmake_install.cmake
CMakeCache.txt
CPackConfig.cmake
CPackSourceConfig.cmake
src/android_version.h
src/android_version_githash.h
src/cmake_config.h
src/cmake_config_githash.h
src/cmake_install.cmake
src/script/CMakeFiles/*
src/script/common/CMakeFiles/*
src/script/cpp_api/CMakeFiles/*
src/script/lua_api/CMakeFiles/*
src/util/CMakeFiles/*
src/unittest/CMakeFiles/*
src/jthread/CMakeFiles/*
src/jthread/Makefile
src/jthread/cmake_config.h
src/jthread/cmake_install.cmake
src/jthread/libjthread.a
src/json/libjson.a
src/lua/build/
src/lua/CMakeFiles/
src/cguittfont/CMakeFiles/
src/cguittfont/libcguittfont.a
src/cguittfont/cmake_install.cmake
src/cguittfont/Makefile
src/gmp/CMakeFiles/
src/gmp/libgmp.a
src/json/CMakeFiles/
src/json/libjsoncpp.a
src/sqlite/CMakeFiles/*
src/sqlite/libsqlite3.a
src/client/CMakeFiles/
src/network/CMakeFiles/
CMakeCache.txt
CPackConfig.cmake
CPackSourceConfig.cmake
Makefile
cmake_install.cmake
locale/
.directory
.kdev4/
*.cbp
*.kdev4
*.layout
*.o
*.a

## Android build files
build/android/assets
Expand Down
15 changes: 7 additions & 8 deletions build/android/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,16 @@ LOCAL_SRC_FILES += \
jni/src/lua/src/lzio.c \
jni/src/lua/src/print.c

# sqlite
# SQLite3
LOCAL_SRC_FILES += deps/sqlite/sqlite3.c

# jthread
LOCAL_SRC_FILES += \
jni/src/jthread/pthread/jevent.cpp \
jni/src/jthread/pthread/jmutex.cpp \
jni/src/jthread/pthread/jsemaphore.cpp \
jni/src/jthread/pthread/jthread.cpp
# Threading
LOCAL_SRC_FILES += \
jni/src/threading/Mutex.cpp \
jni/src/threading/Semaphore.cpp \
jni/src/threading/Thread.cpp

# json
# JSONCPP
LOCAL_SRC_FILES += jni/src/json/jsoncpp.cpp

LOCAL_SHARED_LIBRARIES := iconv openal ogg vorbis gmp
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ add_custom_target(GenerateVersion
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")


add_subdirectory(jthread)
add_subdirectory(threading)
add_subdirectory(network)
add_subdirectory(script)
add_subdirectory(unittest)
Expand Down
18 changes: 9 additions & 9 deletions src/ban.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "ban.h"
#include <fstream>
#include "jthread/jmutexautolock.h"
#include "threading/mutex_auto_lock.h"
#include <sstream>
#include <set>
#include "strfnd.h"
Expand Down Expand Up @@ -48,7 +48,7 @@ BanManager::~BanManager()

void BanManager::load()
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
if(is.good() == false)
Expand All @@ -73,7 +73,7 @@ void BanManager::load()

void BanManager::save()
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
infostream << "BanManager: saving to " << m_banfilepath << std::endl;
std::ostringstream ss(std::ios_base::binary);

Expand All @@ -90,13 +90,13 @@ void BanManager::save()

bool BanManager::isIpBanned(const std::string &ip)
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
return m_ips.find(ip) != m_ips.end();
}

std::string BanManager::getBanDescription(const std::string &ip_or_name)
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
std::string s = "";
for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it) {
if (it->first == ip_or_name || it->second == ip_or_name
Expand All @@ -110,7 +110,7 @@ std::string BanManager::getBanDescription(const std::string &ip_or_name)

std::string BanManager::getBanName(const std::string &ip)
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
StringMap::iterator it = m_ips.find(ip);
if (it == m_ips.end())
return "";
Expand All @@ -119,14 +119,14 @@ std::string BanManager::getBanName(const std::string &ip)

void BanManager::add(const std::string &ip, const std::string &name)
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
m_ips[ip] = name;
m_modified = true;
}

void BanManager::remove(const std::string &ip_or_name)
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
m_ips.erase(it++);
Expand All @@ -140,7 +140,7 @@ void BanManager::remove(const std::string &ip_or_name)

bool BanManager::isModified()
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
return m_modified;
}

8 changes: 5 additions & 3 deletions src/ban.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define BAN_HEADER

#include "util/string.h"
#include "jthread/jthread.h"
#include "jthread/jmutex.h"
#include "threading/thread.h"
#include "threading/mutex.h"
#include "exceptions.h"
#include <map>
#include <string>

class BanManager
{
Expand All @@ -40,7 +42,7 @@ class BanManager
void remove(const std::string &ip_or_name);
bool isModified();
private:
JMutex m_mutex;
Mutex m_mutex;
std::string m_banfilepath;
StringMap m_ips;
bool m_modified;
Expand Down
20 changes: 10 additions & 10 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <algorithm>
#include <sstream>
#include <IFileSystem.h>
#include "jthread/jmutexautolock.h"
#include "threading/mutex_auto_lock.h"
#include "util/auth.h"
#include "util/directiontables.h"
#include "util/pointedthing.h"
Expand Down Expand Up @@ -82,7 +82,7 @@ MeshUpdateQueue::MeshUpdateQueue()

MeshUpdateQueue::~MeshUpdateQueue()
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);

for(std::vector<QueuedMeshUpdate*>::iterator
i = m_queue.begin();
Expand All @@ -102,7 +102,7 @@ void MeshUpdateQueue::addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_se

assert(data); // pre-condition

JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);

if(urgent)
m_urgents.insert(p);
Expand Down Expand Up @@ -141,7 +141,7 @@ void MeshUpdateQueue::addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_se
// Returns NULL if queue is empty
QueuedMeshUpdate *MeshUpdateQueue::pop()
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);

bool must_be_urgent = !m_urgents.empty();
for(std::vector<QueuedMeshUpdate*>::iterator
Expand Down Expand Up @@ -269,7 +269,7 @@ Client::Client(
void Client::Stop()
{
//request all client managed threads to stop
m_mesh_update_thread.Stop();
m_mesh_update_thread.stop();
// Save local server map
if (m_localdb) {
infostream << "Local map saving ended." << std::endl;
Expand All @@ -280,7 +280,7 @@ void Client::Stop()
bool Client::isShutdown()
{

if (!m_mesh_update_thread.IsRunning()) return true;
if (!m_mesh_update_thread.isRunning()) return true;

return false;
}
Expand All @@ -289,8 +289,8 @@ Client::~Client()
{
m_con.Disconnect();

m_mesh_update_thread.Stop();
m_mesh_update_thread.Wait();
m_mesh_update_thread.stop();
m_mesh_update_thread.wait();
while (!m_mesh_update_thread.m_queue_out.empty()) {
MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx();
delete r.mesh;
Expand Down Expand Up @@ -1270,7 +1270,7 @@ void Client::sendPlayerPos()

u16 our_peer_id;
{
//JMutexAutoLock lock(m_con_mutex); //bulk comment-out
//MutexAutoLock lock(m_con_mutex); //bulk comment-out
our_peer_id = m_con.GetPeerID();
}

Expand Down Expand Up @@ -1794,7 +1794,7 @@ void Client::afterContentReceived(IrrlichtDevice *device)

// Start mesh update thread after setting up content definitions
infostream<<"- Starting mesh update thread"<<std::endl;
m_mesh_update_thread.Start();
m_mesh_update_thread.start();

m_state = LC_Ready;
sendReady();
Expand Down
13 changes: 4 additions & 9 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "network/connection.h"
#include "environment.h"
#include "irrlichttypes_extrabloated.h"
#include "jthread/jmutex.h"
#include "threading/mutex.h"
#include <ostream>
#include <map>
#include <set>
Expand Down Expand Up @@ -89,14 +89,14 @@ class MeshUpdateQueue

u32 size()
{
JMutexAutoLock lock(m_mutex);
MutexAutoLock lock(m_mutex);
return m_queue.size();
}

private:
std::vector<QueuedMeshUpdate*> m_queue;
std::set<v3s16> m_urgents;
JMutex m_mutex;
Mutex m_mutex;
};

struct MeshUpdateResult
Expand All @@ -119,19 +119,14 @@ class MeshUpdateThread : public UpdateThread
MeshUpdateQueue m_queue_in;

protected:
const char *getName()
{ return "MeshUpdateThread"; }
virtual void doUpdate();

public:

MeshUpdateThread()
{
}
MeshUpdateThread() : UpdateThread("Mesh") {}

void enqueueUpdate(v3s16 p, MeshMakeData *data,
bool ack_block_to_server, bool urgent);

MutexedQueue<MeshUpdateResult> m_queue_out;

v3s16 m_camera_offset;
Expand Down
6 changes: 3 additions & 3 deletions src/client/clientlauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,14 +651,14 @@ void ClientLauncher::speed_tests()
infostream << "Around 5000/ms should do well here." << std::endl;
TimeTaker timer("Testing mutex speed");

JMutex m;
Mutex m;
u32 n = 0;
u32 i = 0;
do {
n += 10000;
for (; i < n; i++) {
m.Lock();
m.Unlock();
m.lock();
m.unlock();
}
}
// Do at least 10ms
Expand Down
Loading

0 comments on commit e4bff8b

Please sign in to comment.