summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/src/threading_impl.cpp
blob: aa308836a1e5a0aef4cba8da42cb03c7a108670d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*************************************************************************
 *                                                                       *
 * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith.       *
 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
 *                                                                       *
 * Threading subsystem implementation file.                              *
 * Copyright (C) 2011-2019 Oleh Derevenko. All rights reserved.          *
 * e-mail: odar@eleks.com (change all "a" to "e")                        *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of EITHER:                                  *
 *   (1) The GNU Lesser General Public License as published by the Free  *
 *       Software Foundation; either version 2.1 of the License, or (at  *
 *       your option) any later version. The text of the GNU Lesser      *
 *       General Public License is included with this library in the     *
 *       file LICENSE.TXT.                                               *
 *   (2) The BSD-style license that is included with this library in     *
 *       the file LICENSE-BSD.TXT.                                       *
 *                                                                       *
 * This library is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
 *                                                                       *
 *************************************************************************/

/*
 *  Subsystem APIs implementation for built-in threading support provider.
 */


#include <ode/common.h>
#include <ode/threading_impl.h>
#include "config.h"
#include "threading_impl_posix.h"
#include "threading_impl_win.h"
#include "threading_impl.h"


static dMutexGroupID AllocMutexGroup(dThreadingImplementationID impl, dmutexindex_t Mutex_count, const char *const *Mutex_names_ptr/*=NULL*/);
static void FreeMutexGroup(dThreadingImplementationID impl, dMutexGroupID mutex_group);
static void LockMutexGroupMutex(dThreadingImplementationID impl, dMutexGroupID mutex_group, dmutexindex_t mutex_index);
// static int TryLockMutexGroupMutex(dThreadingImplementationID impl, dMutexGroupID mutex_group, dmutexindex_t mutex_index);
static void UnlockMutexGroupMutex(dThreadingImplementationID impl, dMutexGroupID mutex_group, dmutexindex_t mutex_index);

static dCallWaitID AllocThreadedCallWait(dThreadingImplementationID impl);
static void ResetThreadedCallWait(dThreadingImplementationID impl, dCallWaitID call_wait);
static void FreeThreadedCallWait(dThreadingImplementationID impl, dCallWaitID call_wait);

static void PostThreadedCall(
    dThreadingImplementationID impl, int *out_summary_fault/*=NULL*/, 
    dCallReleaseeID *out_post_releasee/*=NULL*/, ddependencycount_t dependencies_count, dCallReleaseeID dependent_releasee/*=NULL*/, 
    dCallWaitID call_wait/*=NULL*/, 
    dThreadedCallFunction *call_func, void *call_context, dcallindex_t instance_index, 
    const char *call_name/*=NULL*/);
static void AlterThreadedCallDependenciesCount(
    dThreadingImplementationID impl, dCallReleaseeID target_releasee, 
    ddependencychange_t dependencies_count_change);
static void WaitThreadedCall(
    dThreadingImplementationID impl, int *out_wait_status/*=NULL*/, 
    dCallWaitID call_wait, const dThreadedWaitTime *timeout_time_ptr/*=NULL*/, 
    const char *wait_name/*=NULL*/);

static unsigned RetrieveThreadingThreadCount(dThreadingImplementationID impl);
static int PreallocateResourcesForThreadedCalls(dThreadingImplementationID impl, ddependencycount_t max_simultaneous_calls_estimate);


static const dxThreadingFunctionsInfo g_builtin_threading_functions = 
{
    sizeof(dxThreadingFunctionsInfo), // unsigned struct_size;

    &AllocMutexGroup, // dMutexGroupAllocFunction *alloc_mutex_group;
    &FreeMutexGroup, // dMutexGroupFreeFunction *free_mutex_group;
    &LockMutexGroupMutex, // dMutexGroupMutexLockFunction *lock_group_mutex;
    &UnlockMutexGroupMutex, // dMutexGroupMutexUnlockFunction *unlock_group_mutex;

    &AllocThreadedCallWait, // dThreadedCallWaitAllocFunction *alloc_call_wait;
    &ResetThreadedCallWait, // dThreadedCallWaitResetFunction *reset_call_wait;
    &FreeThreadedCallWait, // dThreadedCallWaitFreeFunction *free_call_wait;

    &PostThreadedCall, // dThreadedCallPostFunction *post_call;
    &AlterThreadedCallDependenciesCount, // dThreadedCallDependenciesCountAlterFunction *alter_call_dependencies_count;
    &WaitThreadedCall, // dThreadedCallWaitFunction *wait_call;

    &RetrieveThreadingThreadCount, // dThreadingImplThreadCountRetrieveFunction *retrieve_thread_count;
    &PreallocateResourcesForThreadedCalls, // dThreadingImplResourcesForCallsPreallocateFunction *preallocate_resources_for_calls;

    // &TryLockMutexGroupMutex, // dMutexGroupMutexTryLockFunction *trylock_group_mutex;
};


/*extern */dThreadingImplementationID dThreadingAllocateSelfThreadedImplementation()
{
    dxSelfThreadedThreading *threading = new dxSelfThreadedThreading();

    if (threading != NULL && !threading->InitializeObject())
    {
        delete threading;
        threading = NULL;
    }

    dxIThreadingImplementation *impl = threading;
    return (dThreadingImplementationID)impl;
}

/*extern */dThreadingImplementationID dThreadingAllocateMultiThreadedImplementation()
{
#if dBUILTIN_THREADING_IMPL_ENABLED
    dxMultiThreadedThreading *threading = new dxMultiThreadedThreading();

    if (threading != NULL && !threading->InitializeObject())
    {
        delete threading;
        threading = NULL;
    }
#else
    dxIThreadingImplementation *threading = NULL;
#endif // #if dBUILTIN_THREADING_IMPL_ENABLED

    dxIThreadingImplementation *impl = threading;
    return (dThreadingImplementationID)impl;
}

/*extern */const dThreadingFunctionsInfo *dThreadingImplementationGetFunctions(dThreadingImplementationID impl)
{
#if dBUILTIN_THREADING_IMPL_ENABLED
    dAASSERT(impl != NULL);
#endif // #if dBUILTIN_THREADING_IMPL_ENABLED

    const dThreadingFunctionsInfo *functions = NULL;

#if !dBUILTIN_THREADING_IMPL_ENABLED
    if (impl != NULL)
#endif // #if !dBUILTIN_THREADING_IMPL_ENABLED
    {
        functions = &g_builtin_threading_functions;
    }

    return functions;
}

/*extern */void dThreadingImplementationShutdownProcessing(dThreadingImplementationID impl)
{
#if dBUILTIN_THREADING_IMPL_ENABLED
    dAASSERT(impl != NULL);
#endif // #if dBUILTIN_THREADING_IMPL_ENABLED

#if !dBUILTIN_THREADING_IMPL_ENABLED
    if (impl != NULL)
#endif // #if !dBUILTIN_THREADING_IMPL_ENABLED
    {
        ((dxIThreadingImplementation *)impl)->ShutdownProcessing();
    }
}

/*extern */void dThreadingImplementationCleanupForRestart(dThreadingImplementationID impl)
{
#if dBUILTIN_THREADING_IMPL_ENABLED
    dAASSERT(impl != NULL);
#endif // #if dBUILTIN_THREADING_IMPL_ENABLED

#if !dBUILTIN_THREADING_IMPL_ENABLED
    if (impl != NULL)
#endif // #if !dBUILTIN_THREADING_IMPL_ENABLED
    {
        ((dxIThreadingImplementation *)impl)->CleanupForRestart();
    }
}

/*extern */void dThreadingFreeImplementation(dThreadingImplementationID impl)
{
    if (impl != NULL)
    {
        ((dxIThreadingImplementation *)impl)->FreeInstance();
    }
}


/*extern */void dExternalThreadingServeMultiThreadedImplementation(dThreadingImplementationID impl, 
                                                                   dThreadReadyToServeCallback *readiness_callback/*=NULL*/, void *callback_context/*=NULL*/)
{
#if dBUILTIN_THREADING_IMPL_ENABLED
    dAASSERT(impl != NULL);
#endif // #if dBUILTIN_THREADING_IMPL_ENABLED

#if !dBUILTIN_THREADING_IMPL_ENABLED
    if (impl != NULL)
#endif // #if !dBUILTIN_THREADING_IMPL_ENABLED
    {
        ((dxIThreadingImplementation *)impl)->StickToJobsProcessing(readiness_callback, callback_context);
    }
}


//////////////////////////////////////////////////////////////////////////

static dMutexGroupID AllocMutexGroup(dThreadingImplementationID impl, dmutexindex_t Mutex_count, const char *const *Mutex_names_ptr/*=NULL*/)
{
    (void)Mutex_names_ptr; // unused
    dIMutexGroup *mutex_group = ((dxIThreadingImplementation *)impl)->AllocMutexGroup(Mutex_count);
    return (dMutexGroupID)mutex_group;
}

static void FreeMutexGroup(dThreadingImplementationID impl, dMutexGroupID mutex_group)
{
    ((dxIThreadingImplementation *)impl)->FreeMutexGroup((dIMutexGroup *)mutex_group);
}

static void LockMutexGroupMutex(dThreadingImplementationID impl, dMutexGroupID mutex_group, dmutexindex_t mutex_index)
{
    ((dxIThreadingImplementation *)impl)->LockMutexGroupMutex((dIMutexGroup *)mutex_group, mutex_index);
}

// static int TryLockMutexGroupMutex(dThreadingImplementationID impl, dMutexGroupID mutex_group, dmutexindex_t mutex_index)
// {
//   bool trylock_result = ((dxIThreadingImplementation *)impl)->TryLockMutexGroupMutex((dIMutexGroup *)mutex_group, mutex_index);
//   return trylock_result;
// }

static void UnlockMutexGroupMutex(dThreadingImplementationID impl, dMutexGroupID mutex_group, dmutexindex_t mutex_index)
{
    ((dxIThreadingImplementation *)impl)->UnlockMutexGroupMutex((dIMutexGroup *)mutex_group, mutex_index);
}


static dCallWaitID AllocThreadedCallWait(dThreadingImplementationID impl)
{
    dxICallWait *call_wait = ((dxIThreadingImplementation *)impl)->AllocACallWait();
    return (dCallWaitID)call_wait;
}

static void ResetThreadedCallWait(dThreadingImplementationID impl, dCallWaitID call_wait)
{
    ((dxIThreadingImplementation *)impl)->ResetACallWait((dxICallWait *)call_wait);
}

static void FreeThreadedCallWait(dThreadingImplementationID impl, dCallWaitID call_wait)
{
    ((dxIThreadingImplementation *)impl)->FreeACallWait((dxICallWait *)call_wait);
}


static void PostThreadedCall(
    dThreadingImplementationID impl, int *out_summary_fault/*=NULL*/, 
    dCallReleaseeID *out_post_releasee/*=NULL*/, ddependencycount_t dependencies_count, dCallReleaseeID dependent_releasee/*=NULL*/, 
    dCallWaitID call_wait/*=NULL*/, 
    dThreadedCallFunction *call_func, void *call_context, dcallindex_t instance_index, 
    const char *call_name/*=NULL*/)
{
    (void)call_name; // unused
    ((dxIThreadingImplementation *)impl)->ScheduleNewJob(out_summary_fault, out_post_releasee, 
        dependencies_count, dependent_releasee, (dxICallWait *)call_wait, call_func, call_context, instance_index);
}

static void AlterThreadedCallDependenciesCount(
    dThreadingImplementationID impl, dCallReleaseeID target_releasee, 
    ddependencychange_t dependencies_count_change)
{
    ((dxIThreadingImplementation *)impl)->AlterJobDependenciesCount(target_releasee, dependencies_count_change);
}

static void WaitThreadedCall(
    dThreadingImplementationID impl, int *out_wait_status/*=NULL*/, 
    dCallWaitID call_wait, const dThreadedWaitTime *timeout_time_ptr/*=NULL*/, 
    const char *wait_name/*=NULL*/)
{
    (void)wait_name; // unused
    ((dxIThreadingImplementation *)impl)->WaitJobCompletion(out_wait_status, (dxICallWait *)call_wait, timeout_time_ptr);
}


static unsigned RetrieveThreadingThreadCount(dThreadingImplementationID impl)
{
    return ((dxIThreadingImplementation *)impl)->RetrieveActiveThreadsCount();
}

static int PreallocateResourcesForThreadedCalls(dThreadingImplementationID impl, ddependencycount_t max_simultaneous_calls_estimate)
{
    return ((dxIThreadingImplementation *)impl)->PreallocateJobInfos(max_simultaneous_calls_estimate);
}