gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.6KB

  1. /**********************************************************************
  2. * $Id: cpl_atomic_ops.h 44e0c0ecc2e12f7885d8572d0f18dd94e7fbda1c 2016-10-25 02:28:29Z Kurt Schwehr $
  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 efficiency. 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 operation: *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 operation: *ptr - 1
  76. */
  77. #define CPLAtomicDec(ptr) CPLAtomicAdd(ptr, -1)
  78. /** Compares *ptr with oldval. If *ptr == oldval, then *ptr is assigned
  79. * newval and TRUE is returned. Otherwise nothing is done, and FALSE is returned.
  80. *
  81. * Current platforms/architectures where an efficient implementation
  82. * exists are MacOSX, MS Windows, i386/x86_64 with GCC and platforms
  83. * supported by GCC 4.1 or higher. For other platforms supporting
  84. * the pthread library, and when GDAL is configured with thread-support,
  85. * the atomicity will be done with a mutex, but with
  86. * reduced efficiency. For the remaining platforms, a simple compare and
  87. * exchange with no locking will be done...
  88. *
  89. * @param ptr a pointer to an integer (aligned on 32bit boundary).
  90. * @param oldval old value
  91. * @param newval new value
  92. * @return TRUE if the exchange has been done
  93. */
  94. int CPLAtomicCompareAndExchange(volatile int* ptr, int oldval, int newval);
  95. CPL_C_END
  96. #endif /* CPL_ATOMIC_OPS_INCLUDED */
上海开阖软件有限公司 沪ICP备12045867号-1