jthread 1.3.1

This commit is contained in:
darkrose 2013-10-13 23:18:51 +10:00
parent 73c34cb507
commit 19d9a7a32e
17 changed files with 155 additions and 41 deletions

View File

@ -1,6 +1,10 @@
# Look for jthread, use our own if not found # Look for jthread, use our own if not found
FIND_PATH(JTHREAD_INCLUDE_DIR jthread.h) FIND_PACKAGE(PkgConfig)
pkg_check_modules(PC_JTHREAD QUIET jthread)
FIND_PATH(JTHREAD_INCLUDE_DIR jthread.h
HINTS ${PC_JTHREAD_INCLUDEDIR} ${PC_JTHREAD_INCLUDE_DIRS})
FIND_LIBRARY(JTHREAD_LIBRARY NAMES jthread) FIND_LIBRARY(JTHREAD_LIBRARY NAMES jthread)
@ -12,7 +16,7 @@ IF(JTHREAD_FOUND)
MESSAGE(STATUS "Found system jthread header file in ${JTHREAD_INCLUDE_DIR}") MESSAGE(STATUS "Found system jthread header file in ${JTHREAD_INCLUDE_DIR}")
MESSAGE(STATUS "Found system jthread library ${JTHREAD_LIBRARY}") MESSAGE(STATUS "Found system jthread library ${JTHREAD_LIBRARY}")
ELSE(JTHREAD_FOUND) ELSE(JTHREAD_FOUND)
SET(JTHREAD_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/jthread) SET(JTHREAD_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/jthread")
SET(JTHREAD_LIBRARY jthread) SET(JTHREAD_LIBRARY jthread)
MESSAGE(STATUS "Using project jthread library") MESSAGE(STATUS "Using project jthread library")
ENDIF(JTHREAD_FOUND) ENDIF(JTHREAD_FOUND)

View File

@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common_irrlicht.h" #include "common_irrlicht.h"
#include "exceptions.h" #include "exceptions.h"
using namespace jthread;
// Player privileges. These form a bitmask stored in the privs field // Player privileges. These form a bitmask stored in the privs field
// of the player, and define things they're allowed to do. See also // of the player, and define things they're allowed to do. See also
// the static methods Player::privsToString and stringToPrivs that // the static methods Player::privsToString and stringToPrivs that

View File

@ -27,6 +27,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common_irrlicht.h" #include "common_irrlicht.h"
#include "exceptions.h" #include "exceptions.h"
using namespace jthread;
class BanManager class BanManager
{ {
public: public:

View File

@ -39,6 +39,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#else #else
#endif #endif
using namespace jthread;
/* /*
Debug output Debug output
*/ */

View File

@ -1,9 +1,20 @@
if( UNIX ) if( UNIX )
set(jthread_SRCS pthread/jmutex.cpp pthread/jthread.cpp) set(jthread_SRCS pthread/jmutex.cpp pthread/jthread.cpp)
set(jthread_platform_LIBS "") set(jthread_platform_LIBS "")
set(JTHREAD_CONFIG_WIN32THREADS "// Using pthread based threads")
set(JTHREAD_CONFIG_JMUTEXCRITICALSECTION "")
else( UNIX ) else( UNIX )
set(jthread_SRCS win32/jmutex.cpp win32/jthread.cpp) set(jthread_SRCS win32/jmutex.cpp win32/jthread.cpp)
set(jthread_platform_LIBS "") set(jthread_platform_LIBS "")
set(JTHREAD_CONFIG_WIN32THREADS "#define JTHREAD_CONFIG_WIN32THREADS")
set(JTHREAD_WIN32_CRITICALSECTION OFF CACHE BOOL "If set to false, use standard mutex. If set to true, use a critical section object.")
if (JTHREAD_WIN32_CRITICALSECTION)
set(JTHREAD_CONFIG_JMUTEXCRITICALSECTION "#define JTHREAD_CONFIG_JMUTEXCRITICALSECTION")
else (JTHREAD_WIN32_CRITICALSECTION)
set(JTHREAD_CONFIG_JMUTEXCRITICALSECTION "// Using standard Win32 mutex")
endif (JTHREAD_WIN32_CRITICALSECTION)
endif( UNIX ) endif( UNIX )
add_library(jthread ${jthread_SRCS}) add_library(jthread ${jthread_SRCS})
@ -14,3 +25,5 @@ target_link_libraries(
) )
configure_file("${PROJECT_SOURCE_DIR}/jthread/jthreadconfig.h.in"
"${PROJECT_BINARY_DIR}/jthread/jthreadconfig.h")

View File

@ -3,7 +3,7 @@
This file is a part of the JThread package, which contains some object- This file is a part of the JThread package, which contains some object-
oriented thread wrappers for different thread implementations. oriented thread wrappers for different thread implementations.
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) Copyright (c) 2000-2011 Jori Liesenborgs (jori.liesenborgs@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
@ -25,27 +25,29 @@
*/ */
#ifndef JMUTEX_H #ifndef JTHREAD_JMUTEX_H
#define JMUTEX_H #define JTHREAD_JMUTEX_H
#if (defined(WIN32) || defined(_WIN32_WCE)) #include "jthreadconfig.h"
#ifdef JTHREAD_CONFIG_WIN32THREADS
#ifndef _WIN32_WCE #ifndef _WIN32_WCE
#include <process.h> #include <process.h>
#endif // _WIN32_WCE #endif // _WIN32_WCE
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
// CriticalSection is way faster than the alternative
#define JMUTEX_CRITICALSECTION
#else // using pthread #else // using pthread
#include <pthread.h> #include <pthread.h>
#endif // WIN32 #endif // JTHREAD_CONFIG_WIN32THREADS
#define ERR_JMUTEX_ALREADYINIT -1 #define ERR_JMUTEX_ALREADYINIT -1
#define ERR_JMUTEX_NOTINIT -2 #define ERR_JMUTEX_NOTINIT -2
#define ERR_JMUTEX_CANTCREATEMUTEX -3 #define ERR_JMUTEX_CANTCREATEMUTEX -3
class JMutex namespace jthread
{
class JTHREAD_IMPORTEXPORT JMutex
{ {
public: public:
JMutex(); JMutex();
@ -55,16 +57,19 @@ public:
int Unlock(); int Unlock();
bool IsInitialized() { return initialized; } bool IsInitialized() { return initialized; }
private: private:
#if (defined(WIN32) || defined(_WIN32_WCE)) #ifdef JTHREAD_CONFIG_WIN32THREADS
#ifdef JMUTEX_CRITICALSECTION #ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION
CRITICAL_SECTION mutex; CRITICAL_SECTION mutex;
#else // Use standard mutex #else // Use standard mutex
HANDLE mutex; HANDLE mutex;
#endif // JMUTEX_CRITICALSECTION #endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION
#else // pthread mutex #else // pthread mutex
pthread_mutex_t mutex; pthread_mutex_t mutex;
#endif // WIN32 #endif // JTHREAD_CONFIG_WIN32THREADS
bool initialized; bool initialized;
}; };
#endif // JMUTEX_H } // end namespace
#endif // JTHREAD_JMUTEX_H

View File

@ -3,7 +3,7 @@
This file is a part of the JThread package, which contains some object- This file is a part of the JThread package, which contains some object-
oriented thread wrappers for different thread implementations. oriented thread wrappers for different thread implementations.
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) Copyright (c) 2000-2011 Jori Liesenborgs (jori.liesenborgs@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
@ -25,13 +25,17 @@
*/ */
#ifndef JMUTEXAUTOLOCK_H #ifndef JTHREAD_JMUTEXAUTOLOCK_H
#define JMUTEXAUTOLOCK_H #define JTHREAD_JMUTEXAUTOLOCK_H
#include "jthreadconfig.h"
#include "jmutex.h" #include "jmutex.h"
class JMutexAutoLock namespace jthread
{
class JTHREAD_IMPORTEXPORT JMutexAutoLock
{ {
public: public:
JMutexAutoLock(JMutex &m) : mutex(m) { mutex.Lock(); } JMutexAutoLock(JMutex &m) : mutex(m) { mutex.Lock(); }
@ -40,4 +44,7 @@ private:
JMutex &mutex; JMutex &mutex;
}; };
#endif // JMUTEXAUTOLOCK_H } // end namespace
#endif // JTHREAD_JMUTEXAUTOLOCK_H

View File

@ -3,7 +3,7 @@
This file is a part of the JThread package, which contains some object- This file is a part of the JThread package, which contains some object-
oriented thread wrappers for different thread implementations. oriented thread wrappers for different thread implementations.
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) Copyright (c) 2000-2011 Jori Liesenborgs (jori.liesenborgs@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
@ -25,10 +25,11 @@
*/ */
#ifndef JTHREAD_H #ifndef JTHREAD_JTHREAD_H
#define JTHREAD_H #define JTHREAD_JTHREAD_H
#include "jthreadconfig.h"
#include "jmutex.h" #include "jmutex.h"
#define ERR_JTHREAD_CANTINITMUTEX -1 #define ERR_JTHREAD_CANTINITMUTEX -1
@ -37,7 +38,10 @@
#define ERR_JTHREAD_NOTRUNNING -4 #define ERR_JTHREAD_NOTRUNNING -4
#define ERR_JTHREAD_ALREADYRUNNING -5 #define ERR_JTHREAD_ALREADYRUNNING -5
class JThread namespace jthread
{
class JTHREAD_IMPORTEXPORT JThread
{ {
public: public:
JThread(); JThread();
@ -51,7 +55,7 @@ protected:
void ThreadStarted(); void ThreadStarted();
private: private:
#if (defined(WIN32) || defined(_WIN32_WCE)) #ifdef JTHREAD_CONFIG_WIN32THREADS
#ifdef _WIN32_WCE #ifdef _WIN32_WCE
DWORD threadid; DWORD threadid;
static DWORD WINAPI TheThread(void *param); static DWORD WINAPI TheThread(void *param);
@ -64,7 +68,7 @@ private:
static void *TheThread(void *param); static void *TheThread(void *param);
pthread_t threadid; pthread_t threadid;
#endif // WIN32 #endif // JTHREAD_CONFIG_WIN32THREADS
void *retval; void *retval;
bool running; bool running;
@ -73,5 +77,7 @@ private:
bool mutexinit; bool mutexinit;
}; };
#endif // JTHREAD_H } // end namespace
#endif // JTHREAD_JTHREAD_H

View File

@ -0,0 +1,45 @@
/*
This file is a part of the JThread package, which contains some object-
oriented thread wrappers for different thread implementations.
Copyright (c) 2000-2011 Jori Liesenborgs (jori.liesenborgs@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef JTHREADCONFIG_H
#define JTHREADCONFIG_H
#define JTHREAD_IMPORT ${JTHREAD_IMPORT}
#define JTHREAD_EXPORT ${JTHREAD_EXPORT}
#ifdef JTHREAD_COMPILING
#define JTHREAD_IMPORTEXPORT JTHREAD_EXPORT
#else
#define JTHREAD_IMPORTEXPORT JTHREAD_IMPORT
#endif // JTHREAD_COMPILING
${JTHREAD_CONFIG_WIN32THREADS}
${JTHREAD_CONFIG_JMUTEXCRITICALSECTION}
#endif // JTHREADCONFIG_H

View File

@ -3,7 +3,7 @@
This file is a part of the JThread package, which contains some object- This file is a part of the JThread package, which contains some object-
oriented thread wrappers for different thread implementations. oriented thread wrappers for different thread implementations.
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) Copyright (c) 2000-2011 Jori Liesenborgs (jori.liesenborgs@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
@ -27,6 +27,9 @@
#include "jmutex.h" #include "jmutex.h"
namespace jthread
{
JMutex::JMutex() JMutex::JMutex()
{ {
initialized = false; initialized = false;
@ -65,3 +68,6 @@ int JMutex::Unlock()
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
return 0; return 0;
} }
} // end namespace

View File

@ -3,7 +3,7 @@
This file is a part of the JThread package, which contains some object- This file is a part of the JThread package, which contains some object-
oriented thread wrappers for different thread implementations. oriented thread wrappers for different thread implementations.
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) Copyright (c) 2000-2011 Jori Liesenborgs (jori.liesenborgs@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
@ -30,6 +30,9 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
namespace jthread
{
JThread::JThread() JThread::JThread()
{ {
retval = NULL; retval = NULL;
@ -178,3 +181,5 @@ void JThread::ThreadStarted()
continuemutex2.Unlock(); continuemutex2.Unlock();
} }
} // end namespace

View File

@ -3,7 +3,7 @@
This file is a part of the JThread package, which contains some object- This file is a part of the JThread package, which contains some object-
oriented thread wrappers for different thread implementations. oriented thread wrappers for different thread implementations.
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) Copyright (c) 2000-2011 Jori Liesenborgs (jori.liesenborgs@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
@ -27,6 +27,9 @@
#include "jmutex.h" #include "jmutex.h"
namespace jthread
{
JMutex::JMutex() JMutex::JMutex()
{ {
initialized = false; initialized = false;
@ -35,24 +38,24 @@ JMutex::JMutex()
JMutex::~JMutex() JMutex::~JMutex()
{ {
if (initialized) if (initialized)
#ifdef JMUTEX_CRITICALSECTION #ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION
DeleteCriticalSection(&mutex); DeleteCriticalSection(&mutex);
#else #else
CloseHandle(mutex); CloseHandle(mutex);
#endif // JMUTEX_CRITICALSECTION #endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION
} }
int JMutex::Init() int JMutex::Init()
{ {
if (initialized) if (initialized)
return ERR_JMUTEX_ALREADYINIT; return ERR_JMUTEX_ALREADYINIT;
#ifdef JMUTEX_CRITICALSECTION #ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION
InitializeCriticalSection(&mutex); InitializeCriticalSection(&mutex);
#else #else
mutex = CreateMutex(NULL,FALSE,NULL); mutex = CreateMutex(NULL,FALSE,NULL);
if (mutex == NULL) if (mutex == NULL)
return ERR_JMUTEX_CANTCREATEMUTEX; return ERR_JMUTEX_CANTCREATEMUTEX;
#endif // JMUTEX_CRITICALSECTION #endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION
initialized = true; initialized = true;
return 0; return 0;
} }
@ -61,11 +64,11 @@ int JMutex::Lock()
{ {
if (!initialized) if (!initialized)
return ERR_JMUTEX_NOTINIT; return ERR_JMUTEX_NOTINIT;
#ifdef JMUTEX_CRITICALSECTION #ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION
EnterCriticalSection(&mutex); EnterCriticalSection(&mutex);
#else #else
WaitForSingleObject(mutex,INFINITE); WaitForSingleObject(mutex,INFINITE);
#endif // JMUTEX_CRITICALSECTION #endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION
return 0; return 0;
} }
@ -73,11 +76,12 @@ int JMutex::Unlock()
{ {
if (!initialized) if (!initialized)
return ERR_JMUTEX_NOTINIT; return ERR_JMUTEX_NOTINIT;
#ifdef JMUTEX_CRITICALSECTION #ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION
LeaveCriticalSection(&mutex); LeaveCriticalSection(&mutex);
#else #else
ReleaseMutex(mutex); ReleaseMutex(mutex);
#endif // JMUTEX_CRITICALSECTION #endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION
return 0; return 0;
} }
} // end namespace

View File

@ -3,7 +3,7 @@
This file is a part of the JThread package, which contains some object- This file is a part of the JThread package, which contains some object-
oriented thread wrappers for different thread implementations. oriented thread wrappers for different thread implementations.
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) Copyright (c) 2000-2011 Jori Liesenborgs (jori.liesenborgs@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
@ -26,11 +26,15 @@
*/ */
#include "jthread.h" #include "jthread.h"
#include "jmutexautolock.h"
#ifndef _WIN32_WCE #ifndef _WIN32_WCE
#include <process.h> #include <process.h>
#endif // _WIN32_WCE #endif // _WIN32_WCE
namespace jthread
{
JThread::JThread() JThread::JThread()
{ {
retval = NULL; retval = NULL;
@ -130,14 +134,13 @@ bool JThread::IsRunning()
void *JThread::GetReturnValue() void *JThread::GetReturnValue()
{ {
JMutexAutoLock autolock(runningmutex);
void *val; void *val;
runningmutex.Lock();
if (running) if (running)
val = NULL; val = NULL;
else else
val = retval; val = retval;
runningmutex.Unlock();
return val; return val;
} }
@ -175,3 +178,5 @@ void JThread::ThreadStarted()
continuemutex2.Unlock(); continuemutex2.Unlock();
} }
} // end namespace

View File

@ -36,6 +36,8 @@ extern "C" {
#include "sqlite3.h" #include "sqlite3.h"
} }
using namespace jthread;
class MapSector; class MapSector;
class ServerMapSector; class ServerMapSector;
class ClientMapSector; class ClientMapSector;

View File

@ -50,6 +50,8 @@ struct ValueSpec
const char *help; const char *help;
}; };
using namespace jthread;
class Settings class Settings
{ {
public: public:

View File

@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "utility.h" #include "utility.h"
#include <string> #include <string>
using namespace jthread;
/* /*
tile.{h,cpp}: Texture handling stuff. tile.{h,cpp}: Texture handling stuff.
*/ */

View File

@ -36,6 +36,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "exceptions.h" #include "exceptions.h"
#include "porting.h" #include "porting.h"
using namespace jthread;
extern const v3s16 g_6dirs[6]; extern const v3s16 g_6dirs[6];
extern const v3s16 g_26dirs[26]; extern const v3s16 g_26dirs[26];