PageRenderTime 14ms CodeModel.GetById 8ms app.highlight 3ms RepoModel.GetById 1ms 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
 38/* GLOBAL FUNCTIONS:
 39** DESCRIPTION:
 40**     PR Atomic operations
 41*/
 42
 43#ifndef pratom_h___
 44#define pratom_h___
 45
 46#include "prtypes.h"
 47#include "prlock.h"
 48
 49PR_BEGIN_EXTERN_C
 50
 51/*
 52** FUNCTION: PR_AtomicIncrement
 53** DESCRIPTION:
 54**    Atomically increment a 32 bit value.
 55** INPUTS:
 56**    val:  a pointer to the value to increment
 57** RETURN:
 58**    the returned value is the result of the increment
 59*/
 60NSPR_API(PRInt32)	PR_AtomicIncrement(PRInt32 *val);
 61
 62/*
 63** FUNCTION: PR_AtomicDecrement
 64** DESCRIPTION:
 65**    Atomically decrement a 32 bit value.
 66** INPUTS:
 67**    val:  a pointer to the value to decrement
 68** RETURN:
 69**    the returned value is the result of the decrement
 70*/
 71NSPR_API(PRInt32)	PR_AtomicDecrement(PRInt32 *val);
 72
 73/*
 74** FUNCTION: PR_AtomicSet
 75** DESCRIPTION:
 76**    Atomically set a 32 bit value.
 77** INPUTS:
 78**    val: A pointer to a 32 bit value to be set
 79**    newval: The newvalue to assign to val
 80** RETURN:
 81**    Returns the prior value
 82*/
 83NSPR_API(PRInt32) PR_AtomicSet(PRInt32 *val, PRInt32 newval);
 84
 85/*
 86** FUNCTION: PR_AtomicAdd
 87** DESCRIPTION:
 88**    Atomically add a 32 bit value.
 89** INPUTS:
 90**    ptr:  a pointer to the value to increment
 91**	  val:  value to be added
 92** RETURN:
 93**    the returned value is the result of the addition
 94*/
 95NSPR_API(PRInt32)	PR_AtomicAdd(PRInt32 *ptr, PRInt32 val);
 96
 97/*
 98** MACRO: PR_ATOMIC_INCREMENT
 99** MACRO: PR_ATOMIC_DECREMENT
100** MACRO: PR_ATOMIC_SET
101** MACRO: PR_ATOMIC_ADD
102** DESCRIPTION:
103**    Macro versions of the atomic operations.  They may be implemented
104**    as compiler intrinsics.
105**
106** IMPORTANT NOTE TO NSPR MAINTAINERS:
107**    Implement these macros with compiler intrinsics only on platforms
108**    where the PR_AtomicXXX functions are truly atomic (i.e., where the
109**    configuration macro _PR_HAVE_ATOMIC_OPS is defined).  Otherwise,
110**    the macros and functions won't be compatible and can't be used
111**    interchangeably.
112*/
113#if defined(_WIN32) && (_MSC_VER >= 1310)
114
115long __cdecl _InterlockedIncrement(long volatile *Addend);
116#pragma intrinsic(_InterlockedIncrement)
117
118long __cdecl _InterlockedDecrement(long volatile *Addend);
119#pragma intrinsic(_InterlockedDecrement)
120
121long __cdecl _InterlockedExchange(long volatile *Target, long Value);
122#pragma intrinsic(_InterlockedExchange)
123
124long __cdecl _InterlockedExchangeAdd(long volatile *Addend, long Value);
125#pragma intrinsic(_InterlockedExchangeAdd)
126
127#define PR_ATOMIC_INCREMENT(val) _InterlockedIncrement(val)
128#define PR_ATOMIC_DECREMENT(val) _InterlockedDecrement(val)
129#define PR_ATOMIC_SET(val, newval) _InterlockedExchange(val, newval)
130#define PR_ATOMIC_ADD(ptr, val) (_InterlockedExchangeAdd(ptr, val) + (val))
131
132#elif ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && \
133      ((defined(DARWIN) && \
134           (defined(__ppc__) || defined(__i386__))) || \
135       (defined(LINUX) && \
136           (defined(__i386__) || defined(__ia64__) || defined(__x86_64__) || \
137           (defined(__powerpc__) && !defined(__powerpc64__)) || \
138           defined(__alpha))))
139
140/*
141 * Because the GCC manual warns that some processors may support
142 * reduced functionality of __sync_lock_test_and_set, we test for the
143 * processors that we believe support a full atomic exchange operation.
144 */
145
146#define PR_ATOMIC_INCREMENT(val) __sync_add_and_fetch(val, 1)
147#define PR_ATOMIC_DECREMENT(val) __sync_sub_and_fetch(val, 1)
148#define PR_ATOMIC_SET(val, newval) __sync_lock_test_and_set(val, newval)
149#define PR_ATOMIC_ADD(ptr, val) __sync_add_and_fetch(ptr, val)
150
151#else
152
153#define PR_ATOMIC_INCREMENT(val) PR_AtomicIncrement(val)
154#define PR_ATOMIC_DECREMENT(val) PR_AtomicDecrement(val)
155#define PR_ATOMIC_SET(val, newval) PR_AtomicSet(val, newval)
156#define PR_ATOMIC_ADD(ptr, val) PR_AtomicAdd(ptr, val)
157
158#endif
159
160/*
161** LIFO linked-list (stack)
162*/
163typedef struct PRStackElemStr PRStackElem;
164
165struct PRStackElemStr {
166    PRStackElem	*prstk_elem_next;	/* next pointer MUST be at offset 0;
167									  assembly language code relies on this */
168};
169
170typedef struct PRStackStr PRStack;
171
172/*
173** FUNCTION: PR_CreateStack
174** DESCRIPTION:
175**    Create a stack, a LIFO linked list
176** INPUTS:
177**    stack_name:  a pointer to string containing the name of the stack
178** RETURN:
179**    A pointer to the created stack, if successful, else NULL.
180*/
181NSPR_API(PRStack *)	PR_CreateStack(const char *stack_name);
182
183/*
184** FUNCTION: PR_StackPush
185** DESCRIPTION:
186**    Push an element on the top of the stack
187** INPUTS:
188**    stack:		pointer to the stack
189**    stack_elem:	pointer to the stack element
190** RETURN:
191**    None
192*/
193NSPR_API(void)			PR_StackPush(PRStack *stack, PRStackElem *stack_elem);
194
195/*
196** FUNCTION: PR_StackPop
197** DESCRIPTION:
198**    Remove the element on the top of the stack
199** INPUTS:
200**    stack:		pointer to the stack
201** RETURN:
202**    A pointer to the stack element removed from the top of the stack,
203**	  if non-empty,
204**    else NULL
205*/
206NSPR_API(PRStackElem *)	PR_StackPop(PRStack *stack);
207
208/*
209** FUNCTION: PR_DestroyStack
210** DESCRIPTION:
211**    Destroy the stack
212** INPUTS:
213**    stack:		pointer to the stack
214** RETURN:
215**    PR_SUCCESS - if successfully deleted
216**	  PR_FAILURE - if the stack is not empty
217**					PR_GetError will return
218**						PR_INVALID_STATE_ERROR - stack is not empty
219*/
220NSPR_API(PRStatus)		PR_DestroyStack(PRStack *stack);
221
222PR_END_EXTERN_C
223
224#endif /* pratom_h___ */