~ubuntu-branches/ubuntu/trusty/teeworlds/trusty-updates

« back to all changes in this revision

Viewing changes to src/base/tl/threading.h

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-05-05 09:49:34 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20130505094934-uidw42ov1t0jlvrz
Tags: 0.6.2+dfsg-1
* New upstream release.
  - Update patches.
* Pass $CPPFLAGS to the build system.
* Switch to my @debian.org email address.
* Bump Standards-Version to 3.9.4, no changes needed.
* Change Vcs host to anonscm.debian.org.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#pragma once
 
3
 
 
4
#include "../system.h"
 
5
 
 
6
/*
 
7
        atomic_inc - should return the value after increment
 
8
        atomic_dec - should return the value after decrement
 
9
        atomic_compswap - should return the value before the eventual swap
 
10
        sync_barrier - creates a full hardware fence
 
11
*/
 
12
 
 
13
#if defined(__GNUC__)
 
14
 
 
15
        inline unsigned atomic_inc(volatile unsigned *pValue)
 
16
        {
 
17
                return __sync_add_and_fetch(pValue, 1);
 
18
        }
 
19
 
 
20
        inline unsigned atomic_dec(volatile unsigned *pValue)
 
21
        {
 
22
                return __sync_add_and_fetch(pValue, -1);
 
23
        }
 
24
 
 
25
        inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value)
 
26
        {
 
27
                return __sync_val_compare_and_swap(pValue, comperand, value);
 
28
        }
 
29
 
 
30
        inline void sync_barrier()
 
31
        {
 
32
                __sync_synchronize();
 
33
        }
 
34
 
 
35
#elif defined(_MSC_VER)
 
36
        #include <intrin.h>
 
37
 
 
38
        #define WIN32_LEAN_AND_MEAN
 
39
        #include <windows.h>
 
40
 
 
41
        inline unsigned atomic_inc(volatile unsigned *pValue)
 
42
        {
 
43
                return _InterlockedIncrement((volatile long *)pValue);
 
44
        }
 
45
        
 
46
        inline unsigned atomic_dec(volatile unsigned *pValue)
 
47
        {
 
48
                return _InterlockedDecrement((volatile long *)pValue);
 
49
        }
 
50
 
 
51
        inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value)
 
52
        {
 
53
                return _InterlockedCompareExchange((volatile long *)pValue, (long)value, (long)comperand);
 
54
        }
 
55
 
 
56
        inline void sync_barrier()
 
57
        {
 
58
                MemoryBarrier();
 
59
        }
 
60
#else
 
61
        #error missing atomic implementation for this compiler
 
62
#endif
 
63
 
 
64
#if defined(CONF_PLATFORM_MACOSX)
 
65
        /*
 
66
                use semaphore provided by SDL on macosx
 
67
        */
 
68
#else
 
69
        class semaphore
 
70
        {
 
71
                SEMAPHORE sem;
 
72
        public:
 
73
                semaphore() { semaphore_init(&sem); }
 
74
                ~semaphore() { semaphore_destroy(&sem); }
 
75
                void wait() { semaphore_wait(&sem); }
 
76
                void signal() { semaphore_signal(&sem); }
 
77
        };
 
78
#endif
 
79
 
 
80
class lock
 
81
{
 
82
        friend class scope_lock;
 
83
 
 
84
        LOCK var;
 
85
 
 
86
        void take() { lock_wait(var); }
 
87
        void release() { lock_release(var); }
 
88
 
 
89
public:
 
90
        lock()
 
91
        {
 
92
                var = lock_create();
 
93
        }
 
94
 
 
95
        ~lock()
 
96
        {
 
97
                lock_destroy(var);
 
98
        }
 
99
};
 
100
 
 
101
class scope_lock
 
102
{
 
103
        lock *var;
 
104
public:
 
105
        scope_lock(lock *l)
 
106
        {
 
107
                var = l;
 
108
                var->take();
 
109
        }
 
110
 
 
111
        ~scope_lock()
 
112
        {
 
113
                var->release();
 
114
        }
 
115
};