Ver código fonte

core: add a mediocre threading system as the base for the real thing.

legacy
Sam Hocevar sam 13 anos atrás
pai
commit
29db078d6a
4 arquivos alterados com 197 adições e 1 exclusões
  1. +1
    -1
      configure.ac
  2. +1
    -0
      src/core.h
  3. +50
    -0
      src/thread/thread.h
  4. +145
    -0
      src/thread/threadbase.h

+ 1
- 1
configure.ac Ver arquivo

@@ -82,7 +82,7 @@ dnl Code qui fait des warnings == code de porc == deux baffes dans ta gueule
CXXFLAGS="${CXXFLAGS} -Wall -Wextra -Wpointer-arith -Wcast-align -Wcast-qual -Wshadow -Wsign-compare"

AC_CHECK_LIB(m, sin, MATH_LIBS="${MATH_LIBS} -lm")
AC_CHECK_LIB(pthread, main, LIBS="$LIBS -lpthread")

dnl Are we on the PS3?
AC_CHECK_LIB(sysmodule_stub, cellSysmoduleLoadModule,


+ 1
- 0
src/core.h Ver arquivo

@@ -68,6 +68,7 @@ static inline int isnan(float f)
#include "matrix.h"
#include "numeric.h"
#include "timer.h"
#include "thread/thread.h"

// Static classes
#include "log.h"


+ 50
- 0
src/thread/thread.h Ver arquivo

@@ -0,0 +1,50 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

//
// The Threading classes
// ---------------------
//

#if !defined __LOL_THREAD_H__
#define __LOL_THREAD_H__

#if defined __CELLOS_LV2__
# include "platform/ps3/threadbase.h"
#else
# include "threadbase.h"
#endif

namespace lol
{

class Mutex : public MutexBase
{
public:
Mutex() : MutexBase() {}
};

class Condition : public ConditionBase
{
public:
Condition() : ConditionBase() {}
};

class Thread : ThreadBase
{
public:
Thread(void *(*fn)(void *), void *data) : ThreadBase(fn, data) {}
virtual ~Thread() {}
};

} /* namespace lol */

#endif // __LOL_THREAD_H__


+ 145
- 0
src/thread/threadbase.h Ver arquivo

@@ -0,0 +1,145 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

//
// The ThreadBase class
// --------------------
//

#if !defined __LOL_THREADBASE_H__
#define __LOL_THREADBASE_H__

#if defined __linux__ || defined __native_client__
# include <pthread.h>
#else
# error No threading support yet :(
#endif

namespace lol
{

class MutexBase
{
public:
MutexBase()
{
#if defined __linux__ || defined __native_client__
pthread_mutex_init(&m_mutex, NULL);
#endif
}

~MutexBase()
{
#if defined __linux__ || defined __native_client__
pthread_mutex_destroy(&m_mutex);
#endif
}

void Lock()
{
#if defined __linux__ || defined __native_client__
pthread_mutex_lock(&m_mutex);
#endif
}

void Unlock()
{
#if defined __linux__ || defined __native_client__
pthread_mutex_unlock(&m_mutex);
#endif
}

private:
#if defined __linux__ || defined __native_client__
pthread_mutex_t m_mutex;
#endif
};

class ConditionBase
{
public:
ConditionBase()
{
#if defined __linux__ || defined __native_client__
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_cond, NULL);
#endif
}

~ConditionBase()
{
#if defined __linux__ || defined __native_client__
pthread_cond_destroy(&m_cond);
pthread_mutex_destroy(&m_mutex);
#endif
}

void Acquire()
{
#if defined __linux__ || defined __native_client__
pthread_mutex_lock(&m_mutex);
#endif
}

void Release()
{
#if defined __linux__ || defined __native_client__
pthread_mutex_unlock(&m_mutex);
#endif
}

void Wait()
{
#if defined __linux__ || defined __native_client__
pthread_cond_wait(&m_cond, &m_mutex);
#endif
}

void Notify()
{
#if defined __linux__ || defined __native_client__
pthread_cond_signal(&m_cond);
#endif
}

private:
#if defined __linux__ || defined __native_client__
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
#endif
};

class ThreadBase
{
public:
ThreadBase(void *(*fn)(void *), void *data)
{
#if defined __linux__ || defined __native_client__
pthread_create(&m_thread, NULL, fn, data);
#endif
}

virtual ~ThreadBase()
{
#if defined __linux__ || defined __native_client__
pthread_join(m_thread, NULL);
#endif
}

private:
#if defined __linux__ || defined __native_client__
pthread_t m_thread;
#endif
};

} /* namespace lol */

#endif // __LOL_THREADBASE_H__


Carregando…
Cancelar
Salvar