diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 27ae2e9..2660cd9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,370 +1,378 @@ -project(minetest-classic) -cmake_minimum_required( VERSION 2.6 ) - -if(RUN_IN_PLACE) - add_definitions ( -DRUN_IN_PLACE ) -endif(RUN_IN_PLACE) - -# user-visible option to enable/disable gettext usage -OPTION(ENABLE_GETTEXT "Use GetText for internationalization" ON) - -# this is only set to 1 if gettext is enabled _and_ available -set(USE_GETTEXT 0) - -find_package(GettextLib) - -if(GETTEXT_FOUND AND ENABLE_GETTEXT) - message(STATUS "gettext include path: ${GETTEXT_INCLUDE_DIR}") - message(STATUS "gettext msgfmt path: ${GETTEXT_MSGFMT}") - if(WIN32) - message(STATUS "gettext library: ${GETTEXT_LIBRARY}") - message(STATUS "gettext dll: ${GETTEXT_DLL}") - message(STATUS "gettext iconv dll: ${GETTEXT_ICONV_DLL}") - endif() - set(USE_GETTEXT 1) - message(STATUS "GetText enabled; locales found: ${GETTEXT_AVAILABLE_LOCALES}") -elseif(GETTEXT_FOUND AND NOT ENABLE_GETTEXT) - MESSAGE(STATUS "GetText found but disabled;") -else(GETTEXT_FOUND AND ENABLE_GETTEXT) - message(STATUS "GetText disabled") -endif(GETTEXT_FOUND AND ENABLE_GETTEXT) - -if(NOT MSVC) - set(USE_GPROF 0 CACHE BOOL "Use -pg flag for g++") -endif() - -# Use cmake_config.h -add_definitions ( -DUSE_CMAKE_CONFIG_H ) - -if(WIN32) - # Windows - if(MSVC) # MSVC Specifics - # Surpress some useless warnings - add_definitions ( /D "_CRT_SECURE_NO_DEPRECATE" /W1 ) - else() # Probably MinGW = GCC - set(PLATFORM_LIBS ws2_32.lib) - endif() - # Zlib stuff - set(ZLIB_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/../../zlib/zlib-1.2.5" - CACHE PATH "Zlib include directory") - set(ZLIB_LIBRARIES "${PROJECT_SOURCE_DIR}/../../zlib125dll/dll32/zlibwapi.lib" - CACHE FILEPATH "Path to zlibwapi.lib") - set(ZLIB_DLL "${PROJECT_SOURCE_DIR}/../../zlib125dll/dll32/zlibwapi.dll" - CACHE FILEPATH "Path to zlibwapi.dll (for installation)") - set(IRRLICHT_SOURCE_DIR "${PROJECT_SOURCE_DIR}/../../irrlicht-1.7.2" - CACHE PATH "irrlicht dir") -else() - # Unix probably - if(BUILD_CLIENT) - find_package(X11 REQUIRED) - find_package(OpenGL REQUIRED) - find_package(JPEG REQUIRED) - find_package(BZip2 REQUIRED) - find_package(PNG REQUIRED) - if(APPLE) - FIND_LIBRARY(CARBON_LIB Carbon) - FIND_LIBRARY(COCOA_LIB Cocoa) - FIND_LIBRARY(IOKIT_LIB IOKit) - mark_as_advanced( - CARBON_LIB - COCOA_LIB - IOKIT_LIB - ) - SET(CLIENT_PLATFORM_LIBS ${CLIENT_PLATFORM_LIBS} ${CARBON_LIB} ${COCOA_LIB} ${IOKIT_LIB}) - endif(APPLE) - endif(BUILD_CLIENT) - find_package(ZLIB REQUIRED) - set(PLATFORM_LIBS -lpthread ${CMAKE_DL_LIBS}) - #set(CLIENT_PLATFORM_LIBS -lXxf86vm) - # This way Xxf86vm is found on OpenBSD too - find_library(XXF86VM_LIBRARY Xxf86vm) - set(CLIENT_PLATFORM_LIBS ${CLIENT_PLATFORM_LIBS} ${XXF86VM_LIBRARY}) -endif() - -find_package(Sqlite3 REQUIRED) - -configure_file( - "${PROJECT_SOURCE_DIR}/cmake_config.h.in" - "${PROJECT_BINARY_DIR}/cmake_config.h" -) - -set(common_SRCS - log.cpp - content_sao.cpp - mapgen.cpp - content_inventory.cpp - content_nodemeta.cpp - content_craft.cpp - content_craftitem.cpp - content_toolitem.cpp - content_mapnode.cpp - content_mapnode_util.cpp - content_list.cpp - content_nodebox.cpp - auth.cpp - collision.cpp - nodemetadata.cpp - serverobject.cpp - noise.cpp - mineral.cpp - porting.cpp - defaultsettings.cpp - mapnode.cpp - voxel.cpp - inventory.cpp - debug.cpp - serialization.cpp - light.cpp - filesys.cpp - connection.cpp - environment.cpp - server.cpp - servercommand.cpp - socket.cpp - mapblock.cpp - mapsector.cpp - map.cpp - player.cpp - utility.cpp - test.cpp - sha1.cpp - base64.cpp +project(minetest-classic) +cmake_minimum_required( VERSION 2.6 ) + +if(RUN_IN_PLACE) + add_definitions ( -DRUN_IN_PLACE ) +endif(RUN_IN_PLACE) + +# user-visible option to enable/disable gettext usage +OPTION(ENABLE_GETTEXT "Use GetText for internationalization" ON) + +# this is only set to 1 if gettext is enabled _and_ available +set(USE_GETTEXT 0) + +find_package(GettextLib) + +if(GETTEXT_FOUND AND ENABLE_GETTEXT) + message(STATUS "gettext include path: ${GETTEXT_INCLUDE_DIR}") + message(STATUS "gettext msgfmt path: ${GETTEXT_MSGFMT}") + if(WIN32) + message(STATUS "gettext library: ${GETTEXT_LIBRARY}") + message(STATUS "gettext dll: ${GETTEXT_DLL}") + message(STATUS "gettext iconv dll: ${GETTEXT_ICONV_DLL}") + endif() + set(USE_GETTEXT 1) + message(STATUS "GetText enabled; locales found: ${GETTEXT_AVAILABLE_LOCALES}") +elseif(GETTEXT_FOUND AND NOT ENABLE_GETTEXT) + MESSAGE(STATUS "GetText found but disabled;") +else(GETTEXT_FOUND AND ENABLE_GETTEXT) + message(STATUS "GetText disabled") +endif(GETTEXT_FOUND AND ENABLE_GETTEXT) + +if(NOT MSVC) + set(USE_GPROF 0 CACHE BOOL "Use -pg flag for g++") +endif() + +# Use cmake_config.h +add_definitions ( -DUSE_CMAKE_CONFIG_H ) + +if(WIN32) + # Windows + if(MSVC) # MSVC Specifics + # Surpress some useless warnings + add_definitions ( /D "_CRT_SECURE_NO_DEPRECATE" /W1 ) + else() # Probably MinGW = GCC + set(PLATFORM_LIBS ws2_32.lib) + endif() + # Zlib stuff + set(ZLIB_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/../../zlib/zlib-1.2.5" + CACHE PATH "Zlib include directory") + set(ZLIB_LIBRARIES "${PROJECT_SOURCE_DIR}/../../zlib125dll/dll32/zlibwapi.lib" + CACHE FILEPATH "Path to zlibwapi.lib") + set(ZLIB_DLL "${PROJECT_SOURCE_DIR}/../../zlib125dll/dll32/zlibwapi.dll" + CACHE FILEPATH "Path to zlibwapi.dll (for installation)") + set(IRRLICHT_SOURCE_DIR "${PROJECT_SOURCE_DIR}/../../irrlicht-1.7.2" + CACHE PATH "irrlicht dir") +else() + # Unix probably + if(BUILD_CLIENT) + find_package(X11 REQUIRED) + find_package(OpenGL REQUIRED) + find_package(JPEG REQUIRED) + find_package(BZip2 REQUIRED) + find_package(PNG REQUIRED) + if(APPLE) + FIND_LIBRARY(CARBON_LIB Carbon) + FIND_LIBRARY(COCOA_LIB Cocoa) + FIND_LIBRARY(IOKIT_LIB IOKit) + mark_as_advanced( + CARBON_LIB + COCOA_LIB + IOKIT_LIB + ) + SET(CLIENT_PLATFORM_LIBS ${CLIENT_PLATFORM_LIBS} ${CARBON_LIB} ${COCOA_LIB} ${IOKIT_LIB}) + endif(APPLE) + endif(BUILD_CLIENT) + find_package(ZLIB REQUIRED) + set(PLATFORM_LIBS -lpthread ${CMAKE_DL_LIBS}) + #set(CLIENT_PLATFORM_LIBS -lXxf86vm) + # This way Xxf86vm is found on OpenBSD too + find_library(XXF86VM_LIBRARY Xxf86vm) + set(CLIENT_PLATFORM_LIBS ${CLIENT_PLATFORM_LIBS} ${XXF86VM_LIBRARY}) +endif() + +find_package(Jthread REQUIRED) +find_package(Sqlite3 REQUIRED) + +configure_file( + "${PROJECT_SOURCE_DIR}/cmake_config.h.in" + "${PROJECT_BINARY_DIR}/cmake_config.h" +) + +set(common_SRCS + log.cpp + content_sao.cpp + mapgen.cpp + content_inventory.cpp + content_nodemeta.cpp + content_craft.cpp + content_craftitem.cpp + content_toolitem.cpp + content_mapnode.cpp + content_list.cpp + content_nodebox.cpp + auth.cpp + collision.cpp + nodemetadata.cpp + serverobject.cpp + noise.cpp + mineral.cpp + porting.cpp + defaultsettings.cpp + mapnode.cpp + voxel.cpp + inventory.cpp + debug.cpp + serialization.cpp + light.cpp + filesys.cpp + connection.cpp + environment.cpp + server.cpp + servercommand.cpp + socket.cpp + mapblock.cpp + mapsector.cpp + map.cpp + mesh.cpp + player.cpp + utility.cpp + test.cpp + sha1.cpp + base64.cpp ban.cpp - http.cpp - path.cpp -) - -# This gives us the icon -if(WIN32) - if(MINGW) - ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/winresource_rc.o - COMMAND windres.exe -I${CMAKE_CURRENT_SOURCE_DIR} - -i${CMAKE_CURRENT_SOURCE_DIR}/winresource.rc - -o ${CMAKE_CURRENT_BINARY_DIR}/winresource_rc.o) - SET(common_SRCS ${common_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/winresource_rc.o) - else(MINGW) # Probably MSVC - set(common_SRCS ${common_SRCS} winresource.rc) - endif(MINGW) -endif() - -# Client sources -set(minetest_SRCS - ${common_SRCS} - content_mapblock.cpp - content_cao.cpp - mapblock_mesh.cpp - mesh.cpp - farmesh.cpp - keycode.cpp - camera.cpp - clouds.cpp - particles.cpp - clientobject.cpp - guiMainMenu.cpp - guiKeyChangeMenu.cpp - guiMessageMenu.cpp - guiTextInputMenu.cpp - guiFormSpecMenu.cpp - guiPauseMenu.cpp - guiPasswordChange.cpp - guiDeathScreen.cpp - client.cpp - tile.cpp - game.cpp - main.cpp -) - -# Server sources -set(minetestserver_SRCS - ${common_SRCS} - servermain.cpp -) - -include_directories( - ${PROJECT_BINARY_DIR} - ${IRRLICHT_INCLUDE_DIR} - ${ZLIB_INCLUDE_DIR} - ${CMAKE_BUILD_TYPE} - ${PNG_INCLUDE_DIR} - ${GETTEXT_INCLUDE_DIR} - ${SQLITE3_INCLUDE_DIR} -) - -set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin") - -if(BUILD_CLIENT) - add_executable(${PROJECT_NAME} ${minetest_SRCS}) - target_link_libraries( - ${PROJECT_NAME} - ${ZLIB_LIBRARIES} - ${IRRLICHT_LIBRARY} - ${OPENGL_LIBRARIES} - ${JPEG_LIBRARIES} - ${BZIP2_LIBRARIES} - ${PNG_LIBRARIES} - ${X11_LIBRARIES} - ${GETTEXT_LIBRARY} - ${SQLITE3_LIBRARY} - ${PLATFORM_LIBS} - ${CLIENT_PLATFORM_LIBS} - ) -endif(BUILD_CLIENT) - -if(BUILD_SERVER) - add_executable(${PROJECT_NAME}-server ${minetestserver_SRCS}) - target_link_libraries( - ${PROJECT_NAME}-server - ${ZLIB_LIBRARIES} - ${SQLITE3_LIBRARY} - ${PLATFORM_LIBS} - ) -endif(BUILD_SERVER) - -# -# Set some optimizations and tweaks -# - -include(CheckCXXCompilerFlag) - -if(MSVC) - # Visual Studio - - # EHa enables SEH exceptions (used for catching segfaults) - set(CMAKE_CXX_FLAGS_RELEASE "/EHa /O2 /Ob2 /Oi /Ot /Oy /GL /FD /MT /GS- /arch:SSE /fp:fast /D NDEBUG /D _HAS_ITERATOR_DEBUGGING=0 /TP") - #set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG /NODEFAULTLIB:\"libcmtd.lib\" /NODEFAULTLIB:\"libcmt.lib\"") - set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG") - - # Debug build doesn't catch exceptions by itself - # Add some optimizations because otherwise it's VERY slow - set(CMAKE_CXX_FLAGS_DEBUG "/MDd /Zi /Ob0 /Od /RTC1") - - # Flags for C files (sqlite) - # /MT = Link statically with standard library stuff - set(CMAKE_C_FLAGS_RELEASE "/O2 /Ob2 /MT") - - if(BUILD_SERVER) - set_target_properties(${PROJECT_NAME}-server PROPERTIES - COMPILE_DEFINITIONS "SERVER") - endif(BUILD_SERVER) - -else() - # Probably GCC - - if(WARN_ALL) - set(RELEASE_WARNING_FLAGS "-Wall") - else() - set(RELEASE_WARNING_FLAGS "") - endif() - - if(NOT APPLE AND NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - CHECK_CXX_COMPILER_FLAG("-Wno-unused-but-set-variable" HAS_UNUSED_BUT_SET_VARIABLE_WARNING) - if(HAS_UNUSED_BUT_SET_VARIABLE_WARNING) - set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-unused-but-set-variable") - endif(HAS_UNUSED_BUT_SET_VARIABLE_WARNING) - endif() - - if(APPLE) - set(CMAKE_OSX_ARCHITECTURES i386 CACHE STRING "do not build for 64-bit" FORCE) - set(ARCH i386) - endif() - - set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${RELEASE_WARNING_FLAGS} ${WARNING_FLAGS} -O3 -ffast-math -Wall -fomit-frame-pointer -pipe -funroll-loops") - set(CMAKE_CXX_FLAGS_DEBUG "-g -O1 -Wall ${WARNING_FLAGS}") - - if(USE_GPROF) - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pg") - endif() - - if(BUILD_SERVER) - set_target_properties(${PROJECT_NAME}-server PROPERTIES - COMPILE_DEFINITIONS "SERVER") - endif(BUILD_SERVER) - -endif() - -#MESSAGE(STATUS "CMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}") -#MESSAGE(STATUS "CMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}") - -# -# Installation -# - -# Example configuration file -install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/../minetest.conf.example" DESTINATION ${EXAMPLE_CONF_DIR}) - -if(BUILD_CLIENT) - install(TARGETS ${PROJECT_NAME} DESTINATION ${BINDIR}) - - file(GLOB images "${CMAKE_CURRENT_SOURCE_DIR}/../data/textures/*.png") - install(FILES ${images} DESTINATION ${DATADIR}/textures) - file(GLOB models -"${CMAKE_CURRENT_SOURCE_DIR}/../data/models/*.b3d") - install(FILES ${models} DESTINATION ${DATADIR}/models) - file(GLOB modelsx -"${CMAKE_CURRENT_SOURCE_DIR}/../data/models/*.x") - install(FILES ${modelsx} DESTINATION ${DATADIR}/models) - - if(USE_GETTEXT) - foreach(LOCALE ${GETTEXT_AVAILABLE_LOCALES}) - set_mo_paths(MO_BUILD_PATH MO_DEST_PATH ${LOCALE}) - set(MO_BUILD_PATH "${MO_BUILD_PATH}/${PROJECT_NAME}.mo") - install(FILES ${MO_BUILD_PATH} DESTINATION ${MO_DEST_PATH}) - endforeach(LOCALE ${GETTEXT_AVAILABLE_LOCALES}) - endif() - - if(WIN32) - if(DEFINED IRRLICHT_DLL) - install(FILES ${IRRLICHT_DLL} DESTINATION ${BINDIR}) - endif() - if(DEFINED ZLIB_DLL) - install(FILES ${ZLIB_DLL} DESTINATION ${BINDIR}) - endif() - if(USE_GETTEXT) - if(DEFINED GETTEXT_DLL) - install(FILES ${GETTEXT_DLL} DESTINATION ${BINDIR}) - endif() - if(DEFINED GETTEXT_ICONV_DLL) - install(FILES ${GETTEXT_ICONV_DLL} DESTINATION ${BINDIR}) - endif() - endif(USE_GETTEXT) - endif() -endif(BUILD_CLIENT) - -if(BUILD_SERVER) - install(TARGETS ${PROJECT_NAME}-server DESTINATION ${BINDIR}) -endif(BUILD_SERVER) - -if (USE_GETTEXT) - set(MO_FILES) - - foreach(LOCALE ${GETTEXT_AVAILABLE_LOCALES}) - set(PO_FILE_PATH "${GETTEXT_PO_PATH}/${LOCALE}/minetest.po") - set_mo_paths(MO_BUILD_PATH MO_DEST_PATH ${LOCALE}) - set(MO_FILE_PATH "${MO_BUILD_PATH}/${PROJECT_NAME}.mo") - - add_custom_command(OUTPUT ${MO_BUILD_PATH} - COMMAND ${CMAKE_COMMAND} -E make_directory ${MO_BUILD_PATH} - COMMENT "mo-update [${LOCALE}]: Creating locale directory.") - - add_custom_command( - OUTPUT ${MO_FILE_PATH} - COMMAND ${GETTEXT_MSGFMT} -o ${MO_FILE_PATH} ${PO_FILE_PATH} - DEPENDS ${MO_BUILD_PATH} ${PO_FILE_PATH} - WORKING_DIRECTORY "${GETTEXT_PO_PATH}/${LOCALE}" - COMMENT "mo-update [${LOCALE}]: Creating mo file." - ) - - set(MO_FILES ${MO_FILES} ${MO_FILE_PATH}) - endforeach(LOCALE ${GETTEXT_AVAILABLE_LOCALES}) - - add_custom_target(translations ALL COMMENT "mo update" DEPENDS ${MO_FILES}) -endif(USE_GETTEXT) - -# Subdirectories - -if (SQLITE3_FOUND) -else (SQLITE3_FOUND) -add_subdirectory(sqlite) -endif (SQLITE3_FOUND) + http.cpp + path.cpp +) + +# This gives us the icon +if(WIN32) + if(MINGW) + ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/winresource_rc.o + COMMAND windres.exe -I${CMAKE_CURRENT_SOURCE_DIR} + -i${CMAKE_CURRENT_SOURCE_DIR}/winresource.rc + -o ${CMAKE_CURRENT_BINARY_DIR}/winresource_rc.o) + SET(common_SRCS ${common_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/winresource_rc.o) + else(MINGW) # Probably MSVC + set(common_SRCS ${common_SRCS} winresource.rc) + endif(MINGW) +endif() + +# Client sources +set(minetest_SRCS + ${common_SRCS} + content_mapblock.cpp + content_cao.cpp + mapblock_mesh.cpp + farmesh.cpp + keycode.cpp + camera.cpp + clouds.cpp + particles.cpp + clientobject.cpp + guiMainMenu.cpp + guiKeyChangeMenu.cpp + guiMessageMenu.cpp + guiTextInputMenu.cpp + guiFormSpecMenu.cpp + guiPauseMenu.cpp + guiPasswordChange.cpp + guiDeathScreen.cpp + client.cpp + tile.cpp + game.cpp + main.cpp +) + +# Server sources +set(minetestserver_SRCS + ${common_SRCS} + servermain.cpp +) + +include_directories( + ${PROJECT_BINARY_DIR} + ${IRRLICHT_INCLUDE_DIR} + ${ZLIB_INCLUDE_DIR} + ${CMAKE_BUILD_TYPE} + ${PNG_INCLUDE_DIR} + ${GETTEXT_INCLUDE_DIR} + ${JTHREAD_INCLUDE_DIR} + ${SQLITE3_INCLUDE_DIR} +) + +set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin") + +if(BUILD_CLIENT) + add_executable(${PROJECT_NAME} ${minetest_SRCS}) + target_link_libraries( + ${PROJECT_NAME} + ${ZLIB_LIBRARIES} + ${IRRLICHT_LIBRARY} + ${OPENGL_LIBRARIES} + ${JPEG_LIBRARIES} + ${BZIP2_LIBRARIES} + ${PNG_LIBRARIES} + ${X11_LIBRARIES} + ${GETTEXT_LIBRARY} + ${JTHREAD_LIBRARY} + ${SQLITE3_LIBRARY} + ${PLATFORM_LIBS} + ${CLIENT_PLATFORM_LIBS} + ) +endif(BUILD_CLIENT) + +if(BUILD_SERVER) + add_executable(${PROJECT_NAME}-server ${minetestserver_SRCS}) + target_link_libraries( + ${PROJECT_NAME}-server + ${ZLIB_LIBRARIES} + ${JTHREAD_LIBRARY} + ${SQLITE3_LIBRARY} + ${PLATFORM_LIBS} + ) +endif(BUILD_SERVER) + +# +# Set some optimizations and tweaks +# + +include(CheckCXXCompilerFlag) + +if(MSVC) + # Visual Studio + + # EHa enables SEH exceptions (used for catching segfaults) + set(CMAKE_CXX_FLAGS_RELEASE "/EHa /O2 /Ob2 /Oi /Ot /Oy /GL /FD /MT /GS- /arch:SSE /fp:fast /D NDEBUG /D _HAS_ITERATOR_DEBUGGING=0 /TP") + #set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG /NODEFAULTLIB:\"libcmtd.lib\" /NODEFAULTLIB:\"libcmt.lib\"") + set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG") + + # Debug build doesn't catch exceptions by itself + # Add some optimizations because otherwise it's VERY slow + set(CMAKE_CXX_FLAGS_DEBUG "/MDd /Zi /Ob0 /Od /RTC1") + + # Flags for C files (sqlite) + # /MT = Link statically with standard library stuff + set(CMAKE_C_FLAGS_RELEASE "/O2 /Ob2 /MT") + + if(BUILD_SERVER) + set_target_properties(${PROJECT_NAME}-server PROPERTIES + COMPILE_DEFINITIONS "SERVER") + endif(BUILD_SERVER) + +else() + # Probably GCC + + if(WARN_ALL) + set(RELEASE_WARNING_FLAGS "-Wall") + else() + set(RELEASE_WARNING_FLAGS "") + endif() + + if(NOT APPLE AND NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + CHECK_CXX_COMPILER_FLAG("-Wno-unused-but-set-variable" HAS_UNUSED_BUT_SET_VARIABLE_WARNING) + if(HAS_UNUSED_BUT_SET_VARIABLE_WARNING) + set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-unused-but-set-variable") + endif(HAS_UNUSED_BUT_SET_VARIABLE_WARNING) + endif() + + if(APPLE) + set(CMAKE_OSX_ARCHITECTURES i386 CACHE STRING "do not build for 64-bit" FORCE) + set(ARCH i386) + endif() + + set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${RELEASE_WARNING_FLAGS} ${WARNING_FLAGS} -O3 -ffast-math -Wall -fomit-frame-pointer -pipe -funroll-loops") + set(CMAKE_CXX_FLAGS_DEBUG "-g -O1 -Wall ${WARNING_FLAGS}") + + if(USE_GPROF) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pg") + endif() + + if(BUILD_SERVER) + set_target_properties(${PROJECT_NAME}-server PROPERTIES + COMPILE_DEFINITIONS "SERVER") + endif(BUILD_SERVER) + +endif() + +#MESSAGE(STATUS "CMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}") +#MESSAGE(STATUS "CMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}") + +# +# Installation +# + +# Example configuration file +install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/../minetest.conf.example" DESTINATION ${EXAMPLE_CONF_DIR}) + +if(BUILD_CLIENT) + install(TARGETS ${PROJECT_NAME} DESTINATION ${BINDIR}) + + file(GLOB images "${CMAKE_CURRENT_SOURCE_DIR}/../data/textures/*.png") + install(FILES ${images} DESTINATION ${DATADIR}/textures) + file(GLOB models +"${CMAKE_CURRENT_SOURCE_DIR}/../data/models/*.b3d") + install(FILES ${models} DESTINATION ${DATADIR}/models) + file(GLOB modelsx +"${CMAKE_CURRENT_SOURCE_DIR}/../data/models/*.x") + install(FILES ${modelsx} DESTINATION ${DATADIR}/models) + + if(USE_GETTEXT) + foreach(LOCALE ${GETTEXT_AVAILABLE_LOCALES}) + set_mo_paths(MO_BUILD_PATH MO_DEST_PATH ${LOCALE}) + set(MO_BUILD_PATH "${MO_BUILD_PATH}/${PROJECT_NAME}.mo") + install(FILES ${MO_BUILD_PATH} DESTINATION ${MO_DEST_PATH}) + endforeach(LOCALE ${GETTEXT_AVAILABLE_LOCALES}) + endif() + + if(WIN32) + if(DEFINED IRRLICHT_DLL) + install(FILES ${IRRLICHT_DLL} DESTINATION ${BINDIR}) + endif() + if(DEFINED ZLIB_DLL) + install(FILES ${ZLIB_DLL} DESTINATION ${BINDIR}) + endif() + if(USE_GETTEXT) + if(DEFINED GETTEXT_DLL) + install(FILES ${GETTEXT_DLL} DESTINATION ${BINDIR}) + endif() + if(DEFINED GETTEXT_ICONV_DLL) + install(FILES ${GETTEXT_ICONV_DLL} DESTINATION ${BINDIR}) + endif() + endif(USE_GETTEXT) + endif() +endif(BUILD_CLIENT) + +if(BUILD_SERVER) + install(TARGETS ${PROJECT_NAME}-server DESTINATION ${BINDIR}) +endif(BUILD_SERVER) + +if (USE_GETTEXT) + set(MO_FILES) + + foreach(LOCALE ${GETTEXT_AVAILABLE_LOCALES}) + set(PO_FILE_PATH "${GETTEXT_PO_PATH}/${LOCALE}/minetest.po") + set_mo_paths(MO_BUILD_PATH MO_DEST_PATH ${LOCALE}) + set(MO_FILE_PATH "${MO_BUILD_PATH}/${PROJECT_NAME}.mo") + + add_custom_command(OUTPUT ${MO_BUILD_PATH} + COMMAND ${CMAKE_COMMAND} -E make_directory ${MO_BUILD_PATH} + COMMENT "mo-update [${LOCALE}]: Creating locale directory.") + + add_custom_command( + OUTPUT ${MO_FILE_PATH} + COMMAND ${GETTEXT_MSGFMT} -o ${MO_FILE_PATH} ${PO_FILE_PATH} + DEPENDS ${MO_BUILD_PATH} ${PO_FILE_PATH} + WORKING_DIRECTORY "${GETTEXT_PO_PATH}/${LOCALE}" + COMMENT "mo-update [${LOCALE}]: Creating mo file." + ) + + set(MO_FILES ${MO_FILES} ${MO_FILE_PATH}) + endforeach(LOCALE ${GETTEXT_AVAILABLE_LOCALES}) + + add_custom_target(translations ALL COMMENT "mo update" DEPENDS ${MO_FILES}) +endif(USE_GETTEXT) + +# Subdirectories + +if (JTHREAD_FOUND) +else (JTHREAD_FOUND) + add_subdirectory(jthread) +endif (JTHREAD_FOUND) + +if (SQLITE3_FOUND) +else (SQLITE3_FOUND) +add_subdirectory(sqlite) +endif (SQLITE3_FOUND) #end diff --git a/src/auth.cpp b/src/auth.cpp index 818171d..a33392a 100644 --- a/src/auth.cpp +++ b/src/auth.cpp @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "auth.h" #include +#include //#include "main.h" // for g_settings #include #include "strfnd.h" @@ -83,7 +84,7 @@ AuthManager::AuthManager(const std::string &authfilepath): m_authfilepath(authfilepath), m_modified(false) { - m_mutex.init(); + m_mutex.Init(); try{ load(); @@ -102,7 +103,7 @@ AuthManager::~AuthManager() void AuthManager::load() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); dstream<<"AuthManager: loading from "<::Node *n; n = m_authdata.find(username); @@ -185,7 +186,7 @@ bool AuthManager::exists(const std::string &username) void AuthManager::set(const std::string &username, AuthData ad) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); m_authdata[username] = ad; @@ -194,7 +195,7 @@ void AuthManager::set(const std::string &username, AuthData ad) void AuthManager::add(const std::string &username) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); m_authdata[username] = AuthData(); @@ -203,7 +204,7 @@ void AuthManager::add(const std::string &username) std::string AuthManager::getPassword(const std::string &username) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); core::map::Node *n; n = m_authdata.find(username); @@ -216,7 +217,7 @@ std::string AuthManager::getPassword(const std::string &username) void AuthManager::setPassword(const std::string &username, const std::string &password) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); core::map::Node *n; n = m_authdata.find(username); @@ -232,7 +233,7 @@ void AuthManager::setPassword(const std::string &username, u64 AuthManager::getPrivs(const std::string &username) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); core::map::Node *n; n = m_authdata.find(username); @@ -244,7 +245,7 @@ u64 AuthManager::getPrivs(const std::string &username) void AuthManager::setPrivs(const std::string &username, u64 privs) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); core::map::Node *n; n = m_authdata.find(username); @@ -260,7 +261,7 @@ void AuthManager::setPrivs(const std::string &username, u64 privs) bool AuthManager::isModified() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); return m_modified; } diff --git a/src/auth.h b/src/auth.h index ac31f71..f99ddfb 100644 --- a/src/auth.h +++ b/src/auth.h @@ -21,10 +21,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #define AUTH_HEADER #include +#include +#include #include "common_irrlicht.h" #include "exceptions.h" -#include "porting.h" -#include "threads.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 @@ -92,7 +94,7 @@ public: void setPrivs(const std::string &username, u64 privs); bool isModified(); private: - SimpleMutex m_mutex; + JMutex m_mutex; std::string m_authfilepath; core::map m_authdata; bool m_modified; diff --git a/src/ban.cpp b/src/ban.cpp index dd7f7d7..cb51d3f 100644 --- a/src/ban.cpp +++ b/src/ban.cpp @@ -19,17 +19,17 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "ban.h" #include +#include #include #include #include "strfnd.h" #include "debug.h" -#include "threads.h" BanManager::BanManager(const std::string &banfilepath): m_banfilepath(banfilepath), m_modified(false) { - m_mutex.init(); + m_mutex.Init(); try{ load(); } @@ -47,7 +47,7 @@ BanManager::~BanManager() void BanManager::load() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); dstream<<"BanManager: loading from "<::iterator i = m_ips.begin(); @@ -117,7 +117,7 @@ std::string BanManager::getBanDescription(const std::string &ip_or_name) std::string BanManager::getBanName(const std::string &ip) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); std::map::iterator i = m_ips.find(ip); if(i == m_ips.end()) return ""; @@ -126,14 +126,14 @@ std::string BanManager::getBanName(const std::string &ip) void BanManager::add(const std::string &ip, const std::string &name) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); m_ips[ip] = name; m_modified = true; } void BanManager::remove(const std::string &ip_or_name) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); //m_ips.erase(m_ips.find(ip)); // Find out all ip-name pairs that match the ip or name std::set ips_to_delete; @@ -157,7 +157,7 @@ void BanManager::remove(const std::string &ip_or_name) bool BanManager::isModified() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); return m_modified; } diff --git a/src/ban.h b/src/ban.h index c648152..7641dee 100644 --- a/src/ban.h +++ b/src/ban.h @@ -22,10 +22,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include +#include +#include #include "common_irrlicht.h" #include "exceptions.h" -#include "porting.h" -#include "threads.h" + +using namespace jthread; class BanManager { @@ -42,7 +44,7 @@ public: void remove(const std::string &ip_or_name); bool isModified(); private: - SimpleMutex m_mutex; + JMutex m_mutex; std::string m_banfilepath; std::map m_ips; bool m_modified; diff --git a/src/client.cpp b/src/client.cpp index eb1cb44..0cb8b71 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "utility.h" #include #include "clientserver.h" +#include "jmutexautolock.h" #include "main.h" #include #include "porting.h" @@ -31,7 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "profiler.h" #include "log.h" #include "http.h" -#include "threads.h" /* QueuedMeshUpdate @@ -56,12 +56,12 @@ QueuedMeshUpdate::~QueuedMeshUpdate() MeshUpdateQueue::MeshUpdateQueue() { - m_mutex.init(); + m_mutex.Init(); } MeshUpdateQueue::~MeshUpdateQueue() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); core::list::Iterator i; for(i=m_queue.begin(); i!=m_queue.end(); i++) @@ -80,7 +80,7 @@ void MeshUpdateQueue::addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_se assert(data); - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); /* Find if block is already in queue. @@ -115,7 +115,7 @@ void MeshUpdateQueue::addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_se // Returns NULL if queue is empty QueuedMeshUpdate * MeshUpdateQueue::pop() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); core::list::Iterator i = m_queue.begin(); if(i == m_queue.end()) @@ -217,7 +217,7 @@ Client::Client( Add local player */ { - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out Player *player = new LocalPlayer(); @@ -233,7 +233,7 @@ Client::Client( Client::~Client() { { - //SimpleMutexAutoLock conlock(m_con_mutex); //bulk comment-out + //JMutexAutoLock conlock(m_con_mutex); //bulk comment-out m_con.Disconnect(); } if (g_settings->getBool("enable_http")) @@ -245,7 +245,7 @@ Client::~Client() void Client::connect(Address address) { DSTACK(__FUNCTION_NAME); - //SimpleMutexAutoLock lock(m_con_mutex); //bulk comment-out + //JMutexAutoLock lock(m_con_mutex); //bulk comment-out m_con.SetTimeoutMs(0); m_con.Connect(address); if (g_settings->getBool("enable_http")) @@ -254,7 +254,7 @@ void Client::connect(Address address) bool Client::connectedAndInitialized() { - //SimpleMutexAutoLock lock(m_con_mutex); //bulk comment-out + //JMutexAutoLock lock(m_con_mutex); //bulk comment-out if(m_con.Connected() == false) return false; @@ -289,7 +289,7 @@ void Client::step(float dtime) { //TimeTaker timer("m_con_mutex + m_con.RunTimeouts()", m_device); // 0ms - //SimpleMutexAutoLock lock(m_con_mutex); //bulk comment-out + //JMutexAutoLock lock(m_con_mutex); //bulk comment-out m_con.RunTimeouts(dtime); } @@ -329,7 +329,7 @@ void Client::step(float dtime) //counter = 180.0; counter = 60.0; - //SimpleMutexAutoLock lock(m_env_mutex); //bulk comment-out + //JMutexAutoLock lock(m_env_mutex); //bulk comment-out core::list deleted_blocks; @@ -358,7 +358,7 @@ void Client::step(float dtime) */ // Env is locked so con can be locked. - //SimpleMutexAutoLock lock(m_con_mutex); //bulk comment-out + //JMutexAutoLock lock(m_con_mutex); //bulk comment-out core::list::Iterator i = deleted_blocks.begin(); core::list sendlist; @@ -411,7 +411,7 @@ void Client::step(float dtime) { counter = 2.0; - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out Player *myplayer = m_env.getLocalPlayer(); assert(myplayer != NULL); @@ -516,7 +516,7 @@ void Client::step(float dtime) */ { // 0ms - //SimpleMutexAutoLock lock(m_env_mutex); //bulk comment-out + //JMutexAutoLock lock(m_env_mutex); //bulk comment-out // Control local player (0ms) LocalPlayer *player = m_env.getLocalPlayer(); @@ -563,7 +563,7 @@ void Client::step(float dtime) if(counter >= 10) { counter = 0.0; - //SimpleMutexAutoLock lock(m_con_mutex); //bulk comment-out + //JMutexAutoLock lock(m_con_mutex); //bulk comment-out // connectedAndInitialized() is true, peer exists. float avg_rtt = m_con.GetPeerAvgRTT(PEER_ID_SERVER); infostream<<"Client: avg_rtt="< data, bool reliable) { - //SimpleMutexAutoLock lock(m_con_mutex); //bulk comment-out + //JMutexAutoLock lock(m_con_mutex); //bulk comment-out m_con.Send(PEER_ID_SERVER, channelnum, data, reliable); } @@ -1794,7 +1794,7 @@ void Client::sendWantCookie() void Client::sendPlayerPos() { - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out Player *myplayer = m_env.getLocalPlayer(); if(myplayer == NULL) @@ -1802,7 +1802,7 @@ void Client::sendPlayerPos() u16 our_peer_id; { - //SimpleMutexAutoLock lock(m_con_mutex); //bulk comment-out + //JMutexAutoLock lock(m_con_mutex); //bulk comment-out our_peer_id = m_con.GetPeerID(); } @@ -1863,7 +1863,7 @@ void Client::sendPlayerItem(u16 item) void Client::removeNode(v3s16 p) { - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out core::map modified_blocks; @@ -1888,7 +1888,7 @@ void Client::removeNode(v3s16 p) void Client::addNode(v3s16 p, MapNode n) { - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out TimeTaker timer1("Client::addNode()"); @@ -1927,7 +1927,7 @@ void Client::renderPostFx() MapNode Client::getNode(v3s16 p) { - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out return m_env.getMap().getNode(p); } @@ -1943,7 +1943,7 @@ LocalPlayer* Client::getLocalPlayer() void Client::setPlayerControl(PlayerControl &control) { - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->control = control; @@ -1964,7 +1964,7 @@ void Client::selectPlayerItem(u16 item) bool Client::getLocalInventoryUpdated() { // m_inventory_updated is behind envlock - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out bool updated = m_inventory_updated; m_inventory_updated = false; return updated; @@ -1973,7 +1973,7 @@ bool Client::getLocalInventoryUpdated() // Copies the inventory of the local player to parameter void Client::getLocalInventory(Inventory &dst) { - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out Player *player = m_env.getLocalPlayer(); assert(player != NULL); dst = player->inventory; @@ -2096,8 +2096,8 @@ ClientActiveObject * Client::getSelectedActiveObject( void Client::printDebugInfo(std::ostream &os) { - //SimpleMutexAutoLock lock1(m_fetchblock_mutex); - /*SimpleMutexAutoLock lock2(m_incoming_queue_mutex); + //JMutexAutoLock lock1(m_fetchblock_mutex); + /*JMutexAutoLock lock2(m_incoming_queue_mutex); os<<"m_incoming_queue.getSize()="< #include "clientobject.h" #include "particles.h" #include "utility.h" // For IntervalLimiter -#include "threads.h" struct MeshMakeData; @@ -72,13 +72,13 @@ public: u32 size() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); return m_queue.size(); } private: core::list m_queue; - SimpleMutex m_mutex; + JMutex m_mutex; }; class MapBlockMesh; @@ -287,7 +287,7 @@ public: return; } - //SimpleMutexAutoLock envlock(m_env_mutex); //bulk comment-out + //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); std::wstring name = narrow_to_wide(player->getName()); diff --git a/src/connection.cpp b/src/connection.cpp index e696d94..2f19925 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -543,7 +543,6 @@ Connection::~Connection() void * Connection::Thread() { -printf("Connection::Thread()\n"); log_register_thread("Connection"); dout_con<<"Connection thread started"<address; } float Connection::GetPeerAvgRTT(u16 peer_id) { - SimpleMutexAutoLock peerlock(m_peers_mutex); + JMutexAutoLock peerlock(m_peers_mutex); return getPeer(peer_id)->avg_rtt; } diff --git a/src/connection.h b/src/connection.h index 8529fd5..e4fffce 100644 --- a/src/connection.h +++ b/src/connection.h @@ -609,7 +609,7 @@ private: u16 m_peer_id; core::map m_peers; - SimpleMutex m_peers_mutex; + JMutex m_peers_mutex; // Backwards compatibility PeerHandler *m_bc_peerhandler; diff --git a/src/debug.cpp b/src/debug.cpp index ff03b1c..7668006 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -115,16 +115,16 @@ void DebugStack::print(FILE *file, bool everything) core::map g_debug_stacks; -SimpleMutex g_debug_stacks_mutex; +JMutex g_debug_stacks_mutex; void debug_stacks_init() { - g_debug_stacks_mutex.init(); + g_debug_stacks_mutex.Init(); } void debug_stacks_print() { - SimpleMutexAutoLock lock(g_debug_stacks_mutex); + JMutexAutoLock lock(g_debug_stacks_mutex); DEBUGPRINT("Debug stacks:\n"); @@ -146,7 +146,7 @@ DebugStacker::DebugStacker(const char *text) { threadid_t threadid = get_current_thread_id(); - SimpleMutexAutoLock lock(g_debug_stacks_mutex); + JMutexAutoLock lock(g_debug_stacks_mutex); core::map::Node *n; n = g_debug_stacks.find(threadid); @@ -180,7 +180,7 @@ DebugStacker::DebugStacker(const char *text) DebugStacker::~DebugStacker() { - SimpleMutexAutoLock lock(g_debug_stacks_mutex); + JMutexAutoLock lock(g_debug_stacks_mutex); if(m_overflowed == true) return; diff --git a/src/debug.h b/src/debug.h index f6bc3e3..42a3ad8 100644 --- a/src/debug.h +++ b/src/debug.h @@ -21,6 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #define DEBUG_HEADER #include +#include +#include #include #include "common_irrlicht.h" #include "threads.h" @@ -37,6 +39,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #else #endif +using namespace jthread; + /* Debug output */ @@ -125,7 +129,7 @@ extern Nullstream dummyout; Assert */ -extern void assert_fail( +__NORETURN extern void assert_fail( const char *assertion, const char *file, unsigned int line, const char *function); @@ -155,7 +159,7 @@ struct DebugStack }; extern core::map g_debug_stacks; -extern SimpleMutex g_debug_stacks_mutex; +extern JMutex g_debug_stacks_mutex; extern void debug_stacks_init(); extern void debug_stacks_print(); diff --git a/src/http.cpp b/src/http.cpp index be61f57..2cbafaa 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -561,7 +561,7 @@ HTTPClient::HTTPClient(Client *client): m_thread(this) { m_client = client; - m_req_mutex.init(); + m_req_mutex.Init(); } /* destructor */ @@ -613,24 +613,24 @@ void HTTPClient::step() std::vector p; - m_req_mutex.lock(); + m_req_mutex.Lock(); p.swap(m_requests); - m_req_mutex.unlock(); + m_req_mutex.Unlock(); for (std::vector::iterator i = p.begin(); i != p.end(); ++i) { HTTPRequest q = *i; m_socket = new TCPSocket(); if (m_socket == NULL) { - m_req_mutex.lock(); + m_req_mutex.Lock(); m_requests.push_back(q); - m_req_mutex.unlock(); + m_req_mutex.Unlock(); continue; } if (m_socket->Connect(m_address) == false) { delete m_socket; - m_req_mutex.lock(); + m_req_mutex.Lock(); m_requests.push_back(q); - m_req_mutex.unlock(); + m_req_mutex.Unlock(); m_connection_failures++; /* assume the server has no http */ if (m_connection_failures > 4) { @@ -657,9 +657,9 @@ void HTTPClient::step() h = m_recv_headers.read(m_socket); if (h == -1 || m_recv_headers.getResponse() == 500) { delete m_socket; - m_req_mutex.lock(); + m_req_mutex.Lock(); m_requests.push_back(q); - m_req_mutex.unlock(); + m_req_mutex.Unlock(); continue; } @@ -757,9 +757,9 @@ void HTTPClient::pushRequest(HTTPRequestType type, std::string &data) url += data + "/skin"; HTTPRequest r(url); r.data = data; - m_req_mutex.lock(); + m_req_mutex.Lock(); m_requests.push_back(r); - m_req_mutex.unlock(); + m_req_mutex.Unlock(); break; } /* @@ -795,9 +795,9 @@ void HTTPClient::pushRequest(HTTPRequestType type, std::string &data) url += data + "/skin/hash/" + buff; HTTPRequest r(url); r.data = data; - m_req_mutex.lock(); + m_req_mutex.Lock(); m_requests.push_back(r); - m_req_mutex.unlock(); + m_req_mutex.Unlock(); break; } /* @@ -819,9 +819,9 @@ void HTTPClient::pushRequest(HTTPRequestType type, std::string &data) std::string url("/player/"); url += data + "/skin"; HTTPRequest r(url,ptex,data); - m_req_mutex.lock(); + m_req_mutex.Lock(); m_requests.push_back(r); - m_req_mutex.unlock(); + m_req_mutex.Unlock(); break; } default:; @@ -834,9 +834,9 @@ void HTTPClient::pushRequest(std::string &url, std::string &data) if (m_thread.getRun() == false) return; HTTPRequest r(url,data); - m_req_mutex.lock(); + m_req_mutex.Lock(); m_requests.push_back(r); - m_req_mutex.unlock(); + m_req_mutex.Unlock(); } /* send a http GET request to the server */ diff --git a/src/http.h b/src/http.h index 1152c75..4ca1142 100644 --- a/src/http.h +++ b/src/http.h @@ -244,7 +244,7 @@ private: HTTPRequestHeaders m_send_headers; std::string m_cookie; std::vector m_requests; - SimpleMutex m_req_mutex; + JMutex m_req_mutex; HTTPClientThread m_thread; Client *m_client; int m_connection_failures; diff --git a/src/jthread/CMakeLists.txt b/src/jthread/CMakeLists.txt new file mode 100644 index 0000000..e120a9c --- /dev/null +++ b/src/jthread/CMakeLists.txt @@ -0,0 +1,29 @@ +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}) + +target_link_libraries( + jthread + ${jthread_platform_LIBS} +) + + +configure_file("${PROJECT_SOURCE_DIR}/jthread/jthreadconfig.h.in" + "${PROJECT_BINARY_DIR}/jthread/jthreadconfig.h") diff --git a/src/jthread/LICENSE.MIT b/src/jthread/LICENSE.MIT new file mode 100644 index 0000000..2aa4fd5 --- /dev/null +++ b/src/jthread/LICENSE.MIT @@ -0,0 +1,20 @@ +The license of JThread: + +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. + diff --git a/src/jthread/jmutex.h b/src/jthread/jmutex.h new file mode 100644 index 0000000..d4180cf --- /dev/null +++ b/src/jthread/jmutex.h @@ -0,0 +1,75 @@ +/* + + 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 JTHREAD_JMUTEX_H + +#define JTHREAD_JMUTEX_H + +#include "jthreadconfig.h" +#ifdef JTHREAD_CONFIG_WIN32THREADS + #ifndef _WIN32_WCE + #include + #endif // _WIN32_WCE + #include + #include +#else // using pthread + #include +#endif // JTHREAD_CONFIG_WIN32THREADS + +#define ERR_JMUTEX_ALREADYINIT -1 +#define ERR_JMUTEX_NOTINIT -2 +#define ERR_JMUTEX_CANTCREATEMUTEX -3 + +namespace jthread +{ + +class JTHREAD_IMPORTEXPORT JMutex +{ +public: + JMutex(); + ~JMutex(); + int Init(); + int Lock(); + int Unlock(); + bool IsInitialized() { return initialized; } +private: +#ifdef JTHREAD_CONFIG_WIN32THREADS +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION + CRITICAL_SECTION mutex; +#else // Use standard mutex + HANDLE mutex; +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION +#else // pthread mutex + pthread_mutex_t mutex; +#endif // JTHREAD_CONFIG_WIN32THREADS + bool initialized; +}; + +} // end namespace + +#endif // JTHREAD_JMUTEX_H + diff --git a/src/jthread/jmutexautolock.h b/src/jthread/jmutexautolock.h new file mode 100644 index 0000000..8f4d900 --- /dev/null +++ b/src/jthread/jmutexautolock.h @@ -0,0 +1,50 @@ +/* + + 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 JTHREAD_JMUTEXAUTOLOCK_H + +#define JTHREAD_JMUTEXAUTOLOCK_H + +#include "jthreadconfig.h" +#include "jmutex.h" + +namespace jthread +{ + +class JTHREAD_IMPORTEXPORT JMutexAutoLock +{ +public: + JMutexAutoLock(JMutex &m) : mutex(m) { mutex.Lock(); } + ~JMutexAutoLock() { mutex.Unlock(); } +private: + JMutex &mutex; +}; + +} // end namespace + +#endif // JTHREAD_JMUTEXAUTOLOCK_H + diff --git a/src/jthread/jthread.h b/src/jthread/jthread.h new file mode 100644 index 0000000..7f4dc37 --- /dev/null +++ b/src/jthread/jthread.h @@ -0,0 +1,83 @@ +/* + + 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 JTHREAD_JTHREAD_H + +#define JTHREAD_JTHREAD_H + +#include "jthreadconfig.h" +#include "jmutex.h" + +#define ERR_JTHREAD_CANTINITMUTEX -1 +#define ERR_JTHREAD_CANTSTARTTHREAD -2 +#define ERR_JTHREAD_THREADFUNCNOTSET -3 +#define ERR_JTHREAD_NOTRUNNING -4 +#define ERR_JTHREAD_ALREADYRUNNING -5 + +namespace jthread +{ + +class JTHREAD_IMPORTEXPORT JThread +{ +public: + JThread(); + virtual ~JThread(); + int Start(); + int Kill(); + virtual void *Thread() = 0; + bool IsRunning(); + void *GetReturnValue(); +protected: + void ThreadStarted(); +private: + +#ifdef JTHREAD_CONFIG_WIN32THREADS +#ifdef _WIN32_WCE + DWORD threadid; + static DWORD WINAPI TheThread(void *param); +#else + static UINT __stdcall TheThread(void *param); + UINT threadid; +#endif // _WIN32_WCE + HANDLE threadhandle; +#else // pthread type threads + static void *TheThread(void *param); + + pthread_t threadid; +#endif // JTHREAD_CONFIG_WIN32THREADS + void *retval; + bool running; + + JMutex runningmutex; + JMutex continuemutex,continuemutex2; + bool mutexinit; +}; + +} // 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 new file mode 100644 index 0000000..1f5f5ec --- /dev/null +++ b/src/jthread/pthread/jmutex.cpp @@ -0,0 +1,73 @@ +/* + + 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. + +*/ + +#include "jmutex.h" + +namespace jthread +{ + +JMutex::JMutex() +{ + initialized = false; +} + +JMutex::~JMutex() +{ + if (initialized) + pthread_mutex_destroy(&mutex); +} + +int JMutex::Init() +{ + if (initialized) + return ERR_JMUTEX_ALREADYINIT; + + pthread_mutex_init(&mutex,NULL); + initialized = true; + return 0; +} + +int JMutex::Lock() +{ + if (!initialized) + return ERR_JMUTEX_NOTINIT; + + pthread_mutex_lock(&mutex); + return 0; +} + +int JMutex::Unlock() +{ + if (!initialized) + return ERR_JMUTEX_NOTINIT; + + pthread_mutex_unlock(&mutex); + return 0; +} + +} // end namespace + diff --git a/src/jthread/pthread/jthread.cpp b/src/jthread/pthread/jthread.cpp new file mode 100644 index 0000000..647783f --- /dev/null +++ b/src/jthread/pthread/jthread.cpp @@ -0,0 +1,185 @@ +/* + + 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. + +*/ + +#include "jthread.h" +#include +#include +#include + +namespace jthread +{ + +JThread::JThread() +{ + retval = NULL; + mutexinit = false; + running = false; +} + +JThread::~JThread() +{ + Kill(); +} + +int JThread::Start() +{ + int status; + + if (!mutexinit) + { + if (!runningmutex.IsInitialized()) + { + if (runningmutex.Init() < 0) + return ERR_JTHREAD_CANTINITMUTEX; + } + if (!continuemutex.IsInitialized()) + { + if (continuemutex.Init() < 0) + return ERR_JTHREAD_CANTINITMUTEX; + } + if (!continuemutex2.IsInitialized()) + { + if (continuemutex2.Init() < 0) + return ERR_JTHREAD_CANTINITMUTEX; + } + mutexinit = true; + } + + runningmutex.Lock(); + if (running) + { + runningmutex.Unlock(); + return ERR_JTHREAD_ALREADYRUNNING; + } + runningmutex.Unlock(); + + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); + + continuemutex.Lock(); + status = pthread_create(&threadid,&attr,TheThread,this); + pthread_attr_destroy(&attr); + if (status != 0) + { + continuemutex.Unlock(); + return ERR_JTHREAD_CANTSTARTTHREAD; + } + + /* Wait until 'running' is set */ + + runningmutex.Lock(); + while (!running) + { + runningmutex.Unlock(); + + struct timespec req,rem; + + req.tv_sec = 0; + req.tv_nsec = 1000000; + nanosleep(&req,&rem); + + runningmutex.Lock(); + } + runningmutex.Unlock(); + + continuemutex.Unlock(); + + continuemutex2.Lock(); + continuemutex2.Unlock(); + return 0; +} + +int JThread::Kill() +{ + runningmutex.Lock(); + if (!running) + { + runningmutex.Unlock(); + return ERR_JTHREAD_NOTRUNNING; + } + pthread_cancel(threadid); + running = false; + runningmutex.Unlock(); + return 0; +} + +bool JThread::IsRunning() +{ + bool r; + + runningmutex.Lock(); + r = running; + runningmutex.Unlock(); + return r; +} + +void *JThread::GetReturnValue() +{ + void *val; + + runningmutex.Lock(); + if (running) + val = NULL; + else + val = retval; + runningmutex.Unlock(); + return val; +} + +void *JThread::TheThread(void *param) +{ + JThread *jthread; + void *ret; + + jthread = (JThread *)param; + + jthread->continuemutex2.Lock(); + jthread->runningmutex.Lock(); + jthread->running = true; + jthread->runningmutex.Unlock(); + + jthread->continuemutex.Lock(); + jthread->continuemutex.Unlock(); + + ret = jthread->Thread(); + + jthread->runningmutex.Lock(); + jthread->running = false; + jthread->retval = ret; + jthread->runningmutex.Unlock(); + + return NULL; +} + +void JThread::ThreadStarted() +{ + continuemutex2.Unlock(); +} + +} // end namespace + diff --git a/src/jthread/win32/jmutex.cpp b/src/jthread/win32/jmutex.cpp new file mode 100644 index 0000000..4126931 --- /dev/null +++ b/src/jthread/win32/jmutex.cpp @@ -0,0 +1,87 @@ +/* + + 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. + +*/ + +#include "jmutex.h" + +namespace jthread +{ + +JMutex::JMutex() +{ + initialized = false; +} + +JMutex::~JMutex() +{ + if (initialized) +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION + DeleteCriticalSection(&mutex); +#else + CloseHandle(mutex); +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION +} + +int JMutex::Init() +{ + if (initialized) + return ERR_JMUTEX_ALREADYINIT; +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION + InitializeCriticalSection(&mutex); +#else + mutex = CreateMutex(NULL,FALSE,NULL); + if (mutex == NULL) + return ERR_JMUTEX_CANTCREATEMUTEX; +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION + initialized = true; + return 0; +} + +int JMutex::Lock() +{ + if (!initialized) + return ERR_JMUTEX_NOTINIT; +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION + EnterCriticalSection(&mutex); +#else + WaitForSingleObject(mutex,INFINITE); +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION + return 0; +} + +int JMutex::Unlock() +{ + if (!initialized) + return ERR_JMUTEX_NOTINIT; +#ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION + LeaveCriticalSection(&mutex); +#else + ReleaseMutex(mutex); +#endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION + return 0; +} + +} // end namespace diff --git a/src/jthread/win32/jthread.cpp b/src/jthread/win32/jthread.cpp new file mode 100644 index 0000000..abe30ca --- /dev/null +++ b/src/jthread/win32/jthread.cpp @@ -0,0 +1,182 @@ +/* + + 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. + +*/ + +#include "jthread.h" +#include "jmutexautolock.h" + +#ifndef _WIN32_WCE + #include +#endif // _WIN32_WCE + +namespace jthread +{ + +JThread::JThread() +{ + retval = NULL; + mutexinit = false; + running = false; +} + +JThread::~JThread() +{ + Kill(); +} + +int JThread::Start() +{ + if (!mutexinit) + { + if (!runningmutex.IsInitialized()) + { + if (runningmutex.Init() < 0) + return ERR_JTHREAD_CANTINITMUTEX; + } + if (!continuemutex.IsInitialized()) + { + if (continuemutex.Init() < 0) + return ERR_JTHREAD_CANTINITMUTEX; + } + if (!continuemutex2.IsInitialized()) + { + if (continuemutex2.Init() < 0) + return ERR_JTHREAD_CANTINITMUTEX; + } mutexinit = true; + } + + runningmutex.Lock(); + if (running) + { + runningmutex.Unlock(); + return ERR_JTHREAD_ALREADYRUNNING; + } + runningmutex.Unlock(); + + continuemutex.Lock(); +#ifndef _WIN32_WCE + threadhandle = (HANDLE)_beginthreadex(NULL,0,TheThread,this,0,&threadid); +#else + threadhandle = CreateThread(NULL,0,TheThread,this,0,&threadid); +#endif // _WIN32_WCE + if (threadhandle == NULL) + { + continuemutex.Unlock(); + return ERR_JTHREAD_CANTSTARTTHREAD; + } + + /* Wait until 'running' is set */ + + runningmutex.Lock(); + while (!running) + { + runningmutex.Unlock(); + Sleep(1); + runningmutex.Lock(); + } + runningmutex.Unlock(); + + continuemutex.Unlock(); + + continuemutex2.Lock(); + continuemutex2.Unlock(); + + return 0; +} + +int JThread::Kill() +{ + runningmutex.Lock(); + if (!running) + { + runningmutex.Unlock(); + return ERR_JTHREAD_NOTRUNNING; + } + TerminateThread(threadhandle,0); + CloseHandle(threadhandle); + running = false; + runningmutex.Unlock(); + return 0; +} + +bool JThread::IsRunning() +{ + bool r; + + runningmutex.Lock(); + r = running; + runningmutex.Unlock(); + return r; +} + +void *JThread::GetReturnValue() +{ + JMutexAutoLock autolock(runningmutex); + void *val; + + if (running) + val = NULL; + else + val = retval; + return val; +} + +#ifndef _WIN32_WCE +UINT __stdcall JThread::TheThread(void *param) +#else +DWORD WINAPI JThread::TheThread(void *param) +#endif // _WIN32_WCE +{ + JThread *jthread; + void *ret; + + jthread = (JThread *)param; + + jthread->continuemutex2.Lock(); + jthread->runningmutex.Lock(); + jthread->running = true; + jthread->runningmutex.Unlock(); + + jthread->continuemutex.Lock(); + jthread->continuemutex.Unlock(); + + ret = jthread->Thread(); + + jthread->runningmutex.Lock(); + jthread->running = false; + jthread->retval = ret; + CloseHandle(jthread->threadhandle); + jthread->runningmutex.Unlock(); + return 0; +} + +void JThread::ThreadStarted() +{ + continuemutex2.Unlock(); +} + +} // end namespace + diff --git a/src/main.cpp b/src/main.cpp index 76b6866..7aaacff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1013,15 +1013,15 @@ void SpeedTests() dstream<<"Around 5000/ms should do well here."<(-BS*1000000,-BS*1000000,-BS*1000000, BS*1000000,BS*1000000,BS*1000000); @@ -3512,7 +3512,7 @@ ClientMap::ClientMap( ClientMap::~ClientMap() { - /*SimpleMutexAutoLock lock(mesh_mutex); + /*JMutexAutoLock lock(mesh_mutex); if(mesh != NULL) { @@ -3536,7 +3536,7 @@ MapSector * ClientMap::emergeSector(v2s16 p2d) ClientMapSector *sector = new ClientMapSector(this, p2d); { - //SimpleMutexAutoLock lock(m_sector_mutex); // Bulk comment-out + //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out m_sectors.insert(p2d, sector); } @@ -3549,7 +3549,7 @@ void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is) DSTACK(__FUNCTION_NAME); ClientMapSector *sector = NULL; - //SimpleMutexAutoLock lock(m_sector_mutex); // Bulk comment-out + //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out core::map::Node *n = m_sectors.find(p2d); @@ -3562,7 +3562,7 @@ void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is) { sector = new ClientMapSector(this, p2d); { - //SimpleMutexAutoLock lock(m_sector_mutex); // Bulk comment-out + //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out m_sectors.insert(p2d, sector); } } @@ -3646,12 +3646,12 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) //u32 daynight_ratio = m_client->getDayNightRatio(); - m_camera_mutex.lock(); + m_camera_mutex.Lock(); v3f camera_position = m_camera_position; v3f camera_direction = m_camera_direction; f32 camera_fov = m_camera_fov; v3s16 camera_offset = m_camera_offset; - m_camera_mutex.unlock(); + m_camera_mutex.Unlock(); /* Get all blocks and draw all visible ones @@ -3774,7 +3774,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) bool mesh_expired = false; { - SimpleMutexAutoLock lock(block->mesh_mutex); + JMutexAutoLock lock(block->mesh_mutex); mesh_expired = block->getMeshExpired(); @@ -3859,7 +3859,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) Ignore if mesh doesn't exist */ { - SimpleMutexAutoLock lock(block->mesh_mutex); + JMutexAutoLock lock(block->mesh_mutex); MapBlockMesh *mesh = block->mesh; @@ -3923,7 +3923,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) Draw the faces of the block */ { - SimpleMutexAutoLock lock(block->mesh_mutex); + JMutexAutoLock lock(block->mesh_mutex); MapBlockMesh *mesh = block->mesh; assert(mesh); @@ -4001,9 +4001,9 @@ void ClientMap::renderPostFx() // Sadly ISceneManager has no "post effects" render pass, in that case we // could just register for that and handle it in renderMap(). - m_camera_mutex.lock(); + m_camera_mutex.Lock(); v3f camera_position = m_camera_position; - m_camera_mutex.unlock(); + m_camera_mutex.Unlock(); MapNode n = getNodeNoEx(floatToInt(camera_position, BS)); @@ -4140,7 +4140,7 @@ void ClientMap::expireMeshes(bool only_daynight_diffed) } { - SimpleMutexAutoLock lock(block->mesh_mutex); + JMutexAutoLock lock(block->mesh_mutex); if(block->mesh != NULL) { /*block->mesh->drop(); diff --git a/src/map.h b/src/map.h index d2eaadb..e001135 100644 --- a/src/map.h +++ b/src/map.h @@ -20,6 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef MAP_HEADER #define MAP_HEADER +#include +#include +#include #include #include @@ -28,13 +31,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapblock_nodemod.h" #include "constants.h" #include "voxel.h" -#include "porting.h" -#include "threads.h" extern "C" { #include "sqlite3.h" } +using namespace jthread; + class MapSector; class ServerMapSector; class ClientMapSector; @@ -528,7 +531,7 @@ public: void updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset) { - SimpleMutexAutoLock lock(m_camera_mutex); + JMutexAutoLock lock(m_camera_mutex); m_camera_position = pos; m_camera_direction = dir; m_camera_fov = fov; @@ -607,7 +610,7 @@ private: // This is the master heightmap mesh //scene::SMesh *mesh; - //SimpleMutex mesh_mutex; + //JMutex mesh_mutex; MapDrawControl &m_control; @@ -615,7 +618,7 @@ private: v3f m_camera_direction; f32 m_camera_fov; v3s16 m_camera_offset; - SimpleMutex m_camera_mutex; + JMutex m_camera_mutex; core::map m_last_drawn_sectors; }; diff --git a/src/mapblock.cpp b/src/mapblock.cpp index 6f041f9..69bd98b 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -47,9 +47,9 @@ MapBlock::MapBlock(Map *parent, v3s16 pos, bool dummy): #ifndef SERVER m_mesh_expired = false; - mesh_mutex.init(); + mesh_mutex.Init(); mesh = NULL; - m_temp_mods_mutex.init(); + m_temp_mods_mutex.Init(); #endif } @@ -57,7 +57,7 @@ MapBlock::~MapBlock() { #ifndef SERVER { - SimpleMutexAutoLock lock(mesh_mutex); + JMutexAutoLock lock(mesh_mutex); if(mesh) { @@ -142,7 +142,7 @@ void MapBlock::updateMesh(u32 daynight_ratio, Environment *env, v3s16 camera_off DEBUG: If mesh has been generated, don't generate it again */ { - SimpleMutexAutoLock meshlock(mesh_mutex); + JMutexAutoLock meshlock(mesh_mutex); if(mesh != NULL) return; } @@ -167,7 +167,7 @@ void MapBlock::updateMesh(u32 daynight_ratio, Environment *env, v3s16 camera_off void MapBlock::replaceMesh(MapBlockMesh *mesh_new) { - mesh_mutex.lock(); + mesh_mutex.Lock(); //scene::SMesh *mesh_old = mesh[daynight_i]; //mesh[daynight_i] = mesh_new; @@ -202,7 +202,7 @@ void MapBlock::replaceMesh(MapBlockMesh *mesh_new) delete mesh_old; } - mesh_mutex.unlock(); + mesh_mutex.Unlock(); } #endif // !SERVER diff --git a/src/mapblock.h b/src/mapblock.h index aa9e5a6..3ae5e2c 100644 --- a/src/mapblock.h +++ b/src/mapblock.h @@ -20,6 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef MAPBLOCK_HEADER #define MAPBLOCK_HEADER +#include +#include #include #include "debug.h" #include "common_irrlicht.h" @@ -31,7 +33,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "nodemetadata.h" #include "staticobject.h" #include "mapblock_nodemod.h" -#include "threads.h" #ifndef SERVER #include "mapblock_mesh.h" #endif @@ -435,32 +436,32 @@ public: <<", mod.type="< #include "common_irrlicht.h" #include "exceptions.h" #include -#include "porting.h" -#include "threads.h" class MapBlock; class Map; diff --git a/src/porting.h b/src/porting.h index eeb242b..80d4426 100644 --- a/src/porting.h +++ b/src/porting.h @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include // Included for u32 and such #include "common_irrlicht.h" +#include "debug.h" #include "constants.h" #ifdef _MSC_VER @@ -44,7 +45,6 @@ with this program; if not, write to the Free Software Foundation, Inc., # include # define sleep_ms(x) usleep(x*1000) #endif -#include "debug.h" namespace porting { diff --git a/src/profiler.h b/src/profiler.h index e0f2eb8..21bcb3e 100644 --- a/src/profiler.h +++ b/src/profiler.h @@ -23,7 +23,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common_irrlicht.h" #include #include "utility.h" -#include "threads.h" +#include +#include /* Time profiler @@ -34,12 +35,12 @@ class Profiler public: Profiler() { - m_mutex.init(); + m_mutex.Init(); } void add(const std::string &name, float value) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); { /* No average shall have been used; mark add used as -2 */ core::map::Node *n = m_avgcounts.find(name); @@ -62,7 +63,7 @@ public: void avg(const std::string &name, float value) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); { core::map::Node *n = m_avgcounts.find(name); if(n == NULL) @@ -87,7 +88,7 @@ public: void clear() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); for(core::map::Iterator i = m_data.getIterator(); i.atEnd() == false; i++) @@ -104,8 +105,8 @@ public: void printPage(std::ostream &o, u32 page, u32 pagecount) { - SimpleMutexAutoLock lock(m_mutex); - + JMutexAutoLock lock(m_mutex); + u32 minindex, maxindex; paging(m_data.size(), page, pagecount, minindex, maxindex); @@ -146,7 +147,7 @@ public: } private: - SimpleMutex m_mutex; + JMutex m_mutex; core::map m_data; core::map m_avgcounts; }; diff --git a/src/server.cpp b/src/server.cpp index 5ef95ea..9e82506 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "clientserver.h" #include "map.h" +#include "jmutexautolock.h" #include "main.h" #include "constants.h" #include "voxel.h" @@ -40,7 +41,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "profiler.h" #include "log.h" #include "base64.h" -#include "threads.h" #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" @@ -73,7 +73,6 @@ private: void * ServerThread::Thread() { -printf("ServerThread::Thread()\n"); log_register_thread("ServerThread"); DSTACK(__FUNCTION_NAME); @@ -104,13 +103,12 @@ printf("ServerThread::Thread()\n"); END_DEBUG_EXCEPTION_HANDLER(errorstream) -printf("ServerThread::Thread() exit\n"); - return NULL; } void * EmergeThread::Thread() { +printf("EmergeThread::Thread()\n"); log_register_thread("EmergeThread"); DSTACK(__FUNCTION_NAME); @@ -199,7 +197,7 @@ void * EmergeThread::Thread() Fetch block from map or generate a single block */ { - SimpleMutexAutoLock envlock(m_server->m_env_mutex); + JMutexAutoLock envlock(m_server->m_env_mutex); // Load sector if it isn't loaded if(map.getSectorNoGenerateNoEx(p2d) == NULL) @@ -266,7 +264,7 @@ void * EmergeThread::Thread() } {//envlock - SimpleMutexAutoLock envlock(m_server->m_env_mutex); + JMutexAutoLock envlock(m_server->m_env_mutex); if(got_block) { @@ -311,7 +309,7 @@ void * EmergeThread::Thread() */ // NOTE: Server's clients are also behind the connection mutex - SimpleMutexAutoLock lock(m_server->m_con_mutex); + JMutexAutoLock lock(m_server->m_con_mutex); /* Add the originally fetched block to the modified list @@ -970,9 +968,9 @@ Server::Server( m_emergethread_trigger_timer = 0.0; m_savemap_timer = 0.0; - m_env_mutex.init(); - m_con_mutex.init(); - m_step_dtime_mutex.init(); + m_env_mutex.Init(); + m_con_mutex.Init(); + m_step_dtime_mutex.Init(); m_step_dtime = 0.0; // Register us to receive map edit events @@ -998,7 +996,7 @@ Server::~Server() Send shutdown message */ { - SimpleMutexAutoLock conlock(m_con_mutex); + JMutexAutoLock conlock(m_con_mutex); std::wstring line = L"*** Server shutting down"; @@ -1024,7 +1022,7 @@ Server::~Server() } { - SimpleMutexAutoLock envlock(m_env_mutex); + JMutexAutoLock envlock(m_env_mutex); /* Save players @@ -1048,7 +1046,7 @@ Server::~Server() Delete clients */ { - SimpleMutexAutoLock clientslock(m_con_mutex); + JMutexAutoLock clientslock(m_con_mutex); for(core::map::Iterator i = m_clients.getIterator(); @@ -1058,7 +1056,7 @@ Server::~Server() // NOTE: These are removed by env destructor { u16 peer_id = i.getNode()->getKey(); - SimpleMutexAutoLock envlock(m_env_mutex); + JMutexAutoLock envlock(m_env_mutex); m_env.removePlayer(peer_id); }*/ @@ -1077,10 +1075,8 @@ void Server::start(unsigned short port) // Initialize connection m_con.SetTimeoutMs(30); m_con.Serve(port); - if (!m_con.getRun()) { -printf("no con run\n"); + if (!m_con.getRun()) return; - } // Start thread m_thread.start(); @@ -1110,7 +1106,7 @@ bool Server::step(float dtime) if(dtime > 2.0) dtime = 2.0; { - SimpleMutexAutoLock lock(m_step_dtime_mutex); + JMutexAutoLock lock(m_step_dtime_mutex); m_step_dtime += dtime; } return true; @@ -1124,7 +1120,7 @@ void Server::AsyncRunStep() float dtime; { - SimpleMutexAutoLock lock1(m_step_dtime_mutex); + JMutexAutoLock lock1(m_step_dtime_mutex); dtime = m_step_dtime; } @@ -1143,7 +1139,7 @@ void Server::AsyncRunStep() //infostream<<"Server::AsyncRunStep(): dtime="<getFloat("time_speed") * 24000./(24.*3600); @@ -1191,8 +1187,8 @@ void Server::AsyncRunStep() { m_time_of_day_send_timer = g_settings->getFloat("time_send_interval"); - //SimpleMutexAutoLock envlock(m_env_mutex); - SimpleMutexAutoLock conlock(m_con_mutex); + //JMutexAutoLock envlock(m_env_mutex); + JMutexAutoLock conlock(m_con_mutex); for(core::map::Iterator i = m_clients.getIterator(); @@ -1210,7 +1206,7 @@ void Server::AsyncRunStep() } { - SimpleMutexAutoLock lock(m_env_mutex); + JMutexAutoLock lock(m_env_mutex); // Step environment ScopeProfiler sp(g_profiler, "SEnv step"); ScopeProfiler sp2(g_profiler, "SEnv step avg", SPT_AVG); @@ -1220,7 +1216,7 @@ void Server::AsyncRunStep() const float map_timer_and_unload_dtime = 2.92; if(m_map_timer_and_unload_interval.step(dtime, map_timer_and_unload_dtime)) { - SimpleMutexAutoLock lock(m_env_mutex); + JMutexAutoLock lock(m_env_mutex); // Run Map's timers and unload unused data ScopeProfiler sp(g_profiler, "Server: map timer and unload"); m_env.getMap().timerUpdate(map_timer_and_unload_dtime, @@ -1239,7 +1235,7 @@ void Server::AsyncRunStep() { m_liquid_transform_timer -= 1.00; - SimpleMutexAutoLock lock(m_env_mutex); + JMutexAutoLock lock(m_env_mutex); ScopeProfiler sp(g_profiler, "Server: liquid transform"); @@ -1266,7 +1262,7 @@ void Server::AsyncRunStep() Set the modified blocks unsent for all the clients */ - SimpleMutexAutoLock lock2(m_con_mutex); + JMutexAutoLock lock2(m_con_mutex); for(core::map::Iterator i = m_clients.getIterator(); @@ -1290,7 +1286,7 @@ void Server::AsyncRunStep() { counter = 0.0; - SimpleMutexAutoLock lock2(m_con_mutex); + JMutexAutoLock lock2(m_con_mutex); if(m_clients.size() != 0) infostream<<"Players:"<= g_settings->getFloat("objectdata_interval")) { - SimpleMutexAutoLock lock1(m_env_mutex); - SimpleMutexAutoLock lock2(m_con_mutex); + JMutexAutoLock lock1(m_env_mutex); + JMutexAutoLock lock2(m_con_mutex); //ScopeProfiler sp(g_profiler, "Server: sending player positions"); @@ -1751,7 +1747,7 @@ void Server::AsyncRunStep() m_banmanager.save(); // Map - SimpleMutexAutoLock lock(m_env_mutex); + JMutexAutoLock lock(m_env_mutex); /*// Unload unused data (delete from memory) m_env.getMap().unloadUnusedData( @@ -1787,7 +1783,7 @@ void Server::Receive() u32 datasize; try{ { - SimpleMutexAutoLock conlock(m_con_mutex); + JMutexAutoLock conlock(m_con_mutex); datasize = m_con.Receive(peer_id, data); } @@ -1810,7 +1806,7 @@ void Server::Receive() // The peer has been disconnected. // Find the associated player and remove it. - /*SimpleMutexAutoLock envlock(m_env_mutex); + /*JMutexAutoLock envlock(m_env_mutex); infostream<<"ServerThread: peer_id="<m_dig_mutex); + JMutexAutoLock digmutex(client->m_dig_mutex); client->m_dig_tool_item = -1; #endif } @@ -4508,8 +4504,8 @@ void Server::inventoryModified(InventoryContext *c, std::string id) core::list Server::getPlayerInfo() { DSTACK(__FUNCTION_NAME); - SimpleMutexAutoLock envlock(m_env_mutex); - SimpleMutexAutoLock conlock(m_con_mutex); + JMutexAutoLock envlock(m_env_mutex); + JMutexAutoLock conlock(m_con_mutex); core::list list; @@ -4654,7 +4650,7 @@ void Server::SendPlayerInfos() { DSTACK(__FUNCTION_NAME); - //SimpleMutexAutoLock envlock(m_env_mutex); + //JMutexAutoLock envlock(m_env_mutex); // Get connected players core::list players = m_env.getPlayers(true); @@ -4681,7 +4677,7 @@ void Server::SendPlayerInfos() start += 2+PLAYERNAME_SIZE; } - //SimpleMutexAutoLock conlock(m_con_mutex); + //JMutexAutoLock conlock(m_con_mutex); // Send as reliable m_con.SendToAll(0, data, true); @@ -5048,8 +5044,8 @@ void Server::SendBlocks(float dtime) { DSTACK(__FUNCTION_NAME); - SimpleMutexAutoLock envlock(m_env_mutex); - SimpleMutexAutoLock conlock(m_con_mutex); + JMutexAutoLock envlock(m_env_mutex); + JMutexAutoLock conlock(m_con_mutex); //TimeTaker timer("Server::SendBlocks"); @@ -5201,7 +5197,7 @@ void Server::UpdateCrafting(u16 peer_id) RemoteClient* Server::getClient(u16 peer_id) { DSTACK(__FUNCTION_NAME); - //SimpleMutexAutoLock lock(m_con_mutex); + //JMutexAutoLock lock(m_con_mutex); core::map::Node *n; n = m_clients.find(peer_id); // A client should exist for all peers @@ -5422,8 +5418,8 @@ Player *Server::emergePlayer(const char *name, const char *password, u16 peer_id void Server::handlePeerChange(PeerChange &c) { - SimpleMutexAutoLock envlock(m_env_mutex); - SimpleMutexAutoLock conlock(m_con_mutex); + JMutexAutoLock envlock(m_env_mutex); + JMutexAutoLock conlock(m_con_mutex); if(c.type == PEER_ADDED) { diff --git a/src/server.h b/src/server.h index 492566e..21203c8 100644 --- a/src/server.h +++ b/src/server.h @@ -54,12 +54,12 @@ class BlockEmergeQueue public: BlockEmergeQueue() { - m_mutex.init(); + m_mutex.Init(); } ~BlockEmergeQueue() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); core::list::Iterator i; for(i=m_queue.begin(); i!=m_queue.end(); i++) @@ -76,7 +76,7 @@ public: { DSTACK(__FUNCTION_NAME); - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); if(peer_id != 0) { @@ -110,7 +110,7 @@ public: // Returns NULL if queue is empty QueuedBlockEmerge * pop() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); core::list::Iterator i = m_queue.begin(); if(i == m_queue.end()) @@ -122,13 +122,13 @@ public: u32 size() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); return m_queue.size(); } u32 peerItemCount(u16 peer_id) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); u32 count = 0; @@ -145,7 +145,7 @@ public: private: core::list m_queue; - SimpleMutex m_mutex; + JMutex m_mutex; }; class Server; @@ -304,7 +304,7 @@ public: // Time from last placing or removing blocks float m_time_from_building; - /*SimpleMutex m_dig_mutex; + /*JMutex m_dig_mutex; float m_dig_time_remaining; // -1 = not digging s16 m_dig_tool_item; @@ -604,11 +604,11 @@ private: // Environment ServerEnvironment m_env; - SimpleMutex m_env_mutex; + JMutex m_env_mutex; // Connection con::Connection m_con; - SimpleMutex m_con_mutex; + JMutex m_con_mutex; // Connected clients (behind the con mutex) core::map m_clients; @@ -625,7 +625,7 @@ private: // A buffer for time steps // step() increments and AsyncRunStep() run by m_thread reads it. float m_step_dtime; - SimpleMutex m_step_dtime_mutex; + JMutex m_step_dtime_mutex; // The server mainly operates in this thread ServerThread m_thread; diff --git a/src/servermain.cpp b/src/servermain.cpp index ab4e347..bdf786d 100644 --- a/src/servermain.cpp +++ b/src/servermain.cpp @@ -52,6 +52,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include #include #include "common_irrlicht.h" #include "debug.h" @@ -77,7 +78,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "content_craftitem.h" #include "content_toolitem.h" #include "http.h" -#include "threads.h" /* Settings. diff --git a/src/settings.h b/src/settings.h index e270984..d7e5143 100644 --- a/src/settings.h +++ b/src/settings.h @@ -22,6 +22,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common_irrlicht.h" #include +#include +#include +#include #include "strfnd.h" #include #include @@ -29,7 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "debug.h" #include "utility.h" #include "log.h" -#include "threads.h" enum ValueType { @@ -48,17 +50,19 @@ struct ValueSpec const char *help; }; +using namespace jthread; + class Settings { public: Settings() { - m_mutex.init(); + m_mutex.Init(); } void writeLines(std::ostream &os) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); for(core::map::Iterator i = m_settings.getIterator(); @@ -72,7 +76,7 @@ public: bool parseConfigLine(const std::string &line) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); std::string trimmedline = trim(line); @@ -172,7 +176,7 @@ public: core::list &dst, core::map &updated) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); if(is.eof()) return false; @@ -256,7 +260,7 @@ public: } } - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); // Write stuff back { @@ -364,14 +368,14 @@ public: void set(std::string name, std::string value) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); m_settings[name] = value; } void set(std::string name, const char *value) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); m_settings[name] = value; } @@ -379,21 +383,21 @@ public: void setDefault(std::string name, std::string value) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); m_defaults[name] = value; } bool exists(std::string name) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); return (m_settings.find(name) || m_defaults.find(name)); } std::string get(std::string name) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); core::map::Node *n; n = m_settings.find(name); @@ -556,7 +560,7 @@ public: void clear() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); m_settings.clear(); m_defaults.clear(); @@ -564,7 +568,7 @@ public: void updateValue(Settings &other, const std::string &name) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); if(&other == this) return; @@ -580,8 +584,8 @@ public: void update(Settings &other) { - SimpleMutexAutoLock lock(m_mutex); - SimpleMutexAutoLock lock2(other.m_mutex); + JMutexAutoLock lock(m_mutex); + JMutexAutoLock lock2(other.m_mutex); if(&other == this) return; @@ -605,8 +609,8 @@ public: Settings & operator+=(Settings &other) { - SimpleMutexAutoLock lock(m_mutex); - SimpleMutexAutoLock lock2(other.m_mutex); + JMutexAutoLock lock(m_mutex); + JMutexAutoLock lock2(other.m_mutex); if(&other == this) return *this; @@ -633,8 +637,8 @@ public: Settings & operator=(Settings &other) { - SimpleMutexAutoLock lock(m_mutex); - SimpleMutexAutoLock lock2(other.m_mutex); + JMutexAutoLock lock(m_mutex); + JMutexAutoLock lock2(other.m_mutex); if(&other == this) return *this; @@ -649,7 +653,7 @@ private: core::map m_settings; core::map m_defaults; // All methods that access m_settings/m_defaults directly should lock this. - SimpleMutex m_mutex; + JMutex m_mutex; }; #endif diff --git a/src/threads.h b/src/threads.h index c376ab3..1ea7d0a 100644 --- a/src/threads.h +++ b/src/threads.h @@ -20,6 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef THREADS_HEADER #define THREADS_HEADER +#include + #if (defined(WIN32) || defined(_WIN32_WCE)) typedef DWORD threadid_t; #define __NORETURN __declspec(noreturn) @@ -30,10 +32,6 @@ typedef pthread_t threadid_t; #define __FUNCTION_NAME __PRETTY_FUNCTION__ #endif -class SimpleMutex; -class SimpleThread; - -#include "porting.h" inline threadid_t get_current_thread_id() { #if (defined(WIN32) || defined(_WIN32_WCE)) @@ -43,225 +41,5 @@ inline threadid_t get_current_thread_id() #endif } -/* - A simple mutex implementation -*/ -#ifdef _WIN32 -class SimpleMutex -{ - CRITICAL_SECTION mut; - -public: - - SimpleMutex() - { - InitializeCriticalSection(&mut); - } - - ~SimpleMutex() - { - unlock(); - DeleteCriticalSection(&mut); - } - - void init() - { - InitializeCriticalSection(&mut); - } - - void lock() - { - EnterCriticalSection(&mut); - } - - bool trylock() - { - if (!TryEnterCriticalSection(&mut)) - return true; - return false; - } - - void unlock() - { - LeaveCriticalSection(&mut); - } -}; -#else -class SimpleMutex -{ - pthread_mutexattr_t attr; - pthread_mutex_t mut; - -public: - - SimpleMutex() - { - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&mut, &attr); - } - - ~SimpleMutex() - { - unlock(); - pthread_mutex_destroy(&mut); - pthread_mutexattr_destroy(&attr); - } - - void init() - { - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&mut, &attr); - } - - void lock() - { - pthread_mutex_lock(&mut); - } - - int trylock() - { - if (pthread_mutex_trylock(&mut)) - return true; - return false; - } - - void unlock() - { - pthread_mutex_unlock(&mut); - } -}; -#endif - -class SimpleMutexAutoLock -{ - SimpleMutex &mutex; - -public: - SimpleMutexAutoLock(SimpleMutex &m): - mutex(m) - { - mutex.lock(); - } - - ~SimpleMutexAutoLock() - { - mutex.unlock(); - } -}; - -/* - A base class for simple background thread implementation -*/ -class SimpleThread -{ - bool run; - SimpleMutex run_mutex; -#ifdef _WIN32 - HANDLE thread; -#else - pthread_t thread; - pthread_attr_t attr; -#endif - -public: - - SimpleThread(): - run(false), - run_mutex() - { -#ifndef _WIN32 - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); -#endif - } - - virtual ~SimpleThread() - { - kill(); - } - - virtual void * Thread() = 0; - - bool getRun() - { - run_mutex.lock(); - bool r = run; - run_mutex.unlock(); - return r; - } - void setRun(bool a_run) - { - run_mutex.lock(); - run = a_run; - run_mutex.unlock(); - } - - void start() - { - if (getRun()) { -#ifdef _WIN32 - ResumeThread(thread); -#else - pthread_kill(thread,SIGCONT); -#endif - }else{ - setRun(true); -#ifdef _WIN32 - thread = CreateThread(NULL, 0, &runThread, this, 0, NULL); -#else - pthread_create(&thread, &attr, &runThread, this); -#endif - } - } - - void wait() - { - if (getRun()) { -#ifdef _WIN32 - WaitForSingleObject(thread, 2000); - CloseHandle(thread); -#else - pthread_join(thread,NULL); -#endif - } - } - - void stop() - { - setRun(false); - wait(); - } - - void kill() - { - if (getRun()) { - setRun(false); -#ifdef _WIN32 - TerminateThread(thread,0); - CloseHandle(thread); -#else - pthread_kill(thread,SIGKILL); -#endif - } - } - -private: - static void *runThread(void* data) - { - SimpleThread *t = (SimpleThread*)data; - void *r = t->Thread(); - t->setRun(false); -#ifdef _WIN32 - ExitThread(0); - CloseHandle(t->thread); -#else - pthread_exit(r); -#endif - return r; - } -}; - #endif diff --git a/src/tile.cpp b/src/tile.cpp index 3485b56..3e54ddc 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -41,7 +41,7 @@ TextureSource::TextureSource(IrrlichtDevice *device): { assert(m_device); - m_atlaspointer_cache_mutex.init(); + m_atlaspointer_cache_mutex.Init(); m_main_thread = get_current_thread_id(); @@ -93,7 +93,7 @@ u32 TextureSource::getTextureId(const std::string &name) /* See if texture already exists */ - SimpleMutexAutoLock lock(m_atlaspointer_cache_mutex); + JMutexAutoLock lock(m_atlaspointer_cache_mutex); core::map::Node *n; n = m_name_to_id.find(name); if(n != NULL) @@ -192,7 +192,7 @@ u32 TextureSource::getTextureIdDirect(const std::string &name) See if texture already exists */ { - SimpleMutexAutoLock lock(m_atlaspointer_cache_mutex); + JMutexAutoLock lock(m_atlaspointer_cache_mutex); core::map::Node *n; n = m_name_to_id.find(name); @@ -260,7 +260,7 @@ u32 TextureSource::getTextureIdDirect(const std::string &name) // If a base image was found, copy it to baseimg if(base_image_id != 0) { - SimpleMutexAutoLock lock(m_atlaspointer_cache_mutex); + JMutexAutoLock lock(m_atlaspointer_cache_mutex); SourceAtlasPointer ap = m_atlaspointer_cache[base_image_id]; @@ -326,7 +326,7 @@ u32 TextureSource::getTextureIdDirect(const std::string &name) Add texture to caches (add NULL textures too) */ - SimpleMutexAutoLock lock(m_atlaspointer_cache_mutex); + JMutexAutoLock lock(m_atlaspointer_cache_mutex); u32 id = m_atlaspointer_cache.size(); AtlasPointer ap(id); @@ -349,7 +349,7 @@ u32 TextureSource::getTextureIdDirect(const std::string &name) std::string TextureSource::getTextureName(u32 id) { - SimpleMutexAutoLock lock(m_atlaspointer_cache_mutex); + JMutexAutoLock lock(m_atlaspointer_cache_mutex); if(id >= m_atlaspointer_cache.size()) { @@ -365,7 +365,7 @@ std::string TextureSource::getTextureName(u32 id) AtlasPointer TextureSource::getTexture(u32 id) { - SimpleMutexAutoLock lock(m_atlaspointer_cache_mutex); + JMutexAutoLock lock(m_atlaspointer_cache_mutex); if(id >= m_atlaspointer_cache.size()) return AtlasPointer(0, NULL); @@ -382,7 +382,7 @@ void TextureSource::buildMainAtlas() video::IVideoDriver* driver = m_device->getVideoDriver(); assert(driver); - SimpleMutexAutoLock lock(m_atlaspointer_cache_mutex); + JMutexAutoLock lock(m_atlaspointer_cache_mutex); // Create an image of the right size core::dimension2d atlas_dim(4096,4096); diff --git a/src/tile.h b/src/tile.h index 7bd02b0..6545059 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. */ @@ -246,7 +248,7 @@ private: // Maps a texture name to an index in the former. core::map m_name_to_id; // The two former containers are behind this mutex - SimpleMutex m_atlaspointer_cache_mutex; + JMutex m_atlaspointer_cache_mutex; // Main texture atlas. This is filled at startup and is then not touched. video::IImage *m_main_atlas_image; diff --git a/src/utility.h b/src/utility.h index 39b4c69..3dd61a2 100644 --- a/src/utility.h +++ b/src/utility.h @@ -25,6 +25,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include +#include +#include #include #include "common_irrlicht.h" @@ -32,7 +35,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "strfnd.h" #include "exceptions.h" #include "porting.h" -#include "threads.h" + +using namespace jthread; extern const v3s16 g_6dirs[6]; @@ -541,32 +545,32 @@ public: MutexedVariable(T value): m_value(value) { - m_mutex.init(); + m_mutex.Init(); } T get() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); return m_value; } void set(T value) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); m_value = value; } // You'll want to grab this in a SharedPtr - SimpleMutex *getLock() + JMutexAutoLock * getLock() { - return new SimpleMutexAutoLock(m_mutex); + return new JMutexAutoLock(m_mutex); } // You pretty surely want to grab the lock when accessing this T m_value; private: - SimpleMutex m_mutex; + JMutex m_mutex; }; /* @@ -968,6 +972,197 @@ inline void str_replace_char(std::string & str, char from, char to) } } +/* + A simple mutex implementation +*/ +#ifdef _WIN32 +class SimpleMutex +{ + CRITICAL_SECTION mut; + +public: + + SimpleMutex() + { + InitializeCriticalSection(&mut); + } + + ~SimpleMutex() + { + unlock(); + DeleteCriticalSection(&mut); + } + + void lock() + { + EnterCriticalSection(&mut); + } + + bool trylock() + { + if (!TryEnterCriticalSection(&mut)) + return true; + return false; + } + + void unlock() + { + LeaveCriticalSection(&mut); + } +}; +#else +class SimpleMutex +{ + pthread_mutexattr_t attr; + pthread_mutex_t mut; + +public: + + SimpleMutex() + { + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&mut, &attr); + } + + ~SimpleMutex() + { + unlock(); + pthread_mutex_destroy(&mut); + pthread_mutexattr_destroy(&attr); + } + + void lock() + { + pthread_mutex_lock(&mut); + } + + int trylock() + { + if (pthread_mutex_trylock(&mut)) + return true; + return false; + } + + void unlock() + { + pthread_mutex_unlock(&mut); + } +}; +#endif + +/* + A base class for simple background thread implementation +*/ +class SimpleThread +{ + bool run; + SimpleMutex run_mutex; +#ifdef _WIN32 + HANDLE thread; +#else + pthread_t thread; + pthread_attr_t attr; +#endif + +public: + + SimpleThread(): + run(false), + run_mutex() + { +#ifndef _WIN32 + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); +#endif + } + + virtual ~SimpleThread() + { + kill(); + } + + virtual void * Thread() = 0; + + bool getRun() + { + run_mutex.lock(); + bool r = run; + run_mutex.unlock(); + return r; + } + void setRun(bool a_run) + { + run_mutex.lock(); + run = a_run; + run_mutex.unlock(); + } + + void start() + { + if (getRun()) { +#ifdef _WIN32 + ResumeThread(thread); +#else + pthread_kill(thread,SIGCONT); +#endif + }else{ + setRun(true); +#ifdef _WIN32 + thread = CreateThread(NULL, 0, &runThread, this, 0, NULL); +#else + pthread_create(&thread, &attr, &runThread, this); +#endif + } + } + + void wait() + { + if (getRun()) { +#ifdef _WIN32 + WaitForSingleObject(thread, 2000); + CloseHandle(thread); +#else + pthread_join(thread,NULL); +#endif + } + } + + void stop() + { + setRun(false); + wait(); + } + + void kill() + { + if (getRun()) { + setRun(false); +#ifdef _WIN32 + TerminateThread(thread,0); + CloseHandle(thread); +#else + pthread_kill(thread,SIGKILL); +#endif + } + } + +private: + static void *runThread(void* data) + { + SimpleThread *t = (SimpleThread*)data; + void *r = t->Thread(); + t->setRun(false); +#ifdef _WIN32 + ExitThread(0); + CloseHandle(t->thread); +#else + pthread_exit(r); +#endif + return r; + } +}; + /* FIFO queue (well, actually a FILO also) */ @@ -1020,16 +1215,16 @@ class MutexedQueue public: MutexedQueue() { - m_mutex.init(); + m_mutex.Init(); } u32 size() { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); return m_list.size(); } void push_back(T t) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); m_list.push_back(t); } T pop_front(u32 wait_time_max_ms=0) @@ -1039,7 +1234,7 @@ public: for(;;) { { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); if(m_list.size() > 0) { @@ -1049,7 +1244,7 @@ public: return t; } - if (wait_time_ms >= wait_time_max_ms) + if(wait_time_ms >= wait_time_max_ms) throw ItemNotFoundException("MutexedQueue: queue is empty"); } @@ -1065,7 +1260,7 @@ public: for(;;) { { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); if(m_list.size() > 0) { @@ -1085,7 +1280,7 @@ public: } } - SimpleMutex & getMutex() + JMutex & getMutex() { return m_mutex; } @@ -1096,7 +1291,7 @@ public: } protected: - SimpleMutex m_mutex; + JMutex m_mutex; core::list m_list; }; @@ -1165,7 +1360,7 @@ public: void add(Key key, Caller caller, CallerData callerdata, ResultQueue *dest) { - SimpleMutexAutoLock lock(m_queue.getMutex()); + JMutexAutoLock lock(m_queue.getMutex()); /* If the caller is already on the list, only update CallerData @@ -1292,26 +1487,28 @@ class MutexedMap public: MutexedMap() { - m_mutex.init(); + m_mutex.Init(); + assert(m_mutex.IsInitialized()); } void set(const Key &name, const Value &value) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); + m_values[name] = value; } bool get(const Key &name, Value *result) { - SimpleMutexAutoLock lock(m_mutex); + JMutexAutoLock lock(m_mutex); typename core::map::Node *n; n = m_values.find(name); - if (n == NULL) + if(n == NULL) return false; - if (result != NULL) + if(result != NULL) *result = n->getValue(); return true; @@ -1319,7 +1516,7 @@ public: private: core::map m_values; - SimpleMutex m_mutex; + JMutex m_mutex; }; #endif @@ -1341,16 +1538,17 @@ class MutexedIdGenerator public: MutexedIdGenerator() { - m_mutex.init(); + m_mutex.Init(); + assert(m_mutex.IsInitialized()); } // Returns true if found bool getValue(u32 id, T &value) { - if (id == 0) + if(id == 0) return false; - SimpleMutexAutoLock(m_mutex); - if (m_id_to_value.size() < id) + JMutexAutoLock lock(m_mutex); + if(m_id_to_value.size() < id) return false; value = m_id_to_value[id-1]; return true; @@ -1360,10 +1558,10 @@ public: // Otherwise generates an id for the value. u32 getId(const T &value) { - SimpleMutexAutoLock(m_mutex); + JMutexAutoLock lock(m_mutex); typename core::map::Node *n; n = m_value_to_id.find(value); - if (n != NULL) + if(n != NULL) return n->getValue(); m_id_to_value.push_back(value); u32 new_id = m_id_to_value.size(); @@ -1372,7 +1570,7 @@ public: } private: - SimpleMutex m_mutex; + JMutex m_mutex; // Values are stored here at id-1 position (id 1 = [0]) core::array m_id_to_value; core::map m_value_to_id;