Qore Programming Language  0.8.7
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreThreadLocalStorage.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreThreadLocalStorage.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2013 David Nichols, all rights reserved
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 
24 #ifndef _QORE_QORETHREADLOCALSTORAGE_H
25 
26 #define _QORE_QORETHREADLOCALSTORAGE_H
27 
28 #include <pthread.h>
29 #include <assert.h>
30 
32 
35 template<typename T>
37 protected:
39  pthread_key_t key;
40 
43 
46 
47 public:
50  create();
51  }
52 
55  destroy();
56  }
57 
59  DLLLOCAL void create() {
60  pthread_key_create(&key, 0);
61  }
62 
64  DLLLOCAL void destroy() {
65  pthread_key_delete(key);
66  }
67 
69  DLLLOCAL T *get() {
70  return (T *)pthread_getspecific(key);
71  }
72 
74  DLLLOCAL void set(T *ptr) {
75 #ifndef DEBUG
76  pthread_setspecific(key, (void *)ptr);
77 #else
78  int rc = pthread_setspecific(key, (void *)ptr);
79  assert(!rc);
80 #endif
81  }
82 };
83 
84 #endif // _QORE_QORETHREADLOCALSTORAGE_H