]> git.lyx.org Git - lyx.git/blob - 3rdparty/boost/boost/smart_ptr/detail/sp_counted_base_gcc_ia64.hpp
Update to boost 1.72
[lyx.git] / 3rdparty / boost / boost / smart_ptr / detail / sp_counted_base_gcc_ia64.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED
3
4 //
5 //  detail/sp_counted_base_gcc_ia64.hpp - g++ on IA64
6 //
7 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
8 //  Copyright 2004-2006 Peter Dimov
9 //  Copyright 2005 Ben Hutchings
10 //
11 //  Distributed under the Boost Software License, Version 1.0. (See
12 //  accompanying file LICENSE_1_0.txt or copy at
13 //  http://www.boost.org/LICENSE_1_0.txt)
14 //
15 //
16 //  Lock-free algorithm by Alexander Terekhov
17 //
18
19 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
20 #include <boost/config.hpp>
21
22 namespace boost
23 {
24
25 namespace detail
26 {
27
28 inline void atomic_increment( int * pw )
29 {
30     // ++*pw;
31
32     int tmp;
33
34     // No barrier is required here but fetchadd always has an acquire or
35     // release barrier associated with it.  We choose release as it should be
36     // cheaper.
37     __asm__ ("fetchadd4.rel %0=%1,1" :
38          "=r"(tmp), "=m"(*pw) :
39          "m"( *pw ));
40 }
41
42 inline int atomic_decrement( int * pw )
43 {
44     // return --*pw;
45
46     int rv;
47
48     __asm__ ("     fetchadd4.rel %0=%1,-1 ;; \n"
49              "     cmp.eq        p7,p0=1,%0 ;; \n"
50              "(p7) ld4.acq       %0=%1    " :
51              "=&r"(rv), "=m"(*pw) :
52              "m"( *pw ) :
53              "p7");
54
55     return rv;
56 }
57
58 inline int atomic_conditional_increment( int * pw )
59 {
60     // if( *pw != 0 ) ++*pw;
61     // return *pw;
62
63     int rv, tmp, tmp2;
64
65     __asm__ ("0:   ld4          %0=%3           ;; \n"
66          "     cmp.eq       p7,p0=0,%0        ;; \n"
67          "(p7) br.cond.spnt 1f                \n"
68          "     mov          ar.ccv=%0         \n"
69          "     add          %1=1,%0           ;; \n"
70          "     cmpxchg4.acq %2=%3,%1,ar.ccv ;; \n"
71          "     cmp.ne       p7,p0=%0,%2       ;; \n"
72          "(p7) br.cond.spnt 0b                \n"
73          "     mov          %0=%1             ;; \n"
74          "1:" : 
75          "=&r"(rv), "=&r"(tmp), "=&r"(tmp2), "=m"(*pw) :
76          "m"( *pw ) :
77          "ar.ccv", "p7");
78
79     return rv;
80 }
81
82 class BOOST_SYMBOL_VISIBLE sp_counted_base
83 {
84 private:
85
86     sp_counted_base( sp_counted_base const & );
87     sp_counted_base & operator= ( sp_counted_base const & );
88
89     int use_count_;        // #shared
90     int weak_count_;       // #weak + (#shared != 0)
91
92 public:
93
94     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
95     {
96     }
97
98     virtual ~sp_counted_base() // nothrow
99     {
100     }
101
102     // dispose() is called when use_count_ drops to zero, to release
103     // the resources managed by *this.
104
105     virtual void dispose() = 0; // nothrow
106
107     // destroy() is called when weak_count_ drops to zero.
108
109     virtual void destroy() // nothrow
110     {
111         delete this;
112     }
113
114     virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
115     virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
116     virtual void * get_untyped_deleter() = 0;
117
118     void add_ref_copy()
119     {
120         atomic_increment( &use_count_ );
121     }
122
123     bool add_ref_lock() // true on success
124     {
125         return atomic_conditional_increment( &use_count_ ) != 0;
126     }
127
128     void release() // nothrow
129     {
130         if( atomic_decrement( &use_count_ ) == 0 )
131         {
132             dispose();
133             weak_release();
134         }
135     }
136
137     void weak_add_ref() // nothrow
138     {
139         atomic_increment( &weak_count_ );
140     }
141
142     void weak_release() // nothrow
143     {
144         if( atomic_decrement( &weak_count_ ) == 0 )
145         {
146             destroy();
147         }
148     }
149
150     long use_count() const // nothrow
151     {
152         return static_cast<int const volatile &>( use_count_ ); // TODO use ld.acq here
153     }
154 };
155
156 } // namespace detail
157
158 } // namespace boost
159
160 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED