diff options
Diffstat (limited to 'src/jthread')
| -rw-r--r-- | src/jthread/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/jthread/jsemaphore.h | 50 | ||||
| -rw-r--r-- | src/jthread/jthread.h | 5 | ||||
| -rw-r--r-- | src/jthread/pthread/jsemaphore.cpp | 48 | ||||
| -rw-r--r-- | src/jthread/pthread/jthread.cpp | 42 | ||||
| -rw-r--r-- | src/jthread/win32/jsemaphore.cpp | 64 | ||||
| -rw-r--r-- | src/jthread/win32/jthread.cpp | 38 |
7 files changed, 213 insertions, 36 deletions
diff --git a/src/jthread/CMakeLists.txt b/src/jthread/CMakeLists.txt index aa438eaaf..6c29671e6 100644 --- a/src/jthread/CMakeLists.txt +++ b/src/jthread/CMakeLists.txt @@ -2,10 +2,12 @@ if( UNIX ) set(JTHREAD_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/pthread/jmutex.cpp ${CMAKE_CURRENT_SOURCE_DIR}/pthread/jthread.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/pthread/jsemaphore.cpp PARENT_SCOPE) else( UNIX ) set(JTHREAD_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/win32/jmutex.cpp ${CMAKE_CURRENT_SOURCE_DIR}/win32/jthread.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/win32/jsemaphore.cpp PARENT_SCOPE) endif( UNIX ) diff --git a/src/jthread/jsemaphore.h b/src/jthread/jsemaphore.h new file mode 100644 index 000000000..70318d5da --- /dev/null +++ b/src/jthread/jsemaphore.h @@ -0,0 +1,50 @@ +/* +Minetest +Copyright (C) 2013 sapier, < sapier AT gmx DOT net > + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef JSEMAPHORE_H_ +#define JSEMAPHORE_H_ + +#if defined(WIN32) +#include <windows.h> +#define MAX_SEMAPHORE_COUNT 1024 +#else +#include <pthread.h> +#include <semaphore.h> +#endif + +class JSemaphore { +public: + JSemaphore(); + ~JSemaphore(); + JSemaphore(int initval); + + void Post(); + void Wait(); + + int GetValue(); + +private: +#if defined(WIN32) + HANDLE m_hSemaphore; +#else + sem_t m_semaphore; +#endif +}; + +#endif /* JSEMAPHORE_H_ */ diff --git a/src/jthread/jthread.h b/src/jthread/jthread.h index ec1eafaeb..92b05f1c5 100644 --- a/src/jthread/jthread.h +++ b/src/jthread/jthread.h @@ -43,6 +43,7 @@ public: JThread(); virtual ~JThread(); int Start(); + void Stop(); int Kill(); virtual void *Thread() = 0; bool IsRunning(); @@ -63,12 +64,12 @@ private: HANDLE threadhandle; #else // pthread type threads static void *TheThread(void *param); - + pthread_t threadid; #endif // WIN32 void *retval; bool running; - + JMutex runningmutex; JMutex continuemutex,continuemutex2; bool mutexinit; diff --git a/src/jthread/pthread/jsemaphore.cpp b/src/jthread/pthread/jsemaphore.cpp new file mode 100644 index 000000000..963ac83cf --- /dev/null +++ b/src/jthread/pthread/jsemaphore.cpp @@ -0,0 +1,48 @@ +/* +Minetest +Copyright (C) 2013 sapier, < sapier AT gmx DOT net > + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it--trackplacer.lua
--holds code for the track-placing system. the default 'track' item will be a craftitem that places rails as needed. this will neither place or change switches nor place vertical rails.
--all new trackplacer code
local tp={
tracks={}
}
function tp.register_tracktype(nnprefix, n_suffix)
if tp.tracks[nnprefix] then return end--due to the separate registration of slopes and flats for the same nnpref, definition would be overridden here. just don't.
tp.tracks[nnprefix]={
default=n_suffix,
single_conn={},
double_conn={},
--keys:conn1_conn2 (example:1_4)
--values:{name=x, param2=x}
twcycle={},
twrotate={},--indexed by suffix, list, tells order of rotations
modify={}
}
end
function tp.add_double_conn(nnprefix, suffix, rotation, conns)
local nodename=nnprefix.."_"..suffix..rotation
for i=0,3 do
tp.tracks[nnprefix].double_conn[((conns.conn1+4*i)%16).."_"..((conns.conn2+4*i)%16)]={name=nodename, param2=i}
tp.tracks[nnprefix].double_conn[((conns.conn2+4*i)%16).."_"..((conns.conn1+4*i)%16)]={name=nodename, param2=i}
end
tp.tracks[nnprefix].modify[nodename]=true
end
function tp.add_single_conn(nnprefix, suffix, rotation, conns)
local nodename=nnprefix.."_"..suffix..rotation
for i=0,3 do
tp.tracks[nnprefix].single_conn[((conns.conn1+4*i)%16)]={name=nodename, param2=i}
tp.tracks[nnprefix].single_conn[((conns.conn2+4*i)%16)]={name=nodename, param2=i}
end
tp.tracks[nnprefix].modify[nodename]=true
end
function tp.add_worked(nnprefix, suffix, rotation, cycle_follows)
tp.tracks[nnprefix].twcycle[suffix]=cycle_follows
if not tp.tracks[nnprefix].twro - + ret = jthread->Thread(); jthread->runningmutex.Lock(); diff --git a/src/jthread/win32/jsemaphore.cpp b/src/jthread/win32/jsemaphore.cpp new file mode 100644 index 000000000..8eca6d247 --- /dev/null +++ b/src/jthread/win32/jsemaphore.cpp @@ -0,0 +1,64 @@ +/* +Minetest +Copyright (C) 2013 sapier, < sapier AT gmx DOT net > + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ +#include "jthread/jsemaphore.h" + +JSemaphore::JSemaphore() { + m_hSemaphore = CreateSemaphore( + 0, + 0, + MAX_SEMAPHORE_COUNT, + 0); +} + +JSemaphore::~JSemaphore() {
local cnode=minetest.get_node(advtrains.dirCoordSet(pos, conn))
local bconn=(conn+8)%16
if advtrains.is_track_and_drives_on(cnode.name, advtrains.all_tracktypes) then
local cconn1, cconn2=advtrains.get_track_connections(cnode.name, cnode.param2)
return cconn1==bconn or cconn2==bconn
end
return false
end
local dnode=minetest.get_node(pos)
local dconn1, dconn2=advtrains.get_track_connections(dnode.name, dnode.param2)
local t={[true]="true", [false]="false"}
if istrackandbc(pos, dconn1) and istrackandbc(pos, dconn2) then return dconn1, dconn2
elseif istrackandbc(pos, dconn1) then return dconn1
elseif istrackandbc(pos, dconn2) then return dconn2
end
return nil
end
function tp.rail_and_can_be_bent(originpos, conn, nnpref)
local pos=advtrains.dirCoordSet(originpos, conn)
local newdir=(conn+8)%16
local node=minetest.get_node(pos)
local tr=tp.tracks[nnpref]
if not advtrains.is_track_and_drives_on(node.name, advtrains.all_tracktypes) then
return false
end
--rail at other end?
local adj1, adj2=tp.find_already_connected(pos)
if adj1 and adj2 then
return false--dont destroy existing track
elseif adj1 and not adj2 then
if tr.double_conn[adj1.."_"..newdir] then
return true--if exists, connect new rail and old end
end
return false
else
if tr.single_conn[newdir] then--just rotate old rail to right orientation
return true
end
return false
end
end
function tp.bend_rail(originpos, conn, nnpref)
|
