viratomic.h 12.9 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 37 38 39
/**
 * virAtomicIntGet:
 * Gets the current value of atomic.
 *
 * This call acts as a full compiler and hardware memory barrier
 * (before the get)
 */
int virAtomicIntGet(volatile int *atomic)
    ATTRIBUTE_NONNULL(1);
40

41 42 43 44 45 46 47 48 49 50
/**
 * virAtomicIntSet:
 * Sets the value of atomic to newval.
 *
 * This call acts as a full compiler and hardware memory barrier
 * (after the set)
 */
void virAtomicIntSet(volatile int *atomic,
                     int newval)
    ATTRIBUTE_NONNULL(1);
51

52 53 54 55 56 57 58 59 60 61 62
/**
 * 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.
 */
int virAtomicIntInc(volatile int *atomic)
    ATTRIBUTE_NONNULL(1);
63

64 65 66 67 68 69 70 71 72 73
/**
 * 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.
 */
bool virAtomicIntDecAndTest(volatile int *atomic)
74
    ATTRIBUTE_NONNULL(1);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

/**
 * 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.
 */
bool virAtomicIntCompareExchange(volatile int *atomic,
                                 int oldval,
                                 int newval)
92
    ATTRIBUTE_NONNULL(1);
93 94 95 96 97 98 99 100 101 102 103 104

/**
 * 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.
 */
int virAtomicIntAdd(volatile int *atomic,
                    int val)
105
    ATTRIBUTE_NONNULL(1);
106 107 108 109 110 111 112 113 114 115 116 117 118

/**
 * 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; }
 */
unsigned int virAtomicIntAnd(volatile unsigned int *atomic,
                             unsigned int val)
119
    ATTRIBUTE_NONNULL(1);
120 121 122 123 124 125 126 127 128 129 130 131 132

/**
 * 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.
 */
unsigned int virAtomicIntOr(volatile unsigned int *atomic,
                            unsigned int val)
133
    ATTRIBUTE_NONNULL(1);
134 135 136 137 138 139 140 141 142 143 144 145 146

/**
 * 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.
 */
unsigned int virAtomicIntXor(volatile unsigned int *atomic,
                             unsigned int val)
147 148 149
    ATTRIBUTE_NONNULL(1);


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 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
# 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
223

224 225 226 227 228
/*
 * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
 */
inline int
virAtomicIntGet(volatile int *atomic)
229
{
230 231
    MemoryBarrier();
    return *atomic;
232 233
}

234 235 236
inline void
virAtomicIntSet(volatile int *atomic,
                int newval)
237
{
238 239 240
    *atomic = newval;
    MemoryBarrier();
}
241

242 243 244 245 246
inline int
virAtomicIntInc(volatile int *atomic)
{
    return InterlockedIncrement((volatile LONG *)atomic);
}
247

248 249 250 251 252
inline bool
virAtomicIntDecAndTest(volatile int *atomic)
{
    return InterlockedDecrement((volatile LONG *)atomic) == 0;
}
253

254 255 256 257 258 259 260
inline bool
virAtomicIntCompareExchange(volatile int *atomic,
                            int oldval,
                            int newval)
{
    return InterlockedCompareExchange((volatile LONG *)atomic, newval, oldval) == oldval;
}
261

262 263 264 265 266
inline int
virAtomicIntAdd(volatile int *atomic,
                int val)
{
    return InterlockedExchangeAdd((volatile LONG *)atomic, val);
267 268
}

269 270 271
inline unsigned int
virAtomicIntAnd(volatile unsigned int *atomic,
                unsigned int val)
272
{
273 274
    return InterlockedAnd((volatile LONG *)atomic, val);
}
275

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

283 284 285 286 287 288
inline unsigned int
virAtomicIntXor(volatile unsigned int *atomic,
                unsigned int val)
{
    return InterlockedXor((volatile LONG *)atomic, val);
}
289 290


291 292 293
#  else
#   ifdef VIR_ATOMIC_OPS_PTHREAD
#    include <pthread.h>
294

295
extern pthread_mutex_t virAtomicLock;
296

297 298 299
inline int
virAtomicIntGet(volatile int *atomic)
{
300 301
    int value;

302 303 304 305 306 307 308 309 310 311
    pthread_mutex_lock(&virAtomicLock);
    value = *atomic;
    pthread_mutex_unlock(&virAtomicLock);

    return value;
}

inline void
virAtomicIntSet(volatile int *atomic,
                int value)
312
{
313 314 315
    pthread_mutex_lock(&virAtomicLock);
    *atomic = value;
    pthread_mutex_unlock(&virAtomicLock);
316 317
}

318 319
inline int
virAtomicIntInc(volatile int *atomic)
320
{
321 322 323 324 325 326 327
    int value;

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

    return value;
328 329
}

330 331
inline bool
virAtomicIntDecAndTest(volatile int *atomic)
332
{
333 334 335 336 337 338 339
    bool is_zero;

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

    return is_zero;
340 341
}

342 343 344 345 346 347
inline bool
virAtomicIntCompareExchange(volatile int *atomic,
                            int oldval,
                            int newval)
{
    bool success;
348

349
    pthread_mutex_lock(&virAtomicLock);
350

351 352
    if ((success = (*atomic == oldval)))
        *atomic = newval;
353

354
    pthread_mutex_unlock(&virAtomicLock);
355

356 357
    return success;
}
358

359 360 361
inline int
virAtomicIntAdd(volatile int *atomic,
                int val)
362
{
363 364 365 366 367 368 369 370
    int oldval;

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

    return oldval;
371 372
}

373 374 375
inline unsigned int
virAtomicIntAnd(volatile unsigned int *atomic,
                unsigned int val)
376
{
377 378 379 380 381 382 383 384
    unsigned int oldval;

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

    return oldval;
385 386
}

387 388 389
inline unsigned int
virAtomicIntOr(volatile unsigned int *atomic,
               unsigned int val)
390
{
391 392 393 394 395 396 397 398
    unsigned int oldval;

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

    return oldval;
399 400
}

401 402 403
inline unsigned int
virAtomicIntXor(volatile unsigned int *atomic,
                unsigned int val)
404
{
405 406 407 408 409 410 411 412
    unsigned int oldval;

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

    return oldval;
413 414
}

415 416 417 418 419 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

#   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

447
#endif /* __VIR_ATOMIC_H */