source: trunk/libdjvu/atomic.h @ 426

Last change on this file since 426 was 280, checked in by rbri, 12 years ago

DJVU plugin: djvulibre updated to version 3.5.22

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1/* -*- C -*-
2// -------------------------------------------------------------------
3// MiniLock - a quick user space lock
4// Copyright (c) 2008  Leon Bottou. All rights reserved
5//
6// Permission is hereby granted, free of charge, to any person obtaining
7// a copy of this software and associated documentation files (the
8// "Software"), to deal in the Software without restriction, including
9// without limitation the rights to use, copy, modify, merge, publish,
10// distribute, sublicense, and/or sell copies of the Software, and to
11// permit persons to whom the Software is furnished to do so, subject to
12// the following conditions:
13//
14// The above copyright notice and this permission notice shall be
15// included in all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24//
25// ------------------------------------------------------------------- */
26
27#ifndef ATOMIC_H
28#define ATOMIC_H
29
30/* ------------------------------------------------------------
31//  These are primitives to implement very quick locks.
32// 
33//  Non-blocking usage:
34//    if (atomicAcquire(&lock)) {
35//      ... // do something protected by the lock
36//      atomicRelease(&lock);
37//    }
38//
39//  Blocking usage:
40//    atomicAcquireOrSpin(&lock);
41//    // do something protected by the lock
42//    atomicRelease(&lock);
43//
44//  Rules of thumb:
45//  - Acquire and release from the same function with
46//    no intervening function calls.
47//  - Do not use AcquireOrSpin for waiting a long time.
48//    No more than a few microseconds please.
49//
50//  Memory ordering:
51//  Viewed from another processor
52//  - load/stores performed by this cpu after the acquire
53//    cannot appear to have happened before the acquire.
54//  - load/stores performed by this cpu before the release
55//    cannot appear to have happened after the release.
56//
57//  Implementation:
58//  All depends on the definitions from the initial include file.
59//  To perform the non blocking operations:
60//  - use intel builtins if available (icc, gcc>=4.1).
61//  - use win32 interlocked operations (win32).
62//  - use inline assembly code for some platforms.
63//  - use pthreads
64//  To perform the waiting when spinning takes to long:
65//  - use win32 critical sections and events.
66//  - use pthreads mutex and conditions.
67//  This is controlled by the preprocessor symbols:
68//    WIN32
69//    __GNUC__ __GNUC_MAJOR__ __GNUC_MINOR__ 
70//    __INTEL_COMPILER
71//  and can be overriden by defining
72//    HAVE_INTEL_ATOMIC_BUILTINS
73//    OBEY_HAVE_INTEL_ATOMIC_BUILTINS
74//  and by tweaking the files include in atomic.h.
75// ------------------------------------------------------------ */
76
77
78# ifdef __cplusplus
79extern "C" {
80#endif
81 
82/* { int tmp = *lock; *lock = 1; return !tmp; }. */
83int atomicAcquire(int volatile *lock);
84 
85/* { while (!atomicAcquire(lock)) { spin/yield/wait } } */
86void atomicAcquireOrSpin(int volatile *lock);
87
88/* { *lock = 0; } */
89void atomicRelease(int volatile *lock);
90
91/* { *var += 1; return *var; } */
92int atomicIncrement(int volatile *var);
93
94/* { *var -= 1; return *var; } */
95int atomicDecrement(int volatile *var);
96
97/* { if (*var == oldval) { *var = newval; return TRUE; } return FALSE; } */
98int atomicCompareAndSwap(int volatile *var, int oldval, int newval);
99
100
101# ifdef __cplusplus
102}
103#endif
104
105#endif
Note: See TracBrowser for help on using the repository browser.