Porting parallel stuff to unix.

This commit is contained in:
castano 2011-09-27 18:12:32 +00:00
parent 5081360073
commit 2364f539eb
12 changed files with 181 additions and 138 deletions

View File

@ -25,6 +25,12 @@ namespace nv
inline uint32 asUnsigned(int32 x) { return (uint32) x; } inline uint32 asUnsigned(int32 x) { return (uint32) x; }
inline uint64 asUnsigned(int64 x) { return (uint64) x; } inline uint64 asUnsigned(int64 x) { return (uint64) x; }
template <typename T> inline uint32 toU32(T x) {
nvDebugCheck(x <= UINT32_MAX);
nvDebugCheck(x >= 0);
return (uint32) x;
}
/* /*
template <typename T> inline int8 toI8(T x) { template <typename T> inline int8 toI8(T x) {
nvDebugCheck(x <= INT8_MAX); nvDebugCheck(x <= INT8_MAX);

View File

@ -61,7 +61,7 @@ namespace nv {
*ptr = value; // on x86, stores are Release *ptr = value; // on x86, stores are Release
nvCompilerWriteBarrier(); nvCompilerWriteBarrier();
#else #else
#error "Not implemented" #error "Atomics not implemented."
#endif #endif
} }
@ -84,6 +84,25 @@ namespace nv {
return (uint32)_InterlockedDecrement((long *)value); return (uint32)_InterlockedDecrement((long *)value);
} }
#elif NV_CC_GNUC
// Many alternative implementations at:
// http://www.memoryhole.net/kyle/2007/05/atomic_incrementing.html
inline uint32 atomicIncrement(uint32 * value)
{
nvDebugCheck((intptr_t(value) & 3) == 0);
return __sync_fetch_and_add(value, 1);
}
inline uint32 atomicDecrement(uint32 * value)
{
nvDebugCheck((intptr_t(value) & 3) == 0);
return __sync_fetch_and_sub(value, 1);
}
#else
#error "Atomics not implemented."
#endif #endif

View File

@ -1,11 +1,13 @@
PROJECT(nvthreads) PROJECT(nvthreads)
SET(THREADS_SRCS SET(THREADS_SRCS
nvthreads.h nvthread.h nvthread.cpp
Atomic.h
Event.h Event.cpp
Mutex.h Mutex.cpp Mutex.h Mutex.cpp
SpinWaiter.h SpinWaiter.cpp ParallelFor.h ParallelFor.cpp
Thread.h Thread.cpp Thread.h Thread.cpp
ThreadLocalStorage.h ThreadLocalStorage.cpp) ThreadPool.h ThreadPool.cpp)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -48,5 +48,6 @@ void Event::wait() {
} }
#elif NV_OS_UNIX #elif NV_OS_UNIX
// @@ // @@ TODO
#pragma NV_MESSAGE("Implement event using pthreads!")
#endif #endif

View File

@ -86,4 +86,4 @@ void Mutex::unlock()
nvDebugCheck(result == 0); nvDebugCheck(result == 0);
} }
#endif // NV_OS #endif // NV_OS_UNIX

View File

@ -23,6 +23,7 @@ struct Thread::Private
void * arg; void * arg;
}; };
#if NV_OS_WIN32 #if NV_OS_WIN32
unsigned long __stdcall threadFunc(void * arg) { unsigned long __stdcall threadFunc(void * arg) {
@ -32,11 +33,13 @@ unsigned long __stdcall threadFunc(void * arg) {
} }
#elif NV_OS_UNIX #elif NV_OS_UNIX
extern "C" void * threadFunc(void * arg) { extern "C" void * threadFunc(void * arg) {
Thread * thread = (Thread *)arg; Thread * thread = (Thread *)arg;
thread->func(thread->arg); thread->func(thread->arg);
pthread_exit(0); pthread_exit(0);
} }
#endif #endif
@ -134,3 +137,4 @@ bool Thread::isRunning () const
} }
//#endif //#endif
} }

View File

@ -35,7 +35,7 @@ namespace nv
struct Private; struct Private;
AutoPtr<Private> p; AutoPtr<Private> p;
public: public: // @@ Why public? Also in private?!
ThreadFunc * func; ThreadFunc * func;
void * arg; void * arg;

View File

@ -4,6 +4,8 @@
#include "Mutex.h" #include "Mutex.h"
#include "Thread.h" #include "Thread.h"
#include "nvcore/Utils.h"
// Most of the time it's not necessary to protect the thread pool, but if it doesn't add a significant overhead, then it'd be safer to do it. // Most of the time it's not necessary to protect the thread pool, but if it doesn't add a significant overhead, then it'd be safer to do it.
#define PROTECT_THREAD_POOL 1 #define PROTECT_THREAD_POOL 1
@ -47,7 +49,7 @@ AutoPtr<ThreadPool> s_pool;
/*static*/ void ThreadPool::workerFunc(void * arg) { /*static*/ void ThreadPool::workerFunc(void * arg) {
uint i = (uint)arg; uint i = toU32((uintptr_t)arg); // This is OK, because workerCount should always be <<< 2^32
while(true) while(true)
{ {

View File

@ -1,11 +1,16 @@
// This code is in the public domain -- Ignacio Castaño <castano@gmail.com>
#include "nvthread.h" #include "nvthread.h"
#include "Thread.h" #include "Thread.h"
#define WIN32_LEAN_AND_MEAN #if NV_OS_WIN32
#define VC_EXTRALEAN #include "Win32.h"
#include <windows.h> #elif NV_OS_UNIX
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
using namespace nv; using namespace nv;

View File

@ -269,6 +269,10 @@ struct MyOutputHandler : public nvtt::OutputHandler
m_ptr = m_data; m_ptr = m_data;
} }
virtual void endImage()
{
}
virtual bool writeData(const void * data, int size) virtual bool writeData(const void * data, int size)
{ {
memcpy(m_ptr, data, size); memcpy(m_ptr, data, size);