xref: /AOO41X/main/sal/osl/os2/thread.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 
29*cdf0e10cSrcweir #include "system.h"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir #include <osl/thread.h>
33*cdf0e10cSrcweir #include <osl/time.h>
34*cdf0e10cSrcweir #include <rtl/alloc.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 
43*cdf0e10cSrcweir     TID                  m_ThreadId;        /* identifier for this thread */
44*cdf0e10cSrcweir     sal_Int32            m_Flags;
45*cdf0e10cSrcweir     HEV                  m_hEvent;
46*cdf0e10cSrcweir     sal_uInt32           m_Timeout;
47*cdf0e10cSrcweir     oslWorkerFunction    m_WorkerFunction;
48*cdf0e10cSrcweir     void*                m_pData;
49*cdf0e10cSrcweir     sal_Bool             m_StartSuspended;
50*cdf0e10cSrcweir     HAB                  m_hab;
51*cdf0e10cSrcweir     HMQ                  m_hmq;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir } osl_TThreadImpl;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #define THREADIMPL_FLAGS_TERMINATE    0x0001
56*cdf0e10cSrcweir #define THREADIMPL_FLAGS_SLEEP        0x0002
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir // static mutex to control access to private members of oslMutexImpl
60*cdf0e10cSrcweir static HMTX MutexLock = NULL;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir /*****************************************************************************/
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir HAB osl_getPMinternal_HAB(oslThread hThread)
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)hThread;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     if(pThreadImpl == NULL) /* valid ptr? */
69*cdf0e10cSrcweir     {
70*cdf0e10cSrcweir         return NULL;
71*cdf0e10cSrcweir     }
72*cdf0e10cSrcweir     else
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         return pThreadImpl->m_hab;
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir HMQ osl_getPMinternal_HMQ(oslThread hThread)
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)hThread;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     if(pThreadImpl == NULL) /* valid ptr? */
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir         return NULL;
85*cdf0e10cSrcweir     }
86*cdf0e10cSrcweir     else
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         return pThreadImpl->m_hmq;
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir /*****************************************************************************/
94*cdf0e10cSrcweir /* oslWorkerWrapperFunction */
95*cdf0e10cSrcweir /*****************************************************************************/
96*cdf0e10cSrcweir static void oslWorkerWrapperFunction(void* pData)
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     BOOL rc;
99*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)pData;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
102*cdf0e10cSrcweir printf("oslWorkerWrapperFunction pThreadImpl %x, pThreadImpl->m_ThreadId %d\n", pThreadImpl, pThreadImpl->m_ThreadId);
103*cdf0e10cSrcweir #endif
104*cdf0e10cSrcweir     /* Inizialize PM for this thread */
105*cdf0e10cSrcweir     pThreadImpl->m_hab = WinInitialize( 0 );
106*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
107*cdf0e10cSrcweir printf("pThreadImpl->m_ThreadId %d, pThreadImpl->m_hab %x\n", pThreadImpl->m_ThreadId,pThreadImpl->m_hab);
108*cdf0e10cSrcweir #endif
109*cdf0e10cSrcweir     pThreadImpl->m_hmq = WinCreateMsgQueue( pThreadImpl->m_hab, 0 );
110*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
111*cdf0e10cSrcweir printf("pThreadImpl->m_ThreadId %d, pThreadImpl->m_hmq %x\n", pThreadImpl->m_ThreadId,pThreadImpl->m_hmq);
112*cdf0e10cSrcweir #endif
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     /* call worker-function with data */
115*cdf0e10cSrcweir     pThreadImpl->m_WorkerFunction( pThreadImpl->m_pData );
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	/* Free all PM-resources for this thread */
118*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
119*cdf0e10cSrcweir printf("pThreadImpl->m_ThreadId %d, about to destroy queue\n", pThreadImpl->m_ThreadId);
120*cdf0e10cSrcweir #endif
121*cdf0e10cSrcweir     rc = WinDestroyMsgQueue( pThreadImpl->m_hmq );
122*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
123*cdf0e10cSrcweir printf("pThreadImpl->m_ThreadId %d, WinDestroyMsgQueue rc=%d (should be 1)\n", pThreadImpl->m_ThreadId, rc);
124*cdf0e10cSrcweir printf("pThreadImpl->m_ThreadId %d, about to terminate hab\n", pThreadImpl->m_ThreadId);
125*cdf0e10cSrcweir #endif
126*cdf0e10cSrcweir     rc = WinTerminate( pThreadImpl->m_hab );
127*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
128*cdf0e10cSrcweir printf("pThreadImpl->m_ThreadId %d, WinTerminate rc=%d (should be 1)\n", pThreadImpl->m_ThreadId, rc);
129*cdf0e10cSrcweir #endif
130*cdf0e10cSrcweir }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir /*****************************************************************************/
134*cdf0e10cSrcweir /* oslCreateThread */
135*cdf0e10cSrcweir /*****************************************************************************/
136*cdf0e10cSrcweir static oslThread oslCreateThread(oslWorkerFunction pWorker,
137*cdf0e10cSrcweir                                  void* pThreadData,
138*cdf0e10cSrcweir                                  sal_Bool nFlags)
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     /* alloc mem. for our internal data structure */
143*cdf0e10cSrcweir     pThreadImpl = (osl_TThreadImpl*)malloc(sizeof(osl_TThreadImpl));
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     OSL_ASSERT(pThreadImpl);
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     pThreadImpl->m_WorkerFunction= pWorker;
148*cdf0e10cSrcweir     pThreadImpl->m_pData= pThreadData;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     pThreadImpl->m_Flags   = 0;
151*cdf0e10cSrcweir     pThreadImpl->m_hEvent  = 0;
152*cdf0e10cSrcweir     pThreadImpl->m_Timeout = 0;
153*cdf0e10cSrcweir     pThreadImpl->m_StartSuspended = nFlags;
154*cdf0e10cSrcweir     pThreadImpl->m_hab = 0;
155*cdf0e10cSrcweir     pThreadImpl->m_hmq = 0;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     if ( nFlags == sal_True )
158*cdf0e10cSrcweir     {
159*cdf0e10cSrcweir 		DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     pThreadImpl->m_ThreadId = (TID) _beginthread( oslWorkerWrapperFunction,    /* worker-function */
163*cdf0e10cSrcweir                                                   NULL,                        /* unused parameter */
164*cdf0e10cSrcweir                                                   1024*1024,                   /* max. Stacksize */
165*cdf0e10cSrcweir                                                   pThreadImpl );
166*cdf0e10cSrcweir     if ( nFlags == sal_True )
167*cdf0e10cSrcweir     {
168*cdf0e10cSrcweir         if( pThreadImpl->m_ThreadId != -1 )
169*cdf0e10cSrcweir             DosSuspendThread( pThreadImpl->m_ThreadId );
170*cdf0e10cSrcweir 		DosReleaseMutexSem( MutexLock);
171*cdf0e10cSrcweir     }
172*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
173*cdf0e10cSrcweir printf("oslCreateThread pThreadImpl %x, pThreadImpl->m_ThreadId %d\n", pThreadImpl, pThreadImpl->m_ThreadId);
174*cdf0e10cSrcweir #endif
175*cdf0e10cSrcweir     if(pThreadImpl->m_ThreadId == -1)
176*cdf0e10cSrcweir     {
177*cdf0e10cSrcweir         /* create failed */
178*cdf0e10cSrcweir         if (pThreadImpl->m_hEvent != 0)
179*cdf0e10cSrcweir             DosCloseEventSem(pThreadImpl->m_hEvent);
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         free(pThreadImpl);
182*cdf0e10cSrcweir         return 0;
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     pThreadImpl->m_hEvent= 0;
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     return pThreadImpl;
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir /*****************************************************************************/
192*cdf0e10cSrcweir /* osl_createThread */
193*cdf0e10cSrcweir /*****************************************************************************/
194*cdf0e10cSrcweir oslThread SAL_CALL osl_createThread(oslWorkerFunction pWorker,
195*cdf0e10cSrcweir                                  void* pThreadData)
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir     return oslCreateThread(pWorker,pThreadData,sal_False);
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir /*****************************************************************************/
201*cdf0e10cSrcweir /* osl_createSuspendedThread */
202*cdf0e10cSrcweir /*****************************************************************************/
203*cdf0e10cSrcweir oslThread SAL_CALL osl_createSuspendedThread(oslWorkerFunction pWorker,
204*cdf0e10cSrcweir                                           void* pThreadData)
205*cdf0e10cSrcweir {
206*cdf0e10cSrcweir     return oslCreateThread(pWorker,pThreadData,sal_True);
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir /*****************************************************************************/
210*cdf0e10cSrcweir /* osl_getThreadIdentifier */
211*cdf0e10cSrcweir /*****************************************************************************/
212*cdf0e10cSrcweir oslThreadIdentifier SAL_CALL osl_getThreadIdentifier(oslThread Thread)
213*cdf0e10cSrcweir {
214*cdf0e10cSrcweir 	osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 	if (pThreadImpl != NULL)
217*cdf0e10cSrcweir 		return ((oslThreadIdentifier)pThreadImpl->m_ThreadId);
218*cdf0e10cSrcweir 	else
219*cdf0e10cSrcweir         {
220*cdf0e10cSrcweir         PTIB pptib = NULL;
221*cdf0e10cSrcweir         PPIB pppib = NULL;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir         DosGetInfoBlocks( &pptib, &pppib );
224*cdf0e10cSrcweir         return ((oslThreadIdentifier) pptib->tib_ptib2->tib2_ultid );
225*cdf0e10cSrcweir         }
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir /*****************************************************************************/
229*cdf0e10cSrcweir /* osl_destroyThread */
230*cdf0e10cSrcweir /*****************************************************************************/
231*cdf0e10cSrcweir void SAL_CALL osl_destroyThread(oslThread Thread)
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir     if(Thread == 0) /* valid ptr? */
236*cdf0e10cSrcweir     {
237*cdf0e10cSrcweir         /* thread already destroyed or not created */
238*cdf0e10cSrcweir         return;
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir     if(pThreadImpl->m_ThreadId != -1)    /* valid handle ? */
242*cdf0e10cSrcweir     {
243*cdf0e10cSrcweir         /* cancel thread  */
244*cdf0e10cSrcweir         DosKillThread( pThreadImpl->m_ThreadId );
245*cdf0e10cSrcweir     }
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir /*****************************************************************************/
249*cdf0e10cSrcweir /* osl_freeThreadHandle */
250*cdf0e10cSrcweir /*****************************************************************************/
251*cdf0e10cSrcweir void SAL_CALL osl_freeThreadHandle(oslThread Thread)
252*cdf0e10cSrcweir {
253*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     if(Thread == 0)        /* valid ptr? */
256*cdf0e10cSrcweir     {
257*cdf0e10cSrcweir         /* thread already destroyed or not created */
258*cdf0e10cSrcweir         return;
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir     if (pThreadImpl->m_hEvent != 0)
262*cdf0e10cSrcweir         DosCloseEventSem(pThreadImpl->m_hEvent);
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir     /* free memory */
265*cdf0e10cSrcweir     free(Thread);
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir /*****************************************************************************/
269*cdf0e10cSrcweir /* osl_resumeThread */
270*cdf0e10cSrcweir /*****************************************************************************/
271*cdf0e10cSrcweir void SAL_CALL osl_resumeThread(oslThread Thread)
272*cdf0e10cSrcweir {
273*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     OSL_ASSERT(pThreadImpl);        /* valid ptr? */
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir     DosResumeThread( pThreadImpl->m_ThreadId );
278*cdf0e10cSrcweir }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir /*****************************************************************************/
281*cdf0e10cSrcweir /* osl_suspendThread */
282*cdf0e10cSrcweir /*****************************************************************************/
283*cdf0e10cSrcweir void SAL_CALL osl_suspendThread(oslThread Thread)
284*cdf0e10cSrcweir {
285*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir     OSL_ASSERT(pThreadImpl);        /* valid ptr? */
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     DosSuspendThread( pThreadImpl->m_ThreadId );
290*cdf0e10cSrcweir }
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir /*****************************************************************************/
293*cdf0e10cSrcweir /* osl_setThreadPriority */
294*cdf0e10cSrcweir /*****************************************************************************/
295*cdf0e10cSrcweir void SAL_CALL osl_setThreadPriority(oslThread Thread,
296*cdf0e10cSrcweir                            oslThreadPriority Priority)
297*cdf0e10cSrcweir {
298*cdf0e10cSrcweir     ULONG nOs2PriorityClass;
299*cdf0e10cSrcweir     ULONG nOs2PriorityDelta;
300*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir     OSL_ASSERT(pThreadImpl);        /* valid ptr? */
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir     switch(Priority) {
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir     case osl_Thread_PriorityHighest:
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir         nOs2PriorityClass = PRTYC_REGULAR;
309*cdf0e10cSrcweir         nOs2PriorityDelta = PRTYD_MAXIMUM;
310*cdf0e10cSrcweir         break;
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir     case osl_Thread_PriorityAboveNormal:
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir         nOs2PriorityClass = PRTYC_REGULAR;
315*cdf0e10cSrcweir         nOs2PriorityDelta = 16;
316*cdf0e10cSrcweir         break;
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir     case osl_Thread_PriorityNormal:
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir         nOs2PriorityClass = PRTYC_REGULAR;
321*cdf0e10cSrcweir         nOs2PriorityDelta = 0;
322*cdf0e10cSrcweir         break;
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir     case osl_Thread_PriorityBelowNormal:
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir         nOs2PriorityClass = PRTYC_REGULAR;
327*cdf0e10cSrcweir         nOs2PriorityDelta = -16;
328*cdf0e10cSrcweir         break;
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir     case osl_Thread_PriorityLowest:
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir         nOs2PriorityClass = PRTYC_REGULAR;
333*cdf0e10cSrcweir         nOs2PriorityDelta = PRTYD_MINIMUM;
334*cdf0e10cSrcweir         break;
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir     case osl_Thread_PriorityUnknown:
337*cdf0e10cSrcweir         OSL_ASSERT(FALSE);        /* only fools try this...*/
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir         /* let release-version behave friendly */
340*cdf0e10cSrcweir         return;
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir     default:
343*cdf0e10cSrcweir         OSL_ASSERT(FALSE);        /* enum expanded, but forgotten here...*/
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir         /* let release-version behave friendly */
346*cdf0e10cSrcweir         return;
347*cdf0e10cSrcweir     }
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir     DosSetPriority( PRTYS_THREAD,
350*cdf0e10cSrcweir                     nOs2PriorityClass, nOs2PriorityDelta,
351*cdf0e10cSrcweir                     pThreadImpl->m_ThreadId );
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir }
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir /*****************************************************************************/
356*cdf0e10cSrcweir /* osl_getThreadPriority  */
357*cdf0e10cSrcweir /*****************************************************************************/
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir #define BYTE1FROMULONG(ul) ((UCHAR) (ul))
360*cdf0e10cSrcweir #define BYTE2FROMULONG(ul) ((UCHAR) ((ULONG) ul >> 8))
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir oslThreadPriority  SAL_CALL osl_getThreadPriority(const oslThread Thread)
363*cdf0e10cSrcweir {
364*cdf0e10cSrcweir     ULONG nOs2PriorityClass;
365*cdf0e10cSrcweir     ULONG nOs2PriorityDelta;
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir     oslThreadPriority Priority;
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir     /* invalid arguments ?*/
372*cdf0e10cSrcweir     if(pThreadImpl==0 || pThreadImpl->m_ThreadId==-1)
373*cdf0e10cSrcweir     {
374*cdf0e10cSrcweir         return osl_Thread_PriorityUnknown;
375*cdf0e10cSrcweir     }
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir     /* get current priorities */
378*cdf0e10cSrcweir     {
379*cdf0e10cSrcweir     PTIB pptib = NULL;
380*cdf0e10cSrcweir     PPIB pppib = NULL;
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir     DosGetInfoBlocks( &pptib, &pppib );
383*cdf0e10cSrcweir     nOs2PriorityClass = BYTE1FROMULONG( pptib->tib_ptib2->tib2_ulpri );
384*cdf0e10cSrcweir     nOs2PriorityDelta = BYTE2FROMULONG( pptib->tib_ptib2->tib2_ulpri );
385*cdf0e10cSrcweir     }
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir     /* map OS2 priority to enum */
388*cdf0e10cSrcweir     switch(nOs2PriorityClass)
389*cdf0e10cSrcweir     {
390*cdf0e10cSrcweir     case PRTYC_TIMECRITICAL:
391*cdf0e10cSrcweir         Priority= osl_Thread_PriorityHighest;
392*cdf0e10cSrcweir         break;
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir     case PRTYC_REGULAR:
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir         if( nOs2PriorityDelta == 0 )
397*cdf0e10cSrcweir         {
398*cdf0e10cSrcweir             Priority= osl_Thread_PriorityNormal;
399*cdf0e10cSrcweir             break;
400*cdf0e10cSrcweir         }
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir         if( nOs2PriorityDelta < -16 )
403*cdf0e10cSrcweir         {
404*cdf0e10cSrcweir             Priority= osl_Thread_PriorityLowest;
405*cdf0e10cSrcweir             break;
406*cdf0e10cSrcweir         }
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir         if( nOs2PriorityDelta < 0 )
409*cdf0e10cSrcweir         {
410*cdf0e10cSrcweir             Priority= osl_Thread_PriorityBelowNormal;
411*cdf0e10cSrcweir             break;
412*cdf0e10cSrcweir         }
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir         if( nOs2PriorityDelta > 0 )
415*cdf0e10cSrcweir         {
416*cdf0e10cSrcweir             Priority= osl_Thread_PriorityAboveNormal;
417*cdf0e10cSrcweir             break;
418*cdf0e10cSrcweir         }
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir         Priority= osl_Thread_PriorityHighest;
421*cdf0e10cSrcweir         break;
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir     case PRTYC_IDLETIME:
424*cdf0e10cSrcweir         Priority= osl_Thread_PriorityLowest;
425*cdf0e10cSrcweir         break;
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir     default:
428*cdf0e10cSrcweir         OSL_ASSERT(FALSE);        /* OS/2 API changed, incorporate new prio-level! */
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir         /* release-version behaves friendly */
431*cdf0e10cSrcweir         Priority= osl_Thread_PriorityUnknown;
432*cdf0e10cSrcweir     }
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir     return Priority;
435*cdf0e10cSrcweir }
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir /*****************************************************************************/
438*cdf0e10cSrcweir /* osl_isThreadRunning */
439*cdf0e10cSrcweir /*****************************************************************************/
440*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isThreadRunning(const oslThread Thread)
441*cdf0e10cSrcweir {
442*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
443*cdf0e10cSrcweir     APIRET rc;
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir     /* invalid arguments ?*/
446*cdf0e10cSrcweir     if(pThreadImpl==0 || pThreadImpl->m_ThreadId==-1)
447*cdf0e10cSrcweir     {
448*cdf0e10cSrcweir         return sal_False;
449*cdf0e10cSrcweir     }
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir 	if( osl_getThreadIdentifier( 0 ) == osl_getThreadIdentifier( Thread ) )
452*cdf0e10cSrcweir 		return sal_True;
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir     rc = DosWaitThread( &pThreadImpl->m_ThreadId, DCWW_NOWAIT );
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir     return( rc != ERROR_INVALID_THREADID );
457*cdf0e10cSrcweir }
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir /*****************************************************************************/
460*cdf0e10cSrcweir /* osl_joinWithThread */
461*cdf0e10cSrcweir /*****************************************************************************/
462*cdf0e10cSrcweir void SAL_CALL osl_joinWithThread(oslThread Thread)
463*cdf0e10cSrcweir {
464*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
465*cdf0e10cSrcweir 
466*cdf0e10cSrcweir     /* invalid arguments?*/
467*cdf0e10cSrcweir     if(pThreadImpl==0 || pThreadImpl->m_ThreadId==-1)
468*cdf0e10cSrcweir     {
469*cdf0e10cSrcweir         /* assume thread is not running */
470*cdf0e10cSrcweir         return;
471*cdf0e10cSrcweir     }
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir     DosWaitThread( &pThreadImpl->m_ThreadId, DCWW_WAIT );
474*cdf0e10cSrcweir }
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir /*****************************************************************************/
477*cdf0e10cSrcweir /* osl_waitThread */
478*cdf0e10cSrcweir /*****************************************************************************/
479*cdf0e10cSrcweir void SAL_CALL osl_waitThread(const TimeValue* pDelay)
480*cdf0e10cSrcweir {
481*cdf0e10cSrcweir     int millisecs;
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir     OSL_ASSERT(pDelay);
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir     millisecs = pDelay->Seconds * 1000 + pDelay->Nanosec / 1000000;
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir     DosSleep(millisecs);
488*cdf0e10cSrcweir }
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir /*****************************************************************************/
491*cdf0e10cSrcweir /* osl_terminateThread */
492*cdf0e10cSrcweir /*****************************************************************************/
493*cdf0e10cSrcweir void SAL_CALL osl_terminateThread(oslThread Thread)
494*cdf0e10cSrcweir {
495*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir     /* invalid arguments?*/
498*cdf0e10cSrcweir     if (pThreadImpl==0 || pThreadImpl->m_ThreadId==-1)
499*cdf0e10cSrcweir     {
500*cdf0e10cSrcweir         /* assume thread is not running */
501*cdf0e10cSrcweir         return;
502*cdf0e10cSrcweir     }
503*cdf0e10cSrcweir 
504*cdf0e10cSrcweir     DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
505*cdf0e10cSrcweir     pThreadImpl->m_Flags |= THREADIMPL_FLAGS_TERMINATE;
506*cdf0e10cSrcweir     DosReleaseMutexSem( MutexLock);
507*cdf0e10cSrcweir }
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir /*****************************************************************************/
511*cdf0e10cSrcweir /* osl_scheduleThread */
512*cdf0e10cSrcweir /*****************************************************************************/
513*cdf0e10cSrcweir sal_Bool SAL_CALL osl_scheduleThread(oslThread Thread)
514*cdf0e10cSrcweir {
515*cdf0e10cSrcweir     osl_TThreadImpl* pThreadImpl= (osl_TThreadImpl*)Thread;
516*cdf0e10cSrcweir 
517*cdf0e10cSrcweir     osl_yieldThread();
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir     /* invalid arguments?*/
520*cdf0e10cSrcweir     if (pThreadImpl==0 || pThreadImpl->m_ThreadId==-1)
521*cdf0e10cSrcweir     {
522*cdf0e10cSrcweir         /* assume thread is not running */
523*cdf0e10cSrcweir         return sal_False;
524*cdf0e10cSrcweir     }
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir     if (pThreadImpl->m_Flags & THREADIMPL_FLAGS_SLEEP)
527*cdf0e10cSrcweir     {
528*cdf0e10cSrcweir         OSL_ASSERT (pThreadImpl->m_hEvent != 0);
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir         DosWaitEventSem(pThreadImpl->m_hEvent, pThreadImpl->m_Timeout);
531*cdf0e10cSrcweir 
532*cdf0e10cSrcweir 		DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir         pThreadImpl->m_Timeout = 0;
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir         pThreadImpl->m_Flags &= ~THREADIMPL_FLAGS_SLEEP;
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir 		DosReleaseMutexSem( MutexLock);
539*cdf0e10cSrcweir     }
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir     return ((pThreadImpl->m_Flags & THREADIMPL_FLAGS_TERMINATE) == 0);
542*cdf0e10cSrcweir }
543*cdf0e10cSrcweir 
544*cdf0e10cSrcweir /*****************************************************************************/
545*cdf0e10cSrcweir /* osl_yieldThread */
546*cdf0e10cSrcweir /*****************************************************************************/
547*cdf0e10cSrcweir void SAL_CALL osl_yieldThread()
548*cdf0e10cSrcweir {
549*cdf0e10cSrcweir     DosSleep(0);
550*cdf0e10cSrcweir }
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir void osl_setThreadName(char const * name) {
553*cdf0e10cSrcweir     (void) name;
554*cdf0e10cSrcweir }
555*cdf0e10cSrcweir 
556*cdf0e10cSrcweir typedef struct _TLS
557*cdf0e10cSrcweir {
558*cdf0e10cSrcweir 	PULONG							pulPtr;
559*cdf0e10cSrcweir 	oslThreadKeyCallbackFunction	pfnCallback;
560*cdf0e10cSrcweir 	struct _TLS						*pNext, *pPrev;
561*cdf0e10cSrcweir } TLS, *PTLS;
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir static	PTLS		g_pThreadKeyList = NULL;
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir static void AddKeyToList( PTLS pTls )
566*cdf0e10cSrcweir {
567*cdf0e10cSrcweir 	if ( pTls )
568*cdf0e10cSrcweir 	{
569*cdf0e10cSrcweir 		DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
570*cdf0e10cSrcweir 
571*cdf0e10cSrcweir 		pTls->pNext = g_pThreadKeyList;
572*cdf0e10cSrcweir 		pTls->pPrev = 0;
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir 		if ( g_pThreadKeyList )
575*cdf0e10cSrcweir 			g_pThreadKeyList->pPrev = pTls;
576*cdf0e10cSrcweir 
577*cdf0e10cSrcweir 		g_pThreadKeyList = pTls;
578*cdf0e10cSrcweir 
579*cdf0e10cSrcweir 		DosReleaseMutexSem( MutexLock);
580*cdf0e10cSrcweir 	}
581*cdf0e10cSrcweir }
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir static void RemoveKeyFromList( PTLS pTls )
584*cdf0e10cSrcweir {
585*cdf0e10cSrcweir 	if ( pTls )
586*cdf0e10cSrcweir 	{
587*cdf0e10cSrcweir 		DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
588*cdf0e10cSrcweir 		if ( pTls->pPrev )
589*cdf0e10cSrcweir 			pTls->pPrev->pNext = pTls->pNext;
590*cdf0e10cSrcweir 		else
591*cdf0e10cSrcweir 		{
592*cdf0e10cSrcweir 			OSL_ASSERT( pTls == g_pThreadKeyList );
593*cdf0e10cSrcweir 			g_pThreadKeyList = pTls->pNext;
594*cdf0e10cSrcweir 		}
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir 		if ( pTls->pNext )
597*cdf0e10cSrcweir 			pTls->pNext->pPrev = pTls->pPrev;
598*cdf0e10cSrcweir 		DosReleaseMutexSem( MutexLock);
599*cdf0e10cSrcweir 	}
600*cdf0e10cSrcweir }
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir void SAL_CALL _osl_callThreadKeyCallbackOnThreadDetach(void)
603*cdf0e10cSrcweir {
604*cdf0e10cSrcweir 	PTLS	pTls;
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir     DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
607*cdf0e10cSrcweir 	pTls = g_pThreadKeyList;
608*cdf0e10cSrcweir 	while ( pTls )
609*cdf0e10cSrcweir 	{
610*cdf0e10cSrcweir 		if ( pTls->pfnCallback )
611*cdf0e10cSrcweir 		{
612*cdf0e10cSrcweir 			void	*pValue	= (void*)*pTls->pulPtr;
613*cdf0e10cSrcweir 
614*cdf0e10cSrcweir 			if ( pValue )
615*cdf0e10cSrcweir 				pTls->pfnCallback( pValue );
616*cdf0e10cSrcweir 		}
617*cdf0e10cSrcweir 
618*cdf0e10cSrcweir 		pTls = pTls->pNext;
619*cdf0e10cSrcweir 	}
620*cdf0e10cSrcweir     DosReleaseMutexSem( MutexLock);
621*cdf0e10cSrcweir }
622*cdf0e10cSrcweir 
623*cdf0e10cSrcweir /*****************************************************************************/
624*cdf0e10cSrcweir /* osl_createThreadKey */
625*cdf0e10cSrcweir /*****************************************************************************/
626*cdf0e10cSrcweir oslThreadKey SAL_CALL osl_createThreadKey(oslThreadKeyCallbackFunction pCallback)
627*cdf0e10cSrcweir {
628*cdf0e10cSrcweir 	PTLS	pTls = (PTLS)rtl_allocateMemory( sizeof(TLS) );
629*cdf0e10cSrcweir 
630*cdf0e10cSrcweir 	if ( pTls )
631*cdf0e10cSrcweir 	{
632*cdf0e10cSrcweir 		pTls->pfnCallback = pCallback;
633*cdf0e10cSrcweir 		if (DosAllocThreadLocalMemory(1, &pTls->pulPtr) != NO_ERROR)
634*cdf0e10cSrcweir 		{
635*cdf0e10cSrcweir 			rtl_freeMemory( pTls );
636*cdf0e10cSrcweir 			pTls = 0;
637*cdf0e10cSrcweir 		}
638*cdf0e10cSrcweir 		else
639*cdf0e10cSrcweir 		{
640*cdf0e10cSrcweir 			*pTls->pulPtr = 0;
641*cdf0e10cSrcweir 			AddKeyToList( pTls );
642*cdf0e10cSrcweir 		}
643*cdf0e10cSrcweir 	}
644*cdf0e10cSrcweir 
645*cdf0e10cSrcweir 	return ((oslThreadKey)pTls);
646*cdf0e10cSrcweir }
647*cdf0e10cSrcweir 
648*cdf0e10cSrcweir /*****************************************************************************/
649*cdf0e10cSrcweir /* osl_destroyThreadKey */
650*cdf0e10cSrcweir /*****************************************************************************/
651*cdf0e10cSrcweir void SAL_CALL osl_destroyThreadKey(oslThreadKey Key)
652*cdf0e10cSrcweir {
653*cdf0e10cSrcweir 	if (Key != 0)
654*cdf0e10cSrcweir 	{
655*cdf0e10cSrcweir 		PTLS	pTls = (PTLS)Key;
656*cdf0e10cSrcweir 
657*cdf0e10cSrcweir 		RemoveKeyFromList( pTls );
658*cdf0e10cSrcweir 		DosFreeThreadLocalMemory(pTls->pulPtr);
659*cdf0e10cSrcweir 		rtl_freeMemory( pTls );
660*cdf0e10cSrcweir 	}
661*cdf0e10cSrcweir }
662*cdf0e10cSrcweir 
663*cdf0e10cSrcweir /*****************************************************************************/
664*cdf0e10cSrcweir /* osl_getThreadKeyData */
665*cdf0e10cSrcweir /*****************************************************************************/
666*cdf0e10cSrcweir void * SAL_CALL osl_getThreadKeyData(oslThreadKey Key)
667*cdf0e10cSrcweir {
668*cdf0e10cSrcweir 	if (Key != 0)
669*cdf0e10cSrcweir 	{
670*cdf0e10cSrcweir 		PTLS	pTls = (PTLS)Key;
671*cdf0e10cSrcweir 
672*cdf0e10cSrcweir 		return ((void *) *pTls->pulPtr);
673*cdf0e10cSrcweir 	}
674*cdf0e10cSrcweir 
675*cdf0e10cSrcweir 	return (NULL);
676*cdf0e10cSrcweir }
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir /*****************************************************************************/
679*cdf0e10cSrcweir /* osl_setThreadKeyData */
680*cdf0e10cSrcweir /*****************************************************************************/
681*cdf0e10cSrcweir sal_Bool SAL_CALL osl_setThreadKeyData(oslThreadKey Key, void *pData)
682*cdf0e10cSrcweir {
683*cdf0e10cSrcweir 	if (Key != 0)
684*cdf0e10cSrcweir 	{
685*cdf0e10cSrcweir 		PTLS	pTls = (PTLS)Key;
686*cdf0e10cSrcweir 		void*	pOldData = NULL;
687*cdf0e10cSrcweir 		BOOL	fSuccess = TRUE; //YD cannot fail
688*cdf0e10cSrcweir 
689*cdf0e10cSrcweir 		if ( pTls->pfnCallback )
690*cdf0e10cSrcweir 			pOldData = (void*)*pTls->pulPtr;
691*cdf0e10cSrcweir 
692*cdf0e10cSrcweir 		*pTls->pulPtr = (ULONG)pData;
693*cdf0e10cSrcweir 
694*cdf0e10cSrcweir 		if ( fSuccess && pTls->pfnCallback && pOldData )
695*cdf0e10cSrcweir 			pTls->pfnCallback( pOldData );
696*cdf0e10cSrcweir 
697*cdf0e10cSrcweir 		return (sal_Bool)(fSuccess != FALSE);
698*cdf0e10cSrcweir 	}
699*cdf0e10cSrcweir 
700*cdf0e10cSrcweir 	return (sal_False);
701*cdf0e10cSrcweir }
702*cdf0e10cSrcweir 
703*cdf0e10cSrcweir 
704*cdf0e10cSrcweir 
705*cdf0e10cSrcweir /*****************************************************************************/
706*cdf0e10cSrcweir /* osl_getThreadTextEncoding */
707*cdf0e10cSrcweir /*****************************************************************************/
708*cdf0e10cSrcweir 
709*cdf0e10cSrcweir ULONG	g_dwTLSTextEncodingIndex = (ULONG)-1;
710*cdf0e10cSrcweir 
711*cdf0e10cSrcweir sal_uInt32 SAL_CALL _GetACP( void)
712*cdf0e10cSrcweir {
713*cdf0e10cSrcweir 	APIRET	rc;
714*cdf0e10cSrcweir 	ULONG  	aulCpList[8]  = {0};
715*cdf0e10cSrcweir 	ULONG	ulListSize;
716*cdf0e10cSrcweir 
717*cdf0e10cSrcweir 	rc = DosQueryCp( sizeof( aulCpList), aulCpList, &ulListSize);
718*cdf0e10cSrcweir 	if (rc)
719*cdf0e10cSrcweir 		return 437;	// in case of error, return codepage EN_US
720*cdf0e10cSrcweir 	// current codepage is first of list, others are the prepared codepages.
721*cdf0e10cSrcweir 	return aulCpList[0];
722*cdf0e10cSrcweir }
723*cdf0e10cSrcweir 
724*cdf0e10cSrcweir rtl_TextEncoding SAL_CALL osl_getThreadTextEncoding(void)
725*cdf0e10cSrcweir {
726*cdf0e10cSrcweir 	rtl_TextEncoding	_encoding;
727*cdf0e10cSrcweir 
728*cdf0e10cSrcweir 	if ( (ULONG)-1 == g_dwTLSTextEncodingIndex ) {
729*cdf0e10cSrcweir 		rtl_TextEncoding defaultEncoding;
730*cdf0e10cSrcweir 		const char *     pszEncoding;
731*cdf0e10cSrcweir 
732*cdf0e10cSrcweir 		/* create thread specific data key */
733*cdf0e10cSrcweir 		g_dwTLSTextEncodingIndex = osl_createThreadKey( NULL);
734*cdf0e10cSrcweir 
735*cdf0e10cSrcweir 		/* determine default text encoding */
736*cdf0e10cSrcweir 		pszEncoding = getenv ("SOLAR_USER_RTL_TEXTENCODING");
737*cdf0e10cSrcweir 		if (pszEncoding)
738*cdf0e10cSrcweir 			defaultEncoding = atoi(pszEncoding);
739*cdf0e10cSrcweir 		else
740*cdf0e10cSrcweir 			defaultEncoding = rtl_getTextEncodingFromWindowsCodePage( _GetACP());
741*cdf0e10cSrcweir 
742*cdf0e10cSrcweir 		//OSL_ASSERT(defaultEncoding != RTL_TEXTENCODING_DONTKNOW);
743*cdf0e10cSrcweir 		//g_thread.m_textencoding.m_default = defaultEncoding;
744*cdf0e10cSrcweir 		osl_setThreadKeyData( g_dwTLSTextEncodingIndex, (void*)defaultEncoding);
745*cdf0e10cSrcweir 	}
746*cdf0e10cSrcweir 
747*cdf0e10cSrcweir 	_encoding = (rtl_TextEncoding)osl_getThreadKeyData( g_dwTLSTextEncodingIndex );
748*cdf0e10cSrcweir 	if (0 == _encoding) {
749*cdf0e10cSrcweir 		const char *     pszEncoding;
750*cdf0e10cSrcweir 		/* determine default text encoding */
751*cdf0e10cSrcweir 		pszEncoding = getenv ("SOLAR_USER_RTL_TEXTENCODING");
752*cdf0e10cSrcweir 		if (pszEncoding)
753*cdf0e10cSrcweir 			_encoding = atoi(pszEncoding);
754*cdf0e10cSrcweir 		else
755*cdf0e10cSrcweir 			_encoding = rtl_getTextEncodingFromWindowsCodePage( _GetACP());
756*cdf0e10cSrcweir 		/* save for future reference */
757*cdf0e10cSrcweir 		osl_setThreadKeyData( g_dwTLSTextEncodingIndex, (void*)_encoding);
758*cdf0e10cSrcweir 	}
759*cdf0e10cSrcweir 
760*cdf0e10cSrcweir 	return _encoding;
761*cdf0e10cSrcweir }
762*cdf0e10cSrcweir 
763*cdf0e10cSrcweir /*****************************************************************************/
764*cdf0e10cSrcweir /* osl_getThreadTextEncoding */
765*cdf0e10cSrcweir /*****************************************************************************/
766*cdf0e10cSrcweir rtl_TextEncoding SAL_CALL osl_setThreadTextEncoding( rtl_TextEncoding Encoding )
767*cdf0e10cSrcweir {
768*cdf0e10cSrcweir 	rtl_TextEncoding oldEncoding = osl_getThreadTextEncoding();
769*cdf0e10cSrcweir 
770*cdf0e10cSrcweir 	osl_setThreadKeyData( g_dwTLSTextEncodingIndex, (void*)Encoding);
771*cdf0e10cSrcweir 
772*cdf0e10cSrcweir 	return oldEncoding;
773*cdf0e10cSrcweir }
774*cdf0e10cSrcweir 
775*cdf0e10cSrcweir 
776*cdf0e10cSrcweir 
777