]>
git.proxmox.com Git - ceph.git/blob - ceph/src/include/atomic.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
13 * @author Sage Weil <sage@newdream.net>
20 # include "acconfig.h"
24 #include "include/Spinlock.h"
28 class atomic_spinlock_t
{
29 mutable ceph_spinlock_t lock
;
32 atomic_spinlock_t(T i
=0)
34 ceph_spin_init(&lock
);
36 ~atomic_spinlock_t() {
37 ceph_spin_destroy(&lock
);
40 ceph_spin_lock(&lock
);
42 ceph_spin_unlock(&lock
);
45 ceph_spin_lock(&lock
);
47 ceph_spin_unlock(&lock
);
51 ceph_spin_lock(&lock
);
53 ceph_spin_unlock(&lock
);
57 ceph_spin_lock(&lock
);
59 ceph_spin_unlock(&lock
);
62 ceph_spin_lock(&lock
);
64 ceph_spin_unlock(&lock
);
68 ceph_spin_lock(&lock
);
70 ceph_spin_unlock(&lock
);
73 bool compare_and_swap(T o
, T n
) {
75 ceph_spin_lock(&lock
);
80 ceph_spin_unlock(&lock
);
86 atomic_spinlock_t(const atomic_spinlock_t
<T
> &other
);
87 atomic_spinlock_t
&operator=(const atomic_spinlock_t
<T
> &rhs
);
93 // libatomic_ops implementation
94 #define AO_REQUIRE_CAS
95 #include <atomic_ops.h>
97 // reinclude our assert to clobber the system one
98 #include "include/assert.h"
104 atomic_t(AO_t i
=0) : val(i
) {}
109 return AO_fetch_and_add1(&val
) + 1;
112 return AO_fetch_and_sub1_write(&val
) - 1;
114 AO_t
add(AO_t add_me
) {
115 return AO_fetch_and_add(&val
, add_me
) + add_me
;
117 AO_t
sub(AO_t sub_me
) {
118 AO_t negsub
= 0 - sub_me
;
119 return AO_fetch_and_add_write(&val
, negsub
) + negsub
;
122 // cast away const on the pointer. this is only needed to build
123 // on lenny, but not newer debians, so the atomic_ops.h got fixed
124 // at some point. this hack can go away someday...
125 return AO_load_full((AO_t
*)&val
);
127 bool compare_and_swap(AO_t o
, AO_t n
) {
128 return AO_compare_and_swap(&val
, o
, n
);
133 atomic_t(const atomic_t
&other
);
134 atomic_t
&operator=(const atomic_t
&rhs
);
138 typedef atomic_t atomic64_t
;
140 typedef atomic_spinlock_t
<unsigned long long> atomic64_t
;
147 * crappy slow implementation that uses a pthreads spinlock.
149 #include "include/Spinlock.h"
152 typedef atomic_spinlock_t
<unsigned> atomic_t
;
153 typedef atomic_spinlock_t
<unsigned long long> atomic64_t
;