File: spinlock.h

package info (click to toggle)
graphicsmagick 1.3.20-3%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 38,472 kB
  • ctags: 14,330
  • sloc: ansic: 220,724; sh: 24,578; cpp: 18,199; perl: 3,365; makefile: 2,359; tcl: 2,208; python: 1,280
file content (95 lines) | stat: -rw-r--r-- 2,737 bytes parent folder | download | duplicates (4)
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
/*
  Copyright (C) 2003 GraphicsMagick Group
  Copyright (C) 2002 ImageMagick Studio
 
  This program is covered by multiple licenses, which are described in
  Copyright.txt. You should have received a copy of Copyright.txt with this
  package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
 
  ImageMagick Spinlock  Methods. These are highly lightweight functions that
  are used to prevent deadlocks on access to global data when the normal
  semaphore system has not yet been initialized. They are intended to only
  be used to protect operations that are very fast - like a flag that tells
  you that something has been inited, or bumping a counter. Do NOT use this
  for protecting an operation that will take any significant amount of time.
*/
#ifndef _SPINLOCKS_H
#define _SPINLOCKS_H

#if defined(MSWINDOWS)

#define SPINLOCK_DELAY_MILLI_SECS 10

static LONG volatile
  spinlock_mutex = 0;

/* Wait for spin lock */
static void _spinlock_wait (LONG volatile *sl)
{
  /*
    LONG InterlockedCompareExchange(LONG volatile* dest, LONG xchg,
    LONG compare)

    Performs an atomic compare-and-exchange operation on the specified
    values. The function compares two specified 32-bit values and
    exchanges with another 32-bit value based on the outcome of the
    comparison.

    If you are exchanging pointer values, this function has been
    superseded by the InterlockedCompareExchangePointer function.

    To operate on 64-bit values, use the InterlockedCompareExchange64
    function.
  */
  while (InterlockedCompareExchange (sl, 1, 0) != 0)
  {
    /* slight delay - just in case OS does not giveup CPU */
    Sleep (SPINLOCK_DELAY_MILLI_SECS);
  }
}
/* Release spin lock */
static void _spinlock_release (LONG volatile *sl)
{
  /*
    LONG InterlockedExchange (LONG volatile* dest, LONG val)

    Sets a 32-bit variable to the specified value as an atomic
    operation.

    To operate on a pointer variable, use the
    InterlockedExchangePointer function.
    
    To operate on a 64-bit variable, use the InterlockedExchange64
    function.
  */
  InterlockedExchange (sl, 0);
}
#define SPINLOCK_WAIT _spinlock_wait(&spinlock_mutex)
#define SPINLOCK_RELEASE _spinlock_release(&spinlock_mutex)

#else /* not MSWINDOWS */

#if defined(HAVE_PTHREAD)

static pthread_mutex_t
  spinlock_mutex = PTHREAD_MUTEX_INITIALIZER;
#define SPINLOCK_WAIT (void) pthread_mutex_lock(&spinlock_mutex)
#define SPINLOCK_RELEASE (void) pthread_mutex_unlock(&spinlock_mutex)

#else /* not HAVE_PTHREAD */

#define SPINLOCK_WAIT
#define SPINLOCK_RELEASE

#endif /* HAVE_PTHREAD */

#endif /* MSWINDOWS */
#endif /* _SPINLOCKS_H */

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 2
 * fill-column: 78
 * End:
 */