1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_ACC_IA64_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_ACC_IA64_HPP_INCLUDED
3 
4 //
5 //  detail/sp_counted_base_acc_ia64.hpp - aC++ on HP-UX IA64
6 //
7 //  Copyright 2007 Baruch Zilber
8 //  Copyright 2007 Boris Gubenko
9 //
10 //  Distributed under the Boost Software License, Version 1.0. (See
11 //  accompanying file LICENSE_1_0.txt or copy at
12 //  http://www.boost.org/LICENSE_1_0.txt)
13 //
14 //
15 //  Lock-free algorithm by Alexander Terekhov
16 //
17 
18 #include <boost/detail/sp_typeinfo.hpp>
19 #include <machine/sys/inline.h>
20 
21 namespace boost
22 {
23 
24 namespace detail
25 {
26 
atomic_increment(int * pw)27 inline void atomic_increment( int * pw )
28 {
29     // ++*pw;
30 
31     _Asm_fetchadd(_FASZ_W, _SEM_REL, pw, +1, _LDHINT_NONE);
32 }
33 
atomic_decrement(int * pw)34 inline int atomic_decrement( int * pw )
35 {
36     // return --*pw;
37 
38     int r = static_cast<int>(_Asm_fetchadd(_FASZ_W, _SEM_REL, pw, -1, _LDHINT_NONE));
39     if (1 == r)
40     {
41         _Asm_mf();
42     }
43 
44     return r - 1;
45 }
46 
atomic_conditional_increment(int * pw)47 inline int atomic_conditional_increment( int * pw )
48 {
49     // if( *pw != 0 ) ++*pw;
50     // return *pw;
51 
52     int v = *pw;
53 
54     for (;;)
55     {
56         if (0 == v)
57         {
58             return 0;
59         }
60 
61         _Asm_mov_to_ar(_AREG_CCV,
62                        v,
63                        (_UP_CALL_FENCE | _UP_SYS_FENCE | _DOWN_CALL_FENCE | _DOWN_SYS_FENCE));
64         int r = static_cast<int>(_Asm_cmpxchg(_SZ_W, _SEM_ACQ, pw, v + 1, _LDHINT_NONE));
65         if (r == v)
66         {
67             return r + 1;
68         }
69 
70         v = r;
71     }
72 }
73 
74 class sp_counted_base
75 {
76 private:
77 
78     sp_counted_base( sp_counted_base const & );
79     sp_counted_base & operator= ( sp_counted_base const & );
80 
81     int use_count_;        // #shared
82     int weak_count_;       // #weak + (#shared != 0)
83 
84 public:
85 
sp_counted_base()86     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
87     {
88     }
89 
~sp_counted_base()90     virtual ~sp_counted_base() // nothrow
91     {
92     }
93 
94     // dispose() is called when use_count_ drops to zero, to release
95     // the resources managed by *this.
96 
97     virtual void dispose() = 0; // nothrow
98 
99     // destroy() is called when weak_count_ drops to zero.
100 
destroy()101     virtual void destroy() // nothrow
102     {
103         delete this;
104     }
105 
106     virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
107     virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
108     virtual void * get_untyped_deleter() = 0;
109 
add_ref_copy()110     void add_ref_copy()
111     {
112         atomic_increment( &use_count_ );
113     }
114 
add_ref_lock()115     bool add_ref_lock() // true on success
116     {
117         return atomic_conditional_increment( &use_count_ ) != 0;
118     }
119 
release()120     void release() // nothrow
121     {
122         if( atomic_decrement( &use_count_ ) == 0 )
123         {
124             dispose();
125             weak_release();
126         }
127     }
128 
weak_add_ref()129     void weak_add_ref() // nothrow
130     {
131         atomic_increment( &weak_count_ );
132     }
133 
weak_release()134     void weak_release() // nothrow
135     {
136         if( atomic_decrement( &weak_count_ ) == 0 )
137         {
138             destroy();
139         }
140     }
141 
use_count() const142     long use_count() const // nothrow
143     {
144         return static_cast<int const volatile &>( use_count_ ); // TODO use ld.acq here
145     }
146 };
147 
148 } // namespace detail
149 
150 } // namespace boost
151 
152 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_ACC_IA64_HPP_INCLUDED
153