PageRenderTime 33ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/gecko_api/include/pratom.h

http://firefox-mac-pdf.googlecode.com/
C Header | 224 lines | 50 code | 29 blank | 145 comment | 14 complexity | 061f4da8357f245e88c7a15459be98e9 MD5 | raw file
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the Netscape Portable Runtime (NSPR).
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998-2000
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /* GLOBAL FUNCTIONS:
  38. ** DESCRIPTION:
  39. ** PR Atomic operations
  40. */
  41. #ifndef pratom_h___
  42. #define pratom_h___
  43. #include "prtypes.h"
  44. #include "prlock.h"
  45. PR_BEGIN_EXTERN_C
  46. /*
  47. ** FUNCTION: PR_AtomicIncrement
  48. ** DESCRIPTION:
  49. ** Atomically increment a 32 bit value.
  50. ** INPUTS:
  51. ** val: a pointer to the value to increment
  52. ** RETURN:
  53. ** the returned value is the result of the increment
  54. */
  55. NSPR_API(PRInt32) PR_AtomicIncrement(PRInt32 *val);
  56. /*
  57. ** FUNCTION: PR_AtomicDecrement
  58. ** DESCRIPTION:
  59. ** Atomically decrement a 32 bit value.
  60. ** INPUTS:
  61. ** val: a pointer to the value to decrement
  62. ** RETURN:
  63. ** the returned value is the result of the decrement
  64. */
  65. NSPR_API(PRInt32) PR_AtomicDecrement(PRInt32 *val);
  66. /*
  67. ** FUNCTION: PR_AtomicSet
  68. ** DESCRIPTION:
  69. ** Atomically set a 32 bit value.
  70. ** INPUTS:
  71. ** val: A pointer to a 32 bit value to be set
  72. ** newval: The newvalue to assign to val
  73. ** RETURN:
  74. ** Returns the prior value
  75. */
  76. NSPR_API(PRInt32) PR_AtomicSet(PRInt32 *val, PRInt32 newval);
  77. /*
  78. ** FUNCTION: PR_AtomicAdd
  79. ** DESCRIPTION:
  80. ** Atomically add a 32 bit value.
  81. ** INPUTS:
  82. ** ptr: a pointer to the value to increment
  83. ** val: value to be added
  84. ** RETURN:
  85. ** the returned value is the result of the addition
  86. */
  87. NSPR_API(PRInt32) PR_AtomicAdd(PRInt32 *ptr, PRInt32 val);
  88. /*
  89. ** MACRO: PR_ATOMIC_INCREMENT
  90. ** MACRO: PR_ATOMIC_DECREMENT
  91. ** MACRO: PR_ATOMIC_SET
  92. ** MACRO: PR_ATOMIC_ADD
  93. ** DESCRIPTION:
  94. ** Macro versions of the atomic operations. They may be implemented
  95. ** as compiler intrinsics.
  96. **
  97. ** IMPORTANT NOTE TO NSPR MAINTAINERS:
  98. ** Implement these macros with compiler intrinsics only on platforms
  99. ** where the PR_AtomicXXX functions are truly atomic (i.e., where the
  100. ** configuration macro _PR_HAVE_ATOMIC_OPS is defined). Otherwise,
  101. ** the macros and functions won't be compatible and can't be used
  102. ** interchangeably.
  103. */
  104. #if defined(_WIN32) && (_MSC_VER >= 1310)
  105. long __cdecl _InterlockedIncrement(long volatile *Addend);
  106. #pragma intrinsic(_InterlockedIncrement)
  107. long __cdecl _InterlockedDecrement(long volatile *Addend);
  108. #pragma intrinsic(_InterlockedDecrement)
  109. long __cdecl _InterlockedExchange(long volatile *Target, long Value);
  110. #pragma intrinsic(_InterlockedExchange)
  111. long __cdecl _InterlockedExchangeAdd(long volatile *Addend, long Value);
  112. #pragma intrinsic(_InterlockedExchangeAdd)
  113. #define PR_ATOMIC_INCREMENT(val) _InterlockedIncrement(val)
  114. #define PR_ATOMIC_DECREMENT(val) _InterlockedDecrement(val)
  115. #define PR_ATOMIC_SET(val, newval) _InterlockedExchange(val, newval)
  116. #define PR_ATOMIC_ADD(ptr, val) (_InterlockedExchangeAdd(ptr, val) + (val))
  117. #elif ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && \
  118. ((defined(DARWIN) && \
  119. (defined(__ppc__) || defined(__i386__))) || \
  120. (defined(LINUX) && \
  121. (defined(__i386__) || defined(__ia64__) || defined(__x86_64__) || \
  122. (defined(__powerpc__) && !defined(__powerpc64__)) || \
  123. defined(__alpha))))
  124. /*
  125. * Because the GCC manual warns that some processors may support
  126. * reduced functionality of __sync_lock_test_and_set, we test for the
  127. * processors that we believe support a full atomic exchange operation.
  128. */
  129. #define PR_ATOMIC_INCREMENT(val) __sync_add_and_fetch(val, 1)
  130. #define PR_ATOMIC_DECREMENT(val) __sync_sub_and_fetch(val, 1)
  131. #define PR_ATOMIC_SET(val, newval) __sync_lock_test_and_set(val, newval)
  132. #define PR_ATOMIC_ADD(ptr, val) __sync_add_and_fetch(ptr, val)
  133. #else
  134. #define PR_ATOMIC_INCREMENT(val) PR_AtomicIncrement(val)
  135. #define PR_ATOMIC_DECREMENT(val) PR_AtomicDecrement(val)
  136. #define PR_ATOMIC_SET(val, newval) PR_AtomicSet(val, newval)
  137. #define PR_ATOMIC_ADD(ptr, val) PR_AtomicAdd(ptr, val)
  138. #endif
  139. /*
  140. ** LIFO linked-list (stack)
  141. */
  142. typedef struct PRStackElemStr PRStackElem;
  143. struct PRStackElemStr {
  144. PRStackElem *prstk_elem_next; /* next pointer MUST be at offset 0;
  145. assembly language code relies on this */
  146. };
  147. typedef struct PRStackStr PRStack;
  148. /*
  149. ** FUNCTION: PR_CreateStack
  150. ** DESCRIPTION:
  151. ** Create a stack, a LIFO linked list
  152. ** INPUTS:
  153. ** stack_name: a pointer to string containing the name of the stack
  154. ** RETURN:
  155. ** A pointer to the created stack, if successful, else NULL.
  156. */
  157. NSPR_API(PRStack *) PR_CreateStack(const char *stack_name);
  158. /*
  159. ** FUNCTION: PR_StackPush
  160. ** DESCRIPTION:
  161. ** Push an element on the top of the stack
  162. ** INPUTS:
  163. ** stack: pointer to the stack
  164. ** stack_elem: pointer to the stack element
  165. ** RETURN:
  166. ** None
  167. */
  168. NSPR_API(void) PR_StackPush(PRStack *stack, PRStackElem *stack_elem);
  169. /*
  170. ** FUNCTION: PR_StackPop
  171. ** DESCRIPTION:
  172. ** Remove the element on the top of the stack
  173. ** INPUTS:
  174. ** stack: pointer to the stack
  175. ** RETURN:
  176. ** A pointer to the stack element removed from the top of the stack,
  177. ** if non-empty,
  178. ** else NULL
  179. */
  180. NSPR_API(PRStackElem *) PR_StackPop(PRStack *stack);
  181. /*
  182. ** FUNCTION: PR_DestroyStack
  183. ** DESCRIPTION:
  184. ** Destroy the stack
  185. ** INPUTS:
  186. ** stack: pointer to the stack
  187. ** RETURN:
  188. ** PR_SUCCESS - if successfully deleted
  189. ** PR_FAILURE - if the stack is not empty
  190. ** PR_GetError will return
  191. ** PR_INVALID_STATE_ERROR - stack is not empty
  192. */
  193. NSPR_API(PRStatus) PR_DestroyStack(PRStack *stack);
  194. PR_END_EXTERN_C
  195. #endif /* pratom_h___ */