summaryrefslogtreecommitdiff
path: root/decoder/threadlocal.h
blob: d79f5d9d37ccc5487009fdf873c9d9ab3ed89c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef THREADLOCAL_H
#define THREADLOCAL_H

#ifndef SETLOCAL_SWAP
# define SETLOCAL_SWAP 0
#endif

#ifdef BOOST_NO_MT

# define THREADLOCAL

#else

#ifdef _MSC_VER

//FIXME: doesn't work with DLLs ... use TLS apis instead (http://www.boost.org/libs/thread/doc/tss.html)
# define THREADLOCAL __declspec(thread)

#else

# define THREADLOCAL __thread

#endif

#endif

#include <algorithm> //swap

// naturally, the below are only thread-safe if value is THREADLOCAL
template <class D>
struct SaveLocal {
    D &value;
    D old_value;
    SaveLocal(D& val) : value(val), old_value(val) {}
    ~SaveLocal() {
#if SETLOCAL_SWAP
      swap(value,old_value);
#else
      value=old_value;
#endif
    }
};

template <class D>
struct SetLocal {
    D &value;
    D old_value;
    SetLocal(D& val,const D &new_value) : value(val), old_value(
#if SETLOCAL_SWAP
      new_value
#else
      val
#endif
      ) {
#if SETLOCAL_SWAP
      swap(value,old_value);
#else
      value=new_value;
#endif
    }
    ~SetLocal() {
#if SETLOCAL_SWAP
      swap(value,old_value);
#else
      value=old_value;
#endif
    }
};


#endif