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