1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_OSL_THREAD_H
21 #define INCLUDED_OSL_THREAD_H
23 #include "sal/config.h"
26 #include "rtl/textenc.h"
27 #include "sal/saldllapi.h"
34 Opaque data type for threads. As with all other osl-handles
35 you can initialize and/or test it to/for 0.
37 typedef void* oslThread
;
39 /** the function-ptr. representing the threads worker-function.
41 typedef void (SAL_CALL
*oslWorkerFunction
)(void*);
43 /** levels of thread-priority
44 Note that oslThreadPriorityUnknown might be returned
45 by getPriorityOfThread() (e.g. when it is terminated),
46 but mustn't be used with setPriority()!
50 osl_Thread_PriorityHighest
,
51 osl_Thread_PriorityAboveNormal
,
52 osl_Thread_PriorityNormal
,
53 osl_Thread_PriorityBelowNormal
,
54 osl_Thread_PriorityLowest
,
55 osl_Thread_PriorityUnknown
, /* don't use to set */
56 osl_Thread_Priority_FORCE_EQUAL_SIZE
= SAL_MAX_ENUM
60 typedef sal_uInt32 oslThreadIdentifier
;
62 typedef void* oslThreadKey
;
64 /** Create the thread, using the function-ptr pWorker as
65 its main (worker) function. This function receives in
66 its void* parameter the value supplied by pThreadData.
67 Once the OS-structures are initialized,the thread starts
70 @param pWorker Thread worker function
71 @param pThreadData Thread local data
73 @return 0 if creation failed, otherwise a handle to the thread
75 SAL_DLLPUBLIC oslThread SAL_CALL
osl_createThread(oslWorkerFunction pWorker
, void* pThreadData
);
77 /** Create the thread, using the function-ptr pWorker as
78 its main (worker) function. This function receives in
79 its void* parameter the value supplied by pThreadData.
80 The thread will be created, but it won't start running.
81 To wake-up the thread, use resume().
83 @param pWorker Thread worker function
84 @param pThreadData Thread local data
86 @return 0 if creation failed, otherwise a handle to the thread
88 SAL_DLLPUBLIC oslThread SAL_CALL
osl_createSuspendedThread(oslWorkerFunction pWorker
, void* pThreadData
);
90 /** Get the identifier for the specified thread or if parameter
91 Thread is NULL of the current active thread.
93 @param Thread Handle to thread for the thread ID
95 @return identifier of the thread
97 SAL_DLLPUBLIC oslThreadIdentifier SAL_CALL
osl_getThreadIdentifier(oslThread Thread
);
99 /** Release the thread handle.
100 If Thread is NULL, the function won't do anything.
101 Note that we do not interfere with the actual running of
102 the thread, we just free up the memory needed by the handle.
104 @param Thread Handle to thread to release
106 SAL_DLLPUBLIC
void SAL_CALL
osl_destroyThread(oslThread Thread
);
108 /** Wake-up a thread that was suspended with suspend() or
109 createSuspended(). The oslThread must be valid!
111 @param Thread Handle to thread to resume
113 SAL_DLLPUBLIC
void SAL_CALL
osl_resumeThread(oslThread Thread
);
115 /** Suspend the execution of the thread. If you want the thread
116 to continue, call resume(). The oslThread must be valid!
118 @param Thread Handle to thread to suspend
120 SAL_DLLPUBLIC
void SAL_CALL
osl_suspendThread(oslThread Thread
);
122 /** Changes the threads priority.
123 The oslThread must be valid!
125 @param Thread Handle to thread to which to change priority
126 @param Priority Thread priority
128 SAL_DLLPUBLIC
void SAL_CALL
osl_setThreadPriority(oslThread Thread
, oslThreadPriority Priority
);
130 /** Retrieves the threads priority.
131 Returns oslThreadPriorityUnknown for invalid Thread-argument or
132 terminated thread. (i.e. the oslThread might be invalid.)
134 @param Thread Handle to thread for which the priority is retrieved
136 SAL_DLLPUBLIC oslThreadPriority SAL_CALL
osl_getThreadPriority(const oslThread Thread
);
138 /** Returns True if the thread was created and has not terminated yet.
139 Note that according to this definition a "running" thread might be
140 suspended! Also returns False is Thread is NULL.
142 @param Thread Handle to thread
144 SAL_DLLPUBLIC sal_Bool SAL_CALL
osl_isThreadRunning(const oslThread Thread
);
146 /** Blocks the calling thread until Thread has terminated.
147 Returns immediately if Thread is NULL.
149 @param Thread Handle to thread to join
151 SAL_DLLPUBLIC
void SAL_CALL
osl_joinWithThread(oslThread Thread
);
153 /** Suspends the execution of the calling thread for at least the given
156 @param pDelay Timeout value to wait
158 SAL_DLLPUBLIC
void SAL_CALL
osl_waitThread(const TimeValue
* pDelay
);
160 /** The requested thread will get terminate the next time
161 scheduleThread() is called.
163 @param Thread Handle to thread to terminate
165 SAL_DLLPUBLIC
void SAL_CALL
osl_terminateThread(oslThread Thread
);
167 /** Schedules in thread to wait till after time slice of specified
168 thread. scheduleThread() should be called in the working loop
169 of the thread, so any other thread could also get the
170 processor. Returns False if the thread should terminate, so
171 the thread could free any allocated resources.
173 @param Thread Handle to thread to schedule in after
175 SAL_DLLPUBLIC sal_Bool SAL_CALL
osl_scheduleThread(oslThread Thread
);
177 /** Offers the rest of the threads time-slice to the OS.
178 Under POSIX you _need_ to yield(), otherwise, since the
179 threads are not preempted during execution, NO other thread
180 (even with higher priority) gets the processor. Control is
181 only given to another thread if the current thread blocks
184 SAL_DLLPUBLIC
void SAL_CALL
osl_yieldThread(void);
186 /** Attempts to set the name of the current thread.
188 The name of a thread is usually evaluated for debugging purposes. Not all
189 platforms support this. On Linux, a set thread name can be observed with
190 "ps -L". On Windows with the Microsoft compiler, a thread name set while a
191 debugger is attached can be observed within the debugger.
193 @param name the name of the thread; must not be null; on Linux, only the
194 first 16 characters are used
196 SAL_DLLPUBLIC
void SAL_CALL
osl_setThreadName(char const * name
);
198 /* Callback when data stored in a thread key is no longer needed */
200 typedef void (SAL_CALL
*oslThreadKeyCallbackFunction
)(void *);
202 /** Create a key to an associated thread local storage pointer. */
203 SAL_DLLPUBLIC oslThreadKey SAL_CALL
osl_createThreadKey(oslThreadKeyCallbackFunction pCallback
);
205 /** Destroy a key to an associated thread local storage pointer. */
206 SAL_DLLPUBLIC
void SAL_CALL
osl_destroyThreadKey(oslThreadKey Key
);
208 /** Get to key associated thread specific data. */
209 SAL_DLLPUBLIC
void* SAL_CALL
osl_getThreadKeyData(oslThreadKey Key
);
211 /** Set to key associated thread specific data. */
212 SAL_DLLPUBLIC sal_Bool SAL_CALL
osl_setThreadKeyData(oslThreadKey Key
, void *pData
);
214 /** Get the current thread local text encoding. */
215 SAL_DLLPUBLIC rtl_TextEncoding SAL_CALL
osl_getThreadTextEncoding(void);
217 /** Set the thread local text encoding.
218 @return the old text encoding.
220 SAL_DLLPUBLIC rtl_TextEncoding SAL_CALL
osl_setThreadTextEncoding(rtl_TextEncoding Encoding
);
226 #endif // INCLUDED_OSL_THREAD_H
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */