Qore Programming Language: include/qore/QoreThreadLocalStorage.h (6585) Source File

Qore Programming Language 0.8.11

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

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>
36 class QoreThreadLocalStorage {
37 protected:
39 pthread_key_t key;
40
42 DLLLOCAL QoreThreadLocalStorage(const QoreThreadLocalStorage&);
43
45 DLLLOCAL QoreThreadLocalStorage& operator=(const QoreThreadLocalStorage&);
46
47 public:
49 DLLLOCAL QoreThreadLocalStorage() {
50 create();
51 }
52
54 DLLLOCAL ~QoreThreadLocalStorage() {
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
QoreThreadLocalStorage
provides access to thread-local storage
Definition: QoreThreadLocalStorage.h:36
QoreThreadLocalStorage::operator=
DLLLOCAL QoreThreadLocalStorage & operator=(const QoreThreadLocalStorage &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
QoreThreadLocalStorage::QoreThreadLocalStorage
DLLLOCAL QoreThreadLocalStorage()
creates the key
Definition: QoreThreadLocalStorage.h:49
QoreThreadLocalStorage::destroy
DLLLOCAL void destroy()
destroys the key
Definition: QoreThreadLocalStorage.h:64
QoreThreadLocalStorage::create
DLLLOCAL void create()
creates the key
Definition: QoreThreadLocalStorage.h:59
QoreThreadLocalStorage::~QoreThreadLocalStorage
DLLLOCAL ~QoreThreadLocalStorage()
destroys the key
Definition: QoreThreadLocalStorage.h:54
QoreThreadLocalStorage::key
pthread_key_t key
the actual thread local storage key wrapped in this class
Definition: QoreThreadLocalStorage.h:39
QoreThreadLocalStorage::set
DLLLOCAL void set(T *ptr)
sets the key's value
Definition: QoreThreadLocalStorage.h:74