Open Dynamics Engine
threadingutils.h
1 /*************************************************************************
2  * *
3  * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4  * All rights reserved. Email: russ@q12.org Web: www.q12.org *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of EITHER: *
8  * (1) The GNU Lesser General Public License as published by the Free *
9  * Software Foundation; either version 2.1 of the License, or (at *
10  * your option) any later version. The text of the GNU Lesser *
11  * General Public License is included with this library in the *
12  * file LICENSE.TXT. *
13  * (2) The BSD-style license that is included with this library in *
14  * the file LICENSE-BSD.TXT. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
19  * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
20  * *
21  *************************************************************************/
22 
23 #ifndef _ODE_THREADINGUTILS_H_
24 #define _ODE_THREADINGUTILS_H_
25 
26 
27 #include "odeou.h"
28 
29 
30 #if !dTHREADING_INTF_DISABLED
31 
32 static inline
33 bool ThrsafeCompareExchange(volatile atomicord32 *paoDestination, atomicord32 aoComparand, atomicord32 aoExchange)
34 {
35  return AtomicCompareExchange(paoDestination, aoComparand, aoExchange);
36 }
37 
38 static inline
39 atomicord32 ThrsafeExchange(volatile atomicord32 *paoDestination, atomicord32 aoExchange)
40 {
41  return AtomicExchange(paoDestination, aoExchange);
42 }
43 
44 static inline
45 bool ThrsafeCompareExchangePointer(volatile atomicptr *papDestination, atomicptr apComparand, atomicptr apExchange)
46 {
47  return AtomicCompareExchangePointer(papDestination, apComparand, apExchange);
48 }
49 
50 static inline
51 atomicptr ThrsafeExchangePointer(volatile atomicptr *papDestination, atomicptr apExchange)
52 {
53  return AtomicExchangePointer(papDestination, apExchange);
54 }
55 
56 
57 #else // #if dTHREADING_INTF_DISABLED
58 
59 static inline
60 bool ThrsafeCompareExchange(volatile atomicord32 *paoDestination, atomicord32 aoComparand, atomicord32 aoExchange)
61 {
62  return (*paoDestination == aoComparand) ? ((*paoDestination = aoExchange), true) : false;
63 }
64 
65 static inline
66 atomicord32 ThrsafeExchange(volatile atomicord32 *paoDestination, atomicord32 aoExchange)
67 {
68  atomicord32 aoDestinationValue = *paoDestination;
69  *paoDestination = aoExchange;
70  return aoDestinationValue;
71 }
72 
73 static inline
74 bool ThrsafeCompareExchangePointer(volatile atomicptr *papDestination, atomicptr apComparand, atomicptr apExchange)
75 {
76  return (*papDestination == apComparand) ? ((*papDestination = apExchange), true) : false;
77 }
78 
79 static inline
80 atomicptr ThrsafeExchangePointer(volatile atomicptr *papDestination, atomicptr apExchange)
81 {
82  atomicptr apDestinationValue = *papDestination;
83  *papDestination = apExchange;
84  return apDestinationValue;
85 }
86 
87 
88 #endif // #if dTHREADING_INTF_DISABLED
89 
90 
91 static inline
92 unsigned int ThrsafeIncrementIntUpToLimit(volatile unsigned int *storagePointer, unsigned int limitValue)
93 {
94  unsigned int resultValue;
95  while (true) {
96  resultValue = *storagePointer;
97  if (resultValue == limitValue) {
98  break;
99  }
100  if (ThrsafeCompareExchange((volatile atomicord32 *)storagePointer, (atomicord32)resultValue, (atomicord32)(resultValue + 1))) {
101  break;
102  }
103  }
104  return resultValue;
105 }
106 
107 static inline
108 size_t ThrsafeIncrementSizeUpToLimit(volatile size_t *storagePointer, size_t limitValue)
109 {
110  size_t resultValue;
111  while (true) {
112  resultValue = *storagePointer;
113  if (resultValue == limitValue) {
114  break;
115  }
116  if (ThrsafeCompareExchangePointer((volatile atomicptr *)storagePointer, (atomicptr)resultValue, (atomicptr)(resultValue + 1))) {
117  break;
118  }
119  }
120  return resultValue;
121 }
122 
123 
124 
125 #endif // _ODE_THREADINGUTILS_H_