File: wInterlocked.h

package info (click to toggle)
openmama 2.2.2.1-10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,064 kB
  • ctags: 34,353
  • sloc: cpp: 86,630; ansic: 78,639; java: 45,677; cs: 40,365; sh: 8,894; makefile: 1,397; python: 654; xml: 509; lex: 222
file content (181 lines) | stat: -rw-r--r-- 4,554 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* $Id$
 *
 * OpenMAMA: The open middleware agnostic messaging API
 * Copyright (C) 2011 NYSE Technologies, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#ifndef _WOMBAT_WINTERLOCKED_H
#define _WOMBAT_WINTERLOCKED_H

/*
 * This file contains a set of interlock functions for atomic access on scalar
 * variables.
 */

#include "wCommon.h"

/* *************************************************** */
/* Linux implementation. */
/* *************************************************** */

#if defined(__i386__) || defined(__x86_64__)

/* 32-bit atomic exchange. Returns previous value. */
static __inline__ uint32_t
axchg32 (uint32_t* ptr, uint32_t newVal)
{
    __asm__ volatile ("xchgl %k0, %1"
                      : "=r" (newVal)
                      : "m" (*ptr), "0" (newVal)
                      : "memory");
    return newVal;
}

/* 32-bit atomic increment. */
static __inline__ void
ainc32 (uint32_t* ptr)
{
    __asm__ volatile ("lock; incl %0"
                      : "=m" (*ptr)
                      : "m" (*ptr));
}

/* 32-bit atomic decrement. */
static __inline__ void
adec32 (uint32_t* ptr)
{
    __asm__ volatile ("lock; decl %0"
                      : "=m" (*ptr)
                      : "m" (*ptr));
}

#else

#ifdef __GNUC__

/* 32-bit atomic exchange. Returns previous value. */
static __inline__ uint32_t
axchg32 (uint32_t* ptr, uint32_t newVal)
{
    /* Warning: behavior not guaranteed to be consistent
       on all platforms.  Unit tests may be required to validate
       on each build.
       http://gcc.gnu.org/onlinedocs/gcc-4.3.5/gcc/Atomic-Builtins.html
    */
    return __sync_lock_test_and_set(ptr, newVal);
}

/* 32-bit atomic increment. */
static __inline__ void
ainc32 (uint32_t* ptr)
{
    __sync_fetch_and_add(ptr, 1);
}

/* 32-bit atomic decrement. */
static __inline__ void
adec32 (uint32_t* ptr)
{
   __sync_fetch_and_sub(ptr, 1);
}

#else
#error "Unsupported CPU / compiler combination, please implement atomic functions"
#endif


#endif

/* *************************************************** */
/* Type Defines. */
/* *************************************************** */
/* The structure on linux. */
typedef uint32_t wInterlockedInt;

/**
 * This function will initialise a wInterlockedInt.
 *
 * @param[in] value Pointer to the item to be initialized.
 * @return 0 on success.
 */

WCOMMONINLINE int wInterlocked_initialize(wInterlockedInt *value)
{
    return 0;
}

/**
 * This function will destroy a wInterlockedInt.
 *
 * @param[in] value Pointer to the item to be destroyed.
 * @return 0 on success.
 */
WCOMMONINLINE int wInterlocked_destroy(wInterlockedInt *value)
{
    return 0;
}

/**
 * This function will atomically decrement a 32-bit integer value.
 *
 * @param[in] value Pointer to the value to be decremented.
 * @return The decremented integer.
 */
WCOMMONINLINE int wInterlocked_decrement(wInterlockedInt *value)
{
    adec32(value);
    return (int)*value;
}

/**
 * This function will atomically increment a 32-bit integer value.
 *
 * @param[in] value Pointer to the value to be incremented.
 * @return The incremented integer.
 */
WCOMMONINLINE int wInterlocked_increment(wInterlockedInt *value)
{
    ainc32(value);
    return (int)*value;
}

/**
 * This function will return the value of the interlocked variable.
 *
 * @param[in] value Pointer to the value to be read.
 * @return The value itself.
 */
WCOMMONINLINE int wInterlocked_read(wInterlockedInt *value)
{
    return (int)*value;
}

/**
 * This function will atomically set a 32-bit integer value.
 *
 * @param[in] newValue The new value to set.
 * @param[in] value Pointer to the value to be set.
 * @return The updated integer.
 */
WCOMMONINLINE int wInterlocked_set(int newValue, wInterlockedInt *value)
{
    axchg32(value, (uint32_t)newValue);
    return (int)*value;
}

#endif /* _WOMBAT_WINTERLOCKED_H */