Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 // -*- c++ -*-
     19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
     20 
     21 //                     O S C L _ T L S
     22 
     23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
     24 
     25 /*! \addtogroup osclbase OSCL Base
     26  *
     27  * @{
     28  */
     29 
     30 
     31 /**
     32  *  @file oscl_TLS.h
     33  *  @brief This file defines the OsclTLS template class. This class
     34  *         provides a container which used to give access to a single instance
     35  *         of a class within the scope of the TLS.
     36  *
     37  */
     38 
     39 #ifndef OSCL_TLS_H_INCLUDED
     40 #define OSCL_TLS_H_INCLUDED
     41 
     42 #ifndef OSCL_BASE_H_INCLUDED
     43 #include "oscl_base.h"
     44 #endif
     45 
     46 #ifndef OSCL_DEFALLOC_H_INCLUDED
     47 #include "oscl_defalloc.h"
     48 #endif
     49 
     50 
     51 #if (OSCL_TLS_IS_KEYED)
     52 
     53 //Keyed TLS requires global variable support
     54 #else
     55 
     56 //unused value.
     57 typedef OsclAny TOsclTlsKey;
     58 
     59 #endif //OSCL_TLS_IS_KEYED
     60 
     61 
     62 // list of TLS objects
     63 const uint32 OSCL_TLS_ID_MAGICNUM       = 0;
     64 const uint32 OSCL_TLS_ID_ERRORHOOK      = 1;
     65 const uint32 OSCL_TLS_ID_PVLOGGER       = 2;
     66 const uint32 OSCL_TLS_ID_TEST           = 3;
     67 const uint32 OSCL_TLS_ID_PVSCHEDULER    = 4;
     68 const uint32 OSCL_TLS_ID_PVERRORTRAP    = 5;
     69 const uint32 OSCL_TLS_ID_SDPMEDIAPARSER = 6;
     70 const uint32 OSCL_TLS_ID_PAYLOADPARSER  = 7;
     71 const uint32 OSCL_TLS_ID_PVMFRECOGNIZER = 8;
     72 const uint32 OSCL_TLS_ID_WMDRM          = 9;
     73 const uint32 OSCL_TLS_ID_OSCLREGISTRY   = 10;
     74 const uint32 OSCL_TLS_ID_SQLITE3        = 11;
     75 const uint32 OSCL_TLS_ID_BASE_LAST      = 11; // should always equal the largest ID defined here
     76 
     77 #define OSCL_TLS_BASE_SLOTS OSCL_TLS_ID_BASE_LAST +1
     78 
     79 //There may be additional slots defined in the osclconfig.h for the build.
     80 #ifndef OSCL_TLS_EXTERNAL_SLOTS
     81 #define OSCL_TLS_EXTERNAL_SLOTS 0
     82 #endif
     83 
     84 #define OSCL_TLS_MAX_SLOTS ( OSCL_TLS_BASE_SLOTS + OSCL_TLS_EXTERNAL_SLOTS)
     85 
     86 class TLSStorageOps
     87 {
     88     public:
     89         OSCL_IMPORT_REF static void save_registry(TOsclTlsKey* key, OsclAny* ptr, int32&);
     90         OSCL_IMPORT_REF static OsclAny* get_registry(TOsclTlsKey* key);
     91 };
     92 
     93 class OsclTLSRegistry
     94 {
     95     public:
     96         /*
     97         ** Get an entry
     98         ** @param ID: identifier
     99         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
    100         ** @returns: the entry value
    101         */
    102         OSCL_IMPORT_REF static OsclAny* getInstance(uint32 ID, int32 &error);
    103         /*
    104         ** Set an entry
    105         ** @param ID: identifier
    106         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
    107         ** @returns: the entry value
    108         */
    109         OSCL_IMPORT_REF static void registerInstance(OsclAny* ptr, uint32 ID, int32 &error);
    110 
    111     private:
    112         OsclTLSRegistry()
    113         {}
    114         typedef OsclAny* registry_type;
    115         typedef registry_type* registry_pointer_type;
    116         static _OsclBasicLock sLock;  // lock the TLS registry and key
    117 
    118 #if ( OSCL_TLS_IS_KEYED)
    119         class TlsKey
    120         {
    121             public:
    122                 TlsKey(): iRefCnt(0), iOsclTlsKey(NULL)
    123                 {}
    124                 uint32 iRefCnt;
    125                 TOsclTlsKey *iOsclTlsKey;
    126         };
    127 
    128         //The key is a global variable.
    129         static TlsKey* iTlsKey;
    130 #endif
    131 
    132     private:
    133         OSCL_IMPORT_REF static void initialize(Oscl_DefAlloc &alloc, int32 &error);
    134         OSCL_IMPORT_REF static void cleanup(Oscl_DefAlloc &alloc, int32 &error);
    135         friend class OsclBase;
    136 
    137 };
    138 
    139 template < class T, uint32 ID, class Registry = OsclTLSRegistry > class OsclTLS
    140 {
    141     private:
    142         // make the copy constructor and assignment operator private
    143         OsclTLS& operator=(OsclTLS& _Y)
    144         {
    145             return(*this);
    146         }
    147 
    148     protected:
    149         T* _Ptr;
    150 
    151     public:
    152         OsclTLS()
    153         {
    154             int32 err;
    155             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
    156         }
    157 
    158         ~OsclTLS() {};
    159 
    160         /**
    161         * @brief The indirection operator (*) accesses a value indirectly,
    162         * through a pointer
    163         *
    164         * This operator ensures that the OsclTLS can be used like the
    165         * regular pointer that it was initialized with.
    166         */
    167         T& operator*() const
    168         {
    169             return(*_Ptr);
    170         }
    171 
    172         /**
    173         * @brief The indirection operator (->) accesses a value indirectly,
    174         * through a pointer
    175         *
    176         * This operator ensures that the OsclTLS can be used like the
    177         * regular pointer that it was initialized with.
    178         */
    179         T *operator->() const
    180         {
    181             return(_Ptr);
    182         }
    183 
    184 
    185         /**
    186         * @brief set() method sets ownership to the pointer, passed.
    187         * This method is needed when the class is created with a default
    188         * constructor. Returns false in case the class is non-empty.
    189         *
    190         */
    191         bool set()
    192         {
    193             int32 err;
    194             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
    195             return (_Ptr ? true : false);
    196         }
    197 
    198 };
    199 
    200 /*! @} */
    201 
    202 
    203 
    204 #endif
    205 
    206