ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync/* $Id$ */
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Thread Local Storage (TLS), Win32.
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync */
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync/*
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2008-2010 Oracle Corporation
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync *
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * available from http://www.virtualbox.org. This file is free software;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * General Public License (GPL) as published by the Free Software
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync *
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * The contents of this file may alternatively be used under the terms
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * of the Common Development and Distribution License Version 1.0
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * VirtualBox OSE distribution, in which case the provisions of the
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * CDDL are applicable instead of those of the GPL.
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync *
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * You may elect to license modified versions of this file under the
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync * terms and conditions of either the GPL or the CDDL or both.
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync */
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync/*******************************************************************************
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync* Header Files *
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync*******************************************************************************/
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync#define LOG_GROUP RTLOGGROUP_THREAD
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync#include <Windows.h>
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync#include <iprt/thread.h>
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync#include <iprt/log.h>
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync#include <iprt/assert.h>
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync#include <iprt/err.h>
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync#include "internal/thread.h"
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsyncAssertCompile(sizeof(RTTLS) >= sizeof(DWORD));
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsyncRTR3DECL(RTTLS) RTTlsAlloc(void)
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync{
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync DWORD iTls = TlsAlloc();
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return iTls != TLS_OUT_OF_INDEXES ? (RTTLS)iTls : NIL_RTTLS;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync}
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsyncRTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor)
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync{
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync AssertReturn(!pfnDestructor, VERR_NOT_SUPPORTED);
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync DWORD iTls = TlsAlloc();
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync if (iTls != TLS_OUT_OF_INDEXES)
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync {
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync Assert((RTTLS)iTls != NIL_RTTLS);
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync *piTls = (RTTLS)iTls;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync Assert((DWORD)*piTls == iTls);
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return VINF_SUCCESS;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync }
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return VERR_NO_MEMORY;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync}
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsyncRTR3DECL(int) RTTlsFree(RTTLS iTls)
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync{
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync if (iTls == NIL_RTTLS)
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return VINF_SUCCESS;
1f963f01f4f9a3848e72d4748c4f36feb715dba3vboxsync if (TlsFree((DWORD)iTls))
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return VINF_SUCCESS;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return RTErrConvertFromWin32(GetLastError());
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync}
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsyncRTR3DECL(void *) RTTlsGet(RTTLS iTls)
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync{
1f963f01f4f9a3848e72d4748c4f36feb715dba3vboxsync return TlsGetValue((DWORD)iTls);
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync}
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsyncRTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue)
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync{
1f963f01f4f9a3848e72d4748c4f36feb715dba3vboxsync void *pv = TlsGetValue((DWORD)iTls);
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync if (pv)
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync {
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync *ppvValue = pv;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return VINF_SUCCESS;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync }
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync /* TlsGetValue always updates last error */
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync *ppvValue = NULL;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return RTErrConvertFromWin32(GetLastError());
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync}
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsyncRTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue)
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync{
1f963f01f4f9a3848e72d4748c4f36feb715dba3vboxsync if (TlsSetValue((DWORD)iTls, pvValue))
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return VINF_SUCCESS;
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync return RTErrConvertFromWin32(GetLastError());
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync}
ce2999a7174f173a66d18a241dbd8f5a1236be4bvboxsync