| 1 |
/************************************************************************* |
| 2 |
* * |
| 3 |
* ODER's Utilities Library. Copyright (C) 2008 Oleh Derevenko. * |
| 4 |
* All rights reserved. e-mail: [email protected] (change all "a" to "e") * |
| 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 3 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-LESSER.TXT. Since LGPL is the extension of GPL * |
| 13 |
* the text of GNU General Public License is also provided for * |
| 14 |
* your information in file LICENSE.TXT. * |
| 15 |
* (2) The BSD-style license that is included with this library in * |
| 16 |
* the file LICENSE-BSD.TXT. * |
| 17 |
* (3) The zlib/libpng license that is included with this library in * |
| 18 |
* the file LICENSE-ZLIB.TXT * |
| 19 |
* * |
| 20 |
* This library is distributed WITHOUT ANY WARRANTY, including implied * |
| 21 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
| 22 |
* See the files LICENSE.TXT and LICENSE-LESSER.TXT or LICENSE-BSD.TXT * |
| 23 |
* or LICENSE-ZLIB.TXT for more details. * |
| 24 |
* * |
| 25 |
*************************************************************************/ |
| 26 |
|
| 27 |
#include <ou/features.h> |
| 28 |
|
| 29 |
|
| 30 |
#if _OU_FEATURE_SET >= _OU_FEATURE_SET_ATOMICS |
| 31 |
|
| 32 |
#include <ou/atomic.h> |
| 33 |
#include <ou/assert.h> |
| 34 |
#include <ou/namespace.h> |
| 35 |
|
| 36 |
|
| 37 |
BEGIN_NAMESPACE_OU(); |
| 38 |
|
| 39 |
|
| 40 |
#if !defined(__OU_ATOMIC_PTR_FUNCTIONS_DEFINED) |
| 41 |
|
| 42 |
////////////////////////////////////////////////////////////////////////// |
| 43 |
// Implementation via mutex locks |
| 44 |
|
| 45 |
#if !defined(__OU_ATOMIC_INITIALIZATION_FUNCTIONS_REQUIRED) |
| 46 |
|
| 47 |
#error Internal error (Atomic-via-mutex implementations can not appear without initialization) |
| 48 |
|
| 49 |
|
| 50 |
#endif // #if !defined(__OU_ATOMIC_INITIALIZATION_FUNCTIONS_DEFINED) |
| 51 |
|
| 52 |
|
| 53 |
END_NAMESPACE_OU(); |
| 54 |
|
| 55 |
|
| 56 |
#include <pthread.h> |
| 57 |
#include <errno.h> |
| 58 |
|
| 59 |
#if !defined(EOK) |
| 60 |
|
| 61 |
#define EOK 0 |
| 62 |
|
| 63 |
|
| 64 |
#endif |
| 65 |
|
| 66 |
|
| 67 |
BEGIN_NAMESPACE_OU(); |
| 68 |
|
| 69 |
|
| 70 |
static unsigned int g_uiAtomicAPIInitializationCount = 0; |
| 71 |
|
| 72 |
|
| 73 |
#define _OU_ATOMIC_MUTEX_COUNT 8 |
| 74 |
#define _OU_ATOMIC_MUTES_INDEX_SHIFT 3 // Shift by 3 bits as 8 bytes is a common memory alignment |
| 75 |
#define _OU_ATOMIC_MUTEX_INDEX_MASK (_OU_ATOMIC_MUTEX_COUNT - 1) |
| 76 |
|
| 77 |
|
| 78 |
// Mutexes array is used to distribute load over multiple mutexes |
| 79 |
static pthread_mutex_t g_apmAtomicMutexes[_OU_ATOMIC_MUTEX_COUNT]; |
| 80 |
|
| 81 |
|
| 82 |
static inline unsigned int DeriveAtomicMutexIndex(void *pv_Destination) |
| 83 |
{ |
| 84 |
return ((unsigned int)(size_t)pv_Destination >> _OU_ATOMIC_MUTES_INDEX_SHIFT) & _OU_ATOMIC_MUTEX_INDEX_MASK; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
////////////////////////////////////////////////////////////////////////// |
| 89 |
// Atomic ord32 functions implementation |
| 90 |
|
| 91 |
#if !defined(__OU_ATOMIC_ORD32_FUNCTIONS_DEFINED) |
| 92 |
|
| 93 |
/*extern*/ atomicord32 AtomicIncrement(volatile atomicord32 *paoDestination) |
| 94 |
{ |
| 95 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)paoDestination); |
| 96 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 97 |
|
| 98 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 99 |
OU_CHECK(iLockResult == EOK); |
| 100 |
|
| 101 |
const atomicord32 aoNewValue = ++(*paoDestination); |
| 102 |
|
| 103 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 104 |
OU_CHECK(iUnlockResult == EOK); |
| 105 |
|
| 106 |
atomicord32 aoResult = aoNewValue; |
| 107 |
return aoResult; |
| 108 |
} |
| 109 |
|
| 110 |
/*extern*/ atomicord32 AtomicDecrement(volatile atomicord32 *paoDestination) |
| 111 |
{ |
| 112 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)paoDestination); |
| 113 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 114 |
|
| 115 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 116 |
OU_CHECK(iLockResult == EOK); |
| 117 |
|
| 118 |
const atomicord32 aoNewValue = --(*paoDestination); |
| 119 |
|
| 120 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 121 |
OU_CHECK(iUnlockResult == EOK); |
| 122 |
|
| 123 |
atomicord32 aoResult = aoNewValue; |
| 124 |
return aoResult; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/*extern*/ atomicord32 AtomicExchange(volatile atomicord32 *paoDestination, atomicord32 aoExchange) |
| 129 |
{ |
| 130 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)paoDestination); |
| 131 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 132 |
|
| 133 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 134 |
OU_CHECK(iLockResult == EOK); |
| 135 |
|
| 136 |
const atomicord32 aoOldValue = *paoDestination; |
| 137 |
|
| 138 |
*paoDestination = aoExchange; |
| 139 |
|
| 140 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 141 |
OU_CHECK(iUnlockResult == EOK); |
| 142 |
|
| 143 |
atomicord32 aoResult = aoOldValue; |
| 144 |
return aoResult; |
| 145 |
} |
| 146 |
|
| 147 |
/*extern*/ atomicord32 AtomicExchangeAdd(volatile atomicord32 *paoDestination, atomicord32 aoAddend) |
| 148 |
{ |
| 149 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)paoDestination); |
| 150 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 151 |
|
| 152 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 153 |
OU_CHECK(iLockResult == EOK); |
| 154 |
|
| 155 |
const atomicord32 aoOldValue = *paoDestination; |
| 156 |
|
| 157 |
*paoDestination += aoAddend; |
| 158 |
|
| 159 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 160 |
OU_CHECK(iUnlockResult == EOK); |
| 161 |
|
| 162 |
atomicord32 aoResult = aoOldValue; |
| 163 |
return aoResult; |
| 164 |
} |
| 165 |
|
| 166 |
/*extern*/ bool AtomicCompareExchange(volatile atomicord32 *paoDestination, atomicord32 aoComparand, atomicord32 aoExchange) |
| 167 |
{ |
| 168 |
bool bResult = false; |
| 169 |
|
| 170 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)paoDestination); |
| 171 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 172 |
|
| 173 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 174 |
OU_CHECK(iLockResult == EOK); |
| 175 |
|
| 176 |
const atomicord32 aoOldValue = *paoDestination; |
| 177 |
|
| 178 |
if (aoOldValue == aoComparand) |
| 179 |
{ |
| 180 |
*paoDestination = aoExchange; |
| 181 |
|
| 182 |
bResult = true; |
| 183 |
} |
| 184 |
|
| 185 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 186 |
OU_CHECK(iUnlockResult == EOK); |
| 187 |
|
| 188 |
return bResult; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/*extern*/ atomicord32 AtomicAnd(volatile atomicord32 *paoDestination, atomicord32 aoBitMask) |
| 193 |
{ |
| 194 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)paoDestination); |
| 195 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 196 |
|
| 197 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 198 |
OU_CHECK(iLockResult == EOK); |
| 199 |
|
| 200 |
const atomicord32 aoOldValue = *paoDestination; |
| 201 |
|
| 202 |
*paoDestination &= aoBitMask; |
| 203 |
|
| 204 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 205 |
OU_CHECK(iUnlockResult == EOK); |
| 206 |
|
| 207 |
atomicord32 aoResult = aoOldValue; |
| 208 |
return aoResult; |
| 209 |
} |
| 210 |
|
| 211 |
/*extern*/ atomicord32 AtomicOr(volatile atomicord32 *paoDestination, atomicord32 aoBitMask) |
| 212 |
{ |
| 213 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)paoDestination); |
| 214 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 215 |
|
| 216 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 217 |
OU_CHECK(iLockResult == EOK); |
| 218 |
|
| 219 |
const atomicord32 aoOldValue = *paoDestination; |
| 220 |
|
| 221 |
*paoDestination |= aoBitMask; |
| 222 |
|
| 223 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 224 |
OU_CHECK(iUnlockResult == EOK); |
| 225 |
|
| 226 |
atomicord32 aoResult = aoOldValue; |
| 227 |
return aoResult; |
| 228 |
} |
| 229 |
|
| 230 |
/*extern*/ atomicord32 AtomicXor(volatile atomicord32 *paoDestination, atomicord32 aoBitMask) |
| 231 |
{ |
| 232 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)paoDestination); |
| 233 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 234 |
|
| 235 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 236 |
OU_CHECK(iLockResult == EOK); |
| 237 |
|
| 238 |
const atomicord32 aoOldValue = *paoDestination; |
| 239 |
|
| 240 |
*paoDestination ^= aoBitMask; |
| 241 |
|
| 242 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 243 |
OU_CHECK(iUnlockResult == EOK); |
| 244 |
|
| 245 |
atomicord32 aoResult = aoOldValue; |
| 246 |
return aoResult; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
#endif // #if !defined(__OU_ATOMIC_ORD32_FUNCTIONS_DEFINED) |
| 251 |
|
| 252 |
|
| 253 |
////////////////////////////////////////////////////////////////////////// |
| 254 |
// Atomic pointer functions implementation |
| 255 |
|
| 256 |
/*extern*/ atomicptr AtomicExchangePointer(volatile atomicptr *papDestination, atomicptr apExchange) |
| 257 |
{ |
| 258 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)papDestination); |
| 259 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 260 |
|
| 261 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 262 |
OU_CHECK(iLockResult == EOK); |
| 263 |
|
| 264 |
const atomicptr apOldValue = *papDestination; |
| 265 |
|
| 266 |
*papDestination = apExchange; |
| 267 |
|
| 268 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 269 |
OU_CHECK(iUnlockResult == EOK); |
| 270 |
|
| 271 |
atomicptr apResult = apOldValue; |
| 272 |
return apResult; |
| 273 |
} |
| 274 |
|
| 275 |
/*extern*/ bool AtomicCompareExchangePointer(volatile atomicptr *papDestination, atomicptr apComparand, atomicptr apExchange) |
| 276 |
{ |
| 277 |
bool bResult = false; |
| 278 |
|
| 279 |
const unsigned int uiMutexIndex = DeriveAtomicMutexIndex((void *)papDestination); |
| 280 |
pthread_mutex_t *ptmMutexToBeUsed = g_apmAtomicMutexes + uiMutexIndex; |
| 281 |
|
| 282 |
int iLockResult = pthread_mutex_lock(ptmMutexToBeUsed); |
| 283 |
OU_CHECK(iLockResult == EOK); |
| 284 |
|
| 285 |
const atomicptr apOldValue = *papDestination; |
| 286 |
|
| 287 |
if (apOldValue == apComparand) |
| 288 |
{ |
| 289 |
*papDestination = apExchange; |
| 290 |
|
| 291 |
bResult = true; |
| 292 |
} |
| 293 |
|
| 294 |
int iUnlockResult = pthread_mutex_unlock(ptmMutexToBeUsed); |
| 295 |
OU_CHECK(iUnlockResult == EOK); |
| 296 |
|
| 297 |
return bResult; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
////////////////////////////////////////////////////////////////////////// |
| 302 |
// Atomic initialization functions implementation |
| 303 |
|
| 304 |
static void FreeAtomicMutexes(unsigned int nLastMutexIndex=0) |
| 305 |
{ |
| 306 |
const unsigned int nMutexCount = nLastMutexIndex == 0 ? _OU_ATOMIC_MUTEX_COUNT : nLastMutexIndex; |
| 307 |
|
| 308 |
for (unsigned int nMutexIndex = 0; nMutexIndex != nMutexCount; ++nMutexIndex) |
| 309 |
{ |
| 310 |
int iMutexDestroyResult = pthread_mutex_destroy(g_apmAtomicMutexes + nMutexIndex); |
| 311 |
OU_VERIFY(iMutexDestroyResult == EOK); // Ignore the error |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
static bool CreateAtomicMutexesWithAttributes(pthread_mutexattr_t *pmaMutexAttributes) |
| 316 |
{ |
| 317 |
const unsigned int nMutexCount = _OU_ATOMIC_MUTEX_COUNT; |
| 318 |
|
| 319 |
unsigned int nMutexIndex = 0; |
| 320 |
|
| 321 |
for (; nMutexIndex != nMutexCount; ++nMutexIndex) |
| 322 |
{ |
| 323 |
int iMutexInitResult = pthread_mutex_init(g_apmAtomicMutexes + nMutexIndex, pmaMutexAttributes); |
| 324 |
|
| 325 |
if (iMutexInitResult != EOK) |
| 326 |
{ |
| 327 |
if (nMutexIndex != 0) |
| 328 |
{ |
| 329 |
FreeAtomicMutexes(nMutexIndex); |
| 330 |
} |
| 331 |
|
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
bool bResult = nMutexIndex == nMutexCount; |
| 337 |
return bResult; |
| 338 |
} |
| 339 |
|
| 340 |
static bool CreateAtomicMutexes() |
| 341 |
{ |
| 342 |
bool bResult = false; |
| 343 |
|
| 344 |
pthread_mutexattr_t maMutexAttributes; |
| 345 |
|
| 346 |
int iAttrInitResult = pthread_mutexattr_init(&maMutexAttributes); |
| 347 |
|
| 348 |
if (iAttrInitResult == EOK) |
| 349 |
{ |
| 350 |
bResult = CreateAtomicMutexesWithAttributes(&maMutexAttributes); |
| 351 |
|
| 352 |
int iAttrDestroyResult = pthread_mutexattr_destroy(&maMutexAttributes); |
| 353 |
OU_VERIFY(iAttrDestroyResult == EOK); // Ignore error |
| 354 |
} |
| 355 |
|
| 356 |
return bResult; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
static bool InitializeAtomicAPIValidated() |
| 361 |
{ |
| 362 |
bool bResult = false; |
| 363 |
|
| 364 |
do |
| 365 |
{ |
| 366 |
if (!CreateAtomicMutexes()) |
| 367 |
{ |
| 368 |
break; |
| 369 |
} |
| 370 |
|
| 371 |
bResult = true; |
| 372 |
} |
| 373 |
while (false); |
| 374 |
|
| 375 |
return bResult; |
| 376 |
} |
| 377 |
|
| 378 |
static void FinalizeAtomicAPIValidated() |
| 379 |
{ |
| 380 |
FreeAtomicMutexes(); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
/*extern*/ bool InitializeAtomicAPI() |
| 385 |
{ |
| 386 |
OU_ASSERT(g_uiAtomicAPIInitializationCount != 0U - 1U); |
| 387 |
|
| 388 |
bool bResult = false; |
| 389 |
|
| 390 |
do |
| 391 |
{ |
| 392 |
if (g_uiAtomicAPIInitializationCount == 0) // Initialization/finalization must be called from main thread |
| 393 |
{ |
| 394 |
if (!InitializeAtomicAPIValidated()) |
| 395 |
{ |
| 396 |
break; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
++g_uiAtomicAPIInitializationCount; |
| 401 |
|
| 402 |
bResult = true; |
| 403 |
} |
| 404 |
while (false); |
| 405 |
|
| 406 |
return bResult; |
| 407 |
} |
| 408 |
|
| 409 |
/*extern*/ void FinalizeAtomicAPI() |
| 410 |
{ |
| 411 |
OU_ASSERT(g_uiAtomicAPIInitializationCount != 0U); |
| 412 |
|
| 413 |
if (--g_uiAtomicAPIInitializationCount == 0) // Initialization/finalization must be called from main thread |
| 414 |
{ |
| 415 |
FinalizeAtomicAPIValidated(); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
#else // #if defined(__OU_ATOMIC_PTR_FUNCTIONS_DEFINED) |
| 421 |
|
| 422 |
#if !defined(__OU_ATOMIC_ORD32_FUNCTIONS_DEFINED) |
| 423 |
|
| 424 |
#error Internal error (Atomic ord32 functions can not be undefined while pointer functions are defined) |
| 425 |
|
| 426 |
|
| 427 |
#endif // #if !defined(__OU_ATOMIC_ORD32_FUNCTIONS_DEFINED) |
| 428 |
|
| 429 |
|
| 430 |
#if defined(__OU_ATOMIC_INITIALIZATION_FUNCTIONS_REQUIRED) |
| 431 |
|
| 432 |
#error Internal error (Atomic initialization can not be required while atomic functions are defined) |
| 433 |
|
| 434 |
|
| 435 |
#endif // #if defined(__OU_ATOMIC_INITIALIZATION_FUNCTIONS_REQUIRED) |
| 436 |
|
| 437 |
|
| 438 |
#endif // #if !defined(__OU_ATOMIC_PTR_FUNCTIONS_DEFINED) |
| 439 |
|
| 440 |
|
| 441 |
END_NAMESPACE_OU(); |
| 442 |
|
| 443 |
|
| 444 |
#endif // #if _OU_FEATURE_SET >= _OU_FEATURE_SET_ATOMICS |
| 445 |
|