Allow package to be build on any architecture (not just i386).
[php5-apc.git] / arch / i386 / atomic.h
1 /*
2   +----------------------------------------------------------------------+
3   | APC                                                                  |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2006 The PHP Group                                     |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Brian Shire <shire@php.net>                                 |
16   +----------------------------------------------------------------------+
17
18  */
19
20 /* $Id: atomic.h,v 1.1 2006/09/29 07:13:01 shire Exp $ */
21
22
23 #include <sys/syscall.h>
24 #include <sys/time.h>
25 #include <linux/futex.h>
26
27 /* int sys_futex (void *futex, int op, int val, const struct timespec *timeout); */
28 static inline long int apc_sys_futex(void *futex, int op, int val, const struct timespec *timeout) {
29
30   long int ret;
31
32   /* i386 system calls are performed with nt 80h operation.
33    *  the argument order is a, b, c, d, S, D  
34    */ 
35   asm volatile ("int $0x80" 
36        : "=a" (ret)
37        : "0" (SYS_futex), 
38          "b" (futex), 
39          "c" (op), 
40          "d" (val),
41          "S" (timeout)
42        : "memory"
43       );
44
45   return ret;
46
47
48
49
50 static inline int apc_cmpxchg(volatile int *ptr, int old, int new) {
51
52     int prev;
53
54     asm volatile ("LOCK cmpxchgl %1, %2"
55                    : "=a" (prev)
56                    : "r" (new), 
57                      "m" (*(ptr)), 
58                      "0"(old)
59                    : "memory", "cc"
60                  );
61
62     return prev;
63 }
64
65 static inline int apc_xchg(volatile int *ptr, int new) {
66
67   int ret;
68   
69   asm volatile ("LOCK xchgl %[new], %[ptr]"
70                 : "=a" (ret)
71                 : [new] "0" (new), 
72                   [ptr] "m" (*(ptr))
73                 : "memory"
74                );
75
76   return ret;
77   
78 }
79