Removed dependencies to OS-specific headers in sfml-system
git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1023 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
2228419caf
commit
9a7fcc04be
21 changed files with 531 additions and 306 deletions
|
@ -25,7 +25,18 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/Win32/Mutex.hpp>
|
||||
#include <SFML/System/Mutex.hpp>
|
||||
|
||||
|
||||
#if defined(SFML_SYSTEM_WINDOWS)
|
||||
|
||||
#include <SFML/System/Win32/MutexImpl.hpp>
|
||||
|
||||
#else
|
||||
|
||||
#include <SFML/System/Unix/MutexImpl.hpp>
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
namespace sf
|
||||
|
@ -35,7 +46,7 @@ namespace sf
|
|||
////////////////////////////////////////////////////////////
|
||||
Mutex::Mutex()
|
||||
{
|
||||
InitializeCriticalSection(&myHandle);
|
||||
myMutexImpl = new priv::MutexImpl;
|
||||
}
|
||||
|
||||
|
||||
|
@ -44,7 +55,7 @@ Mutex::Mutex()
|
|||
////////////////////////////////////////////////////////////
|
||||
Mutex::~Mutex()
|
||||
{
|
||||
DeleteCriticalSection(&myHandle);
|
||||
delete myMutexImpl;
|
||||
}
|
||||
|
||||
|
||||
|
@ -53,7 +64,7 @@ Mutex::~Mutex()
|
|||
////////////////////////////////////////////////////////////
|
||||
void Mutex::Lock()
|
||||
{
|
||||
EnterCriticalSection(&myHandle);
|
||||
myMutexImpl->Lock();
|
||||
}
|
||||
|
||||
|
||||
|
@ -62,7 +73,7 @@ void Mutex::Lock()
|
|||
////////////////////////////////////////////////////////////
|
||||
void Mutex::Unlock()
|
||||
{
|
||||
LeaveCriticalSection(&myHandle);
|
||||
myMutexImpl->Unlock();
|
||||
}
|
||||
|
||||
} // namespace sf
|
|
@ -35,7 +35,7 @@
|
|||
|
||||
#include <SFML/System/Win32/Platform.hpp>
|
||||
|
||||
#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_MACOS) || defined(SFML_SYSTEM_FREEBSD)
|
||||
#else
|
||||
|
||||
#include <SFML/System/Unix/Platform.hpp>
|
||||
|
||||
|
|
|
@ -33,23 +33,20 @@
|
|||
namespace
|
||||
{
|
||||
// Set the random numbers sequence seed with the current system time, so that it is always different
|
||||
unsigned int SetRandomSeed()
|
||||
unsigned int InitializeSeed()
|
||||
{
|
||||
unsigned int Seed = static_cast<unsigned int>(sf::priv::Platform::GetSystemTime() * 1000);
|
||||
srand(Seed);
|
||||
return Seed;
|
||||
}
|
||||
|
||||
// Global storing the current seed
|
||||
unsigned int GlobalSeed = InitializeSeed();
|
||||
}
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
// Static member variables
|
||||
////////////////////////////////////////////////////////////
|
||||
unsigned int Randomizer::ourSeed = SetRandomSeed();
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Set the seed for the generator. Using a known seed
|
||||
/// allows you to reproduce the same sequence of random number
|
||||
|
@ -57,7 +54,7 @@ unsigned int Randomizer::ourSeed = SetRandomSeed();
|
|||
void Randomizer::SetSeed(unsigned int Seed)
|
||||
{
|
||||
srand(Seed);
|
||||
ourSeed = Seed;
|
||||
GlobalSeed = Seed;
|
||||
}
|
||||
|
||||
|
||||
|
@ -66,7 +63,7 @@ void Randomizer::SetSeed(unsigned int Seed)
|
|||
////////////////////////////////////////////////////////////
|
||||
unsigned int Randomizer::GetSeed()
|
||||
{
|
||||
return ourSeed;
|
||||
return GlobalSeed;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -25,9 +25,18 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/Win32/Thread.hpp>
|
||||
#include <process.h>
|
||||
#include <iostream>
|
||||
#include <SFML/System/Thread.hpp>
|
||||
|
||||
|
||||
#if defined(SFML_SYSTEM_WINDOWS)
|
||||
|
||||
#include <SFML/System/Win32/ThreadImpl.hpp>
|
||||
|
||||
#else
|
||||
|
||||
#include <SFML/System/Unix/ThreadImpl.hpp>
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
namespace sf
|
||||
|
@ -36,9 +45,9 @@ namespace sf
|
|||
/// Default constructor
|
||||
////////////////////////////////////////////////////////////
|
||||
Thread::Thread() :
|
||||
myHandle (NULL),
|
||||
myFunction(NULL),
|
||||
myUserData(NULL)
|
||||
myThreadImpl(NULL),
|
||||
myFunction (NULL),
|
||||
myUserData (NULL)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -48,9 +57,9 @@ myUserData(NULL)
|
|||
/// Construct the thread from a function pointer
|
||||
////////////////////////////////////////////////////////////
|
||||
Thread::Thread(Thread::FuncType Function, void* UserData) :
|
||||
myHandle (NULL),
|
||||
myFunction(Function),
|
||||
myUserData(UserData)
|
||||
myThreadImpl(NULL),
|
||||
myFunction (Function),
|
||||
myUserData (UserData)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -62,8 +71,10 @@ myUserData(UserData)
|
|||
Thread::~Thread()
|
||||
{
|
||||
// Wait for the thread to finish before destroying the instance
|
||||
if (myHandle)
|
||||
Wait();
|
||||
Wait();
|
||||
|
||||
// Destroy the implementation
|
||||
delete myThreadImpl;
|
||||
}
|
||||
|
||||
|
||||
|
@ -72,12 +83,8 @@ Thread::~Thread()
|
|||
////////////////////////////////////////////////////////////
|
||||
void Thread::Launch()
|
||||
{
|
||||
// Create the thread
|
||||
myHandle = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, &Thread::ThreadFunc, this, 0, NULL));
|
||||
|
||||
// Error ?
|
||||
if (myHandle == NULL)
|
||||
std::cerr << "Failed to create thread" << std::endl;
|
||||
delete myThreadImpl;
|
||||
myThreadImpl = new priv::ThreadImpl(this);
|
||||
}
|
||||
|
||||
|
||||
|
@ -86,15 +93,8 @@ void Thread::Launch()
|
|||
////////////////////////////////////////////////////////////
|
||||
void Thread::Wait()
|
||||
{
|
||||
if (myHandle)
|
||||
{
|
||||
// Wait for the thread to finish, no timeout
|
||||
WaitForSingleObject(myHandle, INFINITE);
|
||||
|
||||
// Don't forget to close the thread handle (__endthreadex doesn't do it)
|
||||
CloseHandle(myHandle);
|
||||
myHandle = NULL;
|
||||
}
|
||||
if (myThreadImpl)
|
||||
myThreadImpl->Wait();
|
||||
}
|
||||
|
||||
|
||||
|
@ -106,11 +106,8 @@ void Thread::Wait()
|
|||
////////////////////////////////////////////////////////////
|
||||
void Thread::Terminate()
|
||||
{
|
||||
if (myHandle)
|
||||
{
|
||||
TerminateThread(myHandle, 0);
|
||||
myHandle = NULL;
|
||||
}
|
||||
if (myThreadImpl)
|
||||
myThreadImpl->Terminate();
|
||||
}
|
||||
|
||||
|
||||
|
@ -123,22 +120,4 @@ void Thread::Run()
|
|||
myFunction(myUserData);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Actual thread entry point, dispatches to instances
|
||||
////////////////////////////////////////////////////////////
|
||||
unsigned int __stdcall Thread::ThreadFunc(void* UserData)
|
||||
{
|
||||
// The Thread instance is stored in the user data
|
||||
Thread* ThreadInstance = reinterpret_cast<Thread*>(UserData);
|
||||
|
||||
// Forward to the instance
|
||||
ThreadInstance->Run();
|
||||
|
||||
// Optional, but it is cleaner
|
||||
_endthreadex(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace sf
|
|
@ -1,7 +1,7 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SFML - Simple and Fast Multimedia Library
|
||||
// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
|
||||
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
|
@ -25,15 +25,17 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/Unix/Mutex.hpp>
|
||||
#include <SFML/System/Unix/MutexImpl.hpp>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
namespace priv
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor
|
||||
////////////////////////////////////////////////////////////
|
||||
Mutex::Mutex()
|
||||
MutexImpl::MutexImpl()
|
||||
{
|
||||
pthread_mutex_init(&myMutex, NULL);
|
||||
}
|
||||
|
@ -42,7 +44,7 @@ Mutex::Mutex()
|
|||
////////////////////////////////////////////////////////////
|
||||
/// Destructor
|
||||
////////////////////////////////////////////////////////////
|
||||
Mutex::~Mutex()
|
||||
MutexImpl::~MutexImpl()
|
||||
{
|
||||
pthread_mutex_destroy(&myMutex);
|
||||
}
|
||||
|
@ -51,7 +53,7 @@ Mutex::~Mutex()
|
|||
////////////////////////////////////////////////////////////
|
||||
/// Lock the mutex
|
||||
////////////////////////////////////////////////////////////
|
||||
void Mutex::Lock()
|
||||
void MutexImpl::Lock()
|
||||
{
|
||||
pthread_mutex_lock(&myMutex);
|
||||
}
|
||||
|
@ -60,9 +62,11 @@ void Mutex::Lock()
|
|||
////////////////////////////////////////////////////////////
|
||||
/// Unlock the mutex
|
||||
////////////////////////////////////////////////////////////
|
||||
void Mutex::Unlock()
|
||||
void MutexImpl::Unlock()
|
||||
{
|
||||
pthread_mutex_unlock(&myMutex);
|
||||
}
|
||||
|
||||
} // namespace priv
|
||||
|
||||
} // namespace sf
|
83
src/SFML/System/Unix/MutexImpl.hpp
Normal file
83
src/SFML/System/Unix/MutexImpl.hpp
Normal file
|
@ -0,0 +1,83 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SFML - Simple and Fast Multimedia Library
|
||||
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it freely,
|
||||
// subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented;
|
||||
// you must not claim that you wrote the original software.
|
||||
// If you use this software in a product, an acknowledgment
|
||||
// in the product documentation would be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such,
|
||||
// and must not be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef SFML_MUTEXIMPL_HPP
|
||||
#define SFML_MUTEXIMPL_HPP
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
#include <pthread.h>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
namespace priv
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Unix implementation of mutexes
|
||||
////////////////////////////////////////////////////////////
|
||||
class MutexImpl : NonCopyable
|
||||
{
|
||||
public :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
MutexImpl();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Destructor
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
~MutexImpl();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Lock the mutex
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Lock();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Unlock the mutex
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Unlock();
|
||||
|
||||
private :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
pthread_mutex_t myMutex; ///< pthread handle of the mutex
|
||||
};
|
||||
|
||||
} // namespace priv
|
||||
|
||||
} // namespace sf
|
||||
|
||||
|
||||
#endif // SFML_MUTEXIMPL_HPP
|
|
@ -1,7 +1,7 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SFML - Simple and Fast Multimedia Library
|
||||
// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
|
||||
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
|
@ -25,78 +25,35 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/Unix/Thread.hpp>
|
||||
#include <SFML/System/Unix/ThreadImpl.hpp>
|
||||
#include <SFML/System/Thread.hpp>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
namespace priv
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor
|
||||
////////////////////////////////////////////////////////////
|
||||
Thread::Thread() :
|
||||
myIsActive(false),
|
||||
myFunction(NULL),
|
||||
myUserData(NULL)
|
||||
ThreadImpl::ThreadImpl(Thread* Owner) :
|
||||
myIsActive(true)
|
||||
{
|
||||
myIsActive = pthread_create(&myThread, NULL, &ThreadFunc, Owner) == 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Construct the thread from a function pointer
|
||||
////////////////////////////////////////////////////////////
|
||||
Thread::Thread(Thread::FuncType Function, void* UserData) :
|
||||
myIsActive(false),
|
||||
myFunction(Function),
|
||||
myUserData(UserData)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Virtual destructor
|
||||
////////////////////////////////////////////////////////////
|
||||
Thread::~Thread()
|
||||
{
|
||||
// Wait for the thread to finish before destroying the instance
|
||||
if (myIsActive)
|
||||
Wait();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Create and run the thread
|
||||
////////////////////////////////////////////////////////////
|
||||
void Thread::Launch()
|
||||
{
|
||||
// Create the thread
|
||||
myIsActive = true;
|
||||
int Error = pthread_create(&myThread, NULL, &Thread::ThreadFunc, this);
|
||||
|
||||
// Error ?
|
||||
if (Error != 0)
|
||||
{
|
||||
if (!myIsActive)
|
||||
std::cerr << "Failed to create thread" << std::endl;
|
||||
myIsActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Wait until the thread finishes
|
||||
////////////////////////////////////////////////////////////
|
||||
void Thread::Wait()
|
||||
void ThreadImpl::Wait()
|
||||
{
|
||||
if (myIsActive)
|
||||
{
|
||||
// Wait for the thread to finish, no timeout
|
||||
pthread_join(myThread, NULL);
|
||||
|
||||
// Reset the thread state
|
||||
myIsActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -106,41 +63,30 @@ void Thread::Wait()
|
|||
/// you should rather try to make the thread function
|
||||
/// terminate by itself
|
||||
////////////////////////////////////////////////////////////
|
||||
void Thread::Terminate()
|
||||
void ThreadImpl::Terminate()
|
||||
{
|
||||
if (myIsActive)
|
||||
{
|
||||
pthread_cancel(myThread);
|
||||
myIsActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Function called as the thread entry point
|
||||
/// Global entry point for all threads
|
||||
////////////////////////////////////////////////////////////
|
||||
void Thread::Run()
|
||||
void* ThreadFunc(void* UserData)
|
||||
{
|
||||
if (myFunction)
|
||||
myFunction(myUserData);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Actual thread entry point, dispatches to instances
|
||||
////////////////////////////////////////////////////////////
|
||||
void* Thread::ThreadFunc(void* UserData)
|
||||
{
|
||||
// The sfThread instance is stored in the user data
|
||||
Thread* ThreadToRun = reinterpret_cast<Thread*>(UserData);
|
||||
// The Thread instance is stored in the user data
|
||||
Thread* Owner = static_cast<Thread*>(UserData);
|
||||
|
||||
// Tell the thread to handle cancel requests immediatly
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
||||
|
||||
// Forward to the instance
|
||||
ThreadToRun->Run();
|
||||
// Forward to the owner
|
||||
Owner->Run();
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} // namespace priv
|
||||
|
||||
} // namespace sf
|
95
src/SFML/System/Unix/ThreadImpl.hpp
Normal file
95
src/SFML/System/Unix/ThreadImpl.hpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SFML - Simple and Fast Multimedia Library
|
||||
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it freely,
|
||||
// subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented;
|
||||
// you must not claim that you wrote the original software.
|
||||
// If you use this software in a product, an acknowledgment
|
||||
// in the product documentation would be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such,
|
||||
// and must not be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef SFML_THREADIMPL_HPP
|
||||
#define SFML_THREADIMPL_HPP
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
#include <pthread.h>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
class Thread;
|
||||
|
||||
namespace priv
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Unix implementation of threads
|
||||
////////////////////////////////////////////////////////////
|
||||
class ThreadImpl : NonCopyable
|
||||
{
|
||||
public :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor, launch the thread
|
||||
///
|
||||
/// \param Owner : Owner Thread instance to run
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
ThreadImpl(Thread* Owner);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Wait until the thread finishes
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Wait();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Terminate the thread
|
||||
/// Terminating a thread with this function is not safe,
|
||||
/// you should rather try to make the thread function
|
||||
/// terminate by itself
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Terminate();
|
||||
|
||||
private :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Global entry point for all threads
|
||||
///
|
||||
/// \param UserData : User-defined data (contains the Thread instance)
|
||||
///
|
||||
/// \return Error code
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
static void* EntryPoint(void* UserData);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
pthread_t myThread; ///< pthread thread instance
|
||||
bool myIsActive; ///< Thread state (active or inactive)
|
||||
};
|
||||
|
||||
} // namespace priv
|
||||
|
||||
} // namespace sf
|
||||
|
||||
|
||||
#endif // SFML_THREADIMPL_HPP
|
72
src/SFML/System/Win32/MutexImpl.cpp
Normal file
72
src/SFML/System/Win32/MutexImpl.cpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SFML - Simple and Fast Multimedia Library
|
||||
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it freely,
|
||||
// subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented;
|
||||
// you must not claim that you wrote the original software.
|
||||
// If you use this software in a product, an acknowledgment
|
||||
// in the product documentation would be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such,
|
||||
// and must not be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/Win32/MutexImpl.hpp>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
namespace priv
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor
|
||||
////////////////////////////////////////////////////////////
|
||||
MutexImpl::MutexImpl()
|
||||
{
|
||||
InitializeCriticalSection(&myMutex);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Destructor
|
||||
////////////////////////////////////////////////////////////
|
||||
MutexImpl::~MutexImpl()
|
||||
{
|
||||
DeleteCriticalSection(&myMutex);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Lock the mutex
|
||||
////////////////////////////////////////////////////////////
|
||||
void MutexImpl::Lock()
|
||||
{
|
||||
EnterCriticalSection(&myMutex);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Unlock the mutex
|
||||
////////////////////////////////////////////////////////////
|
||||
void MutexImpl::Unlock()
|
||||
{
|
||||
LeaveCriticalSection(&myMutex);
|
||||
}
|
||||
|
||||
} // namespace priv
|
||||
|
||||
} // namespace sf
|
83
src/SFML/System/Win32/MutexImpl.hpp
Normal file
83
src/SFML/System/Win32/MutexImpl.hpp
Normal file
|
@ -0,0 +1,83 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SFML - Simple and Fast Multimedia Library
|
||||
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it freely,
|
||||
// subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented;
|
||||
// you must not claim that you wrote the original software.
|
||||
// If you use this software in a product, an acknowledgment
|
||||
// in the product documentation would be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such,
|
||||
// and must not be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef SFML_MUTEXIMPL_HPP
|
||||
#define SFML_MUTEXIMPL_HPP
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
namespace priv
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Windows implementation of mutexes
|
||||
////////////////////////////////////////////////////////////
|
||||
class MutexImpl : NonCopyable
|
||||
{
|
||||
public :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
MutexImpl();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Destructor
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
~MutexImpl();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Lock the mutex
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Lock();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Unlock the mutex
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Unlock();
|
||||
|
||||
private :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
CRITICAL_SECTION myMutex; ///< Win32 handle of the mutex
|
||||
};
|
||||
|
||||
} // namespace priv
|
||||
|
||||
} // namespace sf
|
||||
|
||||
|
||||
#endif // SFML_MUTEXIMPL_HPP
|
102
src/SFML/System/Win32/ThreadImpl.cpp
Normal file
102
src/SFML/System/Win32/ThreadImpl.cpp
Normal file
|
@ -0,0 +1,102 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SFML - Simple and Fast Multimedia Library
|
||||
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it freely,
|
||||
// subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented;
|
||||
// you must not claim that you wrote the original software.
|
||||
// If you use this software in a product, an acknowledgment
|
||||
// in the product documentation would be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such,
|
||||
// and must not be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/Win32/ThreadImpl.hpp>
|
||||
#include <SFML/System/Thread.hpp>
|
||||
#include <process.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
namespace priv
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor
|
||||
////////////////////////////////////////////////////////////
|
||||
ThreadImpl::ThreadImpl(Thread* Owner)
|
||||
{
|
||||
myThread = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, &ThreadImpl::EntryPoint, Owner, 0, NULL));
|
||||
|
||||
if (!myThread)
|
||||
std::cerr << "Failed to create thread" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Destructor
|
||||
////////////////////////////////////////////////////////////
|
||||
ThreadImpl::~ThreadImpl()
|
||||
{
|
||||
if (myThread)
|
||||
CloseHandle(myThread);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Wait until the thread finishes
|
||||
////////////////////////////////////////////////////////////
|
||||
void ThreadImpl::Wait()
|
||||
{
|
||||
if (myThread)
|
||||
WaitForSingleObject(myThread, INFINITE);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Terminate the thread
|
||||
/// Terminating a thread with this function is not safe,
|
||||
/// you should rather try to make the thread function
|
||||
/// terminate by itself
|
||||
////////////////////////////////////////////////////////////
|
||||
void ThreadImpl::Terminate()
|
||||
{
|
||||
if (myThread)
|
||||
TerminateThread(myThread, 0);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Global entry point for all threads
|
||||
////////////////////////////////////////////////////////////
|
||||
unsigned int __stdcall ThreadImpl::EntryPoint(void* UserData)
|
||||
{
|
||||
// The Thread instance is stored in the user data
|
||||
Thread* Owner = static_cast<Thread*>(UserData);
|
||||
|
||||
// Forward to the owner
|
||||
Owner->Run();
|
||||
|
||||
// Optional, but it is cleaner
|
||||
_endthreadex(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace priv
|
||||
|
||||
} // namespace sf
|
100
src/SFML/System/Win32/ThreadImpl.hpp
Normal file
100
src/SFML/System/Win32/ThreadImpl.hpp
Normal file
|
@ -0,0 +1,100 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SFML - Simple and Fast Multimedia Library
|
||||
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it freely,
|
||||
// subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented;
|
||||
// you must not claim that you wrote the original software.
|
||||
// If you use this software in a product, an acknowledgment
|
||||
// in the product documentation would be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such,
|
||||
// and must not be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef SFML_THREADIMPL_HPP
|
||||
#define SFML_THREADIMPL_HPP
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
class Thread;
|
||||
|
||||
namespace priv
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Windows implementation of threads
|
||||
////////////////////////////////////////////////////////////
|
||||
class ThreadImpl : NonCopyable
|
||||
{
|
||||
public :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor, launch the thread
|
||||
///
|
||||
/// \param Owner : Owner Thread instance to run
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
ThreadImpl(Thread* Owner);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Destructor
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
~ThreadImpl();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Wait until the thread finishes
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Wait();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Terminate the thread
|
||||
/// Terminating a thread with this function is not safe,
|
||||
/// you should rather try to make the thread function
|
||||
/// terminate by itself
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Terminate();
|
||||
|
||||
private :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Global entry point for all threads
|
||||
///
|
||||
/// \param UserData : User-defined data (contains the Thread instance)
|
||||
///
|
||||
/// \return Error code
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
static unsigned int __stdcall EntryPoint(void* UserData);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
HANDLE myThread; ///< Win32 thread handle
|
||||
};
|
||||
|
||||
} // namespace priv
|
||||
|
||||
} // namespace sf
|
||||
|
||||
|
||||
#endif // SFML_THREADIMPL_HPP
|
Loading…
Add table
Add a link
Reference in a new issue