1 /*************************************************************************
3 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4 * All rights reserved. Email: russ@q12.org Web: www.q12.org *
6 * Builtin ODE threading implementation header. *
7 * Copyright (C) 2011-2024 Oleh Derevenko. All rights reserved. *
8 * e-mail: odar@eleks.com (change all "a" to "e") *
10 * This library is free software; you can redistribute it and/or *
11 * modify it under the terms of EITHER: *
12 * (1) The GNU Lesser General Public License as published by the Free *
13 * Software Foundation; either version 2.1 of the License, or (at *
14 * your option) any later version. The text of the GNU Lesser *
15 * General Public License is included with this library in the *
17 * (2) The BSD-style license that is included with this library in *
18 * the file LICENSE-BSD.TXT. *
20 * This library is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
23 * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
25 *************************************************************************/
28 * A threading implementation built into ODE for those who does not care to
29 * or can't implement an own one.
32 #ifndef _ODE_THREADING_IMPL_H_
33 #define _ODE_THREADING_IMPL_H_
36 #include <ode/odeconfig.h>
37 #include <ode/threading.h>
45 struct dxThreadingThreadPool
;
46 typedef struct dxThreadingThreadPool
*dThreadingThreadPoolID
;
50 * @brief Allocates built-in self-threaded threading implementation object.
52 * A self-threaded implementation is a type of implementation that performs
53 * processing of posted calls by means of caller thread itself. This type of
54 * implementation does not need thread pool to serve it.
56 * Note that since May 9th, 2017 (rev. #2066) the Self-Threaded implementation
57 * returns 0 rather than 1 as available thread count to distinguish from
58 * thread pools with just one thread in them.
60 * The processing is arranged in a way to prevent call stack depth growth
61 * as more and more nested calls are posted.
63 * Note that it is not necessary to create and assign a self-threaded
64 * implementation to a world, as there is a global one used by default
65 * if no implementation is explicitly assigned. You should only assign
66 * each world an individual threading implementation instance if simulations
67 * need to be run in parallel in multiple threads for the worlds.
69 * @returns ID of object allocated or NULL on failure
72 * @see dThreadingAllocateMultiThreadedImplementation
73 * @see dThreadingFreeImplementation
75 ODE_API dThreadingImplementationID
dThreadingAllocateSelfThreadedImplementation();
78 * @brief Allocates built-in multi-threaded threading implementation object.
80 * A multi-threaded implementation is a type of implementation that has to be
81 * served with a thread pool. The thread pool can be either the built-in ODE object
82 * or set of external threads that dedicate themselves to this purpose and stay
83 * in ODE until implementation releases them.
85 * @returns ID of object allocated or NULL on failure
88 * @see dThreadingThreadPoolServeMultiThreadedImplementation
89 * @see dExternalThreadingServeMultiThreadedImplementation
90 * @see dThreadingFreeImplementation
92 ODE_API dThreadingImplementationID
dThreadingAllocateMultiThreadedImplementation();
95 * @brief Retrieves the functions record of a built-in threading implementation.
97 * The implementation can be the one allocated by ODE (from @c dThreadingAllocateMultiThreadedImplementation).
98 * Do not use this function with self-made custom implementations -
99 * they should be bundled with their own set of functions.
101 * @param impl Threading implementation ID
102 * @returns Pointer to associated functions structure
105 * @see dThreadingAllocateMultiThreadedImplementation
107 ODE_API
const dThreadingFunctionsInfo
*dThreadingImplementationGetFunctions(dThreadingImplementationID impl
);
110 * @brief Requests a built-in implementation to release threads serving it.
112 * The function unblocks threads employed in implementation serving and lets them
113 * return to from where they originate. It's the responsibility of external code
114 * to make sure all the calls to ODE that might be dependent on given threading
115 * implementation object had already returned before this call is made. If threading
116 * implementation is still processing some posted calls while this function is
117 * invoked the behavior is implementation dependent.
119 * This call is to be used to request the threads to be released before waiting
120 * for them in host pool or before waiting for them to exit. Implementation object
121 * must not be destroyed before it is known that all the serving threads have already
122 * returned from it. If implementation needs to be reused after this function is called
123 * and all the threads have exited from it a call to @c dThreadingImplementationCleanupForRestart
124 * must be made to restore internal state of the object.
126 * If this function is called for self-threaded built-in threading implementation
127 * the call has no effect.
129 * @param impl Threading implementation ID
132 * @see dThreadingAllocateMultiThreadedImplementation
133 * @see dThreadingImplementationCleanupForRestart
135 ODE_API
void dThreadingImplementationShutdownProcessing(dThreadingImplementationID impl
);
138 * @brief Restores built-in implementation's state to let it be reused after shutdown.
140 * If a multi-threaded built-in implementation needs to be reused after a call
141 * to @c dThreadingImplementationShutdownProcessing this call is to be made to
142 * restore object's internal state. After that the implementation can be served again.
144 * If this function is called for self-threaded built-in threading implementation
145 * the call has no effect.
147 * @param impl Threading implementation ID
150 * @see dThreadingAllocateMultiThreadedImplementation
151 * @see dThreadingImplementationShutdownProcessing
153 ODE_API
void dThreadingImplementationCleanupForRestart(dThreadingImplementationID impl
);
156 * @brief Deletes an instance of built-in threading implementation.
158 * @warning A care must be taken to make sure the implementation is unassigned
159 * from all the objects it was assigned to and that there are no more threads
160 * serving it before attempting to call this function.
162 * @param impl Threading implementation ID
165 * @see dThreadingAllocateMultiThreadedImplementation
167 ODE_API
void dThreadingFreeImplementation(dThreadingImplementationID impl
);
170 typedef void (dThreadReadyToServeCallback
)(void *callback_context
);
173 * @brief An entry point for external threads that would like to serve a built-in
174 * threading implementation object.
176 * A thread that calls this function remains blocked in ODE and serves implementation
177 * object @p impl until being released with @c dThreadingImplementationShutdownProcessing call.
178 * This function can be used to provide external threads instead of ODE's built-in
181 * The optional callback @readiness_callback is called after the thread has reached
182 * and has registered within the implementation. The implementation should not
183 * be used until all dedicated threads register within it as otherwise it will not
184 * have accurate view of the execution resources available.
186 * @param impl Threading implementation ID
187 * @param readiness_callback Optional readiness callback to be called after thread enters the implementation
188 * @param callback_context A value to be passed as parameter to readiness callback
191 * @see dThreadingAllocateMultiThreadedImplementation
192 * @see dThreadingImplementationShutdownProcessing
194 ODE_API
void dExternalThreadingServeMultiThreadedImplementation(dThreadingImplementationID impl
,
195 dThreadReadyToServeCallback
*readiness_callback
/*=NULL*/, void *callback_context
/*=NULL*/);
199 * @brief Creates an instance of built-in thread pool object that can be used to serve
200 * multi-threaded threading implementations.
202 * The threads allocated inherit priority of caller thread. Their affinity is not
203 * explicitly adjusted and gets the value the system assigns by default. Threads
204 * have their stack memory fully committed immediately on start. On POSIX platforms
205 * threads are started with all the possible signals blocked. Threads execute
206 * calls to @c dAllocateODEDataForThread with @p ode_data_allocate_flags
209 * On POSIX platforms this function must be called with signals masked
210 * or other measures must be taken to prevent reception of signals by calling thread
211 * for the duration of the call.
213 * @param thread_count Number of threads to start in pool
214 * @param stack_size Size of stack to be used for every thread or 0 for system default value
215 * @param ode_data_allocate_flags Flags to be passed to @c dAllocateODEDataForThread on behalf of each thread
216 * @returns ID of object allocated or NULL on failure
219 * @see dThreadingAllocateMultiThreadedImplementation
220 * @see dThreadingImplementationShutdownProcessing
221 * @see dThreadingFreeThreadPool
223 ODE_API dThreadingThreadPoolID
dThreadingAllocateThreadPool(unsigned thread_count
,
224 dsizeint stack_size
, unsigned int ode_data_allocate_flags
, void *reserved
/*=NULL*/);
227 * @brief Commands an instance of built-in thread pool to serve a built-in multi-threaded
228 * threading implementation.
230 * A pool can only serve one threading implementation at a time.
231 * Call @c dThreadingImplementationShutdownProcessing to release pool threads
232 * from implementation serving and make them idle. Pool threads must be released
233 * from any implementations before pool is attempted to be deleted.
235 * This function waits for threads to register within implementation before returning.
236 * So, after the function call exits the implementation can be used immediately.
238 * @param pool Thread pool ID to serve the implementation
239 * @param impl Implementation ID of implementation to be served
242 * @see dThreadingAllocateThreadPool
243 * @see dThreadingAllocateMultiThreadedImplementation
244 * @see dThreadingImplementationShutdownProcessing
246 ODE_API
void dThreadingThreadPoolServeMultiThreadedImplementation(dThreadingThreadPoolID pool
, dThreadingImplementationID impl
);
249 * @brief Waits until all pool threads are released from threading implementation
250 * they might be serving.
252 * The function can be used after a call to @c dThreadingImplementationShutdownProcessing
253 * to make sure all the threads have already been released by threading implementation
254 * and it can be deleted or it can be cleaned up for restart and served by another pool
255 * or this pool's threads can be used to serve another threading implementation.
257 * Note that is it not necessary to call this function before pool destruction
258 * since @c dThreadingFreeThreadPool performs similar wait operation implicitly on its own.
260 * It is OK to call this function even if pool was not serving any threading implementation
261 * in which case the call exits immediately with minimal delay.
263 * @param pool Thread pool ID to wait for
266 * @see dThreadingAllocateThreadPool
267 * @see dThreadingImplementationShutdownProcessing
268 * @see dThreadingFreeThreadPool
270 ODE_API
void dThreadingThreadPoolWaitIdleState(dThreadingThreadPoolID pool
);
273 * @brief Deletes a built-in thread pool instance.
275 * The pool threads must be released from any implementations they might be serving
276 * before this function is called. Otherwise the call is going to block
277 * and wait until pool's threads return.
279 * @param pool Thread pool ID to delete
282 * @see dThreadingAllocateThreadPool
283 * @see dThreadingImplementationShutdownProcessing
285 ODE_API
void dThreadingFreeThreadPool(dThreadingThreadPoolID pool
);
292 #endif /* #ifndef _ODE_THREADING_IMPL_H_ */