add vcglib

This commit is contained in:
yuanlin zhong committed 2025-11-03 17:46:44 +08:00
1 parent 6f6c0c8eb7
commit af8100f851
1537 files changed
+634197

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
#ifndef _ATOMIC_INT_H
#define _ATOMIC_INT_H
#ifdef QT_CORE_LIB
#include <QAtomicInt>
namespace mt{
typedef QAtomicInt atomicInt;
}
#elif defined(__APPLE__)
# include "atomic_int_apple.h"
//generic implementation using mutexes
#else
# include "atomic_int_generic.h"
#endif
/*
#elif defined(_WIN32)
# include "atomic_int_win32.h"
#endif
#elif defined(__linux__)
# include "atomic_int_linux.h"
#endif
*/
/*
__linux__
__unix__
__posix__
*/
#endif // _ATOMIC_INT_H
+130
View File
@@ -0,0 +1,130 @@
#ifndef _ATOMIC_INT_APPLE_H
#define _ATOMIC_INT_APPLE_H
#include <libkern/OSAtomic.h>
//http://developer.apple.com/library/mac/#documentation/Darwin/Reference/KernelIOKitFramework/OSAtomic_h/index.html
namespace mt{
class atomicInt
{
private:
volatile int _q_value;
public:
atomicInt()
{
_q_value = 0;
}
atomicInt( int value )
{
_q_value = value;
}
// atomic API
/**
Reads the current value of this QAtomicInt and then adds valueToAdd
to the current value, returning the original value.
Unfortunately, MacOSX does not provide with fetch-and-add functions,
only add-and-fetch. Therefore, we have to simulate them.
Implementation based on SDL:
//http://lists.libsdl.org/pipermail/commits-libsdl.org/2011-January/003568.html
*/
inline int fetchAndAddAcquire( int valueToAdd )
{
//T *originalValue = currentValue;
//currentValue += valueToAdd;
//return originalValue;
int originalValue;
do {
originalValue = _q_value;
} while (!OSAtomicCompareAndSwap32Barrier(originalValue, originalValue+valueToAdd, &_q_value));
return originalValue;
}
/**
Atomically increments the value of this atomicInt.
Returns true if the new value is non-zero, false otherwise.*/
inline bool ref()
{
return OSAtomicIncrement32Barrier(&_q_value) != 0;
}
/*
Atomically decrements the value of this QAtomicInt.
Returns true if the new value is non-zero, false otherwise.*/
inline bool deref()
{
return OSAtomicDecrement32Barrier(&_q_value) != 0;
}
/*
If the current _q_value of this QAtomicInt is the expectedValue,
the test-and-set functions assign the newValue to this QAtomicInt
and return true. If the values are not the same, this function
does nothing and returns false.
*/
inline bool testAndSetOrdered(int expectedValue, int newValue)
{
//if (currentValue == expectedValue) {
// currentValue = newValue;
// return true;
// }
//return false;
return OSAtomicCompareAndSwap32Barrier(expectedValue, newValue, &_q_value);
}
// Non-atomic API
inline bool operator==(int value) const
{
return _q_value == value;
}
inline bool operator!=(int value) const
{
return _q_value != value;
}
inline bool operator!() const
{
return _q_value == 0;
}
inline operator int() const
{
return _q_value;
}
inline atomicInt &operator=(int value)
{
_q_value = value;
return *this;
}
inline bool operator>(int value) const
{
return _q_value > value;
}
inline bool operator<(int value) const
{
return _q_value < value;
}
};
}//namespace
#endif
@@ -0,0 +1,115 @@
#ifndef _ATOMIC_INT_GENERIC_H
#define _ATOMIC_INT_GENERIC_H
#include "mt.h"
namespace mt{
class atomicInt
{
public:
atomicInt()
{
_q_value = 0;
}
atomicInt( int value )
{
_q_value = value;
}
// atomic API
/**
Reads the current _q_value of this QAtomicInt and then adds valueToAdd
to the current _q_value, returning the original _q_value.
*/
inline int fetchAndAddAcquire( int valueToAdd )
{
mutexlocker lock(&m);
int originalValue = _q_value;
_q_value += valueToAdd;
return originalValue;
}
/**
Atomically increments the _q_value of this atomicInt.
Returns true if the new _q_value is non-zero, false otherwise.*/
inline bool ref()
{
mutexlocker lock(&m);
return ++_q_value != 0;
}
/*
Atomically decrements the _q_value of this QAtomicInt.
Returns true if the new _q_value is non-zero, false otherwise.*/
inline bool deref()
{
mutexlocker lock(&m);
return --_q_value != 0;
}
/*
If the current _q_value of this QAtomicInt is the expectedValue,
the test-and-set functions assign the newValue to this QAtomicInt
and return true. If the values are not the same, this function
does nothing and returns false.
*/
inline bool testAndSetOrdered(int expectedValue, int newValue)
{
mutexlocker lock(&m);
if (_q_value == expectedValue) {
_q_value = newValue;
return true;
}
return false;
}
// Non-atomic API
inline bool operator==(int value) const
{
return _q_value == value;
}
inline bool operator!=(int value) const
{
return _q_value != value;
}
inline bool operator!() const
{
return _q_value == 0;
}
inline operator int() const
{
return _q_value;
}
inline atomicInt &operator=(int value)
{
_q_value = value;
return *this;
}
inline bool operator>(int value) const
{
return _q_value > value;
}
inline bool operator<(int value) const
{
return _q_value < value;
}
private:
volatile int _q_value;
mutex m;
};
}//namespace
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef MT_BASE_H
#define MT_BASE_H
#include <assert.h>
#define MT_PREVENT_COPY(CLASS_NAME) \
private: \
CLASS_NAME (const CLASS_NAME &); \
CLASS_NAME & operator = (const CLASS_NAME &);
#define MT_ASSERT assert
namespace mt
{
}
#endif // MT_BASE_H
+53
View File
@@ -0,0 +1,53 @@
#ifndef MT_CONDITION_H
#define MT_CONDITION_H
#include "base.h"
#include "mutex.h"
#include <pthread.h>
namespace mt
{
class condition
{
MT_PREVENT_COPY(condition)
public:
typedef condition this_type;
typedef void base_type;
condition(void)
{
pthread_cond_init(&(this->c), 0);
}
~condition(void)
{
pthread_cond_destroy(&(this->c));
}
void signal(void)
{
pthread_cond_signal(&(this->c));
}
void broadcast(void)
{
pthread_cond_broadcast(&(this->c));
}
void wait(mutex & m)
{
pthread_cond_wait(&(this->c), &(m.m));
}
private:
pthread_cond_t c;
};
}
#endif // MT_CONDITION_H
+39
View File
@@ -0,0 +1,39 @@
#ifndef MT_MT_H
#define MT_MT_H
#ifdef QT_CORE_LIB
#include <QThread>
#include <QMutex>
#include <QSemaphore>
namespace mt {
typedef QThread thread;
typedef QMutex mutex;
typedef QMutexLocker mutexlocker;
typedef QSemaphore semaphore;
//cache.h, token.h
//QAtomicInt
}//namespace
#else
#include "base.h"
#include "mutex.h"
#include "rw_lock.h"
#include "semaphore.h"
#include "thread.h"
#include "scoped_mutex_lock.h"
#include "scoped_read_lock.h"
#include "scoped_write_lock.h"
namespace mt{
typedef scoped_mutex_lock mutexlocker;
}
#endif
#endif // MT_MT_H
+56
View File
@@ -0,0 +1,56 @@
#ifndef MT_MUTEX_H
#define MT_MUTEX_H
#include "base.h"
#include <pthread.h>
namespace mt
{
class condition;
class mutex
{
MT_PREVENT_COPY(mutex)
public:
typedef mutex this_type;
typedef void base_type;
mutex(void)
{
pthread_mutex_init(&(this->m), 0);
}
~mutex(void)
{
pthread_mutex_destroy(&(this->m));
}
void lock(void)
{
pthread_mutex_lock(&(this->m));
}
void unlock(void)
{
pthread_mutex_unlock(&(this->m));
}
bool tryLock(void)
{
int a = pthread_mutex_trylock(&(this->m));
return a == 0;
}
private:
friend class condition;
pthread_mutex_t m;
};
}
#endif // MT_MUTEX_H
+57
View File
@@ -0,0 +1,57 @@
#ifndef MT_RW_LOCK_H
#define MT_RW_LOCK_H
#include "base.h"
#include <pthread.h>
namespace mt
{
class rw_lock
{
MT_PREVENT_COPY(rw_lock)
public:
typedef rw_lock this_type;
typedef void base_type;
rw_lock(void)
{
pthread_rwlock_init(&(this->rw), 0);
}
~rw_lock(void)
{
pthread_rwlock_destroy(&(this->rw));
}
void lock_read(void)
{
pthread_rwlock_rdlock(&(this->rw));
}
void unlock_read(void)
{
pthread_rwlock_unlock(&(this->rw));
}
void lock_write(void)
{
pthread_rwlock_wrlock(&(this->rw));
}
void unlock_write(void)
{
pthread_rwlock_unlock(&(this->rw));
}
private:
pthread_rwlock_t rw;
};
}
#endif // MT_RW_LOCK_H
+88
View File
@@ -0,0 +1,88 @@
#ifndef MT_SCOPED_MUTEX_LOCK_H
#define MT_SCOPED_MUTEX_LOCK_H
#include "mutex.h"
namespace mt
{
class scoped_mutex_lock
{
MT_PREVENT_COPY(scoped_mutex_lock)
public:
typedef scoped_mutex_lock this_type;
typedef void base_type;
scoped_mutex_lock(mutex & m) : mtx(m)
{
this->mtx.lock();
}
~scoped_mutex_lock(void)
{
this->mtx.unlock();
}
//jnoguera 14-12-2011
//method added to mime QMutexLocker
scoped_mutex_lock(mutex * m) : mtx( *m )
{
this->mtx.lock();
}
protected:
mutex & mtx;
};
}
#endif // MT_SCOPED_MUTEX_LOCK_H
/*
#ifndef MT_SCOPED_MUTEX_LOCK_H
#define MT_SCOPED_MUTEX_LOCK_H
#include "mutex.h"
namespace mt
{
class scoped_mutex_lock
{
MT_PREVENT_COPY(scoped_mutex_lock)
public:
typedef scoped_mutex_lock this_type;
typedef void base_type;
scoped_mutex_lock(mutex & m) : mtx(m)
{
this->mtx.lock();
}
~scoped_mutex_lock(void)
{
this->mtx.unlock();
}
protected:
mutex & mtx;
};
}
#endif // MT_SCOPED_MUTEX_LOCK_H
*/
+35
View File
@@ -0,0 +1,35 @@
#ifndef MT_SCOPED_READ_LOCK_H
#define MT_SCOPED_READ_LOCK_H
#include "rw_lock.h"
namespace mt
{
class scoped_read_lock
{
MT_PREVENT_COPY(scoped_read_lock)
public:
typedef scoped_read_lock this_type;
typedef void base_type;
scoped_read_lock(rw_lock & rwl) : rw(rwl)
{
this->rw.lock_read();
}
~scoped_read_lock(void)
{
this->rw.unlock_read();
}
protected:
rw_lock & rw;
};
}
#endif // MT_SCOPED_READ_LOCK_H
+35
View File
@@ -0,0 +1,35 @@
#ifndef MT_SCOPED_WRITE_LOCK_H
#define MT_SCOPED_WRITE_LOCK_H
#include "rw_lock.h"
namespace mt
{
class scoped_write_lock
{
MT_PREVENT_COPY(scoped_write_lock)
public:
typedef scoped_write_lock this_type;
typedef void base_type;
scoped_write_lock(rw_lock & rwl) : rw(rwl)
{
this->rw.lock_write();
}
~scoped_write_lock(void)
{
this->rw.unlock_write();
}
protected:
rw_lock & rw;
};
}
#endif // MT_SCOPED_WRITE_LOCK_H
+96
View File
@@ -0,0 +1,96 @@
#ifndef MT_SEMAPHORE_H
#define MT_SEMAPHORE_H
#include "base.h"
#include <iostream>
#include <semaphore.h>
namespace mt
{
class semaphore
{
MT_PREVENT_COPY(semaphore)
public:
typedef semaphore this_type;
typedef void base_type;
semaphore(void)
{
sem_init(&(this->s), 0, 0);
}
semaphore(int value)
{
sem_init(&(this->s), 0, value);
}
~semaphore(void)
{
sem_destroy(&(this->s));
}
void post(void)
{
sem_post(&(this->s));
}
/*
void post(int n)
{
sem_post_multiple(&(this->s), n);
}
*/
void wait(void)
{
sem_wait(&(this->s));
}
bool trywait(void)
{
return (sem_trywait(&(this->s)) == 0);
}
//methods added for conforming to the QT implementation
//jnoguera 14-12-2011
void release(int n=1)
{
if(n != 1)
std::cout << "Error, mt::semaphore.release() not supported\n";
sem_post(&(this->s));
}
void acquire(int n=1)
{
if(n != 1)
std::cout << "Error, mt::semaphore.tryAcquire() not supported\n";
sem_wait(&(this->s));
}
bool tryAcquire(int n=1)
{
if(n != 1)
std::cout << "Error, mt::semaphore.tryAcquire() not supported\n";
return (sem_trywait(&(this->s)) == 0);
}
int available()
{
int value;
sem_getvalue( &(this->s), &value );
return value;
}
private:
sem_t s;
};
}
#endif // MT_SEMAPHORE_H
+112
View File
@@ -0,0 +1,112 @@
#ifndef MT_THREAD_H
#define MT_THREAD_H
#include <memory.h>
#include "base.h"
#include <pthread.h>
#include <signal.h>
namespace mt
{
class thread
{
MT_PREVENT_COPY(thread)
public:
typedef thread this_type;
typedef void base_type;
thread(void) : flags(0)
{
;
}
virtual ~thread(void)
{
;
}
virtual bool start(void)
{
if ((this->flags & thread_started) != 0) return false;
pthread_create(&(this->tid), 0, this_type::thread_func, reinterpret_cast<void *>(this));
/*
sched_param sp;
memset(&sp, 0, sizeof(sched_param));
sp.sched_priority = sched_get_priority_min(SCHED_OTHER);
sp.sched_priority = 0; // normal
pthread_setschedparam(this->tid, SCHED_OTHER, &sp);
*/
this->flags |= thread_started;
return true;
}
virtual bool wait(void)
{
if ((this->flags & thread_started) == 0) return false;
pthread_join(this->tid, 0);
this->flags &= ~thread_started;
return true;
}
virtual bool kill(void)
{
if ((this->flags & thread_started) == 0) return false;
pthread_kill(this->tid, 0);
this->flags &= ~(thread_started | thread_running);
return true;
}
bool is_started(void) const
{
return ((this->flags & thread_started) != 0);
}
bool is_running(void) const
{
return ((this->flags & thread_running) != 0);
}
protected:
virtual void run(void)
{
;
}
private:
enum thread_flags
{
thread_none = ( 0),
thread_started = (1 << 0),
thread_running = (1 << 1)
};
volatile unsigned int flags;
pthread_t tid;
static void * thread_func(void * param)
{
this_type * p_this = reinterpret_cast<this_type *>(param);
MT_ASSERT(p_this != 0);
p_this->flags |= thread_running;
p_this->run();
p_this->flags &= ~thread_running;
return 0;
}
};
}
#endif // MT_THREAD_H
+41
View File
@@ -0,0 +1,41 @@
#ifndef MT_UTIL_H
#define MT_UTIL_H
#if (defined(_MSC_VER))
#include <windows.h>
#elif (defined(__MINGW32__))
#include <windows.h>
#elif (defined(__linux__))
#include <unistd.h>
#else
#include <unistd.h>
//#error "mt utils.h : unrecognized environment."
#endif
namespace mt
{
inline void sleep_ms(unsigned int msecs)
{
#if (defined(_MSC_VER))
Sleep(DWORD(msecs));
#elif (defined(__MINGW32__))
Sleep(DWORD(msecs));
#elif (defined(__linux__))
const unsigned int secs = ((unsigned int)(msecs / 1000));
const unsigned long usecs = ((unsigned long)((msecs % 1000) * 1000));
sleep(secs);
usleep(usecs);
#else
const unsigned int secs = ((unsigned int)(msecs / 1000));
const unsigned long usecs = ((unsigned long)((msecs % 1000) * 1000));
sleep(secs);
usleep(usecs);
#endif
}
}
#endif // MT_UTIL_H