Revert until we track down the RH9 bug
[mono.git] / mono / io-layer / atomic.c
1 /*
2  * atomic.c:  Workarounds for atomic operations for platforms that dont have
3  *            really atomic asm functions in atomic.h
4  *
5  * Author:
6  *      Dick Porter (dick@ximian.com)
7  *
8  * (C) 2002 Ximian, Inc.
9  */
10
11 #include <config.h>
12 #include <glib.h>
13 #include <pthread.h>
14
15 #include "mono/io-layer/wapi.h"
16
17 #ifndef WAPI_ATOMIC_ASM
18
19 static pthread_mutex_t spin;
20 static mono_once_t spin_once=MONO_ONCE_INIT;
21
22 static void spin_init(void)
23 {
24         pthread_mutex_init(&spin, 0);
25         g_warning("Using non-atomic functions!");
26 }
27
28 gint32 InterlockedCompareExchange(volatile gint32 *dest, gint32 exch,
29                                   gint32 comp)
30 {
31         gint32 old;
32         
33         mono_once(&spin_once, spin_init);
34         pthread_mutex_lock(&spin);
35         
36         old= *dest;
37         if(old==comp) {
38                 *dest=exch;
39         }
40         
41         pthread_mutex_unlock(&spin);
42
43         return(old);
44 }
45
46 gpointer InterlockedCompareExchangePointer(volatile gpointer *dest,
47                                            gpointer exch, gpointer comp)
48 {
49         gpointer old;
50         
51         mono_once(&spin_once, spin_init);
52         pthread_mutex_lock(&spin);
53         
54         old= *dest;
55         if(old==comp) {
56                 *dest=exch;
57         }
58         
59         pthread_mutex_unlock(&spin);
60
61         return(old);
62 }
63
64 gint32 InterlockedIncrement(volatile gint32 *dest)
65 {
66         gint32 ret;
67         
68         mono_once(&spin_once, spin_init);
69         pthread_mutex_lock(&spin);
70         
71         *dest++;
72         ret= *dest;
73         
74         pthread_mutex_unlock(&spin);
75         
76         return(ret);
77 }
78
79 gint32 InterlockedDecrement(volatile gint32 *dest)
80 {
81         gint32 ret;
82         
83         mono_once(&spin_once, spin_init);
84         pthread_mutex_lock(&spin);
85         
86         *dest--;
87         ret= *dest;
88         
89         pthread_mutex_unlock(&spin);
90         
91         return(ret);
92 }
93
94 gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch)
95 {
96         gint32 ret;
97         
98         mono_once(&spin_once, spin_init);
99         pthread_mutex_lock(&spin);
100
101         ret=*dest;
102         *dest=exch;
103         
104         pthread_mutex_unlock(&spin);
105         
106         return(ret);
107 }
108
109 gpointer InterlockedExchangePointer(volatile gpointer *dest, gpointer exch)
110 {
111         gpointer ret;
112         
113         mono_once(&spin_once, spin_init);
114         pthread_mutex_lock(&spin);
115         
116         ret=*dest;
117         *dest=exch;
118         
119         pthread_mutex_unlock(&spin);
120         
121         return(ret);
122 }
123
124 gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add)
125 {
126         gint32 ret;
127         
128         mono_once(&spin_once, spin_init);
129         pthread_mutex_lock(&spin);
130
131         ret= *dest;
132         *dest+=add;
133         
134         pthread_mutex_unlock(&spin);
135         
136         return(ret);
137 }
138
139 #endif