Qore Programming Language  0.8.3
include/qore/QoreThreadLocalStorage.h (4311)
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */
00002 /*
00003   QoreThreadLocalStorage.h
00004 
00005   Qore Programming Language
00006 
00007   Copyright (C) 2003 - 2011 David Nichols, all rights reserved
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 */
00023 
00024 #ifndef _QORE_QORETHREADLOCALSTORAGE_H
00025 
00026 #define _QORE_QORETHREADLOCALSTORAGE_H
00027 
00028 #include <pthread.h>
00029 #include <assert.h>
00030 
00032 
00035 template<typename T>
00036 class QoreThreadLocalStorage {
00037 protected:
00039    pthread_key_t key;
00040    
00042    DLLLOCAL QoreThreadLocalStorage(const QoreThreadLocalStorage&);
00043    
00045    DLLLOCAL QoreThreadLocalStorage& operator=(const QoreThreadLocalStorage&);
00046 
00047 public:
00049    DLLLOCAL QoreThreadLocalStorage() {
00050       create();
00051    }
00052 
00054    DLLLOCAL ~QoreThreadLocalStorage() {
00055       destroy();
00056    }
00057 
00059    DLLLOCAL void create() {
00060       pthread_key_create(&key, 0);
00061    }
00062 
00064    DLLLOCAL void destroy() {
00065       pthread_key_delete(key);
00066    }
00067 
00069    DLLLOCAL T *get() {
00070       return (T *)pthread_getspecific(key);
00071    }
00072 
00074    DLLLOCAL void set(T *ptr) {
00075 #ifndef DEBUG
00076       pthread_setspecific(key, (void *)ptr);
00077 #else
00078       int rc = pthread_setspecific(key, (void *)ptr);
00079       assert(!rc);
00080 #endif
00081    }
00082 };
00083 
00084 #endif // _QORE_QORETHREADLOCALSTORAGE_H
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines