From: Patrik Gornicz Date: Sat, 22 Nov 2008 09:55:22 +0000 (-0500) Subject: created locks abstraction X-Git-Tag: v0.08~10 X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?p=physics.git;a=commitdiff_plain;h=3bccd1d78b605dc5b2898877601ad1a6374e0e44 created locks abstraction --- diff --git a/src/Makefile b/src/Makefile index 10a1a16..cf86e4b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -21,6 +21,7 @@ DIRS += Effects/ DIRS += config/ DIRS += input/ DIRS += graphics/ +DIRS += locks/ SRCS := # := start include $(addsuffix /files.mk,${DIRS}) diff --git a/src/debug.cpp b/src/debug.cpp index e838c8e..5c8baeb 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -22,32 +22,28 @@ using std::cerr; using std::cout; using std::endl; -#include +#include "locks/Mutex.h" +#include "locks/Autolock.h" /// ***** Public Methods ***** -SDL_mutex* muDPF = NULL; +Mutex muDPF; void DPF(int level, const char* pstr) { - // lock - SDL_mutexP( muDPF ); + Autolock lock(muDPF); cout << pstr << endl; - - //unlock - SDL_mutexV( muDPF ); } void debug::init() { - muDPF = SDL_CreateMutex(); + muDPF.init(); } void debug::clean() { - SDL_DestroyMutex( muDPF ); - muDPF = NULL; + muDPF.clean(); } /// ***** Private Methods ***** diff --git a/src/entityCreator.cpp b/src/entityCreator.cpp index 28d6979..2df4457 100644 --- a/src/entityCreator.cpp +++ b/src/entityCreator.cpp @@ -74,7 +74,7 @@ void creator::init() ball = addBall(Vector2(150, 150), 20, cCyan, startBalls); ball->mass = startMass; - for( int i = 0; i<100; i++) + for( int i = 0; i<50; i++) { addBall(Vector2(200+i*2, 200+i*2), 10, cCyan); } diff --git a/src/entityManager.cpp b/src/entityManager.cpp index ecf7955..65b8273 100644 --- a/src/entityManager.cpp +++ b/src/entityManager.cpp @@ -21,6 +21,9 @@ #include #include +#include "locks/Mutex.h" +#include "locks/Autolock.h" + #include "Entities/Entity.h" #include "Entities/Particle.h" #include "Entities/PhysicsEntity.h" @@ -45,15 +48,15 @@ setPhys physics_To_Add; setPhys active_Physics; setPhys physics_To_Remove; -SDL_mutex* particleSetLock = NULL; -SDL_mutex* physicsEntitySetLock = NULL; +Mutex particleSetLock; +Mutex physicsEntitySetLock; /// ***** Initializers/Cleaners ***** void manager::init() { - particleSetLock = SDL_CreateMutex(); - physicsEntitySetLock = SDL_CreateMutex(); + particleSetLock.init(); + physicsEntitySetLock.init(); collision::init(); } @@ -61,11 +64,8 @@ void manager::clean() { collision::clean(); - SDL_DestroyMutex(particleSetLock); - particleSetLock = NULL; - - SDL_DestroyMutex(physicsEntitySetLock); - physicsEntitySetLock = NULL; + physicsEntitySetLock.clean(); + particleSetLock.clean(); } /// ***** Public Methods ***** @@ -128,7 +128,10 @@ void manager::update(float time_step) } void manager::draw() { - SDL_mutexP( particleSetLock ); + { + Autolock lock( particleSetLock ); + DPF(0, "Particle Draw Start"); + // update active Particle*s for( setPart::iterator it = active_Particles.begin(); it != active_Particles.end(); @@ -136,9 +139,14 @@ void manager::draw() { (*it)->draw(); } - SDL_mutexV( particleSetLock ); - SDL_mutexP( physicsEntitySetLock ); + DPF(0, "Particle Draw End"); + } + + { + Autolock lock( physicsEntitySetLock ); + DPF(0, "Physics Draw Start"); + // update active PhysicsEntity*s for( setPhys::iterator it = active_Physics.begin(); it != active_Physics.end(); @@ -146,32 +154,39 @@ void manager::draw() { (*it)->draw(); } - SDL_mutexV( physicsEntitySetLock ); + + DPF(0, "Physics Draw End"); + } } /// ***** Private Methods ***** void updateParticles(float time_step) { - SDL_mutexP( particleSetLock ); + { + Autolock lock( particleSetLock ); + // add new Particle*s to Active for( setPart::iterator it = particles_To_Add.begin(); it != particles_To_Add.end(); it++ ) { + DPF(0, "Particle Insert"); active_Particles.insert(*it); } - particles_To_Add.clear(); // remove dead Particle*s from Active for( setPart::iterator it = particles_To_Remove.begin(); it != particles_To_Remove.end(); it++ ) { + DPF(0, "Particle Delete"); active_Particles.erase(*it); } - particles_To_Remove.clear(); - SDL_mutexV( particleSetLock ); + } + + particles_To_Add.clear(); + particles_To_Remove.clear(); // update active Particle*s for( setPart::iterator it = active_Particles.begin(); @@ -183,25 +198,30 @@ void updateParticles(float time_step) } void updatePhysics(float time_step) { - SDL_mutexP( physicsEntitySetLock ); + { + Autolock lock( physicsEntitySetLock ); + // add new PhysicsEntity*s to Active for( setPhys::iterator it = physics_To_Add.begin(); it != physics_To_Add.end(); it++ ) { + DPF(0, "Physics Insert"); active_Physics.insert(*it); } - physics_To_Add.clear(); // remove dead PhysicsEntity*s from Active for( setPhys::iterator it = physics_To_Remove.begin(); it != physics_To_Remove.end(); it++ ) { + DPF(0, "Physics Delete"); active_Physics.erase(*it); } - physics_To_Remove.clear(); - SDL_mutexV( physicsEntitySetLock ); + } + + physics_To_Remove.clear(); + physics_To_Add.clear(); // apply collision math collision::update(active_Physics); diff --git a/src/locks/Autolock.cpp b/src/locks/Autolock.cpp new file mode 100644 index 0000000..3601bdf --- /dev/null +++ b/src/locks/Autolock.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Autolock.h" + +#include "Mutex.h" + +/// ***** Constructors/Destructors ***** +Autolock::Autolock(Mutex& mu) + : m_mu(mu) +{ + Lock(); +} + +Autolock::~Autolock() +{ + Unlock(); +} + +void Autolock::Lock() +{ + m_mu.Lock(); +} + +void Autolock::Unlock() +{ + m_mu.Unlock(); +} diff --git a/src/locks/Autolock.h b/src/locks/Autolock.h new file mode 100644 index 0000000..de0107c --- /dev/null +++ b/src/locks/Autolock.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef AUTOLOCK_H +#define AUTOLOCK_H + +class Mutex; + +/// ***** Header Class ***** +class Autolock +{ +public: + Autolock(Mutex& mu); + ~Autolock(); + + void Lock(); + void Unlock(); + +// hide copying methods! +private: + Autolock(const Autolock&); + const Autolock& operator ==(const Autolock&); + +private: + Mutex& m_mu; +}; + +/// ***** Header Methods ***** + +#endif // AUTOLOCK_H diff --git a/src/locks/Mutex.cpp b/src/locks/Mutex.cpp new file mode 100644 index 0000000..a533e83 --- /dev/null +++ b/src/locks/Mutex.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Mutex.h" + +#include + +Mutex::Mutex() + : m_pSDL_mutex(NULL) +{ + +} +Mutex::~Mutex() +{ + +} + +void Mutex::init() +{ + m_pSDL_mutex = SDL_CreateMutex(); +} +void Mutex::clean() +{ + SDL_DestroyMutex(m_pSDL_mutex); + m_pSDL_mutex = NULL; +} + +bool Mutex::IsValid() +{ + return NULL != m_pSDL_mutex; +} + +void Mutex::Lock() +{ + if(IsValid()) + SDL_mutexP(m_pSDL_mutex); +} +void Mutex::Unlock() +{ + if(IsValid()) + SDL_mutexV(m_pSDL_mutex); +} diff --git a/src/locks/Mutex.h b/src/locks/Mutex.h new file mode 100644 index 0000000..fdba961 --- /dev/null +++ b/src/locks/Mutex.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef MUTEX_H +#define MUTEX_H + +/// ***** Header Class ***** + +class SDL_mutex; + +class Mutex +{ +public: + Mutex(); + ~Mutex(); + + void init(); + void clean(); + + bool IsValid(); + + void Lock(); + void Unlock(); + +// hide copying methods! +private: + Mutex(const Mutex&); + const Mutex& operator ==(const Mutex&); + +private: + SDL_mutex* m_pSDL_mutex; +}; + +#endif // MUTEX_H diff --git a/src/locks/files.mk b/src/locks/files.mk new file mode 100644 index 0000000..bce9bfe --- /dev/null +++ b/src/locks/files.mk @@ -0,0 +1,9 @@ +CURDIR := locks/ +FILES := # insure blank + +FILES += Autolock.cpp +FILES += Mutex.cpp + +FILES := $(addprefix ${CURDIR},${FILES}) + +SRCS += ${FILES}