source: trunk/src/lol/sys/atomic.h @ 2359

Last change on this file since 2359 was 2359, checked in by sam, 8 years ago

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

File size: 1.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if !defined __LOL_SYS_ATOMIC_H__
12#define __LOL_SYS_ATOMIC_H__
13
14namespace lol
15{
16
17/*
18 * TODO: we should use real atomic stuff here.
19 */
20template<typename T> class Atomic
21{
22public:
23    inline Atomic(T const &value) : m_value(value) {}
24
25    operator T() const
26    {
27        return m_value;
28    }
29
30    inline T operator =(T const &value)
31    {
32        m_mutex.Lock();
33        m_value = value;
34        m_mutex.Unlock();
35    }
36
37    inline T operator++(int)
38    {
39        m_mutex.Lock();
40        T ret = m_value++;
41        m_mutex.Unlock();
42        return ret;
43    }
44
45    inline T& operator++()
46    {
47        m_mutex.Lock();
48        T ret = ++m_value;
49        m_mutex.Unlock();
50        return ret;
51    }
52
53    inline T operator--(int)
54    {
55        m_mutex.Lock();
56        T ret = m_value--;
57        m_mutex.Unlock();
58        return ret;
59    }
60
61    inline T& operator--()
62    {
63        m_mutex.Lock();
64        T ret = --m_value;
65        m_mutex.Unlock();
66        return ret;
67    }
68
69private:
70    Mutex m_mutex; /* FIXME: this shouldn't be needed */
71    T m_value;
72};
73
74} /* namespace lol */
75
76#endif // __LOL_SYS_ATOMIC_H__
77
Note: See TracBrowser for help on using the repository browser.