From 32e3cff46afbc33a8a2bac4f28f5f83837d04d78 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 5 Feb 2013 02:08:48 +0000 Subject: [PATCH] sys: Atomic template class for thread-safe and potentially lockless structures. --- src/Makefile.am | 3 +- src/lol/sys/atomic.h | 77 ++++++++++++++++++++++++++++++++++++++++++++ src/lol/sys/sys.h | 3 +- src/lolcore.vcxproj | 1 + test/Makefile.am | 2 +- 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/lol/sys/atomic.h diff --git a/src/Makefile.am b/src/Makefile.am index d178b96c..60d0c929 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -42,7 +42,8 @@ liblol_headers = \ lol/math/remez.h lol/math/math.h lol/math/geometry.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/color.h \ diff --git a/src/lol/sys/atomic.h b/src/lol/sys/atomic.h new file mode 100644 index 00000000..8412b57c --- /dev/null +++ b/src/lol/sys/atomic.h @@ -0,0 +1,77 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// 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 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__ + diff --git a/src/lol/sys/sys.h b/src/lol/sys/sys.h index 32755422..a43705ef 100644 --- a/src/lol/sys/sys.h +++ b/src/lol/sys/sys.h @@ -11,9 +11,10 @@ #if !defined __LOL_SYS_SYS_H__ #define __LOL_SYS_SYS_H__ +#include +#include #include #include -#include #include #endif // __LOL_SYS_SYS_H__ diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index b50fd910..a331606e 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -606,6 +606,7 @@ + diff --git a/test/Makefile.am b/test/Makefile.am index 7b08e889..0d97201f 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -23,7 +23,7 @@ testsuite_SOURCES = testsuite.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/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_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ testsuite_DEPENDENCIES = $(top_builddir)/src/liblol.a