Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
QoreThreadLocalStorage.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QoreThreadLocalStorage.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_QORETHREADLOCALSTORAGE_H
33
34#define _QORE_QORETHREADLOCALSTORAGE_H
35
36#include <cassert>
37#include <pthread.h>
38
40
43template<typename T>
45protected:
47 pthread_key_t key;
48
51
54
55public:
58 create();
59 }
60
63 destroy();
64 }
65
67 DLLLOCAL void create() {
68 pthread_key_create(&key, 0);
69 }
70
72 DLLLOCAL void destroy() {
73 pthread_key_delete(key);
74 }
75
77 DLLLOCAL T *get() {
78 return (T *)pthread_getspecific(key);
79 }
80
82 DLLLOCAL void set(T *ptr) {
83#ifndef DEBUG
84 pthread_setspecific(key, (void *)ptr);
85#else
86 // make sure we are not overwriting data
87 assert(!ptr || !pthread_getspecific(key));
88
89 int rc = pthread_setspecific(key, (void *)ptr);
90 assert(!rc);
91#endif
92 }
93};
94
95#endif // _QORE_QORETHREADLOCALSTORAGE_H
provides access to thread-local storage
Definition: QoreThreadLocalStorage.h:44
DLLLOCAL QoreThreadLocalStorage(const QoreThreadLocalStorage &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
DLLLOCAL void destroy()
destroys the key
Definition: QoreThreadLocalStorage.h:72
DLLLOCAL QoreThreadLocalStorage()
creates the key
Definition: QoreThreadLocalStorage.h:57
DLLLOCAL QoreThreadLocalStorage & operator=(const QoreThreadLocalStorage &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
DLLLOCAL void set(T *ptr)
sets the key's value
Definition: QoreThreadLocalStorage.h:82
pthread_key_t key
the actual thread local storage key wrapped in this class
Definition: QoreThreadLocalStorage.h:47
DLLLOCAL T * get()
retrieves the key's value
Definition: QoreThreadLocalStorage.h:77
DLLLOCAL void create()
creates the key
Definition: QoreThreadLocalStorage.h:67
DLLLOCAL ~QoreThreadLocalStorage()
destroys the key
Definition: QoreThreadLocalStorage.h:62