/*---------------------------------------------------------------------------* Project: Horizon File: os_ThreadLocalStorage.cpp Copyright (C)2009 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 23600 $ *---------------------------------------------------------------------------*/ #include #include #include #include #include //--------------------------------------------------------------------------- using namespace nn; using nn::Result; namespace nn{ namespace os{ namespace { u16 sTLSMap = 0; // 0-15 ビットで TLS 確保状況を管理 /*! @brief スロットがマッピング済みかどうかを判定します。 @return スロットがマッピング済みであれば true を返します。 */ inline bool IsMappedIndex(s32 index) { return ((sTLSMap >> index) & 1) == 1; } /*! @brief 空いているスレッドローカルストレージのスロットを探索します。 @return 空いているスロット番号を返します。スロットに空きがなかった場合は -1 を返します。 */ s32 SearchFreeTLSIndex() { for(int i = 0; i < TLS_NUM; ++i) { if(!IsMappedIndex(i)) { return i; } } return -1; } /*! @brief スレッドローカルストレージからスロットを確保します。 @return 確保したスロット番号を返します。スロットに空きがなかった場合は -1 を返します。 */ s32 AllocateTLSIndex() { s32 index = SearchFreeTLSIndex(); if(index >= 0 && index < TLS_NUM) { sTLSMap |= (1 << index); } return index; } /*! @brief スレッドローカルストレージにスロットを解放します。 @param[in] スロット番号 */ s32 FreeTLSIndex(int index) { if(!IsMappedIndex(index)) { return -1; } return sTLSMap &= ~(1 << index); } } // namespace ThreadLocalStorage::ThreadLocalStorage() { this->m_Index = AllocateTLSIndex(); NN_TASSERTMSG_(m_Index >= 0, "Out of Thread Local Storage Slot."); } ThreadLocalStorage::~ThreadLocalStorage() { NN_TASSERT_(m_Index >= 0 && m_Index < TLS_NUM); NN_TASSERT_(IsMappedIndex(m_Index)); FreeTLSIndex(m_Index); } uptr ThreadLocalStorage::GetValue() const { NN_TASSERT_(m_Index >= 0 && m_Index < TLS_NUM); NN_TASSERT_(IsMappedIndex(m_Index)); return GetThreadLocalRegion()->tls[m_Index]; } void ThreadLocalStorage::SetValue(uptr value) { NN_TASSERT_(m_Index >= 0 && m_Index < TLS_NUM); NN_TASSERT_(IsMappedIndex(m_Index)); GetThreadLocalRegion()->tls[m_Index] = value; } void ThreadLocalStorage::ClearAllSlots() { ThreadLocalRegion& tlr = *GetThreadLocalRegion(); for( int i = 0; i < TLS_NUM; ++i ) { tlr.tls[i] = 0; } } }} // namespace nn::os