aboutsummaryrefslogtreecommitdiff
path: root/odb/details/posix/tls.txx
blob: 19619463101336962bed6c10d71c2eb92c1d52ee (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// file      : odb/details/posix/tls.txx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <odb/details/unique-ptr.hxx>
#include <odb/details/posix/exceptions.hxx>

namespace odb
{
  namespace details
  {
    // tls<T>
    //

    template <typename T>
    int tls<T>::error_ = 0;

    template <typename T>
    pthread_once_t tls<T>::once_ = PTHREAD_ONCE_INIT;

    template <typename T>
    pthread_key_t tls<T>::key_;

    template <typename T>
    T& tls<T>::
    get () const
    {
      int e (pthread_once (&once_, key_init));

      if (e != 0 || error_ != 0)
        throw posix_exception (e ? e : error_);

      if (void* v = pthread_getspecific (key_))
        return *static_cast<T*> (v);

      unique_ptr<T> p (new T);

      if ((e = pthread_setspecific (key_, p.get ())))
        throw posix_exception (e);

      T& r (*p);
      p.release ();
      return r;
    }

    template <typename T>
    void tls<T>::
    free ()
    {
      int e (pthread_once (&once_, key_init));

      if (e != 0 || error_ != 0)
        throw posix_exception (e ? e : error_);

      if (void* v = pthread_getspecific (key_))
      {
        if ((e = pthread_setspecific (key_, 0)))
          throw posix_exception (e);

        delete static_cast<T*> (v);
      }
    }

    template <typename T>
    void tls<T>::
    key_init ()
    {
      error_ = pthread_key_create (&key_, destructor);
    }

    template <typename T>
    void tls<T>::
    destructor (void* v)
    {
      delete static_cast<T*> (v);
    }

    // tls<T*>
    //

    template <typename T>
    int tls<T*>::error_ = 0;

    template <typename T>
    pthread_once_t tls<T*>::once_ = PTHREAD_ONCE_INIT;

    template <typename T>
    pthread_key_t tls<T*>::key_;

    template <typename T>
    T* tls<T*>::
    get () const
    {
      int e (pthread_once (&once_, key_init));

      if (e != 0 || error_ != 0)
        throw posix_exception (e ? e : error_);

      return static_cast<T*> (pthread_getspecific (key_));
    }

    template <typename T>
    void tls<T*>::
    set (T* p)
    {
      int e (pthread_once (&once_, key_init));

      if (e != 0 || error_ != 0)
        throw posix_exception (e ? e : error_);

      if ((e = pthread_setspecific (key_, p)))
        throw posix_exception (e);
    }

    template <typename T>
    void tls<T*>::
    key_init ()
    {
      error_ = pthread_key_create (&key_, 0);
    }
  }
}