uatomic: update atomic set/read, use STORE_SHARED/LOAD_SHARED
[urcu.git] / compat_arch_x86.c
1 /*
2 * compat_arch_x86.c
3 *
4 * Userspace RCU library - x86 compatibility checks
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library 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 GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdio.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <assert.h>
27 #include <urcu/uatomic_arch.h>
28
29 /*
30 * It does not really matter if the constructor is called before using
31 * the library, as long as the caller checks if __urcu_cas_avail < 0 and calls
32 * compat_arch_init() explicitely if needed.
33 */
34 int __attribute__((constructor)) __urcu_cas_init(void);
35
36 static pthread_mutex_t compat_mutex = PTHREAD_MUTEX_INITIALIZER;
37
38 /*
39 * -1: unknown
40 * 1: available
41 * 0: unavailable
42 */
43 int __urcu_cas_avail = -1;
44
45 /*
46 * Imported from glibc 2.3.5. linuxthreads/sysdeps/i386/pt-machine.h.
47 */
48
49 int get_eflags (void)
50 {
51 int res;
52 __asm__ __volatile__ ("pushfl; popl %0" : "=r" (res) : );
53 return res;
54 }
55
56 void set_eflags (int newflags)
57 {
58 __asm__ __volatile__ ("pushl %0; popfl" : : "r" (newflags) : "cc");
59 }
60
61 int compare_and_swap_is_available (void)
62 {
63 int oldflags = get_eflags ();
64 int changed;
65 /* Flip AC bit in EFLAGS. */
66 set_eflags (oldflags ^ 0x40000);
67 /* See if bit changed. */
68 changed = (get_eflags () ^ oldflags) & 0x40000;
69 /* Restore EFLAGS. */
70 set_eflags (oldflags);
71 /* If the AC flag did not change, it's a 386 and it lacks cmpxchg.
72 Otherwise, it's a 486 or above and it has cmpxchg. */
73 return changed != 0;
74 }
75
76 unsigned long _compat_uatomic_cmpxchg(void *addr, unsigned long old,
77 unsigned long _new, int len)
78 {
79 sigset_t newmask, oldmask;
80 int ret;
81
82 /* Disable signals */
83 ret = sigemptyset(&newmask);
84 assert(!ret);
85 ret = pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
86 assert(!ret);
87 ret = pthread_mutex_lock(&compat_mutex);
88 assert(!ret);
89
90 switch (len) {
91 case 1:
92 {
93 unsigned char result = *(unsigned char *)addr;
94 if (result == old)
95 *(unsigned char *)addr = (unsigned char)_new;
96 return result;
97 }
98 case 2:
99 {
100 unsigned short result = *(unsigned short *)addr;
101 if (result == old)
102 *(unsigned short *)addr = (unsigned short)_new;
103 return result;
104 }
105 case 4:
106 {
107 unsigned int result = *(unsigned int *)addr;
108 if (result == old)
109 *(unsigned int *)addr = (unsigned int)_new;
110 return result;
111 }
112 }
113 /* generate an illegal instruction. Cannot catch this with linker tricks
114 * when optimizations are disabled. */
115 __asm__ __volatile__("ud2");
116 return 0;
117
118 ret = pthread_mutex_unlock(&compat_mutex);
119 assert(!ret);
120 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
121 assert(!ret);
122 }
123
124 int __urcu_cas_init(void)
125 {
126 if (__urcu_cas_avail < 0)
127 __urcu_cas_avail = compare_and_swap_is_available();
128 return __urcu_cas_avail;
129 }
This page took 0.030852 seconds and 4 git commands to generate.