1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "system.h" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <osl/diagnose.h> 31*cdf0e10cSrcweir #include <osl/thread.h> 32*cdf0e10cSrcweir #include <rtl/alloc.h> 33*cdf0e10cSrcweir #include <osl/time.h> 34*cdf0e10cSrcweir #include <osl/interlck.h> 35*cdf0e10cSrcweir #include <rtl/tencinfo.h> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir /* 38*cdf0e10cSrcweir Thread-data structure hidden behind oslThread: 39*cdf0e10cSrcweir */ 40*cdf0e10cSrcweir typedef struct _osl_TThreadImpl 41*cdf0e10cSrcweir { 42*cdf0e10cSrcweir HANDLE m_hThread; /* OS-handle used for all thread-functions */ 43*cdf0e10cSrcweir unsigned m_ThreadId; /* identifier for this thread */ 44*cdf0e10cSrcweir sal_Int32 m_nTerminationRequested; 45*cdf0e10cSrcweir oslWorkerFunction m_WorkerFunction; 46*cdf0e10cSrcweir void* m_pData; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir } osl_TThreadImpl; 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir #define THREADIMPL_FLAGS_TERMINATE 0x0001 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir static unsigned __stdcall oslWorkerWrapperFunction(void* pData); 53*cdf0e10cSrcweir static oslThread oslCreateThread(oslWorkerFunction pWorker, void* pThreadData, sal_uInt32 nFlags); 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir /*****************************************************************************/ 56*cdf0e10cSrcweir /* oslWorkerWrapperFunction */ 57*cdf0e10cSrcweir /*****************************************************************************/ 58*cdf0e10cSrcweir static unsigned __stdcall oslWorkerWrapperFunction(void* pData) 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)pData; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir /* Initialize COM */ 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir CoInitializeEx(NULL, COINIT_MULTITHREADED); 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir /* call worker-function with data */ 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir pThreadImpl->m_WorkerFunction(pThreadImpl->m_pData); 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir CoUninitialize(); 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir return (0); 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir /*****************************************************************************/ 76*cdf0e10cSrcweir /* oslCreateThread */ 77*cdf0e10cSrcweir /*****************************************************************************/ 78*cdf0e10cSrcweir static oslThread oslCreateThread(oslWorkerFunction pWorker, 79*cdf0e10cSrcweir void* pThreadData, 80*cdf0e10cSrcweir sal_uInt32 nFlags) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl; 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir /* alloc mem. for our internal data structure */ 85*cdf0e10cSrcweir pThreadImpl= malloc(sizeof(osl_TThreadImpl)); 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir OSL_ASSERT(pThreadImpl); 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir if ( pThreadImpl == 0 ) 90*cdf0e10cSrcweir { 91*cdf0e10cSrcweir return 0; 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir pThreadImpl->m_WorkerFunction= pWorker; 95*cdf0e10cSrcweir pThreadImpl->m_pData= pThreadData; 96*cdf0e10cSrcweir pThreadImpl->m_nTerminationRequested= 0; 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir pThreadImpl->m_hThread= 99*cdf0e10cSrcweir (HANDLE)_beginthreadex(NULL, /* no security */ 100*cdf0e10cSrcweir 0, /* default stack-size */ 101*cdf0e10cSrcweir oslWorkerWrapperFunction, /* worker-function */ 102*cdf0e10cSrcweir pThreadImpl, /* provide worker-function with data */ 103*cdf0e10cSrcweir nFlags, /* start thread immediately or suspended */ 104*cdf0e10cSrcweir &pThreadImpl->m_ThreadId); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir if(pThreadImpl->m_hThread == 0) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir /* create failed */ 109*cdf0e10cSrcweir free(pThreadImpl); 110*cdf0e10cSrcweir return 0; 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir return (oslThread)pThreadImpl; 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir /*****************************************************************************/ 117*cdf0e10cSrcweir /* osl_createThread */ 118*cdf0e10cSrcweir /*****************************************************************************/ 119*cdf0e10cSrcweir oslThread SAL_CALL osl_createThread(oslWorkerFunction pWorker, 120*cdf0e10cSrcweir void* pThreadData) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir return oslCreateThread(pWorker, pThreadData, 0); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir /*****************************************************************************/ 126*cdf0e10cSrcweir /* osl_createSuspendedThread */ 127*cdf0e10cSrcweir /*****************************************************************************/ 128*cdf0e10cSrcweir oslThread SAL_CALL osl_createSuspendedThread(oslWorkerFunction pWorker, 129*cdf0e10cSrcweir void* pThreadData) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir return oslCreateThread(pWorker, pThreadData, CREATE_SUSPENDED); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir /*****************************************************************************/ 135*cdf0e10cSrcweir /* osl_getThreadIdentifier */ 136*cdf0e10cSrcweir /*****************************************************************************/ 137*cdf0e10cSrcweir oslThreadIdentifier SAL_CALL osl_getThreadIdentifier(oslThread Thread) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir if (pThreadImpl != NULL) 142*cdf0e10cSrcweir return ((oslThreadIdentifier)pThreadImpl->m_ThreadId); 143*cdf0e10cSrcweir else 144*cdf0e10cSrcweir return ((oslThreadIdentifier)GetCurrentThreadId()); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir /*****************************************************************************/ 148*cdf0e10cSrcweir /* osl_destroyThread */ 149*cdf0e10cSrcweir /*****************************************************************************/ 150*cdf0e10cSrcweir void SAL_CALL osl_destroyThread(oslThread Thread) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir if (Thread == 0) /* valid ptr? */ 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir /* thread already destroyed or not created */ 157*cdf0e10cSrcweir return; 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir /* !!!! _exitthreadex does _not_ call CloseHandle !!! */ 161*cdf0e10cSrcweir CloseHandle( pThreadImpl->m_hThread ); 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir /* free memory */ 164*cdf0e10cSrcweir free(Thread); 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir /*****************************************************************************/ 168*cdf0e10cSrcweir /* osl_resumeThread */ 169*cdf0e10cSrcweir /*****************************************************************************/ 170*cdf0e10cSrcweir void SAL_CALL osl_resumeThread(oslThread Thread) 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir OSL_ASSERT(pThreadImpl); /* valid ptr? */ 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir ResumeThread(pThreadImpl->m_hThread); 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir /*****************************************************************************/ 180*cdf0e10cSrcweir /* osl_suspendThread */ 181*cdf0e10cSrcweir /*****************************************************************************/ 182*cdf0e10cSrcweir void SAL_CALL osl_suspendThread(oslThread Thread) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir OSL_ASSERT(pThreadImpl); /* valid ptr? */ 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir SuspendThread(pThreadImpl->m_hThread); 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir /*****************************************************************************/ 192*cdf0e10cSrcweir /* osl_setThreadPriority */ 193*cdf0e10cSrcweir /*****************************************************************************/ 194*cdf0e10cSrcweir void SAL_CALL osl_setThreadPriority(oslThread Thread, 195*cdf0e10cSrcweir oslThreadPriority Priority) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir int winPriority; 198*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir OSL_ASSERT(pThreadImpl); /* valid ptr? */ 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir /* map enum to WIN32 levels 204*cdf0e10cSrcweir it would be faster and more elegant to preset 205*cdf0e10cSrcweir the enums, but that would require an #ifdef in 206*cdf0e10cSrcweir the exported header, which is not desired. 207*cdf0e10cSrcweir */ 208*cdf0e10cSrcweir switch(Priority) { 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir case osl_Thread_PriorityHighest: 211*cdf0e10cSrcweir winPriority= THREAD_PRIORITY_HIGHEST; 212*cdf0e10cSrcweir break; 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir case osl_Thread_PriorityAboveNormal: 215*cdf0e10cSrcweir winPriority= THREAD_PRIORITY_ABOVE_NORMAL; 216*cdf0e10cSrcweir break; 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir case osl_Thread_PriorityNormal: 219*cdf0e10cSrcweir winPriority= THREAD_PRIORITY_NORMAL; 220*cdf0e10cSrcweir break; 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir case osl_Thread_PriorityBelowNormal: 223*cdf0e10cSrcweir winPriority= THREAD_PRIORITY_BELOW_NORMAL; 224*cdf0e10cSrcweir break; 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir case osl_Thread_PriorityLowest: 227*cdf0e10cSrcweir winPriority= THREAD_PRIORITY_LOWEST; 228*cdf0e10cSrcweir break; 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir case osl_Thread_PriorityUnknown: 231*cdf0e10cSrcweir OSL_ASSERT(FALSE); /* only fools try this...*/ 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir /* let release-version behave friendly */ 234*cdf0e10cSrcweir return; 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir default: 237*cdf0e10cSrcweir OSL_ASSERT(FALSE); /* enum expanded, but forgotten here...*/ 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir /* let release-version behave friendly */ 240*cdf0e10cSrcweir return; 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir SetThreadPriority(pThreadImpl->m_hThread, winPriority); 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir /*****************************************************************************/ 247*cdf0e10cSrcweir /* osl_getThreadPriority */ 248*cdf0e10cSrcweir /*****************************************************************************/ 249*cdf0e10cSrcweir oslThreadPriority SAL_CALL osl_getThreadPriority(const oslThread Thread) 250*cdf0e10cSrcweir { 251*cdf0e10cSrcweir int winPriority; 252*cdf0e10cSrcweir oslThreadPriority Priority; 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir /* invalid arguments ?*/ 257*cdf0e10cSrcweir if(pThreadImpl==0 || pThreadImpl->m_hThread==0) 258*cdf0e10cSrcweir { 259*cdf0e10cSrcweir return osl_Thread_PriorityUnknown; 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir winPriority= 263*cdf0e10cSrcweir GetThreadPriority(pThreadImpl->m_hThread); 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir if(winPriority == THREAD_PRIORITY_ERROR_RETURN) 267*cdf0e10cSrcweir { 268*cdf0e10cSrcweir return osl_Thread_PriorityUnknown; 269*cdf0e10cSrcweir } 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir /* map WIN32 priority to enum */ 272*cdf0e10cSrcweir switch(winPriority) 273*cdf0e10cSrcweir { 274*cdf0e10cSrcweir case THREAD_PRIORITY_TIME_CRITICAL: 275*cdf0e10cSrcweir case THREAD_PRIORITY_HIGHEST: 276*cdf0e10cSrcweir Priority= osl_Thread_PriorityHighest; 277*cdf0e10cSrcweir break; 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir case THREAD_PRIORITY_ABOVE_NORMAL: 280*cdf0e10cSrcweir Priority= osl_Thread_PriorityAboveNormal; 281*cdf0e10cSrcweir break; 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir case THREAD_PRIORITY_NORMAL: 284*cdf0e10cSrcweir Priority= osl_Thread_PriorityNormal; 285*cdf0e10cSrcweir break; 286*cdf0e10cSrcweir 287*cdf0e10cSrcweir case THREAD_PRIORITY_BELOW_NORMAL: 288*cdf0e10cSrcweir Priority= osl_Thread_PriorityBelowNormal; 289*cdf0e10cSrcweir break; 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir case THREAD_PRIORITY_IDLE: 292*cdf0e10cSrcweir case THREAD_PRIORITY_LOWEST: 293*cdf0e10cSrcweir Priority= osl_Thread_PriorityLowest; 294*cdf0e10cSrcweir break; 295*cdf0e10cSrcweir 296*cdf0e10cSrcweir default: 297*cdf0e10cSrcweir OSL_ASSERT(FALSE); /* WIN32 API changed, incorporate new prio-level! */ 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir /* release-version behaves friendly */ 300*cdf0e10cSrcweir Priority= osl_Thread_PriorityUnknown; 301*cdf0e10cSrcweir } 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir return Priority; 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir /*****************************************************************************/ 307*cdf0e10cSrcweir /* osl_isThreadRunning */ 308*cdf0e10cSrcweir /*****************************************************************************/ 309*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isThreadRunning(const oslThread Thread) 310*cdf0e10cSrcweir { 311*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir /* invalid arguments ?*/ 314*cdf0e10cSrcweir if(pThreadImpl==0 || pThreadImpl->m_hThread==0) 315*cdf0e10cSrcweir { 316*cdf0e10cSrcweir return sal_False; 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir 319*cdf0e10cSrcweir return (sal_Bool)(WaitForSingleObject(pThreadImpl->m_hThread, 0) != WAIT_OBJECT_0); 320*cdf0e10cSrcweir } 321*cdf0e10cSrcweir 322*cdf0e10cSrcweir /*****************************************************************************/ 323*cdf0e10cSrcweir /* osl_joinWithThread */ 324*cdf0e10cSrcweir /*****************************************************************************/ 325*cdf0e10cSrcweir void SAL_CALL osl_joinWithThread(oslThread Thread) 326*cdf0e10cSrcweir { 327*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 328*cdf0e10cSrcweir 329*cdf0e10cSrcweir /* invalid arguments?*/ 330*cdf0e10cSrcweir if(pThreadImpl==0 || pThreadImpl->m_hThread==0) 331*cdf0e10cSrcweir { 332*cdf0e10cSrcweir /* assume thread is not running */ 333*cdf0e10cSrcweir return; 334*cdf0e10cSrcweir } 335*cdf0e10cSrcweir 336*cdf0e10cSrcweir WaitForSingleObject(pThreadImpl->m_hThread, INFINITE); 337*cdf0e10cSrcweir } 338*cdf0e10cSrcweir 339*cdf0e10cSrcweir /*****************************************************************************/ 340*cdf0e10cSrcweir /* osl_waitThread */ 341*cdf0e10cSrcweir /*****************************************************************************/ 342*cdf0e10cSrcweir void SAL_CALL osl_waitThread(const TimeValue* pDelay) 343*cdf0e10cSrcweir { 344*cdf0e10cSrcweir if (pDelay) 345*cdf0e10cSrcweir { 346*cdf0e10cSrcweir DWORD millisecs = pDelay->Seconds * 1000L + pDelay->Nanosec / 1000000L; 347*cdf0e10cSrcweir 348*cdf0e10cSrcweir Sleep(millisecs); 349*cdf0e10cSrcweir } 350*cdf0e10cSrcweir } 351*cdf0e10cSrcweir 352*cdf0e10cSrcweir /*****************************************************************************/ 353*cdf0e10cSrcweir /* osl_terminateThread */ 354*cdf0e10cSrcweir /*****************************************************************************/ 355*cdf0e10cSrcweir void SAL_CALL osl_terminateThread(oslThread Thread) 356*cdf0e10cSrcweir { 357*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 358*cdf0e10cSrcweir 359*cdf0e10cSrcweir /* invalid arguments?*/ 360*cdf0e10cSrcweir if (pThreadImpl==0 || pThreadImpl->m_hThread==0) 361*cdf0e10cSrcweir { 362*cdf0e10cSrcweir /* assume thread is not running */ 363*cdf0e10cSrcweir return; 364*cdf0e10cSrcweir } 365*cdf0e10cSrcweir 366*cdf0e10cSrcweir osl_incrementInterlockedCount(&(pThreadImpl->m_nTerminationRequested)); 367*cdf0e10cSrcweir } 368*cdf0e10cSrcweir 369*cdf0e10cSrcweir 370*cdf0e10cSrcweir /*****************************************************************************/ 371*cdf0e10cSrcweir /* osl_scheduleThread */ 372*cdf0e10cSrcweir /*****************************************************************************/ 373*cdf0e10cSrcweir sal_Bool SAL_CALL osl_scheduleThread(oslThread Thread) 374*cdf0e10cSrcweir { 375*cdf0e10cSrcweir osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread; 376*cdf0e10cSrcweir 377*cdf0e10cSrcweir osl_yieldThread(); 378*cdf0e10cSrcweir 379*cdf0e10cSrcweir /* invalid arguments?*/ 380*cdf0e10cSrcweir if (pThreadImpl==0 || pThreadImpl->m_hThread==0) 381*cdf0e10cSrcweir { 382*cdf0e10cSrcweir /* assume thread is not running */ 383*cdf0e10cSrcweir return sal_False; 384*cdf0e10cSrcweir } 385*cdf0e10cSrcweir 386*cdf0e10cSrcweir return (sal_Bool)(0 == pThreadImpl->m_nTerminationRequested); 387*cdf0e10cSrcweir } 388*cdf0e10cSrcweir 389*cdf0e10cSrcweir /*****************************************************************************/ 390*cdf0e10cSrcweir /* osl_yieldThread */ 391*cdf0e10cSrcweir /*****************************************************************************/ 392*cdf0e10cSrcweir void SAL_CALL osl_yieldThread(void) 393*cdf0e10cSrcweir { 394*cdf0e10cSrcweir Sleep(0); 395*cdf0e10cSrcweir } 396*cdf0e10cSrcweir 397*cdf0e10cSrcweir void SAL_CALL osl_setThreadName(char const * name) { 398*cdf0e10cSrcweir #ifdef _MSC_VER 399*cdf0e10cSrcweir /* See <http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx>: */ 400*cdf0e10cSrcweir #pragma pack(push, 8) 401*cdf0e10cSrcweir struct { 402*cdf0e10cSrcweir DWORD dwType; 403*cdf0e10cSrcweir LPCSTR szName; 404*cdf0e10cSrcweir DWORD dwThreadID; 405*cdf0e10cSrcweir DWORD dwFlags; 406*cdf0e10cSrcweir } info; 407*cdf0e10cSrcweir #pragma pack(pop) 408*cdf0e10cSrcweir info.dwType = 0x1000; 409*cdf0e10cSrcweir info.szName = name; 410*cdf0e10cSrcweir info.dwThreadID = (DWORD) -1; 411*cdf0e10cSrcweir info.dwFlags = 0; 412*cdf0e10cSrcweir __try { 413*cdf0e10cSrcweir RaiseException( 414*cdf0e10cSrcweir 0x406D1388, 0, sizeof info / sizeof (ULONG_PTR), 415*cdf0e10cSrcweir (ULONG_PTR *) &info); 416*cdf0e10cSrcweir } __except (EXCEPTION_EXECUTE_HANDLER) {} 417*cdf0e10cSrcweir #else 418*cdf0e10cSrcweir (void) name; 419*cdf0e10cSrcweir #endif 420*cdf0e10cSrcweir } 421*cdf0e10cSrcweir 422*cdf0e10cSrcweir typedef struct _TLS 423*cdf0e10cSrcweir { 424*cdf0e10cSrcweir DWORD dwIndex; 425*cdf0e10cSrcweir oslThreadKeyCallbackFunction pfnCallback; 426*cdf0e10cSrcweir struct _TLS *pNext, *pPrev; 427*cdf0e10cSrcweir } TLS, *PTLS; 428*cdf0e10cSrcweir 429*cdf0e10cSrcweir static PTLS g_pThreadKeyList = NULL; 430*cdf0e10cSrcweir CRITICAL_SECTION g_ThreadKeyListCS; 431*cdf0e10cSrcweir 432*cdf0e10cSrcweir static void AddKeyToList( PTLS pTls ) 433*cdf0e10cSrcweir { 434*cdf0e10cSrcweir if ( pTls ) 435*cdf0e10cSrcweir { 436*cdf0e10cSrcweir EnterCriticalSection( &g_ThreadKeyListCS ); 437*cdf0e10cSrcweir 438*cdf0e10cSrcweir pTls->pNext = g_pThreadKeyList; 439*cdf0e10cSrcweir pTls->pPrev = 0; 440*cdf0e10cSrcweir 441*cdf0e10cSrcweir if ( g_pThreadKeyList ) 442*cdf0e10cSrcweir g_pThreadKeyList->pPrev = pTls; 443*cdf0e10cSrcweir 444*cdf0e10cSrcweir g_pThreadKeyList = pTls; 445*cdf0e10cSrcweir 446*cdf0e10cSrcweir LeaveCriticalSection( &g_ThreadKeyListCS ); 447*cdf0e10cSrcweir } 448*cdf0e10cSrcweir } 449*cdf0e10cSrcweir 450*cdf0e10cSrcweir static void RemoveKeyFromList( PTLS pTls ) 451*cdf0e10cSrcweir { 452*cdf0e10cSrcweir if ( pTls ) 453*cdf0e10cSrcweir { 454*cdf0e10cSrcweir EnterCriticalSection( &g_ThreadKeyListCS ); 455*cdf0e10cSrcweir if ( pTls->pPrev ) 456*cdf0e10cSrcweir pTls->pPrev->pNext = pTls->pNext; 457*cdf0e10cSrcweir else 458*cdf0e10cSrcweir { 459*cdf0e10cSrcweir OSL_ASSERT( pTls == g_pThreadKeyList ); 460*cdf0e10cSrcweir g_pThreadKeyList = pTls->pNext; 461*cdf0e10cSrcweir } 462*cdf0e10cSrcweir 463*cdf0e10cSrcweir if ( pTls->pNext ) 464*cdf0e10cSrcweir pTls->pNext->pPrev = pTls->pPrev; 465*cdf0e10cSrcweir LeaveCriticalSection( &g_ThreadKeyListCS ); 466*cdf0e10cSrcweir } 467*cdf0e10cSrcweir } 468*cdf0e10cSrcweir 469*cdf0e10cSrcweir void SAL_CALL _osl_callThreadKeyCallbackOnThreadDetach(void) 470*cdf0e10cSrcweir { 471*cdf0e10cSrcweir PTLS pTls; 472*cdf0e10cSrcweir 473*cdf0e10cSrcweir 474*cdf0e10cSrcweir EnterCriticalSection( &g_ThreadKeyListCS ); 475*cdf0e10cSrcweir pTls = g_pThreadKeyList; 476*cdf0e10cSrcweir while ( pTls ) 477*cdf0e10cSrcweir { 478*cdf0e10cSrcweir if ( pTls->pfnCallback ) 479*cdf0e10cSrcweir { 480*cdf0e10cSrcweir void *pValue = TlsGetValue( pTls->dwIndex ); 481*cdf0e10cSrcweir 482*cdf0e10cSrcweir if ( pValue ) 483*cdf0e10cSrcweir pTls->pfnCallback( pValue ); 484*cdf0e10cSrcweir } 485*cdf0e10cSrcweir 486*cdf0e10cSrcweir pTls = pTls->pNext; 487*cdf0e10cSrcweir } 488*cdf0e10cSrcweir LeaveCriticalSection( &g_ThreadKeyListCS ); 489*cdf0e10cSrcweir } 490*cdf0e10cSrcweir 491*cdf0e10cSrcweir /*****************************************************************************/ 492*cdf0e10cSrcweir /* osl_createThreadKey */ 493*cdf0e10cSrcweir /*****************************************************************************/ 494*cdf0e10cSrcweir oslThreadKey SAL_CALL osl_createThreadKey(oslThreadKeyCallbackFunction pCallback) 495*cdf0e10cSrcweir { 496*cdf0e10cSrcweir PTLS pTls = rtl_allocateMemory( sizeof(TLS) ); 497*cdf0e10cSrcweir 498*cdf0e10cSrcweir if ( pTls ) 499*cdf0e10cSrcweir { 500*cdf0e10cSrcweir pTls->pfnCallback = pCallback; 501*cdf0e10cSrcweir if ( (DWORD)-1 == (pTls->dwIndex = TlsAlloc()) ) 502*cdf0e10cSrcweir { 503*cdf0e10cSrcweir rtl_freeMemory( pTls ); 504*cdf0e10cSrcweir pTls = 0; 505*cdf0e10cSrcweir } 506*cdf0e10cSrcweir else 507*cdf0e10cSrcweir AddKeyToList( pTls ); 508*cdf0e10cSrcweir } 509*cdf0e10cSrcweir 510*cdf0e10cSrcweir return ((oslThreadKey)pTls); 511*cdf0e10cSrcweir } 512*cdf0e10cSrcweir 513*cdf0e10cSrcweir /*****************************************************************************/ 514*cdf0e10cSrcweir /* osl_destroyThreadKey */ 515*cdf0e10cSrcweir /*****************************************************************************/ 516*cdf0e10cSrcweir void SAL_CALL osl_destroyThreadKey(oslThreadKey Key) 517*cdf0e10cSrcweir { 518*cdf0e10cSrcweir if (Key != 0) 519*cdf0e10cSrcweir { 520*cdf0e10cSrcweir PTLS pTls = (PTLS)Key; 521*cdf0e10cSrcweir 522*cdf0e10cSrcweir RemoveKeyFromList( pTls ); 523*cdf0e10cSrcweir TlsFree( pTls->dwIndex ); 524*cdf0e10cSrcweir rtl_freeMemory( pTls ); 525*cdf0e10cSrcweir } 526*cdf0e10cSrcweir } 527*cdf0e10cSrcweir 528*cdf0e10cSrcweir /*****************************************************************************/ 529*cdf0e10cSrcweir /* osl_getThreadKeyData */ 530*cdf0e10cSrcweir /*****************************************************************************/ 531*cdf0e10cSrcweir void* SAL_CALL osl_getThreadKeyData(oslThreadKey Key) 532*cdf0e10cSrcweir { 533*cdf0e10cSrcweir if (Key != 0) 534*cdf0e10cSrcweir { 535*cdf0e10cSrcweir PTLS pTls = (PTLS)Key; 536*cdf0e10cSrcweir 537*cdf0e10cSrcweir return (TlsGetValue( pTls->dwIndex )); 538*cdf0e10cSrcweir } 539*cdf0e10cSrcweir 540*cdf0e10cSrcweir return (NULL); 541*cdf0e10cSrcweir } 542*cdf0e10cSrcweir 543*cdf0e10cSrcweir /*****************************************************************************/ 544*cdf0e10cSrcweir /* osl_setThreadKeyData */ 545*cdf0e10cSrcweir /*****************************************************************************/ 546*cdf0e10cSrcweir sal_Bool SAL_CALL osl_setThreadKeyData(oslThreadKey Key, void *pData) 547*cdf0e10cSrcweir { 548*cdf0e10cSrcweir if (Key != 0) 549*cdf0e10cSrcweir { 550*cdf0e10cSrcweir PTLS pTls = (PTLS)Key; 551*cdf0e10cSrcweir void* pOldData = NULL; 552*cdf0e10cSrcweir BOOL fSuccess; 553*cdf0e10cSrcweir 554*cdf0e10cSrcweir if ( pTls->pfnCallback ) 555*cdf0e10cSrcweir pOldData = TlsGetValue( pTls->dwIndex ); 556*cdf0e10cSrcweir 557*cdf0e10cSrcweir fSuccess = TlsSetValue( pTls->dwIndex, pData ); 558*cdf0e10cSrcweir 559*cdf0e10cSrcweir if ( fSuccess && pTls->pfnCallback && pOldData ) 560*cdf0e10cSrcweir pTls->pfnCallback( pOldData ); 561*cdf0e10cSrcweir 562*cdf0e10cSrcweir return (sal_Bool)(fSuccess != FALSE); 563*cdf0e10cSrcweir } 564*cdf0e10cSrcweir 565*cdf0e10cSrcweir return (sal_False); 566*cdf0e10cSrcweir } 567*cdf0e10cSrcweir 568*cdf0e10cSrcweir 569*cdf0e10cSrcweir /*****************************************************************************/ 570*cdf0e10cSrcweir /* osl_getThreadTextEncoding */ 571*cdf0e10cSrcweir /*****************************************************************************/ 572*cdf0e10cSrcweir 573*cdf0e10cSrcweir DWORD g_dwTLSTextEncodingIndex = (DWORD)-1; 574*cdf0e10cSrcweir 575*cdf0e10cSrcweir 576*cdf0e10cSrcweir rtl_TextEncoding SAL_CALL osl_getThreadTextEncoding(void) 577*cdf0e10cSrcweir { 578*cdf0e10cSrcweir DWORD dwEncoding; 579*cdf0e10cSrcweir rtl_TextEncoding _encoding; 580*cdf0e10cSrcweir BOOL gotACP; 581*cdf0e10cSrcweir 582*cdf0e10cSrcweir if ( (DWORD)-1 == g_dwTLSTextEncodingIndex ) 583*cdf0e10cSrcweir g_dwTLSTextEncodingIndex = TlsAlloc(); 584*cdf0e10cSrcweir 585*cdf0e10cSrcweir dwEncoding = (DWORD)TlsGetValue( g_dwTLSTextEncodingIndex ); 586*cdf0e10cSrcweir _encoding = LOWORD(dwEncoding); 587*cdf0e10cSrcweir gotACP = HIWORD(dwEncoding); 588*cdf0e10cSrcweir 589*cdf0e10cSrcweir 590*cdf0e10cSrcweir if ( !gotACP ) 591*cdf0e10cSrcweir { 592*cdf0e10cSrcweir char *pszEncoding; 593*cdf0e10cSrcweir 594*cdf0e10cSrcweir if ( NULL != (pszEncoding = getenv( "SOLAR_USER_RTL_TEXTENCODING" )) ) 595*cdf0e10cSrcweir _encoding = (rtl_TextEncoding)atoi(pszEncoding); 596*cdf0e10cSrcweir else 597*cdf0e10cSrcweir _encoding = rtl_getTextEncodingFromWindowsCodePage( GetACP() ); 598*cdf0e10cSrcweir 599*cdf0e10cSrcweir TlsSetValue( g_dwTLSTextEncodingIndex, (LPVOID)MAKELONG( _encoding, TRUE ) ); 600*cdf0e10cSrcweir } 601*cdf0e10cSrcweir 602*cdf0e10cSrcweir return _encoding; 603*cdf0e10cSrcweir } 604*cdf0e10cSrcweir 605*cdf0e10cSrcweir /*****************************************************************************/ 606*cdf0e10cSrcweir /* osl_getThreadTextEncoding */ 607*cdf0e10cSrcweir /*****************************************************************************/ 608*cdf0e10cSrcweir rtl_TextEncoding SAL_CALL osl_setThreadTextEncoding( rtl_TextEncoding Encoding ) 609*cdf0e10cSrcweir { 610*cdf0e10cSrcweir rtl_TextEncoding oldEncoding = osl_getThreadTextEncoding(); 611*cdf0e10cSrcweir 612*cdf0e10cSrcweir TlsSetValue( g_dwTLSTextEncodingIndex, (LPVOID)MAKELONG( Encoding, TRUE) ); 613*cdf0e10cSrcweir 614*cdf0e10cSrcweir return oldEncoding; 615*cdf0e10cSrcweir } 616*cdf0e10cSrcweir 617*cdf0e10cSrcweir 618*cdf0e10cSrcweir 619