nsprpub/pr/include/pratom.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /* GLOBAL FUNCTIONS:
     7 ** DESCRIPTION:
     8 **     PR Atomic operations
     9 */
    11 #ifndef pratom_h___
    12 #define pratom_h___
    14 #include "prtypes.h"
    15 #include "prlock.h"
    17 PR_BEGIN_EXTERN_C
    19 /*
    20 ** FUNCTION: PR_AtomicIncrement
    21 ** DESCRIPTION:
    22 **    Atomically increment a 32 bit value.
    23 ** INPUTS:
    24 **    val:  a pointer to the value to increment
    25 ** RETURN:
    26 **    the returned value is the result of the increment
    27 */
    28 NSPR_API(PRInt32)	PR_AtomicIncrement(PRInt32 *val);
    30 /*
    31 ** FUNCTION: PR_AtomicDecrement
    32 ** DESCRIPTION:
    33 **    Atomically decrement a 32 bit value.
    34 ** INPUTS:
    35 **    val:  a pointer to the value to decrement
    36 ** RETURN:
    37 **    the returned value is the result of the decrement
    38 */
    39 NSPR_API(PRInt32)	PR_AtomicDecrement(PRInt32 *val);
    41 /*
    42 ** FUNCTION: PR_AtomicSet
    43 ** DESCRIPTION:
    44 **    Atomically set a 32 bit value.
    45 ** INPUTS:
    46 **    val: A pointer to a 32 bit value to be set
    47 **    newval: The newvalue to assign to val
    48 ** RETURN:
    49 **    Returns the prior value
    50 */
    51 NSPR_API(PRInt32) PR_AtomicSet(PRInt32 *val, PRInt32 newval);
    53 /*
    54 ** FUNCTION: PR_AtomicAdd
    55 ** DESCRIPTION:
    56 **    Atomically add a 32 bit value.
    57 ** INPUTS:
    58 **    ptr:  a pointer to the value to increment
    59 **	  val:  value to be added
    60 ** RETURN:
    61 **    the returned value is the result of the addition
    62 */
    63 NSPR_API(PRInt32)	PR_AtomicAdd(PRInt32 *ptr, PRInt32 val);
    65 /*
    66 ** MACRO: PR_ATOMIC_INCREMENT
    67 ** MACRO: PR_ATOMIC_DECREMENT
    68 ** MACRO: PR_ATOMIC_SET
    69 ** MACRO: PR_ATOMIC_ADD
    70 ** DESCRIPTION:
    71 **    Macro versions of the atomic operations.  They may be implemented
    72 **    as compiler intrinsics.
    73 **
    74 ** IMPORTANT NOTE TO NSPR MAINTAINERS:
    75 **    Implement these macros with compiler intrinsics only on platforms
    76 **    where the PR_AtomicXXX functions are truly atomic (i.e., where the
    77 **    configuration macro _PR_HAVE_ATOMIC_OPS is defined).  Otherwise,
    78 **    the macros and functions won't be compatible and can't be used
    79 **    interchangeably.
    80 */
    81 #if defined(_WIN32) && !defined(_WIN32_WCE) && \
    82     (!defined(_MSC_VER) || (_MSC_VER >= 1310))
    84 long __cdecl _InterlockedIncrement(long volatile *Addend);
    85 long __cdecl _InterlockedDecrement(long volatile *Addend);
    86 long __cdecl _InterlockedExchange(long volatile *Target, long Value);
    87 long __cdecl _InterlockedExchangeAdd(long volatile *Addend, long Value);
    89 #ifdef _MSC_VER
    90 #pragma intrinsic(_InterlockedIncrement)
    91 #pragma intrinsic(_InterlockedDecrement)
    92 #pragma intrinsic(_InterlockedExchange)
    93 #pragma intrinsic(_InterlockedExchangeAdd)
    94 #endif
    96 #define PR_ATOMIC_INCREMENT(val) _InterlockedIncrement((long volatile *)(val))
    97 #define PR_ATOMIC_DECREMENT(val) _InterlockedDecrement((long volatile *)(val))
    98 #define PR_ATOMIC_SET(val, newval) \
    99         _InterlockedExchange((long volatile *)(val), (long)(newval))
   100 #define PR_ATOMIC_ADD(ptr, val) \
   101         (_InterlockedExchangeAdd((long volatile *)(ptr), (long)(val)) + (val))
   103 #elif ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && \
   104       ((defined(__APPLE__) && \
   105            (defined(__ppc__) || defined(__i386__) || defined(__x86_64__))) || \
   106        (defined(__linux__) && \
   107            ((defined(__i386__) && \
   108            defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) || \
   109            defined(__ia64__) || defined(__x86_64__) || \
   110            defined(__powerpc__) || \
   111            (defined(__arm__) && \
   112            defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) || \
   113            defined(__aarch64__) || defined(__alpha))))
   115 /*
   116  * Because the GCC manual warns that some processors may support
   117  * reduced functionality of __sync_lock_test_and_set, we test for the
   118  * processors that we believe support a full atomic exchange operation.
   119  */
   121 #define PR_ATOMIC_INCREMENT(val) __sync_add_and_fetch(val, 1)
   122 #define PR_ATOMIC_DECREMENT(val) __sync_sub_and_fetch(val, 1)
   123 #define PR_ATOMIC_SET(val, newval) __sync_lock_test_and_set(val, newval)
   124 #define PR_ATOMIC_ADD(ptr, val) __sync_add_and_fetch(ptr, val)
   126 #else
   128 #define PR_ATOMIC_INCREMENT(val) PR_AtomicIncrement(val)
   129 #define PR_ATOMIC_DECREMENT(val) PR_AtomicDecrement(val)
   130 #define PR_ATOMIC_SET(val, newval) PR_AtomicSet(val, newval)
   131 #define PR_ATOMIC_ADD(ptr, val) PR_AtomicAdd(ptr, val)
   133 #endif
   135 /*
   136 ** LIFO linked-list (stack)
   137 */
   138 typedef struct PRStackElemStr PRStackElem;
   140 struct PRStackElemStr {
   141     PRStackElem	*prstk_elem_next;	/* next pointer MUST be at offset 0;
   142 									  assembly language code relies on this */
   143 };
   145 typedef struct PRStackStr PRStack;
   147 /*
   148 ** FUNCTION: PR_CreateStack
   149 ** DESCRIPTION:
   150 **    Create a stack, a LIFO linked list
   151 ** INPUTS:
   152 **    stack_name:  a pointer to string containing the name of the stack
   153 ** RETURN:
   154 **    A pointer to the created stack, if successful, else NULL.
   155 */
   156 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);
   170 /*
   171 ** FUNCTION: PR_StackPop
   172 ** DESCRIPTION:
   173 **    Remove the element on the top of the stack
   174 ** INPUTS:
   175 **    stack:		pointer to the stack
   176 ** RETURN:
   177 **    A pointer to the stack element removed from the top of the stack,
   178 **	  if non-empty,
   179 **    else NULL
   180 */
   181 NSPR_API(PRStackElem *)	PR_StackPop(PRStack *stack);
   183 /*
   184 ** FUNCTION: PR_DestroyStack
   185 ** DESCRIPTION:
   186 **    Destroy the stack
   187 ** INPUTS:
   188 **    stack:		pointer to the stack
   189 ** RETURN:
   190 **    PR_SUCCESS - if successfully deleted
   191 **	  PR_FAILURE - if the stack is not empty
   192 **					PR_GetError will return
   193 **						PR_INVALID_STATE_ERROR - stack is not empty
   194 */
   195 NSPR_API(PRStatus)		PR_DestroyStack(PRStack *stack);
   197 PR_END_EXTERN_C
   199 #endif /* pratom_h___ */

mercurial