~ubuntu-branches/ubuntu/utopic/rhythmbox/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/rb-atomic.c

Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * arch-tag: Implementation of atomic integers for Rhythmbox
3
 
 * Copyright (C) 2002, 2003  Red Hat, Inc.
4
 
 * Copyright (C) 2003 CodeFactory AB
5
 
 *
6
 
 * Licensed under the Academic Free License version 1.2
7
 
 * 
8
 
 * This program is free software; you can redistribute it and/or modify
9
 
 * it under the terms of the GNU General Public License as published by
10
 
 * the Free Software Foundation; either version 2 of the License, or
11
 
 * (at your option) any later version.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU General Public License for more details.
17
 
 * 
18
 
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program; if not, write to the Free Software
20
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
 
 *
22
 
 */
23
 
 
24
 
#include "rb-atomic.h"
25
 
 
26
 
#ifdef HAVE_GTK_2_3
27
 
#error "rb-atomic shouldn't be compiled with GTK+ 2.4, please file a bug on bugzilla.gnome.org"
28
 
#else
29
 
 
30
 
#ifdef RB_USE_ATOMIC_INT_486
31
 
/* Taken from CVS version 1.7 of glibc's sysdeps/i386/i486/atomicity.h */
32
 
/* Since the asm stuff here is gcc-specific we go ahead and use "inline" also */
33
 
static inline gint32
34
 
atomic_exchange_and_add (RBAtomic *atomic,
35
 
                         volatile gint32 val)
36
 
{
37
 
  register gint32 result;
38
 
 
39
 
  __asm__ __volatile__ ("lock; xaddl %0,%1"
40
 
                        : "=r" (result), "=m" (atomic->value)
41
 
                        : "0" (val), "m" (atomic->value));
42
 
  return result;
43
 
}
44
 
#else
45
 
GStaticMutex rb_atomic_mutex = G_STATIC_MUTEX_INIT;
46
 
#endif
47
 
 
48
 
/**
49
 
 * Atomically increments an integer
50
 
 *
51
 
 * @param atomic pointer to the integer to increment
52
 
 * @returns the value before incrementing
53
 
 *
54
 
 * @todo implement arch-specific faster atomic ops
55
 
 */
56
 
gint32
57
 
rb_atomic_inc (RBAtomic *atomic)
58
 
{
59
 
#ifdef RB_USE_ATOMIC_INT_486
60
 
  return atomic_exchange_and_add (atomic, 1);
61
 
#else
62
 
  gint32 res;
63
 
  g_static_mutex_lock (&rb_atomic_mutex);
64
 
  res = atomic->value;
65
 
  atomic->value += 1;
66
 
  g_static_mutex_unlock (&rb_atomic_mutex);
67
 
  return res;
68
 
#endif
69
 
}
70
 
 
71
 
/**
72
 
 * Atomically decrement an integer
73
 
 *
74
 
 * @param atomic pointer to the integer to decrement
75
 
 * @returns the value before decrementing
76
 
 *
77
 
 * @todo implement arch-specific faster atomic ops
78
 
 */
79
 
gint32
80
 
rb_atomic_dec (RBAtomic *atomic)
81
 
{
82
 
#ifdef RB_USE_ATOMIC_INT_486
83
 
  return atomic_exchange_and_add (atomic, -1);
84
 
#else
85
 
  gint32 res;
86
 
  
87
 
  g_static_mutex_lock (&rb_atomic_mutex);
88
 
  res = atomic->value;
89
 
  atomic->value -= 1;
90
 
  g_static_mutex_unlock (&rb_atomic_mutex);
91
 
  return res;
92
 
#endif
93
 
}
94
 
 
95
 
#endif /* HAVE_GTK_2_3 */