diff --git a/cmake/Modules/FindJthread.cmake b/cmake/Modules/FindJthread.cmake index 302a3c2..e121221 100644 --- a/cmake/Modules/FindJthread.cmake +++ b/cmake/Modules/FindJthread.cmake @@ -1,6 +1,10 @@ # 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) @@ -12,7 +16,7 @@ IF(JTHREAD_FOUND) MESSAGE(STATUS "Found system jthread header file in ${JTHREAD_INCLUDE_DIR}") MESSAGE(STATUS "Found system jthread library ${JTHREAD_LIBRARY}") ELSE(JTHREAD_FOUND) - SET(JTHREAD_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/jthread) + SET(JTHREAD_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/jthread") SET(JTHREAD_LIBRARY jthread) MESSAGE(STATUS "Using project jthread library") ENDIF(JTHREAD_FOUND) diff --git a/src/auth.h b/src/auth.h index 5ea697a..f99ddfb 100644 --- a/src/auth.h +++ b/src/auth.h @@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common_irrlicht.h" #include "exceptions.h" +using namespace jthread; + // Player privileges. These form a bitmask stored in the privs field // of the player, and define things they're allowed to do. See also // the static methods Player::privsToString and stringToPrivs that diff --git a/src/ban.h b/src/ban.h index 8fdcf4c..7641dee 100644 --- a/src/ban.h +++ b/src/ban.h @@ -27,6 +27,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common_irrlicht.h" #include "exceptions.h" +using namespace jthread; + class BanManager { public: diff --git a/src/debug.h b/src/debug.h index 4aad656..42a3ad8 100644 --- a/src/debug.h +++ b/src/debug.h @@ -39,6 +39,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #else #endif +using namespace jthread; + /* Debug output */ diff --git a/src/jthread/CMakeLists.txt b/src/jthread/CMakeLists.txt index be38f87..e120a9c 100644 --- a/src/jthread/CMakeLists.txt +++ b/src/jthread/CMakeLists.txt @@ -1,9 +1,20 @@ if( UNIX ) set(jthread_SRCS pthread/jmutex.cpp pthread/jthread.cpp) set(jthread_platform_LIBS "") + + set(JTHREAD_CONFIG_WIN32THREADS "// Using pthread based threads") + set(JTHREAD_CONFIG_JMUTEXCRITICALSECTION "") else( UNIX ) set(jthread_SRCS win32/jmutex.cpp win32/jthread.cpp) 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 ) 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") diff --git a/src/jthread/jmutex.h b/src/jthread/jmutex.h index 9ce0130..d4180cf 100644 --- a/src/jthread/jmutex.h +++ b/src/jthread/jmutex.h @@ -3,7 +3,7 @@ This file is a part of the JThread package, which contains some object- 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 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 #include #endif // _WIN32_WCE #include #include - // CriticalSection is way faster than the alternative - #define JMUTEX_CRITICALSECTION #else // using pthread #include -#endif // WIN32 +#endif // JTHREAD_CONFIG_WIN32THREADS #define ERR_JMUTEX_ALREADYINIT -1 #define ERR_JMUTEX_NOTINIT -2 #define ERR_JMUTEX_CANTCREATEMUTEX -3 -class JMutex +namespace jthread +{ + +class JTHREAD_IMPORTEXPORT JMutex { public: JMutex(); @@ -55,16 +57,19 @@ public: int Unlock(); bool IsInitialized() { return initialized; } private: -#if (defined(WIN32) || defined(_WIN32_WCE)) -#ifdef JMUTEX_CRITICALSECTION +#ifdef JTHREAD_CONFIG_WIN32THREADS +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION CRITICAL_SECTION mutex; #else // Use standard mutex HANDLE mutex; -#endif // JMUTEX_CRITICALSECTION +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION #else // pthread mutex pthread_mutex_t mutex; -#endif // WIN32 +#endif // JTHREAD_CONFIG_WIN32THREADS bool initialized; }; -#endif // JMUTEX_H +} // end namespace + +#endif // JTHREAD_JMUTEX_H + diff --git a/src/jthread/jmutexautolock.h b/src/jthread/jmutexautolock.h index 6020a5c..8f4d900 100644 --- a/src/jthread/jmutexautolock.h +++ b/src/jthread/jmutexautolock.h @@ -3,7 +3,7 @@ This file is a part of the JThread package, which contains some object- 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 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" -class JMutexAutoLock +namespace jthread +{ + +class JTHREAD_IMPORTEXPORT JMutexAutoLock { public: JMutexAutoLock(JMutex &m) : mutex(m) { mutex.Lock(); } @@ -40,4 +44,7 @@ private: JMutex &mutex; }; -#endif // JMUTEXAUTOLOCK_H +} // end namespace + +#endif // JTHREAD_JMUTEXAUTOLOCK_H + diff --git a/src/jthread/jthread.h b/src/jthread/jthread.h index 9440a15..7f4dc37 100644 --- a/src/jthread/jthread.h +++ b/src/jthread/jthread.h @@ -3,7 +3,7 @@ This file is a part of the JThread package, which contains some object- 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 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" #define ERR_JTHREAD_CANTINITMUTEX -1 @@ -37,7 +38,10 @@ #define ERR_JTHREAD_NOTRUNNING -4 #define ERR_JTHREAD_ALREADYRUNNING -5 -class JThread +namespace jthread +{ + +class JTHREAD_IMPORTEXPORT JThread { public: JThread(); @@ -51,7 +55,7 @@ protected: void ThreadStarted(); private: -#if (defined(WIN32) || defined(_WIN32_WCE)) +#ifdef JTHREAD_CONFIG_WIN32THREADS #ifdef _WIN32_WCE DWORD threadid; static DWORD WINAPI TheThread(void *param); @@ -64,7 +68,7 @@ private: static void *TheThread(void *param); pthread_t threadid; -#endif // WIN32 +#endif // JTHREAD_CONFIG_WIN32THREADS void *retval; bool running; @@ -73,5 +77,7 @@ private: bool mutexinit; }; -#endif // JTHREAD_H +} // end namespace + +#endif // JTHREAD_JTHREAD_H diff --git a/src/jthread/jthreadconfig.h.in b/src/jthread/jthreadconfig.h.in new file mode 100644 index 0000000..2dbfb07 --- /dev/null +++ b/src/jthread/jthreadconfig.h.in @@ -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 + diff --git a/src/jthread/pthread/jmutex.cpp b/src/jthread/pthread/jmutex.cpp index 6bc3ae5..1f5f5ec 100644 --- a/src/jthread/pthread/jmutex.cpp +++ b/src/jthread/pthread/jmutex.cpp @@ -3,7 +3,7 @@ This file is a part of the JThread package, which contains some object- 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 copy of this software and associated documentation files (the "Software"), @@ -27,6 +27,9 @@ #include "jmutex.h" +namespace jthread +{ + JMutex::JMutex() { initialized = false; @@ -65,3 +68,6 @@ int JMutex::Unlock() pthread_mutex_unlock(&mutex); return 0; } + +} // end namespace + diff --git a/src/jthread/pthread/jthread.cpp b/src/jthread/pthread/jthread.cpp index 978cac2..647783f 100644 --- a/src/jthread/pthread/jthread.cpp +++ b/src/jthread/pthread/jthread.cpp @@ -3,7 +3,7 @@ This file is a part of the JThread package, which contains some object- 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 copy of this software and associated documentation files (the "Software"), @@ -30,6 +30,9 @@ #include #include +namespace jthread +{ + JThread::JThread() { retval = NULL; @@ -178,3 +181,5 @@ void JThread::ThreadStarted() continuemutex2.Unlock(); } +} // end namespace + diff --git a/src/jthread/win32/jmutex.cpp b/src/jthread/win32/jmutex.cpp index 000461e..4126931 100644 --- a/src/jthread/win32/jmutex.cpp +++ b/src/jthread/win32/jmutex.cpp @@ -3,7 +3,7 @@ This file is a part of the JThread package, which contains some object- 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 copy of this software and associated documentation files (the "Software"), @@ -27,6 +27,9 @@ #include "jmutex.h" +namespace jthread +{ + JMutex::JMutex() { initialized = false; @@ -35,24 +38,24 @@ JMutex::JMutex() JMutex::~JMutex() { if (initialized) -#ifdef JMUTEX_CRITICALSECTION +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION DeleteCriticalSection(&mutex); #else CloseHandle(mutex); -#endif // JMUTEX_CRITICALSECTION +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION } int JMutex::Init() { if (initialized) return ERR_JMUTEX_ALREADYINIT; -#ifdef JMUTEX_CRITICALSECTION +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION InitializeCriticalSection(&mutex); #else mutex = CreateMutex(NULL,FALSE,NULL); if (mutex == NULL) return ERR_JMUTEX_CANTCREATEMUTEX; -#endif // JMUTEX_CRITICALSECTION +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION initialized = true; return 0; } @@ -61,11 +64,11 @@ int JMutex::Lock() { if (!initialized) return ERR_JMUTEX_NOTINIT; -#ifdef JMUTEX_CRITICALSECTION +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION EnterCriticalSection(&mutex); #else WaitForSingleObject(mutex,INFINITE); -#endif // JMUTEX_CRITICALSECTION +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION return 0; } @@ -73,11 +76,12 @@ int JMutex::Unlock() { if (!initialized) return ERR_JMUTEX_NOTINIT; -#ifdef JMUTEX_CRITICALSECTION +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION LeaveCriticalSection(&mutex); #else ReleaseMutex(mutex); -#endif // JMUTEX_CRITICALSECTION +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION return 0; } +} // end namespace diff --git a/src/jthread/win32/jthread.cpp b/src/jthread/win32/jthread.cpp index 54b110b..abe30ca 100644 --- a/src/jthread/win32/jthread.cpp +++ b/src/jthread/win32/jthread.cpp @@ -3,7 +3,7 @@ This file is a part of the JThread package, which contains some object- 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 copy of this software and associated documentation files (the "Software"), @@ -26,11 +26,15 @@ */ #include "jthread.h" +#include "jmutexautolock.h" #ifndef _WIN32_WCE #include #endif // _WIN32_WCE +namespace jthread +{ + JThread::JThread() { retval = NULL; @@ -130,14 +134,13 @@ bool JThread::IsRunning() void *JThread::GetReturnValue() { + JMutexAutoLock autolock(runningmutex); void *val; - runningmutex.Lock(); if (running) val = NULL; else val = retval; - runningmutex.Unlock(); return val; } @@ -175,3 +178,5 @@ void JThread::ThreadStarted() continuemutex2.Unlock(); } +} // end namespace + diff --git a/src/map.h b/src/map.h index 692a34b..2373786 100644 --- a/src/map.h +++ b/src/map.h @@ -36,6 +36,8 @@ extern "C" { #include "sqlite3.h" } +using namespace jthread; + class MapSector; class ServerMapSector; class ClientMapSector; diff --git a/src/settings.h b/src/settings.h index f4af93e..d7e5143 100644 --- a/src/settings.h +++ b/src/settings.h @@ -50,6 +50,8 @@ struct ValueSpec const char *help; }; +using namespace jthread; + class Settings { public: diff --git a/src/tile.h b/src/tile.h index e016a88..1c1d135 100644 --- a/src/tile.h +++ b/src/tile.h @@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "utility.h" #include +using namespace jthread; + /* tile.{h,cpp}: Texture handling stuff. */ diff --git a/src/utility.h b/src/utility.h index 8096db4..481cc6f 100644 --- a/src/utility.h +++ b/src/utility.h @@ -36,6 +36,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "exceptions.h" #include "porting.h" +using namespace jthread; + extern const v3s16 g_6dirs[6]; extern const v3s16 g_26dirs[26];