cpl_atomic_ops.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**********************************************************************
  2. * $Id: cpl_atomic_ops.h 27869 2014-10-17 02:22:42Z rouault $
  3. *
  4. * Name: cpl_atomic_ops.h
  5. * Project: CPL - Common Portability Library
  6. * Purpose: Atomic operation functions.
  7. * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
  8. *
  9. **********************************************************************
  10. * Copyright (c) 2009-2010, Even Rouault <even dot rouault at mines-paris dot org>
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. * DEALINGS IN THE SOFTWARE.
  29. ****************************************************************************/
  30. #ifndef _CPL_ATOMIC_OPS_INCLUDED
  31. #define _CPL_ATOMIC_OPS_INCLUDED
  32. #include "cpl_port.h"
  33. CPL_C_START
  34. /** Add a value to a pointed integer in a thread and SMP-safe way
  35. * and return the resulting value of the operation.
  36. *
  37. * This function, which in most cases is implemented by a few
  38. * efficient machine instructions, guarantees that the value pointed
  39. * by ptr will be incremented in a thread and SMP-safe way.
  40. * The variables for this function must be aligned on a 32-bit boundary.
  41. *
  42. * Depending on the platforms, this function can also act as a
  43. * memory barrier, but this should not be assumed.
  44. *
  45. * Current platforms/architectures where an efficient implementation
  46. * exists are MacOSX, MS Windows, i386/x86_64 with GCC and platforms
  47. * supported by GCC 4.1 or higher. For other platforms supporting
  48. * the pthread library, and when GDAL is configured with thread-support,
  49. * the atomicity will be done with a mutex, but with
  50. * reduced efficiently. For the remaining platforms, a simple addition
  51. * with no locking will be done...
  52. *
  53. * @param ptr a pointer to an integer to increment
  54. * @param increment the amount to add to the pointed integer
  55. * @return the pointed value AFTER the result of the addition
  56. */
  57. int CPL_DLL CPLAtomicAdd(volatile int* ptr, int increment);
  58. /** Increment of 1 the pointed integer in a thread and SMP-safe way
  59. * and return the resulting value of the operation.
  60. *
  61. * @see CPLAtomicAdd for the details and guarantees of this atomic
  62. * operation
  63. *
  64. * @param ptr a pointer to an integer to increment
  65. * @return the pointed value AFTER the opeation: *ptr + 1
  66. */
  67. #define CPLAtomicInc(ptr) CPLAtomicAdd(ptr, 1)
  68. /** Decrement of 1 the pointed integer in a thread and SMP-safe way
  69. * and return the resulting value of the operation.
  70. *
  71. * @see CPLAtomicAdd for the details and guarantees of this atomic
  72. * operation
  73. *
  74. * @param ptr a pointer to an integer to decrement
  75. * @return the pointed value AFTER the opeation: *ptr - 1
  76. */
  77. #define CPLAtomicDec(ptr) CPLAtomicAdd(ptr, -1)
  78. CPL_C_END
  79. #endif /* _CPL_ATOMIC_OPS_INCLUDED */