Browse Source

sys: Atomic template class for thread-safe and potentially lockless

structures.
legacy
Sam Hocevar sam 11 years ago
parent
commit
32e3cff46a
5 changed files with 83 additions and 3 deletions
  1. +2
    -1
      src/Makefile.am
  2. +77
    -0
      src/lol/sys/atomic.h
  3. +2
    -1
      src/lol/sys/sys.h
  4. +1
    -0
      src/lolcore.vcxproj
  5. +1
    -1
      test/Makefile.am

+ 2
- 1
src/Makefile.am View File

@@ -42,7 +42,8 @@ liblol_headers = \
lol/math/remez.h lol/math/math.h lol/math/geometry.h \ lol/math/remez.h lol/math/math.h lol/math/geometry.h \
\ \
lol/sys/sys.h \ lol/sys/sys.h \
lol/sys/init.h lol/sys/file.h lol/sys/thread.h lol/sys/timer.h \
lol/sys/init.h lol/sys/file.h lol/sys/thread.h lol/sys/atomic.h \
lol/sys/timer.h \
\ \
lol/image/image.h \ lol/image/image.h \
lol/image/color.h \ lol/image/color.h \


+ 77
- 0
src/lol/sys/atomic.h View File

@@ -0,0 +1,77 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
//

#if !defined __LOL_SYS_ATOMIC_H__
#define __LOL_SYS_ATOMIC_H__

namespace lol
{

/*
* TODO: we should use real atomic stuff here.
*/
template<typename T> class Atomic
{
public:
inline Atomic(T const &value) : m_value(value) {}

operator T() const
{
return m_value;
}

inline T operator =(T const &value)
{
m_mutex.Lock();
m_value = value;
m_mutex.Unlock();
}

inline T operator++(int)
{
m_mutex.Lock();
T ret = m_value++;
m_mutex.Unlock();
return ret;
}

inline T& operator++()
{
m_mutex.Lock();
T ret = ++m_value;
m_mutex.Unlock();
return ret;
}

inline T operator--(int)
{
m_mutex.Lock();
T ret = m_value--;
m_mutex.Unlock();
return ret;
}

inline T& operator--()
{
m_mutex.Lock();
T ret = --m_value;
m_mutex.Unlock();
return ret;
}

private:
Mutex m_mutex; /* FIXME: this shouldn't be needed */
T m_value;
};

} /* namespace lol */

#endif // __LOL_SYS_ATOMIC_H__


+ 2
- 1
src/lol/sys/sys.h View File

@@ -11,9 +11,10 @@
#if !defined __LOL_SYS_SYS_H__ #if !defined __LOL_SYS_SYS_H__
#define __LOL_SYS_SYS_H__ #define __LOL_SYS_SYS_H__


#include <lol/sys/thread.h>
#include <lol/sys/atomic.h>
#include <lol/sys/init.h> #include <lol/sys/init.h>
#include <lol/sys/file.h> #include <lol/sys/file.h>
#include <lol/sys/thread.h>
#include <lol/sys/timer.h> #include <lol/sys/timer.h>


#endif // __LOL_SYS_SYS_H__ #endif // __LOL_SYS_SYS_H__


+ 1
- 0
src/lolcore.vcxproj View File

@@ -606,6 +606,7 @@
<ClInclude Include="lol\math\real.h" /> <ClInclude Include="lol\math\real.h" />
<ClInclude Include="lol\math\remez.h" /> <ClInclude Include="lol\math\remez.h" />
<ClInclude Include="lol\math\vector.h" /> <ClInclude Include="lol\math\vector.h" />
<ClInclude Include="lol\sys\atomic.h" />
<ClInclude Include="lol\sys\file.h" /> <ClInclude Include="lol\sys\file.h" />
<ClInclude Include="lol\sys\init.h" /> <ClInclude Include="lol\sys\init.h" />
<ClInclude Include="lol\sys\sys.h" /> <ClInclude Include="lol\sys\sys.h" />


+ 1
- 1
test/Makefile.am View File

@@ -23,7 +23,7 @@ testsuite_SOURCES = testsuite.cpp \
unit/vector.cpp unit/matrix.cpp unit/half.cpp unit/trig.cpp \ unit/vector.cpp unit/matrix.cpp unit/half.cpp unit/trig.cpp \
unit/build.cpp unit/real.cpp unit/image.cpp unit/quat.cpp unit/cmplx.cpp \ unit/build.cpp unit/real.cpp unit/image.cpp unit/quat.cpp unit/cmplx.cpp \
unit/array.cpp unit/rotation.cpp unit/string.cpp unit/map.cpp \ unit/array.cpp unit/rotation.cpp unit/string.cpp unit/map.cpp \
unit/color.cpp
unit/color.cpp unit/atomic.cpp
testsuite_CPPFLAGS = @LOL_CFLAGS@ testsuite_CPPFLAGS = @LOL_CFLAGS@
testsuite_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ testsuite_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@
testsuite_DEPENDENCIES = $(top_builddir)/src/liblol.a testsuite_DEPENDENCIES = $(top_builddir)/src/liblol.a


Loading…
Cancel
Save