Debian Codegraph @

src/cpHastySpace.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // Copyright 2013 Howling Moon Software. All rights reserved.cxx . . $ `<file>/src/cpHastySpace.c`/
2 // See http://chipmunk2d.net/legal.php for more information.
3
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 //TODO: Move all the thread stuff to another file
8
9 //#include <sys/param.h >
•10
11 #ifdef __APPLE__
12 #include <sys/sysctl.h>
•13 #endif
14
15 #ifndef _WIN32
16 #include <pthread.h>
17 #else
18 #ifndef WIN32_LEAN_AND_MEAN
19 #define WIN32_LEAN_AND_MEAN
20 #endif
21
22 #ifndef NOMINMAX
23 #define NOMINMAX
24 #endif
25
26 #include <process.h> // _beginthreadex
27 #include <windows.h>
28
29 #ifndef ETIMEDOUT
30 #define ETIMEDOUT 1
31 #endif
32
33 // Simple pthread implementation for Windows
34 // Made from scratch to avoid the LGPL licence from pthread-win32
35 enum {
36 SIGNAL = 0,
37 BROADCAST = 1,
38 MAX_EVENTS = 2
39 };
40
41 typedef HANDLE pthread_t;
42 typedef struct
43 {
44 // Based on http://www.cs.wustl.edu/~schmidt/win32-cv-1.html since Windows has no condition variable until NT6
45 UINT waiters_count;
46 // Count of the number of waiters.
47
48 CRITICAL_SECTION waiters_count_lock;
49 // Serialize access to <waiters_count_>.
50
51 HANDLE events[MAX_EVENTS];
52 } pthread_cond_t;
53 typedef CRITICAL_SECTION pthread_mutex_t;
54
55 typedef struct {} pthread_condattr_t; // Dummy;
56
57 int pthread_cond_destroy(pthread_cond_t* cv)
58 {
59 CloseHandle(cv->events[BROADCAST]);
60 CloseHandle(cv->events[SIGNAL]);
61
62 DeleteCriticalSection(&cv->waiters_count_lock);
63
64 return 0;
65 }
66
67 int pthread_cond_init(pthread_cond_t* cv, const pthread_condattr_t* attr)
68 {
69 // Initialize the count to 0.
70 cv->waiters_count = 0;
71
72 // Create an auto-reset event.
73 cv->events[SIGNAL] = CreateEvent(NULL, // no security
74 FALSE, // auto-reset event
75 FALSE, // non-signaled initially
76 NULL); // unnamed
77
78 // Create a manual-reset event.
79 cv->events[BROADCAST] = CreateEvent(NULL, // no security
80 TRUE, // manual-reset
81 FALSE, // non-signaled initially
82 NULL); // unnamed
83
84 InitializeCriticalSection(&cv->waiters_count_lock);
85
86 return 0;
87 }
88
89 int pthread_cond_broadcast(pthread_cond_t *cv)
90 {
91 // Avoid race conditions.
92 EnterCriticalSection(&cv->waiters_count_lock);
93 int have_waiters = cv->waiters_count > 0;
94 LeaveCriticalSection(&cv->waiters_count_lock);
95
96 if (have_waiters)
97 SetEvent(cv->events[BROADCAST]);
98
99 return 0;
100 }
101
102 int pthread_cond_signal(pthread_cond_t* cv)
103 {
104 // Avoid race conditions.
105 EnterCriticalSection(&cv->waiters_count_lock);
106 int have_waiters = cv->waiters_count > 0;
107 LeaveCriticalSection(&cv->waiters_count_lock);
108
109 if (have_waiters)
110 SetEvent(cv->events[SIGNAL]);
111
112 return 0;
113 }
114
115 int pthread_cond_wait(pthread_cond_t* cv, pthread_mutex_t* external_mutex)
116 {
117 // Avoid race conditions.
118 EnterCriticalSection(&cv->waiters_count_lock);
119 cv->waiters_count++;
120 LeaveCriticalSection(&cv->waiters_count_lock);
121
122 // It's ok to release the <external_mutex> here since Win32
123 // manual-reset events maintain state when used with
124 // <SetEvent>. This avoids the "lost wakeup" bug...
125 LeaveCriticalSection(external_mutex);
126
127 // Wait for either event to become signaled due to <pthread_cond_signal>
128 // being called or <pthread_cond_broadcast> being called.
129 int result = WaitForMultipleObjects(2, cv->events, FALSE, INFINITE);
130
131 EnterCriticalSection(&cv->waiters_count_lock);
132 cv->waiters_count--;
133 int last_waiter =
134 result == WAIT_OBJECT_0 + BROADCAST
135 && cv->waiters_count == 0;
136 LeaveCriticalSection(&cv->waiters_count_lock);
137
138 // Some thread called <pthread_cond_broadcast>.
139 if (last_waiter)
140 // We're the last waiter to be notified or to stop waiting, so
141 // reset the manual event.
142 ResetEvent(cv->events[BROADCAST]);
143
144 // Reacquire the <external_mutex>.
145 EnterCriticalSection(external_mutex);
146
147 return result == WAIT_TIMEOUT ? ETIMEDOUT : 0;
148 }
149
150 typedef struct {} pthread_mutexattr_t; //< Dummy
151
152 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
153 {
154 InitializeCriticalSection(mutex);
155 return 0;
156 }
157
158 int pthread_mutex_destroy(pthread_mutex_t* mutex)
159 {
160 DeleteCriticalSection(mutex);
161 return 0;
162 }
163
164 int pthread_mutex_lock(pthread_mutex_t* mutex)
165 {
166 EnterCriticalSection(mutex);
167 return 0;
168 }
169
170 int pthread_mutex_unlock(pthread_mutex_t* mutex)
171 {
172 LeaveCriticalSection(mutex);
173 return 0;
174 }
175
176 typedef struct {} pthread_attr_t;
177
178 typedef struct
179 {
180 void *(*start_routine) (void *);
181 void* arg;
182 } pthread_internal_thread;
183
184 unsigned int __stdcall ThreadProc(void* userdata)
185 {
186 pthread_internal_thread* ud = (pthread_internal_thread*) userdata;
187 ud->start_routine(ud->arg);
188
189 free(ud);
190
191 return 0;
192 }
193
194 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void *(*start_routine) (void *), void *arg)
195 {
196 pthread_internal_thread* ud = (pthread_internal_thread*) malloc(sizeof(pthread_internal_thread));
197 ud->start_routine = start_routine;
198 ud->arg = arg;
199
200 *thread = (HANDLE) (_beginthreadex(NULL, 0, &ThreadProc, ud, 0, NULL));
201 if (!*thread)
202 return 1;
203
204 return 0;
205 }
206
207 int pthread_join(pthread_t thread, void **value_ptr)
208 {
209 WaitForSingleObject(thread, INFINITE);
210 CloseHandle(thread);
211
212 return 0;
213 }
214
215 #endif
216
217 #include "chipmunk/chipmunk_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/
218 #include "chipmunk/cpHastySpace.h"cxx . . $ `<file>/include/chipmunk/cpHastySpace.h`/
219
220
221 //MARK: ARM NEON Solver
222
223 #if __ARM_NEON__
224 #include <arm_neon.h>
225
226 // Tested and known to work fine with Clang 3.0 and GCC 4.2
227 // Doesn't work with Clang 1.6, and I have no idea why.
228 #if defined(__clang_major__) && __clang_major__ < 3
229 #error Compiler not supported.
230 #endif
231
232 #if CP_USE_DOUBLES
233 #if !__arm64
234 #error Cannot use CP_USE_DOUBLES on 32 bit ARM.
235 #endif
236
237 typedef float64_t cpFloat_t;
238 typedef float64x2_t cpFloatx2_t;
239 #define vld vld1q_f64
240 #define vdup_n vdupq_n_f64
241 #define vst vst1q_f64
242 #define vst_lane vst1q_lane_f64
243 #define vadd vaddq_f64
244 #define vsub vsubq_f64
245 #define vpadd vpaddq_f64
246 #define vmul vmulq_f64
247 #define vmul_n vmulq_n_f64
248 #define vneg vnegq_f64
249 #define vget_lane vgetq_lane_f64
250 #define vset_lane vsetq_lane_f64
251 #define vmin vminq_f64
252 #define vmax vmaxq_f64
253 #define vrev(__a) __builtin_shufflevector(__a, __a, 1, 0)
254 #else
255 typedef float32_t cpFloat_t;
256 typedef float32x2_t cpFloatx2_t;
257 #define vld vld1_f32
258 #define vdup_n vdup_n_f32
259 #define vst vst1_f32
260 #define vst_lane vst1_lane_f32
261 #define vadd vadd_f32
262 #define vsub vsub_f32
263 #define vpadd vpadd_f32
264 #define vmul vmul_f32
265 #define vmul_n vmul_n_f32
266 #define vneg vneg_f32
267 #define vget_lane vget_lane_f32
268 #define vset_lane vset_lane_f32
269 #define vmin vmin_f32
270 #define vmax vmax_f32
271 #define vrev vrev64_f32
272 #endif
273
274 // TODO could probably do better here, maybe using vcreate?
275 // especially for the constants
276 // Maybe use the {} notation for GCC/Clang?
277 static inline cpFloatx2_t
278 vmake(cpFloat_t x, cpFloat_t y)
279 {
280 // cpFloatx2_t v = {};
281 // v = vset_lane(x, v, 0);
282 // v = vset_lane(y, v, 1);
283 //
284 // return v;
285
286 // This might not be super compatible, but all the NEON headers use it...
287 return (cpFloatx2_t){x, y};
288 }
289
290 static void
291 cpArbiterApplyImpulse_NEON(cpArbiter *arb)
292 {
293 cpBody *a = arb->body_a;
294 cpBody *b = arb->body_b;
295 cpFloatx2_t surface_vr = vld((cpFloat_t *)&arb->surface_vr);
296 cpFloatx2_t n = vld((cpFloat_t *)&arb->n);
297 cpFloat_t friction = arb->u;
298
299 int numContacts = arb->count;
300 struct cpContact *contacts = arb->contacts;
301 for(int i=0; i<numContacts; i++){
302 struct cpContact *con = contacts + i;
303 cpFloatx2_t r1 = vld((cpFloat_t *)&con->r1);
304 cpFloatx2_t r2 = vld((cpFloat_t *)&con->r2);
305
306 cpFloatx2_t perp = vmake(-1.0, 1.0);
307 cpFloatx2_t r1p = vmul(vrev(r1), perp);
308 cpFloatx2_t r2p = vmul(vrev(r2), perp);
309
310 cpFloatx2_t vBias_a = vld((cpFloat_t *)&a->v_bias);
311 cpFloatx2_t vBias_b = vld((cpFloat_t *)&b->v_bias);
312 cpFloatx2_t wBias = vmake(a->w_bias, b->w_bias);
313
314 cpFloatx2_t vb1 = vadd(vBias_a, vmul_n(r1p, vget_lane(wBias, 0)));
315 cpFloatx2_t vb2 = vadd(vBias_b, vmul_n(r2p, vget_lane(wBias, 1)));
316 cpFloatx2_t vbr = vsub(vb2, vb1);
317
318 cpFloatx2_t v_a = vld((cpFloat_t *)&a->v);
319 cpFloatx2_t v_b = vld((cpFloat_t *)&b->v);
320 cpFloatx2_t w = vmake(a->w, b->w);
321 cpFloatx2_t v1 = vadd(v_a, vmul_n(r1p, vget_lane(w, 0)));
322 cpFloatx2_t v2 = vadd(v_b, vmul_n(r2p, vget_lane(w, 1)));
323 cpFloatx2_t vr = vsub(v2, v1);
324
325 cpFloatx2_t vbn_vrn = vpadd(vmul(vbr, n), vmul(vr, n));
326
327 cpFloatx2_t v_offset = vmake(con->bias, -con->bounce);
328 cpFloatx2_t jOld = vmake(con->jBias, con->jnAcc);
329 cpFloatx2_t jbn_jn = vmul_n(vsub(v_offset, vbn_vrn), con->nMass);
330 jbn_jn = vmax(vadd(jOld, jbn_jn), vdup_n(0.0));
331 cpFloatx2_t jApply = vsub(jbn_jn, jOld);
332
333 cpFloatx2_t t = vmul(vrev(n), perp);
334 cpFloatx2_t vrt_tmp = vmul(vadd(vr, surface_vr), t);
335 cpFloatx2_t vrt = vpadd(vrt_tmp, vrt_tmp);
336
337 cpFloatx2_t jtOld = {}; jtOld = vset_lane(con->jtAcc, jtOld, 0);
338 cpFloatx2_t jtMax = vrev(vmul_n(jbn_jn, friction));
339 cpFloatx2_t jt = vmul_n(vrt, -con->tMass);
340 jt = vmax(vneg(jtMax), vmin(vadd(jtOld, jt), jtMax));
341 cpFloatx2_t jtApply = vsub(jt, jtOld);
342
343 cpFloatx2_t i_inv = vmake(-a->i_inv, b->i_inv);
344 cpFloatx2_t nperp = vmake(1.0, -1.0);
345
346 cpFloatx2_t jBias = vmul_n(n, vget_lane(jApply, 0));
347 cpFloatx2_t jBiasCross = vmul(vrev(jBias), nperp);
348 cpFloatx2_t biasCrosses = vpadd(vmul(r1, jBiasCross), vmul(r2, jBiasCross));
349 wBias = vadd(wBias, vmul(i_inv, biasCrosses));
350
351 vBias_a = vsub(vBias_a, vmul_n(jBias, a->m_inv));
352 vBias_b = vadd(vBias_b, vmul_n(jBias, b->m_inv));
353
354 cpFloatx2_t j = vadd(vmul_n(n, vget_lane(jApply, 1)), vmul_n(t, vget_lane(jtApply, 0)));
355 cpFloatx2_t jCross = vmul(vrev(j), nperp);
356 cpFloatx2_t crosses = vpadd(vmul(r1, jCross), vmul(r2, jCross));
357 w = vadd(w, vmul(i_inv, crosses));
358
359 v_a = vsub(v_a, vmul_n(j, a->m_inv));
360 v_b = vadd(v_b, vmul_n(j, b->m_inv));
361
362 // TODO would moving these earlier help pipeline them better?
363 vst((cpFloat_t *)&a->v_bias, vBias_a);
364 vst((cpFloat_t *)&b->v_bias, vBias_b);
365 vst_lane((cpFloat_t *)&a->w_bias, wBias, 0);
366 vst_lane((cpFloat_t *)&b->w_bias, wBias, 1);
367
368 vst((cpFloat_t *)&a->v, v_a);
369 vst((cpFloat_t *)&b->v, v_b);
370 vst_lane((cpFloat_t *)&a->w, w, 0);
371 vst_lane((cpFloat_t *)&b->w, w, 1);
372
373 vst_lane((cpFloat_t *)&con->jBias, jbn_jn, 0);
374 vst_lane((cpFloat_t *)&con->jnAcc, jbn_jn, 1);
375 vst_lane((cpFloat_t *)&con->jtAcc, jt, 0);
376 }
377 }
378
379 #endif
380
381 //MARK: PThreads
382
383 // Right now using more than 2 threads probably wont help your performance any.
384 // If you are using a ridiculous number of iterations it could help though.
385 #define MAX_THREADScxx . . $ `src/cpHastySpace.c:385:9`! 2
386
387 struct ThreadContextcxx . . $ ThreadContext# {
388 pthread_tcxx . . $ pthread_t# threadcxx . . $ ThreadContext#thread.;
389 cpHastySpacecxx . . $ cpHastySpace# *spacecxx . . $ ThreadContext#space.;
390 unsigned long thread_numcxx . . $ ThreadContext#thread_num.;
391 };
392
393 typedef void (*cpHastySpaceWorkFunctioncxx . . $ cpHastySpaceWorkFunction#)(cpSpacecxx . . $ cpSpace# *space, unsigned long worker, unsigned long worker_count);
394
395 struct cpHastySpacecxx . . $ cpHastySpace# {
396 cpSpacecxx . . $ cpSpace# spacecxx . . $ cpHastySpace#space.;
397
398 // Number of worker threads (including the main thread)
399 unsigned long num_threadscxx . . $ cpHastySpace#num_threads.;
400
401 // Number of worker threads currently executing. (also including the main thread)
402 unsigned long num_workingcxx . . $ cpHastySpace#num_working.;
403
404 // Number of constraints (plus contacts) that must exist per step to start the worker threads.
405 unsigned long constraint_count_thresholdcxx . . $ cpHastySpace#constraint_count_threshold.;
406
407 pthread_mutex_tcxx . . $ pthread_mutex_t# mutexcxx . . $ cpHastySpace#mutex.;
408 pthread_cond_tcxx . . $ pthread_cond_t# cond_workcxx . . $ cpHastySpace#cond_work., cond_resumecxx . . $ cpHastySpace#cond_resume.;
409
410 // Work function to invoke.
411 cpHastySpaceWorkFunctioncxx . . $ cpHastySpaceWorkFunction# workcxx . . $ cpHastySpace#work.;
412
413 struct ThreadContextcxx . . $ ThreadContext# workerscxx . . $ cpHastySpace#workers.[MAX_THREADScxx . . $ `src/cpHastySpace.c:385:9`! - 1];
414 };
415
416 static void *
417 WorkerThreadLoopcxx . . $ WorkerThreadLoop(3adde8fa37acc81e).(struct ThreadContextcxx . . $ ThreadContext# *context)
418 {
419 cpHastySpacecxx . . $ cpHastySpace# *hasty = context->spacecxx . . $ ThreadContext#space.;
420
421 unsigned long thread = context->thread_numcxx . . $ ThreadContext#thread_num.;
422 unsigned long num_threads = hasty->num_threadscxx . . $ cpHastySpace#num_threads.;
423
424 for(;;){
425 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&hasty->mutexcxx . . $ cpHastySpace#mutex.); {
426 if(--hasty->num_workingcxx . . $ cpHastySpace#num_working. == 0){
427 pthread_cond_signalcxx . . $ pthread_cond_signal(98095c34a2b0149d).(&hasty->cond_resumecxx . . $ cpHastySpace#cond_resume.);
428 }
429
430 pthread_cond_waitcxx . . $ pthread_cond_wait(6145be15a5d9db5d).(&hasty->cond_workcxx . . $ cpHastySpace#cond_work., &hasty->mutexcxx . . $ cpHastySpace#mutex.);
431 } pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&hasty->mutexcxx . . $ cpHastySpace#mutex.);
432
433 cpHastySpaceWorkFunctioncxx . . $ cpHastySpaceWorkFunction# func = hasty->workcxx . . $ cpHastySpace#work.;
434 if(func){
435 hasty->workcxx . . $ cpHastySpace#work.(&hasty->spacecxx . . $ cpHastySpace#space., thread, num_threads);
436 } else {
437 break;
438 }
439 }
440
441 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
442 }
443
444 static void
445 RunWorkerscxx . . $ RunWorkers(c2439614a475d833).(cpHastySpacecxx . . $ cpHastySpace# *hasty, cpHastySpaceWorkFunctioncxx . . $ cpHastySpaceWorkFunction# func)
446 {
447 hasty->num_workingcxx . . $ cpHastySpace#num_working. = hasty->num_threadscxx . . $ cpHastySpace#num_threads. - 1;
448 hasty->workcxx . . $ cpHastySpace#work. = func;
449
450 if(hasty->num_workingcxx . . $ cpHastySpace#num_working. > 0){
451 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&hasty->mutexcxx . . $ cpHastySpace#mutex.); {
452 pthread_cond_broadcastcxx . . $ pthread_cond_broadcast(98095c34a2b0149d).(&hasty->cond_workcxx . . $ cpHastySpace#cond_work.);
453 } pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&hasty->mutexcxx . . $ cpHastySpace#mutex.);
454
455 func((cpSpacecxx . . $ cpSpace# *)hasty, 0, hasty->num_threadscxx . . $ cpHastySpace#num_threads.);
456
457 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&hasty->mutexcxx . . $ cpHastySpace#mutex.); {
458 if(hasty->num_workingcxx . . $ cpHastySpace#num_working. > 0){
459 pthread_cond_waitcxx . . $ pthread_cond_wait(6145be15a5d9db5d).(&hasty->cond_resumecxx . . $ cpHastySpace#cond_resume., &hasty->mutexcxx . . $ cpHastySpace#mutex.);
460 }
461 } pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&hasty->mutexcxx . . $ cpHastySpace#mutex.);
462 } else {
463 func((cpSpacecxx . . $ cpSpace# *)hasty, 0, hasty->num_threadscxx . . $ cpHastySpace#num_threads.);
464 }
465
466 hasty->workcxx . . $ cpHastySpace#work. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
467 }
468
469 static void
470 Solvercxx . . $ Solver(804d257ee7e16a42).(cpSpacecxx . . $ cpSpace# *space, unsigned long worker, unsigned long worker_count)
471 {
472 cpArraycxx . . $ cpArray# *constraints = space->constraintscxx . . $ cpSpace#constraints.;
473 cpArraycxx . . $ cpArray# *arbiters = space->arbiterscxx . . $ cpSpace#arbiters.;
474
475 cpFloatcxx . . $ cpFloat# dt = space->curr_dtcxx . . $ cpSpace#curr_dt.;
476 unsigned long iterations = (space->iterationscxx . . $ cpSpace#iterations. + worker_count - 1)/worker_count;
477
478 for(unsigned long i=0; i<iterations; i++){
479 for(int j=0; j<arbiters->numcxx . . $ cpArray#num.; j++){
480 cpArbitercxx . . $ cpArbiter# *arb = (cpArbitercxx . . $ cpArbiter# *)arbiters->arrcxx . . $ cpArray#arr.[j];
481 #ifdef __ARM_NEON__
482 cpArbiterApplyImpulse_NEON(arb);
483 #else
484 cpArbiterApplyImpulsecxx . . $ cpArbiterApplyImpulse(9130529b054e83ea).(arb);
485 #endif
486 }
487
488 for(int j=0; j<constraints->numcxx . . $ cpArray#num.; j++){
489 cpConstraintcxx . . $ cpConstraint# *constraint = (cpConstraintcxx . . $ cpConstraint# *)constraints->arrcxx . . $ cpArray#arr.[j];
490 constraint->klasscxx . . $ cpConstraint#klass.->applyImpulsecxx . . $ cpConstraintClass#applyImpulse.(constraint, dt);
491 }
492 }
493 }
494
495 //MARK: Thread Management Functions
496
497 static void
498 HaltThreadscxx . . $ HaltThreads(e19725e2897132c9).(cpHastySpacecxx . . $ cpHastySpace# *hasty)
499 {
500 pthread_mutex_tcxx . . $ pthread_mutex_t# *mutex = &hasty->mutexcxx . . $ cpHastySpace#mutex.;
501 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(mutex); {
502 hasty->workcxx . . $ cpHastySpace#work. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; // NULL work function means break and exit
503 pthread_cond_broadcastcxx . . $ pthread_cond_broadcast(98095c34a2b0149d).(&hasty->cond_workcxx . . $ cpHastySpace#cond_work.);
504 } pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(mutex);
505
506 for(unsigned long i=0; i<(hasty->num_threadscxx . . $ cpHastySpace#num_threads.-1); i++){
507 pthread_joincxx . . $ pthread_join(269902cf949f12c5).(hasty->workerscxx . . $ cpHastySpace#workers.[i].threadcxx . . $ ThreadContext#thread., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
508 }
509 }
510
511 void
512 cpHastySpaceSetThreadscxx . . $ cpHastySpaceSetThreads(e66ca77e962a9a54).(cpSpacecxx . . $ cpSpace# *space, unsigned long threads)
513 {
514 #if TARGET_IPHONE_SIMULATOR == 1
515 // Individual values appear to be written non-atomically when compiled as debug for the simulator.
516 // No idea why, so threads are disabled.
517 threads = 1;
518 #endif
519
520 cpHastySpacecxx . . $ cpHastySpace# *hasty = (cpHastySpacecxx . . $ cpHastySpace# *)space;
521 HaltThreadscxx . . $ HaltThreads(e19725e2897132c9).(hasty);
522
523 #ifdef __APPLE__
524 if(threads == 0){
525 size_t size = sizeof(threads);
526 sysctlbyname("hw.ncpu", &threads, &size, NULL, 0);
527 }
528 #else
529 if(threads == 0) threads = 1;
530 #endif
531
532 hasty->num_threadscxx . . $ cpHastySpace#num_threads. = (threads < MAX_THREADScxx . . $ `src/cpHastySpace.c:385:9`! ? threads : MAX_THREADScxx . . $ `src/cpHastySpace.c:385:9`!);
533 hasty->num_workingcxx . . $ cpHastySpace#num_working. = hasty->num_threadscxx . . $ cpHastySpace#num_threads. - 1;
534
535 // Create the worker threads and wait for them to signal ready.
536 if(hasty->num_workingcxx . . $ cpHastySpace#num_working. > 0){
537 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&hasty->mutexcxx . . $ cpHastySpace#mutex.);
538 for(unsigned long i=0; i<(hasty->num_threadscxx . . $ cpHastySpace#num_threads.-1); i++){
539 hasty->workerscxx . . $ cpHastySpace#workers.[i].spacecxx . . $ ThreadContext#space. = hasty;
540 hasty->workerscxx . . $ cpHastySpace#workers.[i].thread_numcxx . . $ ThreadContext#thread_num. = i + 1;
541
542 pthread_createcxx . . $ pthread_create(2141a84bcac899e5).(&hasty->workerscxx . . $ cpHastySpace#workers.[i].threadcxx . . $ ThreadContext#thread., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, (void*(*)(void*))WorkerThreadLoopcxx . . $ WorkerThreadLoop(3adde8fa37acc81e)., &hasty->workerscxx . . $ cpHastySpace#workers.[i]);
543 }
544
545 pthread_cond_waitcxx . . $ pthread_cond_wait(6145be15a5d9db5d).(&hasty->cond_resumecxx . . $ cpHastySpace#cond_resume., &hasty->mutexcxx . . $ cpHastySpace#mutex.);
546 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&hasty->mutexcxx . . $ cpHastySpace#mutex.);
547 }
548 }
549
550 unsigned long
551 cpHastySpaceGetThreadscxx . . $ cpHastySpaceGetThreads(ac4107c9bb0e5161).(cpSpacecxx . . $ cpSpace# *space)
552 {
553 return ((cpHastySpacecxx . . $ cpHastySpace# *)space)->num_threadscxx . . $ cpHastySpace#num_threads.;
554 }
555
556 //MARK: Overriden cpSpace Functions.
557
558 cpSpacecxx . . $ cpSpace# *
559 cpHastySpaceNewcxx . . $ cpHastySpaceNew(1f0082e822a7ce76).(void)
560 {
561 cpHastySpacecxx . . $ cpHastySpace# *hasty = (cpHastySpacecxx . . $ cpHastySpace# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpHastySpacecxx . . $ cpHastySpace#));
562 cpSpaceInitcxx . . $ cpSpaceInit(179ace4a7c88b215).((cpSpacecxx . . $ cpSpace# *)hasty);
563
564 pthread_mutex_initcxx . . $ pthread_mutex_init(db9d29dab44ea1aa).(&hasty->mutexcxx . . $ cpHastySpace#mutex., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
565 pthread_cond_initcxx . . $ pthread_cond_init(fc876bc1ec1249fc).(&hasty->cond_workcxx . . $ cpHastySpace#cond_work., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
566 pthread_cond_initcxx . . $ pthread_cond_init(fc876bc1ec1249fc).(&hasty->cond_resumecxx . . $ cpHastySpace#cond_resume., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
567
568 // TODO magic number, should test this more thoroughly.
569 hasty->constraint_count_thresholdcxx . . $ cpHastySpace#constraint_count_threshold. = 50;
570
571 // Default to 1 thread for determinism.
572 hasty->num_threadscxx . . $ cpHastySpace#num_threads. = 1;
573 cpHastySpaceSetThreadscxx . . $ cpHastySpaceSetThreads(e66ca77e962a9a54).((cpSpacecxx . . $ cpSpace# *)hasty, 1);
574
575 return (cpSpacecxx . . $ cpSpace# *)hasty;
576 }
577
578 void
579 cpHastySpaceFreecxx . . $ cpHastySpaceFree(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space)
580 {
581 cpHastySpacecxx . . $ cpHastySpace# *hasty = (cpHastySpacecxx . . $ cpHastySpace# *)space;
582
583 HaltThreadscxx . . $ HaltThreads(e19725e2897132c9).(hasty);
584
585 pthread_mutex_destroycxx . . $ pthread_mutex_destroy(d89ae235521888f9).(&hasty->mutexcxx . . $ cpHastySpace#mutex.);
586 pthread_cond_destroycxx . . $ pthread_cond_destroy(98095c34a2b0149d).(&hasty->cond_workcxx . . $ cpHastySpace#cond_work.);
587 pthread_cond_destroycxx . . $ pthread_cond_destroy(98095c34a2b0149d).(&hasty->cond_resumecxx . . $ cpHastySpace#cond_resume.);
588
589 cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space);
590 }
591
592 void
593 cpHastySpaceStepcxx . . $ cpHastySpaceStep(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, cpFloatcxx . . $ cpFloat# dt)
594 {
595 // don't step if the timestep is 0!
596 if(dt == 0.0f) return;
597
598 space->stampcxx . . $ cpSpace#stamp.++;
599
600 cpFloatcxx . . $ cpFloat# prev_dt = space->curr_dtcxx . . $ cpSpace#curr_dt.;
601 space->curr_dtcxx . . $ cpSpace#curr_dt. = dt;
602
603 cpArraycxx . . $ cpArray# *bodies = space->dynamicBodiescxx . . $ cpSpace#dynamicBodies.;
604 cpArraycxx . . $ cpArray# *constraints = space->constraintscxx . . $ cpSpace#constraints.;
605 cpArraycxx . . $ cpArray# *arbiters = space->arbiterscxx . . $ cpSpace#arbiters.;
606
607 // Reset and empty the arbiter list.
608 for(int i=0; i<arbiters->numcxx . . $ cpArray#num.; i++){
609 cpArbitercxx . . $ cpArbiter# *arb = (cpArbitercxx . . $ cpArbiter# *)arbiters->arrcxx . . $ cpArray#arr.[i];
610 arb->statecxx . . $ cpArbiter#state. = CP_ARBITER_STATE_NORMALcxx . . $ CP_ARBITER_STATE_NORMAL.;
611
612 // If both bodies are awake, unthread the arbiter from the contact graph.
613 if(!cpBodyIsSleepingcxx . . $ cpBodyIsSleeping(25bfad07631ec420).(arb->body_acxx . . $ cpArbiter#body_a.) && !cpBodyIsSleepingcxx . . $ cpBodyIsSleeping(25bfad07631ec420).(arb->body_bcxx . . $ cpArbiter#body_b.)){
614 cpArbiterUnthreadcxx . . $ cpArbiterUnthread(9130529b054e83ea).(arb);
615 }
616 }
617 arbiters->numcxx . . $ cpArray#num. = 0;
618
619 cpSpaceLockcxx . . $ cpSpaceLock(728b9c9a69722ef8).(space); {
620 // Integrate positions
621 for(int i=0; i<bodies->numcxx . . $ cpArray#num.; i++){
622 cpBodycxx . . $ cpBody# *body = (cpBodycxx . . $ cpBody# *)bodies->arrcxx . . $ cpArray#arr.[i];
623 body->position_funccxx . . $ cpBody#position_func.(body, dt);
624 }
625
626 // Find colliding pairs.
627 cpSpacePushFreshContactBuffercxx . . $ cpSpacePushFreshContactBuffer(728b9c9a69722ef8).(space);
628 cpSpatialIndexEachcxx . . $ cpSpatialIndexEach(63a26082660cb28c).(space->dynamicShapescxx . . $ cpSpace#dynamicShapes., (cpSpatialIndexIteratorFunccxx . . $ cpSpatialIndexIteratorFunc#)cpShapeUpdateFunccxx . . $ cpShapeUpdateFunc(ebab24832b4ea92d)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
629 cpSpatialIndexReindexQuerycxx . . $ cpSpatialIndexReindexQuery(3ad3419756960b2c).(space->dynamicShapescxx . . $ cpSpace#dynamicShapes., (cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc#)cpSpaceCollideShapescxx . . $ cpSpaceCollideShapes(2fb593a639e5759d)., space);
630 } cpSpaceUnlockcxx . . $ cpSpaceUnlock(67df6f6b25fde2).(space, cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!);
631
632 // Rebuild the contact graph (and detect sleeping components if sleeping is enabled)
633 cpSpaceProcessComponentscxx . . $ cpSpaceProcessComponents(fa66a0a48611083e).(space, dt);
634
635 cpSpaceLockcxx . . $ cpSpaceLock(728b9c9a69722ef8).(space); {
636 // Clear out old cached arbiters and call separate callbacks
637 cpHashSetFiltercxx . . $ cpHashSetFilter(ded8e246e9dd8428).(space->cachedArbiterscxx . . $ cpSpace#cachedArbiters., (cpHashSetFilterFunccxx . . $ cpHashSetFilterFunc#)cpSpaceArbiterSetFiltercxx . . $ cpSpaceArbiterSetFilter(e9b4df504d3a695b)., space);
638
639 // Prestep the arbiters and constraints.
640 cpFloatcxx . . $ cpFloat# slop = space->collisionSlopcxx . . $ cpSpace#collisionSlop.;
641 cpFloatcxx . . $ cpFloat# biasCoef = 1.0f - cpfpowcxx . . $ `include/chipmunk/chipmunk_types.h:75:10`!(space->collisionBiascxx . . $ cpSpace#collisionBias., dt);
642 for(int i=0; i<arbiters->numcxx . . $ cpArray#num.; i++){
643 cpArbiterPreStepcxx . . $ cpArbiterPreStep(53bd72b40478409e).((cpArbitercxx . . $ cpArbiter# *)arbiters->arrcxx . . $ cpArray#arr.[i], dt, slop, biasCoef);
644 }
645
646 for(int i=0; i<constraints->numcxx . . $ cpArray#num.; i++){
647 cpConstraintcxx . . $ cpConstraint# *constraint = (cpConstraintcxx . . $ cpConstraint# *)constraints->arrcxx . . $ cpArray#arr.[i];
648
649 cpConstraintPreSolveFunccxx . . $ cpConstraintPreSolveFunc# preSolve = constraint->preSolvecxx . . $ cpConstraint#preSolve.;
650 if(preSolve) preSolve(constraint, space);
651
652 constraint->klasscxx . . $ cpConstraint#klass.->preStepcxx . . $ cpConstraintClass#preStep.(constraint, dt);
653 }
654
655 // Integrate velocities.
656 cpFloatcxx . . $ cpFloat# damping = cpfpowcxx . . $ `include/chipmunk/chipmunk_types.h:75:10`!(space->dampingcxx . . $ cpSpace#damping., dt);
657 cpVectcxx . . $ cpVect# gravity = space->gravitycxx . . $ cpSpace#gravity.;
658 for(int i=0; i<bodies->numcxx . . $ cpArray#num.; i++){
659 cpBodycxx . . $ cpBody# *body = (cpBodycxx . . $ cpBody# *)bodies->arrcxx . . $ cpArray#arr.[i];
660 body->velocity_funccxx . . $ cpBody#velocity_func.(body, gravity, damping, dt);
661 }
662
663 // Apply cached impulses
664 cpFloatcxx . . $ cpFloat# dt_coef = (prev_dt == 0.0f ? 0.0f : dt/prev_dt);
665 for(int i=0; i<arbiters->numcxx . . $ cpArray#num.; i++){
666 cpArbiterApplyCachedImpulsecxx . . $ cpArbiterApplyCachedImpulse(9e1a510df1a8740d).((cpArbitercxx . . $ cpArbiter# *)arbiters->arrcxx . . $ cpArray#arr.[i], dt_coef);
667 }
668
669 for(int i=0; i<constraints->numcxx . . $ cpArray#num.; i++){
670 cpConstraintcxx . . $ cpConstraint# *constraint = (cpConstraintcxx . . $ cpConstraint# *)constraints->arrcxx . . $ cpArray#arr.[i];
671 constraint->klasscxx . . $ cpConstraint#klass.->applyCachedImpulsecxx . . $ cpConstraintClass#applyCachedImpulse.(constraint, dt_coef);
672 }
673
674 // Run the impulse solver.
675 cpHastySpacecxx . . $ cpHastySpace# *hasty = (cpHastySpacecxx . . $ cpHastySpace# *)space;
676 if((unsigned long)(arbiters->numcxx . . $ cpArray#num. + constraints->numcxx . . $ cpArray#num.) > hasty->constraint_count_thresholdcxx . . $ cpHastySpace#constraint_count_threshold.){
677 RunWorkerscxx . . $ RunWorkers(c2439614a475d833).(hasty, Solvercxx . . $ Solver(804d257ee7e16a42).);
678 } else {
679 Solvercxx . . $ Solver(804d257ee7e16a42).(space, 0, 1);
680 }
681
682 // Run the constraint post-solve callbacks
683 for(int i=0; i<constraints->numcxx . . $ cpArray#num.; i++){
684 cpConstraintcxx . . $ cpConstraint# *constraint = (cpConstraintcxx . . $ cpConstraint# *)constraints->arrcxx . . $ cpArray#arr.[i];
685
686 cpConstraintPostSolveFunccxx . . $ cpConstraintPostSolveFunc# postSolve = constraint->postSolvecxx . . $ cpConstraint#postSolve.;
687 if(postSolve) postSolve(constraint, space);
688 }
689
690 // run the post-solve callbacks
691 for(int i=0; i<arbiters->numcxx . . $ cpArray#num.; i++){
692 cpArbitercxx . . $ cpArbiter# *arb = (cpArbitercxx . . $ cpArbiter# *) arbiters->arrcxx . . $ cpArray#arr.[i];
693
694 cpCollisionHandlercxx . . $ cpCollisionHandler# *handler = arb->handlercxx . . $ cpArbiter#handler.;
695 handler->postSolveFunccxx . . $ cpCollisionHandler#postSolveFunc.(arb, space, handler->userDatacxx . . $ cpCollisionHandler#userData.);
696 }
697 } cpSpaceUnlockcxx . . $ cpSpaceUnlock(67df6f6b25fde2).(space, cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!);
698 }