1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff *
34219820Sjeff */
35219820Sjeff
36219820Sjeff/*
37219820Sjeff * Abstract:
38219820Sjeff *	Implementation specific header files for atomic operations.
39219820Sjeff */
40219820Sjeff
41219820Sjeff#ifndef _CL_ATOMIC_OSD_H_
42219820Sjeff#define _CL_ATOMIC_OSD_H_
43219820Sjeff
44219820Sjeff#include <complib/cl_types.h>
45219820Sjeff#include <complib/cl_spinlock.h>
46219820Sjeff
47219820Sjeff#ifdef __cplusplus
48219820Sjeff#  define BEGIN_C_DECLS extern "C" {
49219820Sjeff#  define END_C_DECLS   }
50219820Sjeff#else				/* !__cplusplus */
51219820Sjeff#  define BEGIN_C_DECLS
52219820Sjeff#  define END_C_DECLS
53219820Sjeff#endif				/* __cplusplus */
54219820Sjeff
55219820SjeffBEGIN_C_DECLS extern cl_spinlock_t cl_atomic_spinlock;
56219820Sjeff
57219820Sjeffstatic inline int32_t cl_atomic_inc(IN atomic32_t * const p_value)
58219820Sjeff{
59219820Sjeff	int32_t new_val;
60219820Sjeff
61219820Sjeff	cl_spinlock_acquire(&cl_atomic_spinlock);
62219820Sjeff	new_val = *p_value + 1;
63219820Sjeff	*p_value = new_val;
64219820Sjeff	cl_spinlock_release(&cl_atomic_spinlock);
65219820Sjeff	return (new_val);
66219820Sjeff}
67219820Sjeff
68219820Sjeffstatic inline int32_t cl_atomic_dec(IN atomic32_t * const p_value)
69219820Sjeff{
70219820Sjeff	int32_t new_val;
71219820Sjeff
72219820Sjeff	cl_spinlock_acquire(&cl_atomic_spinlock);
73219820Sjeff	new_val = *p_value - 1;
74219820Sjeff	*p_value = new_val;
75219820Sjeff	cl_spinlock_release(&cl_atomic_spinlock);
76219820Sjeff	return (new_val);
77219820Sjeff}
78219820Sjeff
79219820Sjeffstatic inline int32_t
80219820Sjeffcl_atomic_add(IN atomic32_t * const p_value, IN const int32_t increment)
81219820Sjeff{
82219820Sjeff	int32_t new_val;
83219820Sjeff
84219820Sjeff	cl_spinlock_acquire(&cl_atomic_spinlock);
85219820Sjeff	new_val = *p_value + increment;
86219820Sjeff	*p_value = new_val;
87219820Sjeff	cl_spinlock_release(&cl_atomic_spinlock);
88219820Sjeff	return (new_val);
89219820Sjeff}
90219820Sjeff
91219820Sjeffstatic inline int32_t
92219820Sjeffcl_atomic_sub(IN atomic32_t * const p_value, IN const int32_t decrement)
93219820Sjeff{
94219820Sjeff	int32_t new_val;
95219820Sjeff
96219820Sjeff	cl_spinlock_acquire(&cl_atomic_spinlock);
97219820Sjeff	new_val = *p_value + decrement;
98219820Sjeff	*p_value = new_val;
99219820Sjeff	cl_spinlock_release(&cl_atomic_spinlock);
100219820Sjeff	return (new_val);
101219820Sjeff}
102219820Sjeff
103219820SjeffEND_C_DECLS
104219820Sjeff#endif				/* _CL_ATOMIC_OSD_H_ */
105