viratomic.h 13.4 KB
Newer Older
1 2 3
/*
 * viratomic.h: atomic integer operations
 *
4
 * Copyright (C) 2012 Red Hat, Inc.
5
 *
6 7 8
 * Based on code taken from GLib 2.32, under the LGPLv2+
 *
 * Copyright (C) 2011 Ryan Lortie
9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
O
Osier Yang 已提交
21 22
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
23 24 25 26 27 28
 *
 */

#ifndef __VIR_ATOMIC_H__
# define __VIR_ATOMIC_H__

29
# include "internal.h"
30

31 32 33 34 35 36
# ifdef VIR_ATOMIC_OPS_GCC
#  define VIR_STATIC /* Nothing; we just never define the functions */
# else
#  define VIR_STATIC static
# endif

37 38 39 40 41 42 43
/**
 * virAtomicIntGet:
 * Gets the current value of atomic.
 *
 * This call acts as a full compiler and hardware memory barrier
 * (before the get)
 */
44
VIR_STATIC int virAtomicIntGet(volatile int *atomic)
45
    ATTRIBUTE_NONNULL(1);
46

47 48 49 50 51 52 53
/**
 * virAtomicIntSet:
 * Sets the value of atomic to newval.
 *
 * This call acts as a full compiler and hardware memory barrier
 * (after the set)
 */
54
VIR_STATIC void virAtomicIntSet(volatile int *atomic,
55
                                int newval)
56
    ATTRIBUTE_NONNULL(1);
57

58 59 60 61 62 63 64 65 66
/**
 * virAtomicIntInc:
 * Increments the value of atomic by 1.
 *
 * Think of this operation as an atomic version of
 * { *atomic += 1; return *atomic; }
 *
 * This call acts as a full compiler and hardware memory barrier.
 */
67
VIR_STATIC int virAtomicIntInc(volatile int *atomic)
68
    ATTRIBUTE_NONNULL(1);
69

70 71 72 73 74 75 76 77 78
/**
 * virAtomicIntDecAndTest:
 * Decrements the value of atomic by 1.
 *
 * Think of this operation as an atomic version of
 * { *atomic -= 1; return *atomic == 0; }
 *
 * This call acts as a full compiler and hardware memory barrier.
 */
79
VIR_STATIC bool virAtomicIntDecAndTest(volatile int *atomic)
80
    ATTRIBUTE_NONNULL(1);
81 82 83 84 85 86 87 88 89 90 91 92 93 94

/**
 * virAtomicIntCompareExchange:
 * Compares atomic to oldval and, if equal, sets it to newval. If
 * atomic was not equal to oldval then no change occurs.
 *
 * This compare and exchange is done atomically.
 *
 * Think of this operation as an atomic version of
 * { if (*atomic == oldval) { *atomic = newval; return true; }
 *    else return false; }
 *
 * This call acts as a full compiler and hardware memory barrier.
 */
95
VIR_STATIC bool virAtomicIntCompareExchange(volatile int *atomic,
96 97
                                            int oldval,
                                            int newval)
98
    ATTRIBUTE_NONNULL(1);
99 100 101 102 103 104 105 106 107 108

/**
 * virAtomicIntAdd:
 * Atomically adds val to the value of atomic.
 *
 * Think of this operation as an atomic version of
 * { tmp = *atomic; *atomic += val; return tmp; }
 *
 * This call acts as a full compiler and hardware memory barrier.
 */
109
VIR_STATIC int virAtomicIntAdd(volatile int *atomic,
110
                               int val)
111
    ATTRIBUTE_NONNULL(1);
112 113 114 115 116 117 118 119 120 121 122

/**
 * virAtomicIntAnd:
 * Performs an atomic bitwise 'and' of the value of atomic
 * and val, storing the result back in atomic.
 *
 * This call acts as a full compiler and hardware memory barrier.
 *
 * Think of this operation as an atomic version of
 * { tmp = *atomic; *atomic &= val; return tmp; }
 */
123
VIR_STATIC unsigned int virAtomicIntAnd(volatile unsigned int *atomic,
124
                                        unsigned int val)
125
    ATTRIBUTE_NONNULL(1);
126 127 128 129 130 131 132 133 134 135 136

/**
 * virAtomicIntOr:
 * Performs an atomic bitwise 'or' of the value of atomic
 * and val, storing the result back in atomic.
 *
 * Think of this operation as an atomic version of
 * { tmp = *atomic; *atomic |= val; return tmp; }
 *
 * This call acts as a full compiler and hardware memory barrier.
 */
137
VIR_STATIC unsigned int virAtomicIntOr(volatile unsigned int *atomic,
138
                                       unsigned int val)
139
    ATTRIBUTE_NONNULL(1);
140 141 142 143 144 145 146 147 148 149 150

/**
 * virAtomicIntXor:
 * Performs an atomic bitwise 'xor' of the value of atomic
 * and val, storing the result back in atomic.
 *
 * Think of this operation as an atomic version of
 * { tmp = *atomic; *atomic ^= val; return tmp; }
 *
 * This call acts as a full compiler and hardware memory barrier.
 */
151
VIR_STATIC unsigned int virAtomicIntXor(volatile unsigned int *atomic,
152
                                        unsigned int val)
153 154
    ATTRIBUTE_NONNULL(1);

155
# undef VIR_STATIC
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
# ifdef VIR_ATOMIC_OPS_GCC

#  define virAtomicIntGet(atomic)                                       \
    (__extension__ ({                                                   \
            (void)verify_true(sizeof(*(atomic)) == sizeof(int));        \
            (void)(0 ? *(atomic) ^ *(atomic) : 0);                      \
            __sync_synchronize();                                       \
            (int)*(atomic);                                             \
        }))
#  define virAtomicIntSet(atomic, newval)                               \
    (__extension__ ({                                                   \
            (void)verify_true(sizeof(*(atomic)) == sizeof(int));        \
            (void)(0 ? *(atomic) ^ (newval) : 0);                       \
            *(atomic) = (newval);                                       \
            __sync_synchronize();                                       \
        }))
#  define virAtomicIntInc(atomic)                                       \
    (__extension__ ({                                                   \
            (void)verify_true(sizeof(*(atomic)) == sizeof(int));        \
            (void)(0 ? *(atomic) ^ *(atomic) : 0);                      \
            __sync_add_and_fetch((atomic), 1);                          \
        }))
#  define virAtomicIntDecAndTest(atomic)                                \
    (__extension__ ({                                                   \
            (void)verify_true(sizeof(*(atomic)) == sizeof(int));        \
            (void)(0 ? *(atomic) ^ *(atomic) : 0);                      \
            __sync_fetch_and_sub((atomic), 1) == 1;                     \
        }))
#  define virAtomicIntCompareExchange(atomic, oldval, newval)           \
    (__extension__ ({                                                   \
            (void)verify_true(sizeof(*(atomic)) == sizeof(int));        \
            (void)(0 ? *(atomic) ^ (newval) ^ (oldval) : 0);            \
            (bool)__sync_bool_compare_and_swap((atomic),                \
                                               (oldval), (newval));     \
        }))
#  define virAtomicIntAdd(atomic, val)                                  \
    (__extension__ ({                                                   \
            (void)verify_true(sizeof(*(atomic)) == sizeof(int));        \
            (void)(0 ? *(atomic) ^ (val) : 0);                          \
            (int) __sync_fetch_and_add((atomic), (val));                \
        }))
#  define virAtomicIntAnd(atomic, val)                                  \
    (__extension__ ({                                                   \
            (void)verify_true(sizeof(*(atomic)) == sizeof(int));              \
            (void) (0 ? *(atomic) ^ (val) : 0);                         \
            (unsigned int) __sync_fetch_and_and((atomic), (val));       \
        }))
#  define virAtomicIntOr(atomic, val)                                   \
    (__extension__ ({                                                   \
            (void)verify_true(sizeof(*(atomic)) == sizeof(int));              \
            (void) (0 ? *(atomic) ^ (val) : 0);                         \
            (unsigned int) __sync_fetch_and_or((atomic), (val));        \
        }))
#  define virAtomicIntXor(atomic, val)                                  \
    (__extension__ ({                                                   \
            (void)verify_true(sizeof(*(atomic)) == sizeof(int));              \
            (void) (0 ? *(atomic) ^ (val) : 0);                         \
            (unsigned int) __sync_fetch_and_xor((atomic), (val));       \
        }))


# else

#  ifdef VIR_ATOMIC_OPS_WIN32

#   include <winsock2.h>
#   include <windows.h>
#   include <intrin.h>
#   if !defined(_M_AMD64) && !defined (_M_IA64) && !defined(_M_X64)
#    define InterlockedAnd _InterlockedAnd
#    define InterlockedOr _InterlockedOr
#    define InterlockedXor _InterlockedXor
#   endif
230

231 232 233
/*
 * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
 */
234
static inline int
235
virAtomicIntGet(volatile int *atomic)
236
{
237 238
    MemoryBarrier();
    return *atomic;
239 240
}

241
static inline void
242 243
virAtomicIntSet(volatile int *atomic,
                int newval)
244
{
245 246 247
    *atomic = newval;
    MemoryBarrier();
}
248

249
static inline int
250 251 252 253
virAtomicIntInc(volatile int *atomic)
{
    return InterlockedIncrement((volatile LONG *)atomic);
}
254

255
static inline bool
256 257 258 259
virAtomicIntDecAndTest(volatile int *atomic)
{
    return InterlockedDecrement((volatile LONG *)atomic) == 0;
}
260

261
static inline bool
262 263 264 265 266 267
virAtomicIntCompareExchange(volatile int *atomic,
                            int oldval,
                            int newval)
{
    return InterlockedCompareExchange((volatile LONG *)atomic, newval, oldval) == oldval;
}
268

269
static inline int
270 271 272 273
virAtomicIntAdd(volatile int *atomic,
                int val)
{
    return InterlockedExchangeAdd((volatile LONG *)atomic, val);
274 275
}

276
static inline unsigned int
277 278
virAtomicIntAnd(volatile unsigned int *atomic,
                unsigned int val)
279
{
280 281
    return InterlockedAnd((volatile LONG *)atomic, val);
}
282

283
static inline unsigned int
284 285 286 287 288
virAtomicIntOr(volatile unsigned int *atomic,
               unsigned int val)
{
    return InterlockedOr((volatile LONG *)atomic, val);
}
289

290
static inline unsigned int
291 292 293 294 295
virAtomicIntXor(volatile unsigned int *atomic,
                unsigned int val)
{
    return InterlockedXor((volatile LONG *)atomic, val);
}
296 297


298 299 300
#  else
#   ifdef VIR_ATOMIC_OPS_PTHREAD
#    include <pthread.h>
301

302
extern pthread_mutex_t virAtomicLock;
303

304
static inline int
305 306
virAtomicIntGet(volatile int *atomic)
{
307 308
    int value;

309 310 311 312 313 314 315
    pthread_mutex_lock(&virAtomicLock);
    value = *atomic;
    pthread_mutex_unlock(&virAtomicLock);

    return value;
}

316
static inline void
317 318
virAtomicIntSet(volatile int *atomic,
                int value)
319
{
320 321 322
    pthread_mutex_lock(&virAtomicLock);
    *atomic = value;
    pthread_mutex_unlock(&virAtomicLock);
323 324
}

325
static inline int
326
virAtomicIntInc(volatile int *atomic)
327
{
328 329 330 331 332 333 334
    int value;

    pthread_mutex_lock(&virAtomicLock);
    value = ++(*atomic);
    pthread_mutex_unlock(&virAtomicLock);

    return value;
335 336
}

337
static inline bool
338
virAtomicIntDecAndTest(volatile int *atomic)
339
{
340 341 342 343 344 345 346
    bool is_zero;

    pthread_mutex_lock(&virAtomicLock);
    is_zero = --(*atomic) == 0;
    pthread_mutex_unlock(&virAtomicLock);

    return is_zero;
347 348
}

349
static inline bool
350 351 352 353 354
virAtomicIntCompareExchange(volatile int *atomic,
                            int oldval,
                            int newval)
{
    bool success;
355

356
    pthread_mutex_lock(&virAtomicLock);
357

358 359
    if ((success = (*atomic == oldval)))
        *atomic = newval;
360

361
    pthread_mutex_unlock(&virAtomicLock);
362

363 364
    return success;
}
365

366
static inline int
367 368
virAtomicIntAdd(volatile int *atomic,
                int val)
369
{
370 371 372 373 374 375 376 377
    int oldval;

    pthread_mutex_lock(&virAtomicLock);
    oldval = *atomic;
    *atomic = oldval + val;
    pthread_mutex_unlock(&virAtomicLock);

    return oldval;
378 379
}

380
static inline unsigned int
381 382
virAtomicIntAnd(volatile unsigned int *atomic,
                unsigned int val)
383
{
384 385 386 387 388 389 390 391
    unsigned int oldval;

    pthread_mutex_lock(&virAtomicLock);
    oldval = *atomic;
    *atomic = oldval & val;
    pthread_mutex_unlock(&virAtomicLock);

    return oldval;
392 393
}

394
static inline unsigned int
395 396
virAtomicIntOr(volatile unsigned int *atomic,
               unsigned int val)
397
{
398 399 400 401 402 403 404 405
    unsigned int oldval;

    pthread_mutex_lock(&virAtomicLock);
    oldval = *atomic;
    *atomic = oldval | val;
    pthread_mutex_unlock(&virAtomicLock);

    return oldval;
406 407
}

408
static inline unsigned int
409 410
virAtomicIntXor(volatile unsigned int *atomic,
                unsigned int val)
411
{
412 413 414 415 416 417 418 419
    unsigned int oldval;

    pthread_mutex_lock(&virAtomicLock);
    oldval = *atomic;
    *atomic = oldval ^ val;
    pthread_mutex_unlock(&virAtomicLock);

    return oldval;
420 421
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

#   else
#    error "No atomic integer impl for this platform"
#   endif
#  endif

/* The int/unsigned int casts here ensure that you can
 * pass either an int or unsigned int to all atomic op
 * functions, in the same way that we can with GCC
 * atomic op helpers.
 */
#  define virAtomicIntGet(atomic)               \
    virAtomicIntGet((int *)atomic)
#  define virAtomicIntSet(atomic, val)          \
    virAtomicIntSet((int *)atomic, val)
#  define virAtomicIntInc(atomic)               \
    virAtomicIntInc((int *)atomic)
#  define virAtomicIntDecAndTest(atomic)        \
    virAtomicIntDecAndTest((int *)atomic)
#  define virAtomicIntCompareExchange(atomic, oldval, newval)   \
    virAtomicIntCompareExchange((int *)atomic, oldval, newval)
#  define virAtomicIntAdd(atomic, val)          \
    virAtomicIntAdd((int *)atomic, val)
#  define virAtomicIntAnd(atomic, val)          \
    virAtomicIntAnd((unsigned int *)atomic, val)
#  define virAtomicIntOr(atomic, val)           \
    virAtomicIntOr((unsigned int *)atomic, val)
#  define virAtomicIntXor(atomic, val)          \
    virAtomicIntXor((unsigned int *)atomic, val)

# endif

454
#endif /* __VIR_ATOMIC_H */