| 1 | #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_HPP_INCLUDED
|
|---|
| 2 | #define BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_HPP_INCLUDED
|
|---|
| 3 |
|
|---|
| 4 | // MS compatible compilers support #pragma once
|
|---|
| 5 |
|
|---|
| 6 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
|---|
| 7 | # pragma once
|
|---|
| 8 | #endif
|
|---|
| 9 |
|
|---|
| 10 | //
|
|---|
| 11 | // detail/sp_counted_base_gcc_mips.hpp - g++ on MIPS
|
|---|
| 12 | //
|
|---|
| 13 | // Copyright (c) 2009, Spirent Communications, Inc.
|
|---|
| 14 | //
|
|---|
| 15 | // Distributed under the Boost Software License, Version 1.0. (See
|
|---|
| 16 | // accompanying file LICENSE_1_0.txt or copy at
|
|---|
| 17 | // http://www.boost.org/LICENSE_1_0.txt)
|
|---|
| 18 | //
|
|---|
| 19 | //
|
|---|
| 20 | // Lock-free algorithm by Alexander Terekhov
|
|---|
| 21 | //
|
|---|
| 22 |
|
|---|
| 23 | #include "sp_typeinfo.hpp"
|
|---|
| 24 |
|
|---|
| 25 | namespace boost
|
|---|
| 26 | {
|
|---|
| 27 |
|
|---|
| 28 | namespace detail
|
|---|
| 29 | {
|
|---|
| 30 |
|
|---|
| 31 | inline void atomic_increment( int * pw )
|
|---|
| 32 | {
|
|---|
| 33 | // ++*pw;
|
|---|
| 34 |
|
|---|
| 35 | int tmp;
|
|---|
| 36 |
|
|---|
| 37 | __asm__ __volatile__
|
|---|
| 38 | (
|
|---|
| 39 | "0:\n\t"
|
|---|
| 40 | "ll %0, %1\n\t"
|
|---|
| 41 | "addiu %0, 1\n\t"
|
|---|
| 42 | "sc %0, %1\n\t"
|
|---|
| 43 | "beqz %0, 0b":
|
|---|
| 44 | "=&r"( tmp ), "=m"( *pw ):
|
|---|
| 45 | "m"( *pw )
|
|---|
| 46 | );
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | inline int atomic_decrement( int * pw )
|
|---|
| 50 | {
|
|---|
| 51 | // return --*pw;
|
|---|
| 52 |
|
|---|
| 53 | int rv, tmp;
|
|---|
| 54 |
|
|---|
| 55 | __asm__ __volatile__
|
|---|
| 56 | (
|
|---|
| 57 | "0:\n\t"
|
|---|
| 58 | "ll %1, %2\n\t"
|
|---|
| 59 | "addiu %0, %1, -1\n\t"
|
|---|
| 60 | "sc %0, %2\n\t"
|
|---|
| 61 | "beqz %0, 0b\n\t"
|
|---|
| 62 | "addiu %0, %1, -1":
|
|---|
| 63 | "=&r"( rv ), "=&r"( tmp ), "=m"( *pw ):
|
|---|
| 64 | "m"( *pw ):
|
|---|
| 65 | "memory"
|
|---|
| 66 | );
|
|---|
| 67 |
|
|---|
| 68 | return rv;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | inline int atomic_conditional_increment( int * pw )
|
|---|
| 72 | {
|
|---|
| 73 | // if( *pw != 0 ) ++*pw;
|
|---|
| 74 | // return *pw;
|
|---|
| 75 |
|
|---|
| 76 | int rv, tmp;
|
|---|
| 77 |
|
|---|
| 78 | __asm__ __volatile__
|
|---|
| 79 | (
|
|---|
| 80 | "0:\n\t"
|
|---|
| 81 | "ll %0, %2\n\t"
|
|---|
| 82 | "beqz %0, 1f\n\t"
|
|---|
| 83 | "addiu %1, %0, 1\n\t"
|
|---|
| 84 | "sc %1, %2\n\t"
|
|---|
| 85 | "beqz %1, 0b\n\t"
|
|---|
| 86 | "addiu %0, %0, 1\n\t"
|
|---|
| 87 | "1:":
|
|---|
| 88 | "=&r"( rv ), "=&r"( tmp ), "=m"( *pw ):
|
|---|
| 89 | "m"( *pw ):
|
|---|
| 90 | "memory"
|
|---|
| 91 | );
|
|---|
| 92 |
|
|---|
| 93 | return rv;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | class sp_counted_base
|
|---|
| 97 | {
|
|---|
| 98 | private:
|
|---|
| 99 |
|
|---|
| 100 | sp_counted_base( sp_counted_base const & );
|
|---|
| 101 | sp_counted_base & operator= ( sp_counted_base const & );
|
|---|
| 102 |
|
|---|
| 103 | int use_count_; // #shared
|
|---|
| 104 | int weak_count_; // #weak + (#shared != 0)
|
|---|
| 105 |
|
|---|
| 106 | public:
|
|---|
| 107 |
|
|---|
| 108 | sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
|
|---|
| 109 | {
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | virtual ~sp_counted_base() // nothrow
|
|---|
| 113 | {
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | // dispose() is called when use_count_ drops to zero, to release
|
|---|
| 117 | // the resources managed by *this.
|
|---|
| 118 |
|
|---|
| 119 | virtual void dispose() = 0; // nothrow
|
|---|
| 120 |
|
|---|
| 121 | // destroy() is called when weak_count_ drops to zero.
|
|---|
| 122 |
|
|---|
| 123 | virtual void destroy() // nothrow
|
|---|
| 124 | {
|
|---|
| 125 | delete this;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
|
|---|
| 129 |
|
|---|
| 130 | void add_ref_copy()
|
|---|
| 131 | {
|
|---|
| 132 | atomic_increment( &use_count_ );
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | bool add_ref_lock() // true on success
|
|---|
| 136 | {
|
|---|
| 137 | return atomic_conditional_increment( &use_count_ ) != 0;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | void release() // nothrow
|
|---|
| 141 | {
|
|---|
| 142 | if( atomic_decrement( &use_count_ ) == 0 )
|
|---|
| 143 | {
|
|---|
| 144 | dispose();
|
|---|
| 145 | weak_release();
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | void weak_add_ref() // nothrow
|
|---|
| 150 | {
|
|---|
| 151 | atomic_increment( &weak_count_ );
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | void weak_release() // nothrow
|
|---|
| 155 | {
|
|---|
| 156 | if( atomic_decrement( &weak_count_ ) == 0 )
|
|---|
| 157 | {
|
|---|
| 158 | destroy();
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | long use_count() const // nothrow
|
|---|
| 163 | {
|
|---|
| 164 | return static_cast<int const volatile &>( use_count_ );
|
|---|
| 165 | }
|
|---|
| 166 | };
|
|---|
| 167 |
|
|---|
| 168 | } // namespace detail
|
|---|
| 169 |
|
|---|
| 170 | } // namespace boost
|
|---|
| 171 |
|
|---|
| 172 | #endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_HPP_INCLUDED
|
|---|