1132 lines
123 KiB
HTML
1132 lines
123 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
<title>LCOV - coverage.info - libtorch/include/c10/util/intrusive_ptr.h</title>
|
|
<link rel="stylesheet" type="text/css" href="../../../../gcov.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<table width="100%" border=0 cellspacing=0 cellpadding=0>
|
|
<tr><td class="title">LCOV - code coverage report</td></tr>
|
|
<tr><td class="ruler"><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
|
|
|
|
<tr>
|
|
<td width="100%">
|
|
<table cellpadding=1 border=0 width="100%">
|
|
<tr>
|
|
<td width="10%" class="headerItem">Current view:</td>
|
|
<td width="10%" class="headerValue"><a href="../../../../index.html">top level</a> - <a href="index.html">libtorch/include/c10/util</a> - intrusive_ptr.h<span style="font-size: 80%;"> (source / <a href="intrusive_ptr.h.func-c.html">functions</a>)</span></td>
|
|
<td width="5%"></td>
|
|
<td width="5%"></td>
|
|
<td width="5%" class="headerCovTableHead">Coverage</td>
|
|
<td width="5%" class="headerCovTableHead" title="Covered + Uncovered code">Total</td>
|
|
<td width="5%" class="headerCovTableHead" title="Exercised code only">Hit</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="headerItem">Test:</td>
|
|
<td class="headerValue">coverage.info</td>
|
|
<td></td>
|
|
<td class="headerItem">Lines:</td>
|
|
<td class="headerCovTableEntryMed">83.8 %</td>
|
|
<td class="headerCovTableEntry">105</td>
|
|
<td class="headerCovTableEntry">88</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="headerItem">Test Date:</td>
|
|
<td class="headerValue">2024-04-30 13:17:26</td>
|
|
<td></td>
|
|
<td class="headerItem">Functions:</td>
|
|
<td class="headerCovTableEntryLo">66.7 %</td>
|
|
<td class="headerCovTableEntry">72</td>
|
|
<td class="headerCovTableEntry">48</td>
|
|
</tr>
|
|
<tr><td><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr><td class="ruler"><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
|
|
</table>
|
|
|
|
<table cellpadding=0 cellspacing=0 border=0>
|
|
<tr>
|
|
<td><br></td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
<pre class="sourceHeading"> Line data Source code</pre>
|
|
<pre class="source">
|
|
<span id="L1"><span class="lineNum"> 1</span> : #pragma once</span>
|
|
<span id="L2"><span class="lineNum"> 2</span> : </span>
|
|
<span id="L3"><span class="lineNum"> 3</span> : #include <c10/util/Exception.h></span>
|
|
<span id="L4"><span class="lineNum"> 4</span> : #include <c10/util/MaybeOwned.h></span>
|
|
<span id="L5"><span class="lineNum"> 5</span> : #include <atomic></span>
|
|
<span id="L6"><span class="lineNum"> 6</span> : #include <climits></span>
|
|
<span id="L7"><span class="lineNum"> 7</span> : #include <memory></span>
|
|
<span id="L8"><span class="lineNum"> 8</span> : </span>
|
|
<span id="L9"><span class="lineNum"> 9</span> : namespace pybind11 {</span>
|
|
<span id="L10"><span class="lineNum"> 10</span> : template <typename, typename...></span>
|
|
<span id="L11"><span class="lineNum"> 11</span> : class class_;</span>
|
|
<span id="L12"><span class="lineNum"> 12</span> : }</span>
|
|
<span id="L13"><span class="lineNum"> 13</span> : </span>
|
|
<span id="L14"><span class="lineNum"> 14</span> : namespace c10 {</span>
|
|
<span id="L15"><span class="lineNum"> 15</span> : class intrusive_ptr_target;</span>
|
|
<span id="L16"><span class="lineNum"> 16</span> : namespace raw {</span>
|
|
<span id="L17"><span class="lineNum"> 17</span> : namespace weak_intrusive_ptr {</span>
|
|
<span id="L18"><span class="lineNum"> 18</span> : inline void incref(intrusive_ptr_target* self);</span>
|
|
<span id="L19"><span class="lineNum"> 19</span> : }</span>
|
|
<span id="L20"><span class="lineNum"> 20</span> : namespace intrusive_ptr {</span>
|
|
<span id="L21"><span class="lineNum"> 21</span> : inline void incref(intrusive_ptr_target* self);</span>
|
|
<span id="L22"><span class="lineNum"> 22</span> : }</span>
|
|
<span id="L23"><span class="lineNum"> 23</span> : </span>
|
|
<span id="L24"><span class="lineNum"> 24</span> : // constructor tag used by intrusive_ptr constructors</span>
|
|
<span id="L25"><span class="lineNum"> 25</span> : struct DontIncreaseRefcount {};</span>
|
|
<span id="L26"><span class="lineNum"> 26</span> : } // namespace raw</span>
|
|
<span id="L27"><span class="lineNum"> 27</span> : /**</span>
|
|
<span id="L28"><span class="lineNum"> 28</span> : * intrusive_ptr<T> is an alternative to shared_ptr<T> that has better</span>
|
|
<span id="L29"><span class="lineNum"> 29</span> : * performance because it does the refcounting intrusively</span>
|
|
<span id="L30"><span class="lineNum"> 30</span> : * (i.e. in a member of the object itself).</span>
|
|
<span id="L31"><span class="lineNum"> 31</span> : * Your class T needs to inherit from intrusive_ptr_target to allow it to be</span>
|
|
<span id="L32"><span class="lineNum"> 32</span> : * used in an intrusive_ptr<T>. Your class's constructor should not allow</span>
|
|
<span id="L33"><span class="lineNum"> 33</span> : *`this` to escape to other threads or create an intrusive_ptr from `this`.</span>
|
|
<span id="L34"><span class="lineNum"> 34</span> : */</span>
|
|
<span id="L35"><span class="lineNum"> 35</span> : </span>
|
|
<span id="L36"><span class="lineNum"> 36</span> : // Note [Stack allocated intrusive_ptr_target safety]</span>
|
|
<span id="L37"><span class="lineNum"> 37</span> : // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
|
|
<span id="L38"><span class="lineNum"> 38</span> : // A well known problem with std::enable_shared_from_this is that it</span>
|
|
<span id="L39"><span class="lineNum"> 39</span> : // allows you to create a std::shared_ptr from a stack allocated object,</span>
|
|
<span id="L40"><span class="lineNum"> 40</span> : // which is totally bogus because the object will die once you return</span>
|
|
<span id="L41"><span class="lineNum"> 41</span> : // from the stack. In intrusive_ptr, we can detect that this has occurred,</span>
|
|
<span id="L42"><span class="lineNum"> 42</span> : // because we set the refcount/weakcount of objects which inherit from</span>
|
|
<span id="L43"><span class="lineNum"> 43</span> : // intrusive_ptr_target to zero, *unless* we can prove that the object</span>
|
|
<span id="L44"><span class="lineNum"> 44</span> : // was dynamically allocated (e.g., via make_intrusive).</span>
|
|
<span id="L45"><span class="lineNum"> 45</span> : //</span>
|
|
<span id="L46"><span class="lineNum"> 46</span> : // Thus, whenever you transmute a T* into a intrusive_ptr<T>, we check</span>
|
|
<span id="L47"><span class="lineNum"> 47</span> : // and make sure that the refcount isn't zero (or, a more subtle</span>
|
|
<span id="L48"><span class="lineNum"> 48</span> : // test for weak_intrusive_ptr<T>, for which the refcount may validly</span>
|
|
<span id="L49"><span class="lineNum"> 49</span> : // be zero, but the weak refcount better not be zero), because that</span>
|
|
<span id="L50"><span class="lineNum"> 50</span> : // tells us if the object was allocated by us. If it wasn't, no</span>
|
|
<span id="L51"><span class="lineNum"> 51</span> : // intrusive_ptr for you!</span>
|
|
<span id="L52"><span class="lineNum"> 52</span> : </span>
|
|
<span id="L53"><span class="lineNum"> 53</span> : // NOLINTNEXTLINE(cppcoreguidelines-virtual-class-destructor)</span>
|
|
<span id="L54"><span class="lineNum"> 54</span> : class C10_API intrusive_ptr_target {</span>
|
|
<span id="L55"><span class="lineNum"> 55</span> : // Note [Weak references for intrusive refcounting]</span>
|
|
<span id="L56"><span class="lineNum"> 56</span> : // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
|
|
<span id="L57"><span class="lineNum"> 57</span> : // Here's the scheme:</span>
|
|
<span id="L58"><span class="lineNum"> 58</span> : //</span>
|
|
<span id="L59"><span class="lineNum"> 59</span> : // - refcount == number of strong references to the object</span>
|
|
<span id="L60"><span class="lineNum"> 60</span> : // weakcount == number of weak references to the object,</span>
|
|
<span id="L61"><span class="lineNum"> 61</span> : // plus one more if refcount > 0</span>
|
|
<span id="L62"><span class="lineNum"> 62</span> : // An invariant: refcount > 0 => weakcount > 0</span>
|
|
<span id="L63"><span class="lineNum"> 63</span> : //</span>
|
|
<span id="L64"><span class="lineNum"> 64</span> : // - c10::StorageImpl stays live as long as there are any strong</span>
|
|
<span id="L65"><span class="lineNum"> 65</span> : // or weak pointers to it (weakcount > 0, since strong</span>
|
|
<span id="L66"><span class="lineNum"> 66</span> : // references count as a +1 to weakcount)</span>
|
|
<span id="L67"><span class="lineNum"> 67</span> : //</span>
|
|
<span id="L68"><span class="lineNum"> 68</span> : // - finalizers are called and data_ptr is deallocated when refcount == 0</span>
|
|
<span id="L69"><span class="lineNum"> 69</span> : //</span>
|
|
<span id="L70"><span class="lineNum"> 70</span> : // - Once refcount == 0, it can never again be > 0 (the transition</span>
|
|
<span id="L71"><span class="lineNum"> 71</span> : // from > 0 to == 0 is monotonic)</span>
|
|
<span id="L72"><span class="lineNum"> 72</span> : //</span>
|
|
<span id="L73"><span class="lineNum"> 73</span> : // - When you access c10::StorageImpl via a weak pointer, you must</span>
|
|
<span id="L74"><span class="lineNum"> 74</span> : // atomically increment the use count, if it is greater than 0.</span>
|
|
<span id="L75"><span class="lineNum"> 75</span> : // If it is not, you must report that the storage is dead.</span>
|
|
<span id="L76"><span class="lineNum"> 76</span> : //</span>
|
|
<span id="L77"><span class="lineNum"> 77</span> : mutable std::atomic<size_t> refcount_;</span>
|
|
<span id="L78"><span class="lineNum"> 78</span> : mutable std::atomic<size_t> weakcount_;</span>
|
|
<span id="L79"><span class="lineNum"> 79</span> : </span>
|
|
<span id="L80"><span class="lineNum"> 80</span> : template <typename T, typename NullType></span>
|
|
<span id="L81"><span class="lineNum"> 81</span> : friend class intrusive_ptr;</span>
|
|
<span id="L82"><span class="lineNum"> 82</span> : friend inline void raw::intrusive_ptr::incref(intrusive_ptr_target* self);</span>
|
|
<span id="L83"><span class="lineNum"> 83</span> : </span>
|
|
<span id="L84"><span class="lineNum"> 84</span> : template <typename T, typename NullType></span>
|
|
<span id="L85"><span class="lineNum"> 85</span> : friend class weak_intrusive_ptr;</span>
|
|
<span id="L86"><span class="lineNum"> 86</span> : friend inline void raw::weak_intrusive_ptr::incref(</span>
|
|
<span id="L87"><span class="lineNum"> 87</span> : intrusive_ptr_target* self);</span>
|
|
<span id="L88"><span class="lineNum"> 88</span> : </span>
|
|
<span id="L89"><span class="lineNum"> 89</span> : template <typename T></span>
|
|
<span id="L90"><span class="lineNum"> 90</span> : friend struct ExclusivelyOwnedTensorTraits;</span>
|
|
<span id="L91"><span class="lineNum"> 91</span> : </span>
|
|
<span id="L92"><span class="lineNum"> 92</span> : protected:</span>
|
|
<span id="L93"><span class="lineNum"> 93</span> : // protected destructor. We never want to destruct intrusive_ptr_target*</span>
|
|
<span id="L94"><span class="lineNum"> 94</span> : // directly.</span>
|
|
<span id="L95"><span class="lineNum"> 95</span> <span class="tlaGNC tlaBgGNC"> 520215158 : virtual ~intrusive_ptr_target() {</span></span>
|
|
<span id="L96"><span class="lineNum"> 96</span> : // Disable -Wterminate and -Wexceptions so we're allowed to use assertions</span>
|
|
<span id="L97"><span class="lineNum"> 97</span> : // (i.e. throw exceptions) in a destructor.</span>
|
|
<span id="L98"><span class="lineNum"> 98</span> : // We also have to disable -Wunknown-warning-option and -Wpragmas, because</span>
|
|
<span id="L99"><span class="lineNum"> 99</span> : // some other compilers don't know about -Wterminate or -Wexceptions and</span>
|
|
<span id="L100"><span class="lineNum"> 100</span> : // will show a warning about unknown warning options otherwise.</span>
|
|
<span id="L101"><span class="lineNum"> 101</span> : #if defined(_MSC_VER) && !defined(__clang__)</span>
|
|
<span id="L102"><span class="lineNum"> 102</span> : #pragma warning(push)</span>
|
|
<span id="L103"><span class="lineNum"> 103</span> : #pragma warning( \</span>
|
|
<span id="L104"><span class="lineNum"> 104</span> : disable : 4297) // function assumed not to throw an exception but does</span>
|
|
<span id="L105"><span class="lineNum"> 105</span> : #else</span>
|
|
<span id="L106"><span class="lineNum"> 106</span> : #pragma GCC diagnostic push</span>
|
|
<span id="L107"><span class="lineNum"> 107</span> : #pragma GCC diagnostic ignored "-Wpragmas"</span>
|
|
<span id="L108"><span class="lineNum"> 108</span> : #pragma GCC diagnostic ignored "-Wunknown-warning-option"</span>
|
|
<span id="L109"><span class="lineNum"> 109</span> : #pragma GCC diagnostic ignored "-Wterminate"</span>
|
|
<span id="L110"><span class="lineNum"> 110</span> : #pragma GCC diagnostic ignored "-Wexceptions"</span>
|
|
<span id="L111"><span class="lineNum"> 111</span> : #endif</span>
|
|
<span id="L112"><span class="lineNum"> 112</span> <span class="tlaGNC"> 1040430316 : TORCH_INTERNAL_ASSERT_DEBUG_ONLY(</span></span>
|
|
<span id="L113"><span class="lineNum"> 113</span> : // Second condition is there to accommodate</span>
|
|
<span id="L114"><span class="lineNum"> 114</span> : // unsafe_adapt_non_heap_allocated: since we are doing our own</span>
|
|
<span id="L115"><span class="lineNum"> 115</span> : // deallocation in that case, it is correct for each</span>
|
|
<span id="L116"><span class="lineNum"> 116</span> : // expected_decref to have happened (some user code tried to</span>
|
|
<span id="L117"><span class="lineNum"> 117</span> : // decref and thus free the object, but it didn't happen right</span>
|
|
<span id="L118"><span class="lineNum"> 118</span> : // away) or not (no user code tried to free the object, and</span>
|
|
<span id="L119"><span class="lineNum"> 119</span> : // now it's getting destroyed through whatever mechanism the</span>
|
|
<span id="L120"><span class="lineNum"> 120</span> : // caller of unsafe_adapt_non_heap_allocated wanted to</span>
|
|
<span id="L121"><span class="lineNum"> 121</span> : // use). We choose our reference count such that the count</span>
|
|
<span id="L122"><span class="lineNum"> 122</span> : // will not dip below INT_MAX regardless.</span>
|
|
<span id="L123"><span class="lineNum"> 123</span> : refcount_.load() == 0 || refcount_.load() >= INT_MAX,</span>
|
|
<span id="L124"><span class="lineNum"> 124</span> : "Tried to destruct an intrusive_ptr_target that still has intrusive_ptr to it; refcount was ",</span>
|
|
<span id="L125"><span class="lineNum"> 125</span> : refcount_.load());</span>
|
|
<span id="L126"><span class="lineNum"> 126</span> <span class="tlaGNC"> 1040430316 : TORCH_INTERNAL_ASSERT_DEBUG_ONLY(</span></span>
|
|
<span id="L127"><span class="lineNum"> 127</span> : // See ~intrusive_ptr for optimization that will frequently result in 1</span>
|
|
<span id="L128"><span class="lineNum"> 128</span> : // at destruction time.</span>
|
|
<span id="L129"><span class="lineNum"> 129</span> : weakcount_.load() == 1 || weakcount_.load() == 0 ||</span>
|
|
<span id="L130"><span class="lineNum"> 130</span> : weakcount_.load() == INT_MAX - 1 || weakcount_.load() == INT_MAX,</span>
|
|
<span id="L131"><span class="lineNum"> 131</span> : "Tried to destruct an intrusive_ptr_target that still has weak_intrusive_ptr to it");</span>
|
|
<span id="L132"><span class="lineNum"> 132</span> : #if defined(_MSC_VER) && !defined(__clang__)</span>
|
|
<span id="L133"><span class="lineNum"> 133</span> : #pragma warning(pop)</span>
|
|
<span id="L134"><span class="lineNum"> 134</span> : #else</span>
|
|
<span id="L135"><span class="lineNum"> 135</span> : #pragma GCC diagnostic pop</span>
|
|
<span id="L136"><span class="lineNum"> 136</span> : #endif</span>
|
|
<span id="L137"><span class="lineNum"> 137</span> <span class="tlaGNC"> 520215158 : }</span></span>
|
|
<span id="L138"><span class="lineNum"> 138</span> : </span>
|
|
<span id="L139"><span class="lineNum"> 139</span> <span class="tlaGNC"> 378768 : constexpr intrusive_ptr_target() noexcept : refcount_(0), weakcount_(0) {}</span></span>
|
|
<span id="L140"><span class="lineNum"> 140</span> : </span>
|
|
<span id="L141"><span class="lineNum"> 141</span> : // intrusive_ptr_target supports copy and move: but refcount and weakcount</span>
|
|
<span id="L142"><span class="lineNum"> 142</span> : // don't participate (since they are intrinsic properties of the memory</span>
|
|
<span id="L143"><span class="lineNum"> 143</span> : // location)</span>
|
|
<span id="L144"><span class="lineNum"> 144</span> : intrusive_ptr_target(intrusive_ptr_target&& /*other*/) noexcept</span>
|
|
<span id="L145"><span class="lineNum"> 145</span> : : intrusive_ptr_target() {}</span>
|
|
<span id="L146"><span class="lineNum"> 146</span> : </span>
|
|
<span id="L147"><span class="lineNum"> 147</span> : intrusive_ptr_target& operator=(intrusive_ptr_target&& /*other*/) noexcept {</span>
|
|
<span id="L148"><span class="lineNum"> 148</span> : return *this;</span>
|
|
<span id="L149"><span class="lineNum"> 149</span> : }</span>
|
|
<span id="L150"><span class="lineNum"> 150</span> : </span>
|
|
<span id="L151"><span class="lineNum"> 151</span> : intrusive_ptr_target(const intrusive_ptr_target& /*other*/) noexcept</span>
|
|
<span id="L152"><span class="lineNum"> 152</span> : : intrusive_ptr_target() {}</span>
|
|
<span id="L153"><span class="lineNum"> 153</span> : </span>
|
|
<span id="L154"><span class="lineNum"> 154</span> : intrusive_ptr_target& operator=(</span>
|
|
<span id="L155"><span class="lineNum"> 155</span> : const intrusive_ptr_target& /*other*/) noexcept {</span>
|
|
<span id="L156"><span class="lineNum"> 156</span> : return *this;</span>
|
|
<span id="L157"><span class="lineNum"> 157</span> : }</span>
|
|
<span id="L158"><span class="lineNum"> 158</span> : </span>
|
|
<span id="L159"><span class="lineNum"> 159</span> : private:</span>
|
|
<span id="L160"><span class="lineNum"> 160</span> : /**</span>
|
|
<span id="L161"><span class="lineNum"> 161</span> : * This is called when refcount reaches zero.</span>
|
|
<span id="L162"><span class="lineNum"> 162</span> : * You can override this to release expensive resources.</span>
|
|
<span id="L163"><span class="lineNum"> 163</span> : * There might still be weak references, so your object might not get</span>
|
|
<span id="L164"><span class="lineNum"> 164</span> : * destructed yet, but you can assume the object isn't used anymore,</span>
|
|
<span id="L165"><span class="lineNum"> 165</span> : * i.e. no more calls to methods or accesses to members (we just can't</span>
|
|
<span id="L166"><span class="lineNum"> 166</span> : * destruct it yet because we need the weakcount accessible).</span>
|
|
<span id="L167"><span class="lineNum"> 167</span> : *</span>
|
|
<span id="L168"><span class="lineNum"> 168</span> : * If there are no weak references (i.e. your class is about to be</span>
|
|
<span id="L169"><span class="lineNum"> 169</span> : * destructed), this function WILL NOT be called.</span>
|
|
<span id="L170"><span class="lineNum"> 170</span> : */</span>
|
|
<span id="L171"><span class="lineNum"> 171</span> <span class="tlaUNC tlaBgUNC"> 0 : virtual void release_resources() {}</span></span>
|
|
<span id="L172"><span class="lineNum"> 172</span> : };</span>
|
|
<span id="L173"><span class="lineNum"> 173</span> : </span>
|
|
<span id="L174"><span class="lineNum"> 174</span> : namespace detail {</span>
|
|
<span id="L175"><span class="lineNum"> 175</span> : template <class TTarget></span>
|
|
<span id="L176"><span class="lineNum"> 176</span> : struct intrusive_target_default_null_type final {</span>
|
|
<span id="L177"><span class="lineNum"> 177</span> <span class="tlaGNC tlaBgGNC"> 149296920 : static constexpr TTarget* singleton() noexcept {</span></span>
|
|
<span id="L178"><span class="lineNum"> 178</span> <span class="tlaGNC"> 149296920 : return nullptr;</span></span>
|
|
<span id="L179"><span class="lineNum"> 179</span> : }</span>
|
|
<span id="L180"><span class="lineNum"> 180</span> : };</span>
|
|
<span id="L181"><span class="lineNum"> 181</span> : </span>
|
|
<span id="L182"><span class="lineNum"> 182</span> : template <class TTarget, class ToNullType, class FromNullType></span>
|
|
<span id="L183"><span class="lineNum"> 183</span> <span class="tlaGNC"> 378768 : TTarget* assign_ptr_(TTarget* rhs) {</span></span>
|
|
<span id="L184"><span class="lineNum"> 184</span> <span class="tlaGNC"> 378768 : if (FromNullType::singleton() == rhs) {</span></span>
|
|
<span id="L185"><span class="lineNum"> 185</span> <span class="tlaUNC tlaBgUNC"> 0 : return ToNullType::singleton();</span></span>
|
|
<span id="L186"><span class="lineNum"> 186</span> : } else {</span>
|
|
<span id="L187"><span class="lineNum"> 187</span> <span class="tlaGNC tlaBgGNC"> 378768 : return rhs;</span></span>
|
|
<span id="L188"><span class="lineNum"> 188</span> : }</span>
|
|
<span id="L189"><span class="lineNum"> 189</span> : }</span>
|
|
<span id="L190"><span class="lineNum"> 190</span> : </span>
|
|
<span id="L191"><span class="lineNum"> 191</span> : // Increment needs to be acquire-release to make use_count() and</span>
|
|
<span id="L192"><span class="lineNum"> 192</span> : // unique() reliable.</span>
|
|
<span id="L193"><span class="lineNum"> 193</span> <span class="tlaGNC"> 440756 : inline size_t atomic_refcount_increment(std::atomic<size_t>& refcount) {</span></span>
|
|
<span id="L194"><span class="lineNum"> 194</span> <span class="tlaGNC"> 881512 : return refcount.fetch_add(1, std::memory_order_acq_rel) + 1;</span></span>
|
|
<span id="L195"><span class="lineNum"> 195</span> : }</span>
|
|
<span id="L196"><span class="lineNum"> 196</span> : </span>
|
|
<span id="L197"><span class="lineNum"> 197</span> : // weak_use_count() is only used for testing, so we don't need it to</span>
|
|
<span id="L198"><span class="lineNum"> 198</span> : // be reliable. Relaxed should be fine.</span>
|
|
<span id="L199"><span class="lineNum"> 199</span> : inline size_t atomic_weakcount_increment(std::atomic<size_t>& weakcount) {</span>
|
|
<span id="L200"><span class="lineNum"> 200</span> : return weakcount.fetch_add(1, std::memory_order_relaxed) + 1;</span>
|
|
<span id="L201"><span class="lineNum"> 201</span> : }</span>
|
|
<span id="L202"><span class="lineNum"> 202</span> : </span>
|
|
<span id="L203"><span class="lineNum"> 203</span> : // Both decrements need to be acquire-release for correctness. See</span>
|
|
<span id="L204"><span class="lineNum"> 204</span> : // e.g. std::shared_ptr implementation.</span>
|
|
<span id="L205"><span class="lineNum"> 205</span> <span class="tlaGNC"> 212684736 : inline size_t atomic_refcount_decrement(std::atomic<size_t>& refcount) {</span></span>
|
|
<span id="L206"><span class="lineNum"> 206</span> <span class="tlaGNC"> 425369472 : return refcount.fetch_sub(1, std::memory_order_acq_rel) - 1;</span></span>
|
|
<span id="L207"><span class="lineNum"> 207</span> : }</span>
|
|
<span id="L208"><span class="lineNum"> 208</span> : </span>
|
|
<span id="L209"><span class="lineNum"> 209</span> <span class="tlaUNC tlaBgUNC"> 0 : inline size_t atomic_weakcount_decrement(std::atomic<size_t>& weakcount) {</span></span>
|
|
<span id="L210"><span class="lineNum"> 210</span> <span class="tlaUNC"> 0 : return weakcount.fetch_sub(1, std::memory_order_acq_rel) - 1;</span></span>
|
|
<span id="L211"><span class="lineNum"> 211</span> : }</span>
|
|
<span id="L212"><span class="lineNum"> 212</span> : </span>
|
|
<span id="L213"><span class="lineNum"> 213</span> : } // namespace detail</span>
|
|
<span id="L214"><span class="lineNum"> 214</span> : </span>
|
|
<span id="L215"><span class="lineNum"> 215</span> : template <class TTarget, class NullType></span>
|
|
<span id="L216"><span class="lineNum"> 216</span> : class weak_intrusive_ptr;</span>
|
|
<span id="L217"><span class="lineNum"> 217</span> : </span>
|
|
<span id="L218"><span class="lineNum"> 218</span> : template <</span>
|
|
<span id="L219"><span class="lineNum"> 219</span> : class TTarget,</span>
|
|
<span id="L220"><span class="lineNum"> 220</span> : class NullType = detail::intrusive_target_default_null_type<TTarget>></span>
|
|
<span id="L221"><span class="lineNum"> 221</span> : class intrusive_ptr final {</span>
|
|
<span id="L222"><span class="lineNum"> 222</span> : private:</span>
|
|
<span id="L223"><span class="lineNum"> 223</span> : // the following static assert would be nice to have but it requires</span>
|
|
<span id="L224"><span class="lineNum"> 224</span> : // the target class T to be fully defined when intrusive_ptr<T> is instantiated</span>
|
|
<span id="L225"><span class="lineNum"> 225</span> : // this is a problem for classes that contain pointers to themselves</span>
|
|
<span id="L226"><span class="lineNum"> 226</span> : // static_assert(</span>
|
|
<span id="L227"><span class="lineNum"> 227</span> : // std::is_base_of<intrusive_ptr_target, TTarget>::value,</span>
|
|
<span id="L228"><span class="lineNum"> 228</span> : // "intrusive_ptr can only be used for classes that inherit from</span>
|
|
<span id="L229"><span class="lineNum"> 229</span> : // intrusive_ptr_target.");</span>
|
|
<span id="L230"><span class="lineNum"> 230</span> : #ifndef _WIN32</span>
|
|
<span id="L231"><span class="lineNum"> 231</span> : // This static_assert triggers on MSVC</span>
|
|
<span id="L232"><span class="lineNum"> 232</span> : // error C2131: expression did not evaluate to a constant</span>
|
|
<span id="L233"><span class="lineNum"> 233</span> : static_assert(</span>
|
|
<span id="L234"><span class="lineNum"> 234</span> : NullType::singleton() == NullType::singleton(),</span>
|
|
<span id="L235"><span class="lineNum"> 235</span> : "NullType must have a constexpr singleton() method");</span>
|
|
<span id="L236"><span class="lineNum"> 236</span> : #endif</span>
|
|
<span id="L237"><span class="lineNum"> 237</span> : static_assert(</span>
|
|
<span id="L238"><span class="lineNum"> 238</span> : std::is_base_of<</span>
|
|
<span id="L239"><span class="lineNum"> 239</span> : TTarget,</span>
|
|
<span id="L240"><span class="lineNum"> 240</span> : typename std::remove_pointer<decltype(NullType::singleton())>::type>::</span>
|
|
<span id="L241"><span class="lineNum"> 241</span> : value,</span>
|
|
<span id="L242"><span class="lineNum"> 242</span> : "NullType::singleton() must return a element_type* pointer");</span>
|
|
<span id="L243"><span class="lineNum"> 243</span> : </span>
|
|
<span id="L244"><span class="lineNum"> 244</span> : TTarget* target_;</span>
|
|
<span id="L245"><span class="lineNum"> 245</span> : </span>
|
|
<span id="L246"><span class="lineNum"> 246</span> : template <typename T></span>
|
|
<span id="L247"><span class="lineNum"> 247</span> : friend struct ExclusivelyOwnedTensorTraits;</span>
|
|
<span id="L248"><span class="lineNum"> 248</span> : template <class TTarget2, class NullType2></span>
|
|
<span id="L249"><span class="lineNum"> 249</span> : friend class intrusive_ptr;</span>
|
|
<span id="L250"><span class="lineNum"> 250</span> : friend class weak_intrusive_ptr<TTarget, NullType>;</span>
|
|
<span id="L251"><span class="lineNum"> 251</span> : </span>
|
|
<span id="L252"><span class="lineNum"> 252</span> : // Make pybind11::class_ be a friend class of intrusive_ptr, so that custom</span>
|
|
<span id="L253"><span class="lineNum"> 253</span> : // smart holder in pybind11 could access the private constructor of</span>
|
|
<span id="L254"><span class="lineNum"> 254</span> : // intrusive_ptr(T*) which took the ownership of the object. This is required</span>
|
|
<span id="L255"><span class="lineNum"> 255</span> : // by customer holder macro PYBIND11_DECLARE_HOLDER_TYPE, where it uses</span>
|
|
<span id="L256"><span class="lineNum"> 256</span> : // intrusive_ptr(TTarget*) to initialize and take ownership of the object. For</span>
|
|
<span id="L257"><span class="lineNum"> 257</span> : // details, see</span>
|
|
<span id="L258"><span class="lineNum"> 258</span> : // https://pybind11.readthedocs.io/en/stable/advanced/smart_ptrs.html#custom-smart-pointers</span>
|
|
<span id="L259"><span class="lineNum"> 259</span> : template <typename, typename...></span>
|
|
<span id="L260"><span class="lineNum"> 260</span> : friend class pybind11::class_;</span>
|
|
<span id="L261"><span class="lineNum"> 261</span> : </span>
|
|
<span id="L262"><span class="lineNum"> 262</span> <span class="tlaGNC tlaBgGNC"> 441642 : void retain_() {</span></span>
|
|
<span id="L263"><span class="lineNum"> 263</span> <span class="tlaGNC"> 441642 : if (target_ != NullType::singleton()) {</span></span>
|
|
<span id="L264"><span class="lineNum"> 264</span> : size_t new_refcount =</span>
|
|
<span id="L265"><span class="lineNum"> 265</span> <span class="tlaGNC"> 440756 : detail::atomic_refcount_increment(target_->refcount_);</span></span>
|
|
<span id="L266"><span class="lineNum"> 266</span> <span class="tlaGNC"> 440756 : TORCH_INTERNAL_ASSERT_DEBUG_ONLY(</span></span>
|
|
<span id="L267"><span class="lineNum"> 267</span> : new_refcount != 1,</span>
|
|
<span id="L268"><span class="lineNum"> 268</span> : "intrusive_ptr: Cannot increase refcount after it reached zero.");</span>
|
|
<span id="L269"><span class="lineNum"> 269</span> : }</span>
|
|
<span id="L270"><span class="lineNum"> 270</span> <span class="tlaGNC"> 441642 : }</span></span>
|
|
<span id="L271"><span class="lineNum"> 271</span> : </span>
|
|
<span id="L272"><span class="lineNum"> 272</span> <span class="tlaGNC"> 710681430 : void reset_() noexcept {</span></span>
|
|
<span id="L273"><span class="lineNum"> 273</span> <span class="tlaGNC"> 923366166 : if (target_ != NullType::singleton() &&</span></span>
|
|
<span id="L274"><span class="lineNum"> 274</span> <span class="tlaGNC"> 212684736 : detail::atomic_refcount_decrement(target_->refcount_) == 0) {</span></span>
|
|
<span id="L275"><span class="lineNum"> 275</span> : // See comment above about weakcount. As long as refcount>0,</span>
|
|
<span id="L276"><span class="lineNum"> 276</span> : // weakcount is one larger than the actual number of weak references.</span>
|
|
<span id="L277"><span class="lineNum"> 277</span> : // So we need to decrement it here.</span>
|
|
<span id="L278"><span class="lineNum"> 278</span> <span class="tlaGNC"> 211864418 : bool should_delete =</span></span>
|
|
<span id="L279"><span class="lineNum"> 279</span> <span class="tlaGNC"> 211864418 : target_->weakcount_.load(std::memory_order_acquire) == 1;</span></span>
|
|
<span id="L280"><span class="lineNum"> 280</span> <span class="tlaGNC"> 211864418 : if (!should_delete) {</span></span>
|
|
<span id="L281"><span class="lineNum"> 281</span> : // justification for const_cast: release_resources is basically a</span>
|
|
<span id="L282"><span class="lineNum"> 282</span> : // destructor and a destructor always mutates the object, even for const</span>
|
|
<span id="L283"><span class="lineNum"> 283</span> : // objects. NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)</span>
|
|
<span id="L284"><span class="lineNum"> 284</span> <span class="tlaUNC tlaBgUNC"> 0 : const_cast<std::remove_const_t<TTarget>*>(target_)->release_resources();</span></span>
|
|
<span id="L285"><span class="lineNum"> 285</span> <span class="tlaUNC"> 0 : should_delete =</span></span>
|
|
<span id="L286"><span class="lineNum"> 286</span> <span class="tlaUNC"> 0 : detail::atomic_weakcount_decrement(target_->weakcount_) == 0;</span></span>
|
|
<span id="L287"><span class="lineNum"> 287</span> : }</span>
|
|
<span id="L288"><span class="lineNum"> 288</span> <span class="tlaGNC tlaBgGNC"> 211864418 : if (should_delete) {</span></span>
|
|
<span id="L289"><span class="lineNum"> 289</span> <span class="tlaGNC"> 211864418 : delete target_;</span></span>
|
|
<span id="L290"><span class="lineNum"> 290</span> : }</span>
|
|
<span id="L291"><span class="lineNum"> 291</span> : }</span>
|
|
<span id="L292"><span class="lineNum"> 292</span> <span class="tlaGNC"> 710681430 : }</span></span>
|
|
<span id="L293"><span class="lineNum"> 293</span> : </span>
|
|
<span id="L294"><span class="lineNum"> 294</span> : // raw pointer constructors are not public because we shouldn't make</span>
|
|
<span id="L295"><span class="lineNum"> 295</span> : // intrusive_ptr out of raw pointers except from inside the make_intrusive(),</span>
|
|
<span id="L296"><span class="lineNum"> 296</span> : // reclaim() and weak_intrusive_ptr::lock() implementations.</span>
|
|
<span id="L297"><span class="lineNum"> 297</span> : </span>
|
|
<span id="L298"><span class="lineNum"> 298</span> : // This constructor will increase the ref counter for you.</span>
|
|
<span id="L299"><span class="lineNum"> 299</span> : // This constructor will be used by the make_intrusive(), and also pybind11,</span>
|
|
<span id="L300"><span class="lineNum"> 300</span> : // which wrap the intrusive_ptr holder around the raw pointer and incref</span>
|
|
<span id="L301"><span class="lineNum"> 301</span> : // correspondingly (pybind11 requires raw pointer constructor to incref by</span>
|
|
<span id="L302"><span class="lineNum"> 302</span> : // default).</span>
|
|
<span id="L303"><span class="lineNum"> 303</span> <span class="tlaGNC"> 37304192 : explicit intrusive_ptr(TTarget* target)</span></span>
|
|
<span id="L304"><span class="lineNum"> 304</span> <span class="tlaGNC"> 37304192 : : intrusive_ptr(target, raw::DontIncreaseRefcount{}) {</span></span>
|
|
<span id="L305"><span class="lineNum"> 305</span> <span class="tlaGNC"> 37304192 : if (target_ != NullType::singleton()) {</span></span>
|
|
<span id="L306"><span class="lineNum"> 306</span> : // We just created result.target_, so we know no other thread has</span>
|
|
<span id="L307"><span class="lineNum"> 307</span> : // access to it, so we know we needn't care about memory ordering.</span>
|
|
<span id="L308"><span class="lineNum"> 308</span> : // (On x86_64, a store with memory_order_relaxed generates a plain old</span>
|
|
<span id="L309"><span class="lineNum"> 309</span> : // `mov`, whereas an atomic increment does a lock-prefixed `add`, which is</span>
|
|
<span id="L310"><span class="lineNum"> 310</span> : // much more expensive: https://godbolt.org/z/eKPzj8.)</span>
|
|
<span id="L311"><span class="lineNum"> 311</span> <span class="tlaGNC"> 37304192 : TORCH_INTERNAL_ASSERT_DEBUG_ONLY(</span></span>
|
|
<span id="L312"><span class="lineNum"> 312</span> : target_->refcount_ == 0 && target_->weakcount_ == 0,</span>
|
|
<span id="L313"><span class="lineNum"> 313</span> : "intrusive_ptr: Newly-created target had non-zero refcounts. Does its "</span>
|
|
<span id="L314"><span class="lineNum"> 314</span> : "constructor do something strange like incref or create an "</span>
|
|
<span id="L315"><span class="lineNum"> 315</span> : "intrusive_ptr from `this`?");</span>
|
|
<span id="L316"><span class="lineNum"> 316</span> <span class="tlaGNC"> 37304192 : target_->refcount_.store(1, std::memory_order_relaxed);</span></span>
|
|
<span id="L317"><span class="lineNum"> 317</span> <span class="tlaGNC"> 37304192 : target_->weakcount_.store(1, std::memory_order_relaxed);</span></span>
|
|
<span id="L318"><span class="lineNum"> 318</span> : }</span>
|
|
<span id="L319"><span class="lineNum"> 319</span> <span class="tlaGNC"> 37304192 : }</span></span>
|
|
<span id="L320"><span class="lineNum"> 320</span> : </span>
|
|
<span id="L321"><span class="lineNum"> 321</span> : public:</span>
|
|
<span id="L322"><span class="lineNum"> 322</span> : using element_type = TTarget;</span>
|
|
<span id="L323"><span class="lineNum"> 323</span> : </span>
|
|
<span id="L324"><span class="lineNum"> 324</span> <span class="tlaGNC"> 37370160 : intrusive_ptr() noexcept</span></span>
|
|
<span id="L325"><span class="lineNum"> 325</span> <span class="tlaGNC"> 37370160 : : intrusive_ptr(NullType::singleton(), raw::DontIncreaseRefcount{}) {}</span></span>
|
|
<span id="L326"><span class="lineNum"> 326</span> : </span>
|
|
<span id="L327"><span class="lineNum"> 327</span> : intrusive_ptr(std::nullptr_t) noexcept</span>
|
|
<span id="L328"><span class="lineNum"> 328</span> : : intrusive_ptr(NullType::singleton(), raw::DontIncreaseRefcount{}) {}</span>
|
|
<span id="L329"><span class="lineNum"> 329</span> : </span>
|
|
<span id="L330"><span class="lineNum"> 330</span> : // This constructor will not increase the ref counter for you.</span>
|
|
<span id="L331"><span class="lineNum"> 331</span> : // We use the tagged dispatch mechanism to explicitly mark this constructor</span>
|
|
<span id="L332"><span class="lineNum"> 332</span> : // to not increase the refcount</span>
|
|
<span id="L333"><span class="lineNum"> 333</span> <span class="tlaGNC"> 179950728 : explicit intrusive_ptr(TTarget* target, raw::DontIncreaseRefcount) noexcept</span></span>
|
|
<span id="L334"><span class="lineNum"> 334</span> <span class="tlaGNC"> 179950728 : : target_(target) {}</span></span>
|
|
<span id="L335"><span class="lineNum"> 335</span> : </span>
|
|
<span id="L336"><span class="lineNum"> 336</span> : explicit intrusive_ptr(std::unique_ptr<TTarget> rhs) noexcept</span>
|
|
<span id="L337"><span class="lineNum"> 337</span> : : intrusive_ptr(rhs.release()) {}</span>
|
|
<span id="L338"><span class="lineNum"> 338</span> : </span>
|
|
<span id="L339"><span class="lineNum"> 339</span> <span class="tlaGNC"> 772197404 : intrusive_ptr(intrusive_ptr&& rhs) noexcept : target_(rhs.target_) {</span></span>
|
|
<span id="L340"><span class="lineNum"> 340</span> <span class="tlaGNC"> 772197404 : rhs.target_ = NullType::singleton();</span></span>
|
|
<span id="L341"><span class="lineNum"> 341</span> <span class="tlaGNC"> 772197404 : }</span></span>
|
|
<span id="L342"><span class="lineNum"> 342</span> : </span>
|
|
<span id="L343"><span class="lineNum"> 343</span> : template <class From, class FromNullType></span>
|
|
<span id="L344"><span class="lineNum"> 344</span> : /* implicit */ intrusive_ptr(intrusive_ptr<From, FromNullType>&& rhs) noexcept</span>
|
|
<span id="L345"><span class="lineNum"> 345</span> : : target_(</span>
|
|
<span id="L346"><span class="lineNum"> 346</span> : detail::assign_ptr_<TTarget, NullType, FromNullType>(rhs.target_)) {</span>
|
|
<span id="L347"><span class="lineNum"> 347</span> : static_assert(</span>
|
|
<span id="L348"><span class="lineNum"> 348</span> : std::is_convertible<From*, TTarget*>::value,</span>
|
|
<span id="L349"><span class="lineNum"> 349</span> : "Type mismatch. intrusive_ptr move constructor got pointer of wrong type.");</span>
|
|
<span id="L350"><span class="lineNum"> 350</span> : rhs.target_ = FromNullType::singleton();</span>
|
|
<span id="L351"><span class="lineNum"> 351</span> : }</span>
|
|
<span id="L352"><span class="lineNum"> 352</span> : </span>
|
|
<span id="L353"><span class="lineNum"> 353</span> <span class="tlaGNC"> 62874 : intrusive_ptr(const intrusive_ptr& rhs) : target_(rhs.target_) {</span></span>
|
|
<span id="L354"><span class="lineNum"> 354</span> <span class="tlaGNC"> 62874 : retain_();</span></span>
|
|
<span id="L355"><span class="lineNum"> 355</span> <span class="tlaGNC"> 62874 : }</span></span>
|
|
<span id="L356"><span class="lineNum"> 356</span> : </span>
|
|
<span id="L357"><span class="lineNum"> 357</span> : template <class From, class FromNullType></span>
|
|
<span id="L358"><span class="lineNum"> 358</span> <span class="tlaGNC"> 378768 : /* implicit */ intrusive_ptr(const intrusive_ptr<From, FromNullType>& rhs)</span></span>
|
|
<span id="L359"><span class="lineNum"> 359</span> <span class="tlaGNC"> 378768 : : target_(</span></span>
|
|
<span id="L360"><span class="lineNum"> 360</span> <span class="tlaGNC"> 378768 : detail::assign_ptr_<TTarget, NullType, FromNullType>(rhs.target_)) {</span></span>
|
|
<span id="L361"><span class="lineNum"> 361</span> : static_assert(</span>
|
|
<span id="L362"><span class="lineNum"> 362</span> : std::is_convertible<From*, TTarget*>::value,</span>
|
|
<span id="L363"><span class="lineNum"> 363</span> : "Type mismatch. intrusive_ptr copy constructor got pointer of wrong type.");</span>
|
|
<span id="L364"><span class="lineNum"> 364</span> <span class="tlaGNC"> 378768 : retain_();</span></span>
|
|
<span id="L365"><span class="lineNum"> 365</span> <span class="tlaGNC"> 378768 : }</span></span>
|
|
<span id="L366"><span class="lineNum"> 366</span> : </span>
|
|
<span id="L367"><span class="lineNum"> 367</span> <span class="tlaGNC"> 710681430 : ~intrusive_ptr() noexcept {</span></span>
|
|
<span id="L368"><span class="lineNum"> 368</span> <span class="tlaGNC"> 710681430 : reset_();</span></span>
|
|
<span id="L369"><span class="lineNum"> 369</span> <span class="tlaGNC"> 710681430 : }</span></span>
|
|
<span id="L370"><span class="lineNum"> 370</span> : </span>
|
|
<span id="L371"><span class="lineNum"> 371</span> <span class="tlaGNC"> 435080 : intrusive_ptr& operator=(intrusive_ptr&& rhs) & noexcept {</span></span>
|
|
<span id="L372"><span class="lineNum"> 372</span> <span class="tlaGNC"> 435080 : return operator=<TTarget, NullType>(std::move(rhs));</span></span>
|
|
<span id="L373"><span class="lineNum"> 373</span> : }</span>
|
|
<span id="L374"><span class="lineNum"> 374</span> : </span>
|
|
<span id="L375"><span class="lineNum"> 375</span> : template <class From, class FromNullType></span>
|
|
<span id="L376"><span class="lineNum"> 376</span> <span class="tlaGNC"> 435080 : intrusive_ptr& operator=(intrusive_ptr<From, FromNullType>&& rhs) & noexcept {</span></span>
|
|
<span id="L377"><span class="lineNum"> 377</span> : static_assert(</span>
|
|
<span id="L378"><span class="lineNum"> 378</span> : std::is_convertible<From*, TTarget*>::value,</span>
|
|
<span id="L379"><span class="lineNum"> 379</span> : "Type mismatch. intrusive_ptr move assignment got pointer of wrong type.");</span>
|
|
<span id="L380"><span class="lineNum"> 380</span> <span class="tlaGNC"> 435080 : intrusive_ptr tmp = std::move(rhs);</span></span>
|
|
<span id="L381"><span class="lineNum"> 381</span> <span class="tlaGNC"> 435080 : swap(tmp);</span></span>
|
|
<span id="L382"><span class="lineNum"> 382</span> <span class="tlaGNC"> 870160 : return *this;</span></span>
|
|
<span id="L383"><span class="lineNum"> 383</span> <span class="tlaGNC"> 435080 : }</span></span>
|
|
<span id="L384"><span class="lineNum"> 384</span> : </span>
|
|
<span id="L385"><span class="lineNum"> 385</span> <span class="tlaGNC"> 1820 : intrusive_ptr& operator=(const intrusive_ptr& rhs) & noexcept {</span></span>
|
|
<span id="L386"><span class="lineNum"> 386</span> <span class="tlaGNC"> 1820 : return operator=<TTarget, NullType>(rhs);</span></span>
|
|
<span id="L387"><span class="lineNum"> 387</span> : }</span>
|
|
<span id="L388"><span class="lineNum"> 388</span> : </span>
|
|
<span id="L389"><span class="lineNum"> 389</span> : template <class From, class FromNullType></span>
|
|
<span id="L390"><span class="lineNum"> 390</span> <span class="tlaGNC"> 1820 : intrusive_ptr& operator=(const intrusive_ptr<From, NullType>& rhs) & {</span></span>
|
|
<span id="L391"><span class="lineNum"> 391</span> : static_assert(</span>
|
|
<span id="L392"><span class="lineNum"> 392</span> : std::is_convertible<From*, TTarget*>::value,</span>
|
|
<span id="L393"><span class="lineNum"> 393</span> : "Type mismatch. intrusive_ptr copy assignment got pointer of wrong type.");</span>
|
|
<span id="L394"><span class="lineNum"> 394</span> <span class="tlaGNC"> 1820 : intrusive_ptr tmp = rhs;</span></span>
|
|
<span id="L395"><span class="lineNum"> 395</span> <span class="tlaGNC"> 1820 : swap(tmp);</span></span>
|
|
<span id="L396"><span class="lineNum"> 396</span> <span class="tlaGNC"> 1820 : return *this;</span></span>
|
|
<span id="L397"><span class="lineNum"> 397</span> <span class="tlaGNC"> 1820 : }</span></span>
|
|
<span id="L398"><span class="lineNum"> 398</span> : </span>
|
|
<span id="L399"><span class="lineNum"> 399</span> <span class="tlaGNC"> 105695220 : TTarget* get() const noexcept {</span></span>
|
|
<span id="L400"><span class="lineNum"> 400</span> <span class="tlaGNC"> 105695220 : return target_;</span></span>
|
|
<span id="L401"><span class="lineNum"> 401</span> : }</span>
|
|
<span id="L402"><span class="lineNum"> 402</span> : </span>
|
|
<span id="L403"><span class="lineNum"> 403</span> <span class="tlaUNC tlaBgUNC"> 0 : TTarget& operator*() const noexcept {</span></span>
|
|
<span id="L404"><span class="lineNum"> 404</span> <span class="tlaUNC"> 0 : return *target_;</span></span>
|
|
<span id="L405"><span class="lineNum"> 405</span> : }</span>
|
|
<span id="L406"><span class="lineNum"> 406</span> : </span>
|
|
<span id="L407"><span class="lineNum"> 407</span> <span class="tlaGNC tlaBgGNC"> 121964308 : TTarget* operator->() const noexcept {</span></span>
|
|
<span id="L408"><span class="lineNum"> 408</span> : // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDelete)</span>
|
|
<span id="L409"><span class="lineNum"> 409</span> <span class="tlaGNC"> 121964308 : return target_;</span></span>
|
|
<span id="L410"><span class="lineNum"> 410</span> : }</span>
|
|
<span id="L411"><span class="lineNum"> 411</span> : </span>
|
|
<span id="L412"><span class="lineNum"> 412</span> <span class="tlaGNC"> 466880 : operator bool() const noexcept {</span></span>
|
|
<span id="L413"><span class="lineNum"> 413</span> <span class="tlaGNC"> 466880 : return target_ != NullType::singleton();</span></span>
|
|
<span id="L414"><span class="lineNum"> 414</span> : }</span>
|
|
<span id="L415"><span class="lineNum"> 415</span> : </span>
|
|
<span id="L416"><span class="lineNum"> 416</span> : void reset() noexcept {</span>
|
|
<span id="L417"><span class="lineNum"> 417</span> : reset_();</span>
|
|
<span id="L418"><span class="lineNum"> 418</span> : target_ = NullType::singleton();</span>
|
|
<span id="L419"><span class="lineNum"> 419</span> : }</span>
|
|
<span id="L420"><span class="lineNum"> 420</span> : </span>
|
|
<span id="L421"><span class="lineNum"> 421</span> <span class="tlaGNC"> 436900 : void swap(intrusive_ptr& rhs) noexcept {</span></span>
|
|
<span id="L422"><span class="lineNum"> 422</span> <span class="tlaGNC"> 436900 : TTarget* tmp = target_;</span></span>
|
|
<span id="L423"><span class="lineNum"> 423</span> <span class="tlaGNC"> 436900 : target_ = rhs.target_;</span></span>
|
|
<span id="L424"><span class="lineNum"> 424</span> <span class="tlaGNC"> 436900 : rhs.target_ = tmp;</span></span>
|
|
<span id="L425"><span class="lineNum"> 425</span> <span class="tlaGNC"> 436900 : }</span></span>
|
|
<span id="L426"><span class="lineNum"> 426</span> : </span>
|
|
<span id="L427"><span class="lineNum"> 427</span> : // We do a lot of null-pointer checks in our code, good to have this be cheap.</span>
|
|
<span id="L428"><span class="lineNum"> 428</span> : bool defined() const noexcept {</span>
|
|
<span id="L429"><span class="lineNum"> 429</span> : return target_ != NullType::singleton();</span>
|
|
<span id="L430"><span class="lineNum"> 430</span> : }</span>
|
|
<span id="L431"><span class="lineNum"> 431</span> : </span>
|
|
<span id="L432"><span class="lineNum"> 432</span> <span class="tlaGNC"> 458920 : size_t use_count() const noexcept {</span></span>
|
|
<span id="L433"><span class="lineNum"> 433</span> <span class="tlaGNC"> 458920 : if (target_ == NullType::singleton()) {</span></span>
|
|
<span id="L434"><span class="lineNum"> 434</span> <span class="tlaUNC tlaBgUNC"> 0 : return 0;</span></span>
|
|
<span id="L435"><span class="lineNum"> 435</span> : }</span>
|
|
<span id="L436"><span class="lineNum"> 436</span> <span class="tlaGNC tlaBgGNC"> 917840 : return target_->refcount_.load(std::memory_order_acquire);</span></span>
|
|
<span id="L437"><span class="lineNum"> 437</span> : }</span>
|
|
<span id="L438"><span class="lineNum"> 438</span> : </span>
|
|
<span id="L439"><span class="lineNum"> 439</span> : size_t weak_use_count() const noexcept {</span>
|
|
<span id="L440"><span class="lineNum"> 440</span> : if (target_ == NullType::singleton()) {</span>
|
|
<span id="L441"><span class="lineNum"> 441</span> : return 0;</span>
|
|
<span id="L442"><span class="lineNum"> 442</span> : }</span>
|
|
<span id="L443"><span class="lineNum"> 443</span> : return target_->weakcount_.load(std::memory_order_acquire);</span>
|
|
<span id="L444"><span class="lineNum"> 444</span> : }</span>
|
|
<span id="L445"><span class="lineNum"> 445</span> : </span>
|
|
<span id="L446"><span class="lineNum"> 446</span> : bool unique() const noexcept {</span>
|
|
<span id="L447"><span class="lineNum"> 447</span> : return use_count() == 1;</span>
|
|
<span id="L448"><span class="lineNum"> 448</span> : }</span>
|
|
<span id="L449"><span class="lineNum"> 449</span> : </span>
|
|
<span id="L450"><span class="lineNum"> 450</span> : /**</span>
|
|
<span id="L451"><span class="lineNum"> 451</span> : * Returns an owning (!) pointer to the underlying object and makes the</span>
|
|
<span id="L452"><span class="lineNum"> 452</span> : * intrusive_ptr instance invalid. That means the refcount is not decreased.</span>
|
|
<span id="L453"><span class="lineNum"> 453</span> : * You *must* put the returned pointer back into a intrusive_ptr using</span>
|
|
<span id="L454"><span class="lineNum"> 454</span> : * intrusive_ptr::reclaim(ptr) to properly destruct it.</span>
|
|
<span id="L455"><span class="lineNum"> 455</span> : * This is helpful for C APIs.</span>
|
|
<span id="L456"><span class="lineNum"> 456</span> : */</span>
|
|
<span id="L457"><span class="lineNum"> 457</span> : TTarget* release() noexcept {</span>
|
|
<span id="L458"><span class="lineNum"> 458</span> : // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.Assign)</span>
|
|
<span id="L459"><span class="lineNum"> 459</span> : TTarget* result = target_;</span>
|
|
<span id="L460"><span class="lineNum"> 460</span> : target_ = NullType::singleton();</span>
|
|
<span id="L461"><span class="lineNum"> 461</span> : return result;</span>
|
|
<span id="L462"><span class="lineNum"> 462</span> : }</span>
|
|
<span id="L463"><span class="lineNum"> 463</span> : </span>
|
|
<span id="L464"><span class="lineNum"> 464</span> : /**</span>
|
|
<span id="L465"><span class="lineNum"> 465</span> : * Takes an owning pointer to TTarget* and creates an intrusive_ptr that takes</span>
|
|
<span id="L466"><span class="lineNum"> 466</span> : * over ownership. That means the refcount is not increased.</span>
|
|
<span id="L467"><span class="lineNum"> 467</span> : * This is the counter-part to intrusive_ptr::release() and the pointer</span>
|
|
<span id="L468"><span class="lineNum"> 468</span> : * passed in *must* have been created using intrusive_ptr::release().</span>
|
|
<span id="L469"><span class="lineNum"> 469</span> : */</span>
|
|
<span id="L470"><span class="lineNum"> 470</span> <span class="tlaGNC"> 105276376 : static intrusive_ptr reclaim(TTarget* owning_ptr) {</span></span>
|
|
<span id="L471"><span class="lineNum"> 471</span> <span class="tlaGNC"> 315829128 : TORCH_INTERNAL_ASSERT_DEBUG_ONLY(</span></span>
|
|
<span id="L472"><span class="lineNum"> 472</span> : owning_ptr == NullType::singleton() ||</span>
|
|
<span id="L473"><span class="lineNum"> 473</span> : owning_ptr->refcount_.load() == 0 || owning_ptr->weakcount_.load(),</span>
|
|
<span id="L474"><span class="lineNum"> 474</span> : "TTarget violates the invariant that refcount > 0 => weakcount > 0");</span>
|
|
<span id="L475"><span class="lineNum"> 475</span> <span class="tlaGNC"> 105276376 : return intrusive_ptr(owning_ptr, raw::DontIncreaseRefcount{});</span></span>
|
|
<span id="L476"><span class="lineNum"> 476</span> : }</span>
|
|
<span id="L477"><span class="lineNum"> 477</span> : </span>
|
|
<span id="L478"><span class="lineNum"> 478</span> : /**</span>
|
|
<span id="L479"><span class="lineNum"> 479</span> : * Takes an owning pointer to TTarget* and creates an intrusive_ptr</span>
|
|
<span id="L480"><span class="lineNum"> 480</span> : * representing a new reference, i.e. the raw pointer retains</span>
|
|
<span id="L481"><span class="lineNum"> 481</span> : * ownership.</span>
|
|
<span id="L482"><span class="lineNum"> 482</span> : */</span>
|
|
<span id="L483"><span class="lineNum"> 483</span> : static intrusive_ptr reclaim_copy(TTarget* owning_ptr) {</span>
|
|
<span id="L484"><span class="lineNum"> 484</span> : auto ret = reclaim(owning_ptr);</span>
|
|
<span id="L485"><span class="lineNum"> 485</span> : ret.retain_();</span>
|
|
<span id="L486"><span class="lineNum"> 486</span> : return ret;</span>
|
|
<span id="L487"><span class="lineNum"> 487</span> : }</span>
|
|
<span id="L488"><span class="lineNum"> 488</span> : </span>
|
|
<span id="L489"><span class="lineNum"> 489</span> : /**</span>
|
|
<span id="L490"><span class="lineNum"> 490</span> : * Allocate a heap object with args and wrap it inside a intrusive_ptr and</span>
|
|
<span id="L491"><span class="lineNum"> 491</span> : * incref. This is a helper function to let make_intrusive() access private</span>
|
|
<span id="L492"><span class="lineNum"> 492</span> : * intrusive_ptr constructors.</span>
|
|
<span id="L493"><span class="lineNum"> 493</span> : */</span>
|
|
<span id="L494"><span class="lineNum"> 494</span> : template <class... Args></span>
|
|
<span id="L495"><span class="lineNum"> 495</span> <span class="tlaGNC"> 37304192 : static intrusive_ptr make(Args&&... args) {</span></span>
|
|
<span id="L496"><span class="lineNum"> 496</span> <span class="tlaGNC"> 37304192 : return intrusive_ptr(new TTarget(std::forward<Args>(args)...));</span></span>
|
|
<span id="L497"><span class="lineNum"> 497</span> : }</span>
|
|
<span id="L498"><span class="lineNum"> 498</span> : </span>
|
|
<span id="L499"><span class="lineNum"> 499</span> : /**</span>
|
|
<span id="L500"><span class="lineNum"> 500</span> : * Turn a new instance of TTarget (e.g., literally allocated</span>
|
|
<span id="L501"><span class="lineNum"> 501</span> : * using new TTarget(...) into an intrusive_ptr. If possible,</span>
|
|
<span id="L502"><span class="lineNum"> 502</span> : * use intrusive_ptr::make instead which statically guarantees</span>
|
|
<span id="L503"><span class="lineNum"> 503</span> : * that the allocation was done properly.</span>
|
|
<span id="L504"><span class="lineNum"> 504</span> : *</span>
|
|
<span id="L505"><span class="lineNum"> 505</span> : * At the moment, the only reason this method exists is because</span>
|
|
<span id="L506"><span class="lineNum"> 506</span> : * pybind11 holder types expect to be able to allocate in</span>
|
|
<span id="L507"><span class="lineNum"> 507</span> : * this way (because pybind11 handles the new allocation itself).</span>
|
|
<span id="L508"><span class="lineNum"> 508</span> : */</span>
|
|
<span id="L509"><span class="lineNum"> 509</span> : static intrusive_ptr unsafe_steal_from_new(TTarget* raw_ptr) {</span>
|
|
<span id="L510"><span class="lineNum"> 510</span> : return intrusive_ptr(raw_ptr);</span>
|
|
<span id="L511"><span class="lineNum"> 511</span> : }</span>
|
|
<span id="L512"><span class="lineNum"> 512</span> : </span>
|
|
<span id="L513"><span class="lineNum"> 513</span> : /**</span>
|
|
<span id="L514"><span class="lineNum"> 514</span> : * Turn an instance of TTarget that should not be reference counted</span>
|
|
<span id="L515"><span class="lineNum"> 515</span> : * (e.g., allocated into an arena with placement new) into an</span>
|
|
<span id="L516"><span class="lineNum"> 516</span> : * intrusive_ptr. This is gratuitously unsafe and should only be</span>
|
|
<span id="L517"><span class="lineNum"> 517</span> : * used if you can guarantee that the pointer will not escape and be</span>
|
|
<span id="L518"><span class="lineNum"> 518</span> : * refcounted as normal.</span>
|
|
<span id="L519"><span class="lineNum"> 519</span> : *</span>
|
|
<span id="L520"><span class="lineNum"> 520</span> : * `expected_decrefs` is a debugging parameter: it indicates the</span>
|
|
<span id="L521"><span class="lineNum"> 521</span> : * number of strong owners the intrusive_ptr_target in question is</span>
|
|
<span id="L522"><span class="lineNum"> 522</span> : * expected to get. In most use cases, this will likely be 1.</span>
|
|
<span id="L523"><span class="lineNum"> 523</span> : *</span>
|
|
<span id="L524"><span class="lineNum"> 524</span> : * The reason this method exists is for manually sharing</span>
|
|
<span id="L525"><span class="lineNum"> 525</span> : * StorageImpls across Tensors in the static runtime. It needs</span>
|
|
<span id="L526"><span class="lineNum"> 526</span> : * access to private intrusive_ptr members so that the refcounts can</span>
|
|
<span id="L527"><span class="lineNum"> 527</span> : * be initialized to custom values.</span>
|
|
<span id="L528"><span class="lineNum"> 528</span> : */</span>
|
|
<span id="L529"><span class="lineNum"> 529</span> : static intrusive_ptr unsafe_adapt_non_heap_allocated(</span>
|
|
<span id="L530"><span class="lineNum"> 530</span> : TTarget* raw_ptr,</span>
|
|
<span id="L531"><span class="lineNum"> 531</span> : size_t expected_decrefs) {</span>
|
|
<span id="L532"><span class="lineNum"> 532</span> : intrusive_ptr result(raw_ptr, raw::DontIncreaseRefcount{});</span>
|
|
<span id="L533"><span class="lineNum"> 533</span> : // INT_MAX is impractically huge for a reference count, while</span>
|
|
<span id="L534"><span class="lineNum"> 534</span> : // being in no danger of overflowing size_t. We actually only need to</span>
|
|
<span id="L535"><span class="lineNum"> 535</span> : // initialize the refcount to 2 -- we are just doing an unbalanced</span>
|
|
<span id="L536"><span class="lineNum"> 536</span> : // incref to prevent the non-heap-allocated target from being</span>
|
|
<span id="L537"><span class="lineNum"> 537</span> : // freed, and we are optimizing that incref by directly</span>
|
|
<span id="L538"><span class="lineNum"> 538</span> : // initializing the refcounts rather than doing an expensive</span>
|
|
<span id="L539"><span class="lineNum"> 539</span> : // atomic increment. The reason to use INT_MAX is to accommodate</span>
|
|
<span id="L540"><span class="lineNum"> 540</span> : // the debug assertions in ~intrusive_ptr_target.</span>
|
|
<span id="L541"><span class="lineNum"> 541</span> : #ifdef NDEBUG</span>
|
|
<span id="L542"><span class="lineNum"> 542</span> : expected_decrefs = 0;</span>
|
|
<span id="L543"><span class="lineNum"> 543</span> : #endif</span>
|
|
<span id="L544"><span class="lineNum"> 544</span> : result.target_->refcount_.store(</span>
|
|
<span id="L545"><span class="lineNum"> 545</span> : INT_MAX + expected_decrefs, std::memory_order_relaxed);</span>
|
|
<span id="L546"><span class="lineNum"> 546</span> : result.target_->weakcount_.store(INT_MAX, std::memory_order_relaxed);</span>
|
|
<span id="L547"><span class="lineNum"> 547</span> : return result;</span>
|
|
<span id="L548"><span class="lineNum"> 548</span> : }</span>
|
|
<span id="L549"><span class="lineNum"> 549</span> : </span>
|
|
<span id="L550"><span class="lineNum"> 550</span> : /**</span>
|
|
<span id="L551"><span class="lineNum"> 551</span> : * Turn a **non-owning raw pointer** to an intrusive_ptr. It is</span>
|
|
<span id="L552"><span class="lineNum"> 552</span> : * the moral equivalent of enable_shared_from_this on a shared pointer.</span>
|
|
<span id="L553"><span class="lineNum"> 553</span> : *</span>
|
|
<span id="L554"><span class="lineNum"> 554</span> : * This method is only valid for objects that are already live. If</span>
|
|
<span id="L555"><span class="lineNum"> 555</span> : * you are looking for the moral equivalent of unique_ptr<T>(T*)</span>
|
|
<span id="L556"><span class="lineNum"> 556</span> : * constructor, see steal_from_new.</span>
|
|
<span id="L557"><span class="lineNum"> 557</span> : *</span>
|
|
<span id="L558"><span class="lineNum"> 558</span> : * TODO: https://github.com/pytorch/pytorch/issues/56482</span>
|
|
<span id="L559"><span class="lineNum"> 559</span> : */</span>
|
|
<span id="L560"><span class="lineNum"> 560</span> : static intrusive_ptr unsafe_reclaim_from_nonowning(TTarget* raw_ptr) {</span>
|
|
<span id="L561"><span class="lineNum"> 561</span> : // See Note [Stack allocated intrusive_ptr_target safety]</span>
|
|
<span id="L562"><span class="lineNum"> 562</span> : TORCH_INTERNAL_ASSERT_DEBUG_ONLY(</span>
|
|
<span id="L563"><span class="lineNum"> 563</span> : raw_ptr == NullType::singleton() || raw_ptr->refcount_.load() > 0,</span>
|
|
<span id="L564"><span class="lineNum"> 564</span> : "intrusive_ptr: Can only reclaim pointers that are owned by someone");</span>
|
|
<span id="L565"><span class="lineNum"> 565</span> : auto ptr = reclaim(raw_ptr); // doesn't increase refcount</span>
|
|
<span id="L566"><span class="lineNum"> 566</span> : ptr.retain_();</span>
|
|
<span id="L567"><span class="lineNum"> 567</span> : return ptr;</span>
|
|
<span id="L568"><span class="lineNum"> 568</span> : }</span>
|
|
<span id="L569"><span class="lineNum"> 569</span> : };</span>
|
|
<span id="L570"><span class="lineNum"> 570</span> : </span>
|
|
<span id="L571"><span class="lineNum"> 571</span> : template <</span>
|
|
<span id="L572"><span class="lineNum"> 572</span> : class TTarget,</span>
|
|
<span id="L573"><span class="lineNum"> 573</span> : class NullType = detail::intrusive_target_default_null_type<TTarget>,</span>
|
|
<span id="L574"><span class="lineNum"> 574</span> : class... Args></span>
|
|
<span id="L575"><span class="lineNum"> 575</span> <span class="tlaGNC"> 37304192 : inline intrusive_ptr<TTarget, NullType> make_intrusive(Args&&... args) {</span></span>
|
|
<span id="L576"><span class="lineNum"> 576</span> <span class="tlaGNC"> 37304192 : return intrusive_ptr<TTarget, NullType>::make(std::forward<Args>(args)...);</span></span>
|
|
<span id="L577"><span class="lineNum"> 577</span> : }</span>
|
|
<span id="L578"><span class="lineNum"> 578</span> : </span>
|
|
<span id="L579"><span class="lineNum"> 579</span> : template <class TTarget, class NullType></span>
|
|
<span id="L580"><span class="lineNum"> 580</span> : inline void swap(</span>
|
|
<span id="L581"><span class="lineNum"> 581</span> : intrusive_ptr<TTarget, NullType>& lhs,</span>
|
|
<span id="L582"><span class="lineNum"> 582</span> : intrusive_ptr<TTarget, NullType>& rhs) noexcept {</span>
|
|
<span id="L583"><span class="lineNum"> 583</span> : lhs.swap(rhs);</span>
|
|
<span id="L584"><span class="lineNum"> 584</span> : }</span>
|
|
<span id="L585"><span class="lineNum"> 585</span> : </span>
|
|
<span id="L586"><span class="lineNum"> 586</span> : // To allow intrusive_ptr inside std::map or std::set, we need operator<</span>
|
|
<span id="L587"><span class="lineNum"> 587</span> : template <class TTarget1, class NullType1, class TTarget2, class NullType2></span>
|
|
<span id="L588"><span class="lineNum"> 588</span> : inline bool operator<(</span>
|
|
<span id="L589"><span class="lineNum"> 589</span> : const intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L590"><span class="lineNum"> 590</span> : const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {</span>
|
|
<span id="L591"><span class="lineNum"> 591</span> : return lhs.get() < rhs.get();</span>
|
|
<span id="L592"><span class="lineNum"> 592</span> : }</span>
|
|
<span id="L593"><span class="lineNum"> 593</span> : </span>
|
|
<span id="L594"><span class="lineNum"> 594</span> : template <class TTarget1, class NullType1, class TTarget2, class NullType2></span>
|
|
<span id="L595"><span class="lineNum"> 595</span> : inline bool operator==(</span>
|
|
<span id="L596"><span class="lineNum"> 596</span> : const intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L597"><span class="lineNum"> 597</span> : const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {</span>
|
|
<span id="L598"><span class="lineNum"> 598</span> : return lhs.get() == rhs.get();</span>
|
|
<span id="L599"><span class="lineNum"> 599</span> : }</span>
|
|
<span id="L600"><span class="lineNum"> 600</span> : </span>
|
|
<span id="L601"><span class="lineNum"> 601</span> : template <class TTarget1, class NullType1></span>
|
|
<span id="L602"><span class="lineNum"> 602</span> : inline bool operator==(</span>
|
|
<span id="L603"><span class="lineNum"> 603</span> : const intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L604"><span class="lineNum"> 604</span> : std::nullptr_t) noexcept {</span>
|
|
<span id="L605"><span class="lineNum"> 605</span> : return lhs.get() == nullptr;</span>
|
|
<span id="L606"><span class="lineNum"> 606</span> : }</span>
|
|
<span id="L607"><span class="lineNum"> 607</span> : </span>
|
|
<span id="L608"><span class="lineNum"> 608</span> : template <class TTarget2, class NullType2></span>
|
|
<span id="L609"><span class="lineNum"> 609</span> : inline bool operator==(</span>
|
|
<span id="L610"><span class="lineNum"> 610</span> : std::nullptr_t,</span>
|
|
<span id="L611"><span class="lineNum"> 611</span> : const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {</span>
|
|
<span id="L612"><span class="lineNum"> 612</span> : return nullptr == rhs.get();</span>
|
|
<span id="L613"><span class="lineNum"> 613</span> : }</span>
|
|
<span id="L614"><span class="lineNum"> 614</span> : </span>
|
|
<span id="L615"><span class="lineNum"> 615</span> : template <class TTarget1, class NullType1, class TTarget2, class NullType2></span>
|
|
<span id="L616"><span class="lineNum"> 616</span> : inline bool operator!=(</span>
|
|
<span id="L617"><span class="lineNum"> 617</span> : const intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L618"><span class="lineNum"> 618</span> : const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {</span>
|
|
<span id="L619"><span class="lineNum"> 619</span> : return !operator==(lhs, rhs);</span>
|
|
<span id="L620"><span class="lineNum"> 620</span> : }</span>
|
|
<span id="L621"><span class="lineNum"> 621</span> : </span>
|
|
<span id="L622"><span class="lineNum"> 622</span> : template <class TTarget1, class NullType1></span>
|
|
<span id="L623"><span class="lineNum"> 623</span> : inline bool operator!=(</span>
|
|
<span id="L624"><span class="lineNum"> 624</span> : const intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L625"><span class="lineNum"> 625</span> : std::nullptr_t) noexcept {</span>
|
|
<span id="L626"><span class="lineNum"> 626</span> : return !operator==(lhs, nullptr);</span>
|
|
<span id="L627"><span class="lineNum"> 627</span> : }</span>
|
|
<span id="L628"><span class="lineNum"> 628</span> : </span>
|
|
<span id="L629"><span class="lineNum"> 629</span> : template <class TTarget2, class NullType2></span>
|
|
<span id="L630"><span class="lineNum"> 630</span> : inline bool operator!=(</span>
|
|
<span id="L631"><span class="lineNum"> 631</span> : std::nullptr_t,</span>
|
|
<span id="L632"><span class="lineNum"> 632</span> : const intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {</span>
|
|
<span id="L633"><span class="lineNum"> 633</span> : return !operator==(nullptr, rhs);</span>
|
|
<span id="L634"><span class="lineNum"> 634</span> : }</span>
|
|
<span id="L635"><span class="lineNum"> 635</span> : template <typename T></span>
|
|
<span id="L636"><span class="lineNum"> 636</span> : struct MaybeOwnedTraits<c10::intrusive_ptr<T>> {</span>
|
|
<span id="L637"><span class="lineNum"> 637</span> : using owned_type = c10::intrusive_ptr<T>;</span>
|
|
<span id="L638"><span class="lineNum"> 638</span> : using borrow_type = c10::intrusive_ptr<T>;</span>
|
|
<span id="L639"><span class="lineNum"> 639</span> : </span>
|
|
<span id="L640"><span class="lineNum"> 640</span> : static borrow_type createBorrow(const owned_type& from) {</span>
|
|
<span id="L641"><span class="lineNum"> 641</span> : return borrow_type::reclaim(from.get());</span>
|
|
<span id="L642"><span class="lineNum"> 642</span> : }</span>
|
|
<span id="L643"><span class="lineNum"> 643</span> : </span>
|
|
<span id="L644"><span class="lineNum"> 644</span> : static void assignBorrow(borrow_type& lhs, const borrow_type& rhs) {</span>
|
|
<span id="L645"><span class="lineNum"> 645</span> : lhs.release();</span>
|
|
<span id="L646"><span class="lineNum"> 646</span> : lhs = borrow_type::reclaim(rhs.get());</span>
|
|
<span id="L647"><span class="lineNum"> 647</span> : }</span>
|
|
<span id="L648"><span class="lineNum"> 648</span> : </span>
|
|
<span id="L649"><span class="lineNum"> 649</span> : static void destroyBorrow(borrow_type& toDestroy) {</span>
|
|
<span id="L650"><span class="lineNum"> 650</span> : toDestroy.release();</span>
|
|
<span id="L651"><span class="lineNum"> 651</span> : }</span>
|
|
<span id="L652"><span class="lineNum"> 652</span> : </span>
|
|
<span id="L653"><span class="lineNum"> 653</span> : static const owned_type& referenceFromBorrow(const borrow_type& borrow) {</span>
|
|
<span id="L654"><span class="lineNum"> 654</span> : return borrow;</span>
|
|
<span id="L655"><span class="lineNum"> 655</span> : }</span>
|
|
<span id="L656"><span class="lineNum"> 656</span> : </span>
|
|
<span id="L657"><span class="lineNum"> 657</span> : static const owned_type* pointerFromBorrow(const borrow_type& borrow) {</span>
|
|
<span id="L658"><span class="lineNum"> 658</span> : return &borrow;</span>
|
|
<span id="L659"><span class="lineNum"> 659</span> : }</span>
|
|
<span id="L660"><span class="lineNum"> 660</span> : </span>
|
|
<span id="L661"><span class="lineNum"> 661</span> : static bool debugBorrowIsValid(const borrow_type& /*borrow*/) {</span>
|
|
<span id="L662"><span class="lineNum"> 662</span> : return true;</span>
|
|
<span id="L663"><span class="lineNum"> 663</span> : }</span>
|
|
<span id="L664"><span class="lineNum"> 664</span> : };</span>
|
|
<span id="L665"><span class="lineNum"> 665</span> : </span>
|
|
<span id="L666"><span class="lineNum"> 666</span> : template <</span>
|
|
<span id="L667"><span class="lineNum"> 667</span> : typename TTarget,</span>
|
|
<span id="L668"><span class="lineNum"> 668</span> : class NullType = detail::intrusive_target_default_null_type<TTarget>></span>
|
|
<span id="L669"><span class="lineNum"> 669</span> : class weak_intrusive_ptr final {</span>
|
|
<span id="L670"><span class="lineNum"> 670</span> : private:</span>
|
|
<span id="L671"><span class="lineNum"> 671</span> : static_assert(</span>
|
|
<span id="L672"><span class="lineNum"> 672</span> : std::is_base_of<intrusive_ptr_target, TTarget>::value,</span>
|
|
<span id="L673"><span class="lineNum"> 673</span> : "intrusive_ptr can only be used for classes that inherit from intrusive_ptr_target.");</span>
|
|
<span id="L674"><span class="lineNum"> 674</span> : #ifndef _WIN32</span>
|
|
<span id="L675"><span class="lineNum"> 675</span> : // This static_assert triggers on MSVC</span>
|
|
<span id="L676"><span class="lineNum"> 676</span> : // error C2131: expression did not evaluate to a constant</span>
|
|
<span id="L677"><span class="lineNum"> 677</span> : static_assert(</span>
|
|
<span id="L678"><span class="lineNum"> 678</span> : NullType::singleton() == NullType::singleton(),</span>
|
|
<span id="L679"><span class="lineNum"> 679</span> : "NullType must have a constexpr singleton() method");</span>
|
|
<span id="L680"><span class="lineNum"> 680</span> : #endif</span>
|
|
<span id="L681"><span class="lineNum"> 681</span> : static_assert(</span>
|
|
<span id="L682"><span class="lineNum"> 682</span> : std::is_base_of<</span>
|
|
<span id="L683"><span class="lineNum"> 683</span> : TTarget,</span>
|
|
<span id="L684"><span class="lineNum"> 684</span> : typename std::remove_pointer<decltype(NullType::singleton())>::type>::</span>
|
|
<span id="L685"><span class="lineNum"> 685</span> : value,</span>
|
|
<span id="L686"><span class="lineNum"> 686</span> : "NullType::singleton() must return a element_type* pointer");</span>
|
|
<span id="L687"><span class="lineNum"> 687</span> : </span>
|
|
<span id="L688"><span class="lineNum"> 688</span> : TTarget* target_;</span>
|
|
<span id="L689"><span class="lineNum"> 689</span> : </span>
|
|
<span id="L690"><span class="lineNum"> 690</span> : template <class TTarget2, class NullType2></span>
|
|
<span id="L691"><span class="lineNum"> 691</span> : friend class weak_intrusive_ptr;</span>
|
|
<span id="L692"><span class="lineNum"> 692</span> : </span>
|
|
<span id="L693"><span class="lineNum"> 693</span> : void retain_() {</span>
|
|
<span id="L694"><span class="lineNum"> 694</span> : if (target_ != NullType::singleton()) {</span>
|
|
<span id="L695"><span class="lineNum"> 695</span> : size_t new_weakcount =</span>
|
|
<span id="L696"><span class="lineNum"> 696</span> : detail::atomic_weakcount_increment(target_->weakcount_);</span>
|
|
<span id="L697"><span class="lineNum"> 697</span> : TORCH_INTERNAL_ASSERT_DEBUG_ONLY(</span>
|
|
<span id="L698"><span class="lineNum"> 698</span> : new_weakcount != 1,</span>
|
|
<span id="L699"><span class="lineNum"> 699</span> : "weak_intrusive_ptr: Cannot increase weakcount after it reached zero.");</span>
|
|
<span id="L700"><span class="lineNum"> 700</span> : }</span>
|
|
<span id="L701"><span class="lineNum"> 701</span> : }</span>
|
|
<span id="L702"><span class="lineNum"> 702</span> : </span>
|
|
<span id="L703"><span class="lineNum"> 703</span> : void reset_() noexcept {</span>
|
|
<span id="L704"><span class="lineNum"> 704</span> : if (target_ != NullType::singleton() &&</span>
|
|
<span id="L705"><span class="lineNum"> 705</span> : detail::atomic_weakcount_decrement(target_->weakcount_) == 0) {</span>
|
|
<span id="L706"><span class="lineNum"> 706</span> : // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDelete)</span>
|
|
<span id="L707"><span class="lineNum"> 707</span> : delete target_;</span>
|
|
<span id="L708"><span class="lineNum"> 708</span> : }</span>
|
|
<span id="L709"><span class="lineNum"> 709</span> : target_ = NullType::singleton();</span>
|
|
<span id="L710"><span class="lineNum"> 710</span> : }</span>
|
|
<span id="L711"><span class="lineNum"> 711</span> : </span>
|
|
<span id="L712"><span class="lineNum"> 712</span> : constexpr explicit weak_intrusive_ptr(TTarget* target) : target_(target) {}</span>
|
|
<span id="L713"><span class="lineNum"> 713</span> : </span>
|
|
<span id="L714"><span class="lineNum"> 714</span> : public:</span>
|
|
<span id="L715"><span class="lineNum"> 715</span> : using element_type = TTarget;</span>
|
|
<span id="L716"><span class="lineNum"> 716</span> : </span>
|
|
<span id="L717"><span class="lineNum"> 717</span> : explicit weak_intrusive_ptr(const intrusive_ptr<TTarget, NullType>& ptr)</span>
|
|
<span id="L718"><span class="lineNum"> 718</span> : : weak_intrusive_ptr(ptr.get()) {</span>
|
|
<span id="L719"><span class="lineNum"> 719</span> : retain_();</span>
|
|
<span id="L720"><span class="lineNum"> 720</span> : }</span>
|
|
<span id="L721"><span class="lineNum"> 721</span> : </span>
|
|
<span id="L722"><span class="lineNum"> 722</span> : weak_intrusive_ptr(weak_intrusive_ptr&& rhs) noexcept : target_(rhs.target_) {</span>
|
|
<span id="L723"><span class="lineNum"> 723</span> : rhs.target_ = NullType::singleton();</span>
|
|
<span id="L724"><span class="lineNum"> 724</span> : }</span>
|
|
<span id="L725"><span class="lineNum"> 725</span> : </span>
|
|
<span id="L726"><span class="lineNum"> 726</span> : template <class From, class FromNullType></span>
|
|
<span id="L727"><span class="lineNum"> 727</span> : /* implicit */ weak_intrusive_ptr(</span>
|
|
<span id="L728"><span class="lineNum"> 728</span> : weak_intrusive_ptr<From, FromNullType>&& rhs) noexcept</span>
|
|
<span id="L729"><span class="lineNum"> 729</span> : : target_(</span>
|
|
<span id="L730"><span class="lineNum"> 730</span> : detail::assign_ptr_<TTarget, NullType, FromNullType>(rhs.target_)) {</span>
|
|
<span id="L731"><span class="lineNum"> 731</span> : static_assert(</span>
|
|
<span id="L732"><span class="lineNum"> 732</span> : std::is_convertible<From*, TTarget*>::value,</span>
|
|
<span id="L733"><span class="lineNum"> 733</span> : "Type mismatch. weak_intrusive_ptr move constructor got pointer of wrong type.");</span>
|
|
<span id="L734"><span class="lineNum"> 734</span> : rhs.target_ = FromNullType::singleton();</span>
|
|
<span id="L735"><span class="lineNum"> 735</span> : }</span>
|
|
<span id="L736"><span class="lineNum"> 736</span> : </span>
|
|
<span id="L737"><span class="lineNum"> 737</span> : weak_intrusive_ptr(const weak_intrusive_ptr& rhs) : target_(rhs.target_) {</span>
|
|
<span id="L738"><span class="lineNum"> 738</span> : retain_();</span>
|
|
<span id="L739"><span class="lineNum"> 739</span> : }</span>
|
|
<span id="L740"><span class="lineNum"> 740</span> : </span>
|
|
<span id="L741"><span class="lineNum"> 741</span> : template <class From, class FromNullType></span>
|
|
<span id="L742"><span class="lineNum"> 742</span> : /* implicit */ weak_intrusive_ptr(</span>
|
|
<span id="L743"><span class="lineNum"> 743</span> : const weak_intrusive_ptr<From, FromNullType>& rhs)</span>
|
|
<span id="L744"><span class="lineNum"> 744</span> : : target_(</span>
|
|
<span id="L745"><span class="lineNum"> 745</span> : detail::assign_ptr_<TTarget, NullType, FromNullType>(rhs.target_)) {</span>
|
|
<span id="L746"><span class="lineNum"> 746</span> : static_assert(</span>
|
|
<span id="L747"><span class="lineNum"> 747</span> : std::is_convertible<From*, TTarget*>::value,</span>
|
|
<span id="L748"><span class="lineNum"> 748</span> : "Type mismatch. weak_intrusive_ptr copy constructor got pointer of wrong type.");</span>
|
|
<span id="L749"><span class="lineNum"> 749</span> : retain_();</span>
|
|
<span id="L750"><span class="lineNum"> 750</span> : }</span>
|
|
<span id="L751"><span class="lineNum"> 751</span> : </span>
|
|
<span id="L752"><span class="lineNum"> 752</span> : ~weak_intrusive_ptr() noexcept {</span>
|
|
<span id="L753"><span class="lineNum"> 753</span> : reset_();</span>
|
|
<span id="L754"><span class="lineNum"> 754</span> : }</span>
|
|
<span id="L755"><span class="lineNum"> 755</span> : </span>
|
|
<span id="L756"><span class="lineNum"> 756</span> : weak_intrusive_ptr& operator=(weak_intrusive_ptr&& rhs) & noexcept {</span>
|
|
<span id="L757"><span class="lineNum"> 757</span> : return operator=<TTarget, NullType>(std::move(rhs));</span>
|
|
<span id="L758"><span class="lineNum"> 758</span> : }</span>
|
|
<span id="L759"><span class="lineNum"> 759</span> : </span>
|
|
<span id="L760"><span class="lineNum"> 760</span> : template <class From, class FromNullType></span>
|
|
<span id="L761"><span class="lineNum"> 761</span> : weak_intrusive_ptr& operator=(</span>
|
|
<span id="L762"><span class="lineNum"> 762</span> : weak_intrusive_ptr<From, FromNullType>&& rhs) & noexcept {</span>
|
|
<span id="L763"><span class="lineNum"> 763</span> : static_assert(</span>
|
|
<span id="L764"><span class="lineNum"> 764</span> : std::is_convertible<From*, TTarget*>::value,</span>
|
|
<span id="L765"><span class="lineNum"> 765</span> : "Type mismatch. weak_intrusive_ptr move assignment got pointer of wrong type.");</span>
|
|
<span id="L766"><span class="lineNum"> 766</span> : weak_intrusive_ptr tmp = std::move(rhs);</span>
|
|
<span id="L767"><span class="lineNum"> 767</span> : swap(tmp);</span>
|
|
<span id="L768"><span class="lineNum"> 768</span> : return *this;</span>
|
|
<span id="L769"><span class="lineNum"> 769</span> : }</span>
|
|
<span id="L770"><span class="lineNum"> 770</span> : </span>
|
|
<span id="L771"><span class="lineNum"> 771</span> : weak_intrusive_ptr& operator=(const weak_intrusive_ptr& rhs) & noexcept {</span>
|
|
<span id="L772"><span class="lineNum"> 772</span> : return operator=<TTarget, NullType>(rhs);</span>
|
|
<span id="L773"><span class="lineNum"> 773</span> : }</span>
|
|
<span id="L774"><span class="lineNum"> 774</span> : </span>
|
|
<span id="L775"><span class="lineNum"> 775</span> : weak_intrusive_ptr& operator=(</span>
|
|
<span id="L776"><span class="lineNum"> 776</span> : const intrusive_ptr<TTarget, NullType>& rhs) & noexcept {</span>
|
|
<span id="L777"><span class="lineNum"> 777</span> : weak_intrusive_ptr tmp(rhs);</span>
|
|
<span id="L778"><span class="lineNum"> 778</span> : swap(tmp);</span>
|
|
<span id="L779"><span class="lineNum"> 779</span> : return *this;</span>
|
|
<span id="L780"><span class="lineNum"> 780</span> : }</span>
|
|
<span id="L781"><span class="lineNum"> 781</span> : </span>
|
|
<span id="L782"><span class="lineNum"> 782</span> : template <class From, class FromNullType></span>
|
|
<span id="L783"><span class="lineNum"> 783</span> : weak_intrusive_ptr& operator=(</span>
|
|
<span id="L784"><span class="lineNum"> 784</span> : const weak_intrusive_ptr<From, NullType>& rhs) & {</span>
|
|
<span id="L785"><span class="lineNum"> 785</span> : static_assert(</span>
|
|
<span id="L786"><span class="lineNum"> 786</span> : std::is_convertible<From*, TTarget*>::value,</span>
|
|
<span id="L787"><span class="lineNum"> 787</span> : "Type mismatch. weak_intrusive_ptr copy assignment got pointer of wrong type.");</span>
|
|
<span id="L788"><span class="lineNum"> 788</span> : weak_intrusive_ptr tmp = rhs;</span>
|
|
<span id="L789"><span class="lineNum"> 789</span> : swap(tmp);</span>
|
|
<span id="L790"><span class="lineNum"> 790</span> : return *this;</span>
|
|
<span id="L791"><span class="lineNum"> 791</span> : }</span>
|
|
<span id="L792"><span class="lineNum"> 792</span> : </span>
|
|
<span id="L793"><span class="lineNum"> 793</span> : void reset() noexcept {</span>
|
|
<span id="L794"><span class="lineNum"> 794</span> : reset_();</span>
|
|
<span id="L795"><span class="lineNum"> 795</span> : }</span>
|
|
<span id="L796"><span class="lineNum"> 796</span> : </span>
|
|
<span id="L797"><span class="lineNum"> 797</span> : void swap(weak_intrusive_ptr& rhs) noexcept {</span>
|
|
<span id="L798"><span class="lineNum"> 798</span> : TTarget* tmp = target_;</span>
|
|
<span id="L799"><span class="lineNum"> 799</span> : target_ = rhs.target_;</span>
|
|
<span id="L800"><span class="lineNum"> 800</span> : rhs.target_ = tmp;</span>
|
|
<span id="L801"><span class="lineNum"> 801</span> : }</span>
|
|
<span id="L802"><span class="lineNum"> 802</span> : </span>
|
|
<span id="L803"><span class="lineNum"> 803</span> : // NB: This should ONLY be used by the std::hash implementation</span>
|
|
<span id="L804"><span class="lineNum"> 804</span> : // for weak_intrusive_ptr. Another way you could do this is</span>
|
|
<span id="L805"><span class="lineNum"> 805</span> : // friend std::hash<weak_intrusive_ptr>, but this triggers two</span>
|
|
<span id="L806"><span class="lineNum"> 806</span> : // bugs:</span>
|
|
<span id="L807"><span class="lineNum"> 807</span> : //</span>
|
|
<span id="L808"><span class="lineNum"> 808</span> : // (1) It triggers an nvcc bug, where std::hash in a friend class</span>
|
|
<span id="L809"><span class="lineNum"> 809</span> : // declaration gets preprocessed into hash, which then cannot</span>
|
|
<span id="L810"><span class="lineNum"> 810</span> : // actually be found. The error in this case looks like:</span>
|
|
<span id="L811"><span class="lineNum"> 811</span> : //</span>
|
|
<span id="L812"><span class="lineNum"> 812</span> : // error: no template named 'hash'; did you mean 'std::hash'?</span>
|
|
<span id="L813"><span class="lineNum"> 813</span> : //</span>
|
|
<span id="L814"><span class="lineNum"> 814</span> : // (2) On OS X, std::hash is declared as a struct, not a class.</span>
|
|
<span id="L815"><span class="lineNum"> 815</span> : // This twings:</span>
|
|
<span id="L816"><span class="lineNum"> 816</span> : //</span>
|
|
<span id="L817"><span class="lineNum"> 817</span> : // error: class 'hash' was previously declared as a struct</span>
|
|
<span id="L818"><span class="lineNum"> 818</span> : // [-Werror,-Wmismatched-tags]</span>
|
|
<span id="L819"><span class="lineNum"> 819</span> : //</span>
|
|
<span id="L820"><span class="lineNum"> 820</span> : // Both of these are work-aroundable, but on the whole, I decided</span>
|
|
<span id="L821"><span class="lineNum"> 821</span> : // it would be simpler and easier to make work if we just expose</span>
|
|
<span id="L822"><span class="lineNum"> 822</span> : // an unsafe getter for target_</span>
|
|
<span id="L823"><span class="lineNum"> 823</span> : //</span>
|
|
<span id="L824"><span class="lineNum"> 824</span> : TTarget* _unsafe_get_target() const noexcept {</span>
|
|
<span id="L825"><span class="lineNum"> 825</span> : return target_;</span>
|
|
<span id="L826"><span class="lineNum"> 826</span> : }</span>
|
|
<span id="L827"><span class="lineNum"> 827</span> : </span>
|
|
<span id="L828"><span class="lineNum"> 828</span> : size_t use_count() const noexcept {</span>
|
|
<span id="L829"><span class="lineNum"> 829</span> : if (target_ == NullType::singleton()) {</span>
|
|
<span id="L830"><span class="lineNum"> 830</span> : return 0;</span>
|
|
<span id="L831"><span class="lineNum"> 831</span> : }</span>
|
|
<span id="L832"><span class="lineNum"> 832</span> : return target_->refcount_.load(</span>
|
|
<span id="L833"><span class="lineNum"> 833</span> : std::memory_order_acquire); // refcount, not weakcount!</span>
|
|
<span id="L834"><span class="lineNum"> 834</span> : }</span>
|
|
<span id="L835"><span class="lineNum"> 835</span> : </span>
|
|
<span id="L836"><span class="lineNum"> 836</span> : size_t weak_use_count() const noexcept {</span>
|
|
<span id="L837"><span class="lineNum"> 837</span> : if (target_ == NullType::singleton()) {</span>
|
|
<span id="L838"><span class="lineNum"> 838</span> : return 0;</span>
|
|
<span id="L839"><span class="lineNum"> 839</span> : }</span>
|
|
<span id="L840"><span class="lineNum"> 840</span> : return target_->weakcount_.load(std::memory_order_acquire);</span>
|
|
<span id="L841"><span class="lineNum"> 841</span> : }</span>
|
|
<span id="L842"><span class="lineNum"> 842</span> : </span>
|
|
<span id="L843"><span class="lineNum"> 843</span> : bool expired() const noexcept {</span>
|
|
<span id="L844"><span class="lineNum"> 844</span> : return use_count() == 0;</span>
|
|
<span id="L845"><span class="lineNum"> 845</span> : }</span>
|
|
<span id="L846"><span class="lineNum"> 846</span> : </span>
|
|
<span id="L847"><span class="lineNum"> 847</span> : intrusive_ptr<TTarget, NullType> lock() const noexcept {</span>
|
|
<span id="L848"><span class="lineNum"> 848</span> : if (expired()) {</span>
|
|
<span id="L849"><span class="lineNum"> 849</span> : return intrusive_ptr<TTarget, NullType>();</span>
|
|
<span id="L850"><span class="lineNum"> 850</span> : } else {</span>
|
|
<span id="L851"><span class="lineNum"> 851</span> : auto refcount = target_->refcount_.load(std::memory_order_seq_cst);</span>
|
|
<span id="L852"><span class="lineNum"> 852</span> : do {</span>
|
|
<span id="L853"><span class="lineNum"> 853</span> : if (refcount == 0) {</span>
|
|
<span id="L854"><span class="lineNum"> 854</span> : // Object already destructed, no strong references left anymore.</span>
|
|
<span id="L855"><span class="lineNum"> 855</span> : // Return nullptr.</span>
|
|
<span id="L856"><span class="lineNum"> 856</span> : return intrusive_ptr<TTarget, NullType>();</span>
|
|
<span id="L857"><span class="lineNum"> 857</span> : }</span>
|
|
<span id="L858"><span class="lineNum"> 858</span> : } while (</span>
|
|
<span id="L859"><span class="lineNum"> 859</span> : !target_->refcount_.compare_exchange_weak(refcount, refcount + 1));</span>
|
|
<span id="L860"><span class="lineNum"> 860</span> : return intrusive_ptr<TTarget, NullType>(</span>
|
|
<span id="L861"><span class="lineNum"> 861</span> : target_, raw::DontIncreaseRefcount{});</span>
|
|
<span id="L862"><span class="lineNum"> 862</span> : }</span>
|
|
<span id="L863"><span class="lineNum"> 863</span> : }</span>
|
|
<span id="L864"><span class="lineNum"> 864</span> : </span>
|
|
<span id="L865"><span class="lineNum"> 865</span> : /**</span>
|
|
<span id="L866"><span class="lineNum"> 866</span> : * Returns an owning (but still only weakly referenced) pointer to the</span>
|
|
<span id="L867"><span class="lineNum"> 867</span> : * underlying object and makes the weak_intrusive_ptr instance invalid.</span>
|
|
<span id="L868"><span class="lineNum"> 868</span> : * That means the weakcount is not decreased.</span>
|
|
<span id="L869"><span class="lineNum"> 869</span> : * You *must* put the returned pointer back into a weak_intrusive_ptr using</span>
|
|
<span id="L870"><span class="lineNum"> 870</span> : * weak_intrusive_ptr::reclaim(ptr) to properly destruct it.</span>
|
|
<span id="L871"><span class="lineNum"> 871</span> : * This is helpful for C APIs.</span>
|
|
<span id="L872"><span class="lineNum"> 872</span> : */</span>
|
|
<span id="L873"><span class="lineNum"> 873</span> : TTarget* release() noexcept {</span>
|
|
<span id="L874"><span class="lineNum"> 874</span> : TTarget* result = target_;</span>
|
|
<span id="L875"><span class="lineNum"> 875</span> : target_ = NullType::singleton();</span>
|
|
<span id="L876"><span class="lineNum"> 876</span> : return result;</span>
|
|
<span id="L877"><span class="lineNum"> 877</span> : }</span>
|
|
<span id="L878"><span class="lineNum"> 878</span> : </span>
|
|
<span id="L879"><span class="lineNum"> 879</span> : /**</span>
|
|
<span id="L880"><span class="lineNum"> 880</span> : * Takes an owning (but must be weakly referenced) pointer to TTarget* and</span>
|
|
<span id="L881"><span class="lineNum"> 881</span> : * creates a weak_intrusive_ptr that takes over ownership.</span>
|
|
<span id="L882"><span class="lineNum"> 882</span> : * This means that the weakcount is not increased.</span>
|
|
<span id="L883"><span class="lineNum"> 883</span> : * This is the counter-part to weak_intrusive_ptr::release() and the pointer</span>
|
|
<span id="L884"><span class="lineNum"> 884</span> : * passed in *must* have been created using weak_intrusive_ptr::release().</span>
|
|
<span id="L885"><span class="lineNum"> 885</span> : */</span>
|
|
<span id="L886"><span class="lineNum"> 886</span> : static weak_intrusive_ptr reclaim(TTarget* owning_weak_ptr) {</span>
|
|
<span id="L887"><span class="lineNum"> 887</span> : // See Note [Stack allocated intrusive_ptr_target safety]</span>
|
|
<span id="L888"><span class="lineNum"> 888</span> : // if refcount > 0, weakcount must be >1 for weak references to exist.</span>
|
|
<span id="L889"><span class="lineNum"> 889</span> : // see weak counting explanation at top of this file.</span>
|
|
<span id="L890"><span class="lineNum"> 890</span> : // if refcount == 0, weakcount only must be >0.</span>
|
|
<span id="L891"><span class="lineNum"> 891</span> : TORCH_INTERNAL_ASSERT_DEBUG_ONLY(</span>
|
|
<span id="L892"><span class="lineNum"> 892</span> : owning_weak_ptr == NullType::singleton() ||</span>
|
|
<span id="L893"><span class="lineNum"> 893</span> : owning_weak_ptr->weakcount_.load() > 1 ||</span>
|
|
<span id="L894"><span class="lineNum"> 894</span> : (owning_weak_ptr->refcount_.load() == 0 &&</span>
|
|
<span id="L895"><span class="lineNum"> 895</span> : owning_weak_ptr->weakcount_.load() > 0),</span>
|
|
<span id="L896"><span class="lineNum"> 896</span> : "weak_intrusive_ptr: Can only weak_intrusive_ptr::reclaim() owning pointers that were created using weak_intrusive_ptr::release().");</span>
|
|
<span id="L897"><span class="lineNum"> 897</span> : return weak_intrusive_ptr(owning_weak_ptr);</span>
|
|
<span id="L898"><span class="lineNum"> 898</span> : }</span>
|
|
<span id="L899"><span class="lineNum"> 899</span> : </span>
|
|
<span id="L900"><span class="lineNum"> 900</span> : /**</span>
|
|
<span id="L901"><span class="lineNum"> 901</span> : * Takes a pointer to TTarget* (may be weak or strong) and creates a</span>
|
|
<span id="L902"><span class="lineNum"> 902</span> : * new weak_intrusive_ptr representing a new weak reference, i.e.</span>
|
|
<span id="L903"><span class="lineNum"> 903</span> : * the raw pointer retains ownership.</span>
|
|
<span id="L904"><span class="lineNum"> 904</span> : */</span>
|
|
<span id="L905"><span class="lineNum"> 905</span> : static weak_intrusive_ptr reclaim_copy(TTarget* owning_ptr) {</span>
|
|
<span id="L906"><span class="lineNum"> 906</span> : auto ret = reclaim(owning_ptr);</span>
|
|
<span id="L907"><span class="lineNum"> 907</span> : ret.retain_();</span>
|
|
<span id="L908"><span class="lineNum"> 908</span> : return ret;</span>
|
|
<span id="L909"><span class="lineNum"> 909</span> : }</span>
|
|
<span id="L910"><span class="lineNum"> 910</span> : </span>
|
|
<span id="L911"><span class="lineNum"> 911</span> : template <class TTarget1, class NullType1, class TTarget2, class NullType2></span>
|
|
<span id="L912"><span class="lineNum"> 912</span> : friend bool operator<(</span>
|
|
<span id="L913"><span class="lineNum"> 913</span> : const weak_intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L914"><span class="lineNum"> 914</span> : const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept;</span>
|
|
<span id="L915"><span class="lineNum"> 915</span> : template <class TTarget1, class NullType1, class TTarget2, class NullType2></span>
|
|
<span id="L916"><span class="lineNum"> 916</span> : friend bool operator==(</span>
|
|
<span id="L917"><span class="lineNum"> 917</span> : const weak_intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L918"><span class="lineNum"> 918</span> : const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept;</span>
|
|
<span id="L919"><span class="lineNum"> 919</span> : };</span>
|
|
<span id="L920"><span class="lineNum"> 920</span> : </span>
|
|
<span id="L921"><span class="lineNum"> 921</span> : template <class TTarget, class NullType></span>
|
|
<span id="L922"><span class="lineNum"> 922</span> : inline void swap(</span>
|
|
<span id="L923"><span class="lineNum"> 923</span> : weak_intrusive_ptr<TTarget, NullType>& lhs,</span>
|
|
<span id="L924"><span class="lineNum"> 924</span> : weak_intrusive_ptr<TTarget, NullType>& rhs) noexcept {</span>
|
|
<span id="L925"><span class="lineNum"> 925</span> : lhs.swap(rhs);</span>
|
|
<span id="L926"><span class="lineNum"> 926</span> : }</span>
|
|
<span id="L927"><span class="lineNum"> 927</span> : </span>
|
|
<span id="L928"><span class="lineNum"> 928</span> : // To allow weak_intrusive_ptr inside std::map or std::set, we need operator<</span>
|
|
<span id="L929"><span class="lineNum"> 929</span> : template <class TTarget1, class NullType1, class TTarget2, class NullType2></span>
|
|
<span id="L930"><span class="lineNum"> 930</span> : inline bool operator<(</span>
|
|
<span id="L931"><span class="lineNum"> 931</span> : const weak_intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L932"><span class="lineNum"> 932</span> : const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {</span>
|
|
<span id="L933"><span class="lineNum"> 933</span> : return lhs.target_ < rhs.target_;</span>
|
|
<span id="L934"><span class="lineNum"> 934</span> : }</span>
|
|
<span id="L935"><span class="lineNum"> 935</span> : </span>
|
|
<span id="L936"><span class="lineNum"> 936</span> : template <class TTarget1, class NullType1, class TTarget2, class NullType2></span>
|
|
<span id="L937"><span class="lineNum"> 937</span> : inline bool operator==(</span>
|
|
<span id="L938"><span class="lineNum"> 938</span> : const weak_intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L939"><span class="lineNum"> 939</span> : const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {</span>
|
|
<span id="L940"><span class="lineNum"> 940</span> : return lhs.target_ == rhs.target_;</span>
|
|
<span id="L941"><span class="lineNum"> 941</span> : }</span>
|
|
<span id="L942"><span class="lineNum"> 942</span> : </span>
|
|
<span id="L943"><span class="lineNum"> 943</span> : template <class TTarget1, class NullType1, class TTarget2, class NullType2></span>
|
|
<span id="L944"><span class="lineNum"> 944</span> : inline bool operator!=(</span>
|
|
<span id="L945"><span class="lineNum"> 945</span> : const weak_intrusive_ptr<TTarget1, NullType1>& lhs,</span>
|
|
<span id="L946"><span class="lineNum"> 946</span> : const weak_intrusive_ptr<TTarget2, NullType2>& rhs) noexcept {</span>
|
|
<span id="L947"><span class="lineNum"> 947</span> : return !operator==(lhs, rhs);</span>
|
|
<span id="L948"><span class="lineNum"> 948</span> : }</span>
|
|
<span id="L949"><span class="lineNum"> 949</span> : </span>
|
|
<span id="L950"><span class="lineNum"> 950</span> : // Alias for documentary purposes, to more easily distinguish</span>
|
|
<span id="L951"><span class="lineNum"> 951</span> : // weak raw intrusive pointers from intrusive pointers.</span>
|
|
<span id="L952"><span class="lineNum"> 952</span> : using weak_intrusive_ptr_target = intrusive_ptr_target;</span>
|
|
<span id="L953"><span class="lineNum"> 953</span> : </span>
|
|
<span id="L954"><span class="lineNum"> 954</span> : // This namespace provides some methods for working with</span>
|
|
<span id="L955"><span class="lineNum"> 955</span> : // raw pointers that subclass intrusive_ptr_target. They are not provided</span>
|
|
<span id="L956"><span class="lineNum"> 956</span> : // as methods on intrusive_ptr_target, because ideally you would not need these</span>
|
|
<span id="L957"><span class="lineNum"> 957</span> : // methods at all (use smart pointers), but if you are dealing with legacy code</span>
|
|
<span id="L958"><span class="lineNum"> 958</span> : // that still needs to pass around raw pointers, you may find these quite</span>
|
|
<span id="L959"><span class="lineNum"> 959</span> : // useful.</span>
|
|
<span id="L960"><span class="lineNum"> 960</span> : //</span>
|
|
<span id="L961"><span class="lineNum"> 961</span> : // An important usage note: some functions are only valid if you have a</span>
|
|
<span id="L962"><span class="lineNum"> 962</span> : // strong raw pointer to the object, while others are only valid if you</span>
|
|
<span id="L963"><span class="lineNum"> 963</span> : // have a weak raw pointer to the object. ONLY call intrusive_ptr namespace</span>
|
|
<span id="L964"><span class="lineNum"> 964</span> : // functions on strong pointers, and weak_intrusive_ptr namespace functions</span>
|
|
<span id="L965"><span class="lineNum"> 965</span> : // on weak pointers. If you mix it up, you may get an assert failure.</span>
|
|
<span id="L966"><span class="lineNum"> 966</span> : namespace raw {</span>
|
|
<span id="L967"><span class="lineNum"> 967</span> : </span>
|
|
<span id="L968"><span class="lineNum"> 968</span> : namespace intrusive_ptr {</span>
|
|
<span id="L969"><span class="lineNum"> 969</span> : </span>
|
|
<span id="L970"><span class="lineNum"> 970</span> : // WARNING: Unlike the reclaim() API, it is NOT valid to pass</span>
|
|
<span id="L971"><span class="lineNum"> 971</span> : // NullType::singleton to this function</span>
|
|
<span id="L972"><span class="lineNum"> 972</span> <span class="tlaUNC tlaBgUNC"> 0 : inline void incref(intrusive_ptr_target* self) {</span></span>
|
|
<span id="L973"><span class="lineNum"> 973</span> <span class="tlaUNC"> 0 : if (self) {</span></span>
|
|
<span id="L974"><span class="lineNum"> 974</span> <span class="tlaUNC"> 0 : detail::atomic_refcount_increment(self->refcount_);</span></span>
|
|
<span id="L975"><span class="lineNum"> 975</span> : }</span>
|
|
<span id="L976"><span class="lineNum"> 976</span> <span class="tlaUNC"> 0 : }</span></span>
|
|
<span id="L977"><span class="lineNum"> 977</span> : </span>
|
|
<span id="L978"><span class="lineNum"> 978</span> : // WARNING: Unlike the reclaim() API, it is NOT valid to pass</span>
|
|
<span id="L979"><span class="lineNum"> 979</span> : // NullType::singleton to this function</span>
|
|
<span id="L980"><span class="lineNum"> 980</span> <span class="tlaUNC"> 0 : inline void decref(intrusive_ptr_target* self) {</span></span>
|
|
<span id="L981"><span class="lineNum"> 981</span> : // Let it die</span>
|
|
<span id="L982"><span class="lineNum"> 982</span> <span class="tlaUNC"> 0 : c10::intrusive_ptr<intrusive_ptr_target>::reclaim(self);</span></span>
|
|
<span id="L983"><span class="lineNum"> 983</span> : // NB: Caller still has 'self' pointer, but it's now invalid.</span>
|
|
<span id="L984"><span class="lineNum"> 984</span> : // If you want more safety, used the actual c10::intrusive_ptr class</span>
|
|
<span id="L985"><span class="lineNum"> 985</span> <span class="tlaUNC"> 0 : }</span></span>
|
|
<span id="L986"><span class="lineNum"> 986</span> : </span>
|
|
<span id="L987"><span class="lineNum"> 987</span> : template <typename T></span>
|
|
<span id="L988"><span class="lineNum"> 988</span> : inline T* make_weak(T* self) {</span>
|
|
<span id="L989"><span class="lineNum"> 989</span> : // NB: 'this' is a strong pointer, but we return a weak pointer</span>
|
|
<span id="L990"><span class="lineNum"> 990</span> : auto ptr = c10::intrusive_ptr<T>::reclaim(self);</span>
|
|
<span id="L991"><span class="lineNum"> 991</span> : c10::weak_intrusive_ptr<T> wptr(ptr);</span>
|
|
<span id="L992"><span class="lineNum"> 992</span> : ptr.release();</span>
|
|
<span id="L993"><span class="lineNum"> 993</span> : return wptr.release();</span>
|
|
<span id="L994"><span class="lineNum"> 994</span> : }</span>
|
|
<span id="L995"><span class="lineNum"> 995</span> : </span>
|
|
<span id="L996"><span class="lineNum"> 996</span> : inline size_t use_count(intrusive_ptr_target* self) {</span>
|
|
<span id="L997"><span class="lineNum"> 997</span> : auto ptr = c10::intrusive_ptr<intrusive_ptr_target>::reclaim(self);</span>
|
|
<span id="L998"><span class="lineNum"> 998</span> : auto r = ptr.use_count();</span>
|
|
<span id="L999"><span class="lineNum"> 999</span> : ptr.release();</span>
|
|
<span id="L1000"><span class="lineNum"> 1000</span> : return r;</span>
|
|
<span id="L1001"><span class="lineNum"> 1001</span> : }</span>
|
|
<span id="L1002"><span class="lineNum"> 1002</span> : </span>
|
|
<span id="L1003"><span class="lineNum"> 1003</span> : } // namespace intrusive_ptr</span>
|
|
<span id="L1004"><span class="lineNum"> 1004</span> : </span>
|
|
<span id="L1005"><span class="lineNum"> 1005</span> : namespace weak_intrusive_ptr {</span>
|
|
<span id="L1006"><span class="lineNum"> 1006</span> : </span>
|
|
<span id="L1007"><span class="lineNum"> 1007</span> : inline void incref(weak_intrusive_ptr_target* self) {</span>
|
|
<span id="L1008"><span class="lineNum"> 1008</span> : detail::atomic_weakcount_increment(self->weakcount_);</span>
|
|
<span id="L1009"><span class="lineNum"> 1009</span> : }</span>
|
|
<span id="L1010"><span class="lineNum"> 1010</span> : </span>
|
|
<span id="L1011"><span class="lineNum"> 1011</span> : inline void decref(weak_intrusive_ptr_target* self) {</span>
|
|
<span id="L1012"><span class="lineNum"> 1012</span> : // Let it die</span>
|
|
<span id="L1013"><span class="lineNum"> 1013</span> : c10::weak_intrusive_ptr<intrusive_ptr_target>::reclaim(self);</span>
|
|
<span id="L1014"><span class="lineNum"> 1014</span> : // NB: You still "have" the 'self' pointer, but it's now invalid.</span>
|
|
<span id="L1015"><span class="lineNum"> 1015</span> : // If you want more safety, used the actual c10::weak_intrusive_ptr class</span>
|
|
<span id="L1016"><span class="lineNum"> 1016</span> : }</span>
|
|
<span id="L1017"><span class="lineNum"> 1017</span> : </span>
|
|
<span id="L1018"><span class="lineNum"> 1018</span> : template <typename T></span>
|
|
<span id="L1019"><span class="lineNum"> 1019</span> : inline T* lock(T* self) {</span>
|
|
<span id="L1020"><span class="lineNum"> 1020</span> : auto wptr = c10::weak_intrusive_ptr<T>::reclaim(self);</span>
|
|
<span id="L1021"><span class="lineNum"> 1021</span> : auto ptr = wptr.lock();</span>
|
|
<span id="L1022"><span class="lineNum"> 1022</span> : wptr.release();</span>
|
|
<span id="L1023"><span class="lineNum"> 1023</span> : return ptr.release();</span>
|
|
<span id="L1024"><span class="lineNum"> 1024</span> : }</span>
|
|
<span id="L1025"><span class="lineNum"> 1025</span> : </span>
|
|
<span id="L1026"><span class="lineNum"> 1026</span> : // This gives the STRONG refcount of a WEAK pointer</span>
|
|
<span id="L1027"><span class="lineNum"> 1027</span> : inline size_t use_count(weak_intrusive_ptr_target* self) {</span>
|
|
<span id="L1028"><span class="lineNum"> 1028</span> : auto wptr = c10::weak_intrusive_ptr<intrusive_ptr_target>::reclaim(self);</span>
|
|
<span id="L1029"><span class="lineNum"> 1029</span> : auto r = wptr.use_count();</span>
|
|
<span id="L1030"><span class="lineNum"> 1030</span> : wptr.release();</span>
|
|
<span id="L1031"><span class="lineNum"> 1031</span> : return r;</span>
|
|
<span id="L1032"><span class="lineNum"> 1032</span> : }</span>
|
|
<span id="L1033"><span class="lineNum"> 1033</span> : </span>
|
|
<span id="L1034"><span class="lineNum"> 1034</span> : } // namespace weak_intrusive_ptr</span>
|
|
<span id="L1035"><span class="lineNum"> 1035</span> : </span>
|
|
<span id="L1036"><span class="lineNum"> 1036</span> : } // namespace raw</span>
|
|
<span id="L1037"><span class="lineNum"> 1037</span> : </span>
|
|
<span id="L1038"><span class="lineNum"> 1038</span> : } // namespace c10</span>
|
|
<span id="L1039"><span class="lineNum"> 1039</span> : </span>
|
|
<span id="L1040"><span class="lineNum"> 1040</span> : namespace std {</span>
|
|
<span id="L1041"><span class="lineNum"> 1041</span> : // To allow intrusive_ptr and weak_intrusive_ptr inside std::unordered_map or</span>
|
|
<span id="L1042"><span class="lineNum"> 1042</span> : // std::unordered_set, we need std::hash</span>
|
|
<span id="L1043"><span class="lineNum"> 1043</span> : template <class TTarget, class NullType></span>
|
|
<span id="L1044"><span class="lineNum"> 1044</span> : struct hash<c10::intrusive_ptr<TTarget, NullType>> {</span>
|
|
<span id="L1045"><span class="lineNum"> 1045</span> : size_t operator()(const c10::intrusive_ptr<TTarget, NullType>& x) const {</span>
|
|
<span id="L1046"><span class="lineNum"> 1046</span> : return std::hash<TTarget*>()(x.get());</span>
|
|
<span id="L1047"><span class="lineNum"> 1047</span> : }</span>
|
|
<span id="L1048"><span class="lineNum"> 1048</span> : };</span>
|
|
<span id="L1049"><span class="lineNum"> 1049</span> : template <class TTarget, class NullType></span>
|
|
<span id="L1050"><span class="lineNum"> 1050</span> : struct hash<c10::weak_intrusive_ptr<TTarget, NullType>> {</span>
|
|
<span id="L1051"><span class="lineNum"> 1051</span> : size_t operator()(const c10::weak_intrusive_ptr<TTarget, NullType>& x) const {</span>
|
|
<span id="L1052"><span class="lineNum"> 1052</span> : return std::hash<TTarget*>()(x._unsafe_get_target());</span>
|
|
<span id="L1053"><span class="lineNum"> 1053</span> : }</span>
|
|
<span id="L1054"><span class="lineNum"> 1054</span> : };</span>
|
|
<span id="L1055"><span class="lineNum"> 1055</span> : } // namespace std</span>
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<br>
|
|
|
|
<table width="100%" border=0 cellspacing=0 cellpadding=0>
|
|
<tr><td class="ruler"><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
|
|
<tr><td class="versionInfo">Generated by: <a href="https://github.com//linux-test-project/lcov" target="_parent">LCOV version 2.0-1</a></td></tr>
|
|
</table>
|
|
<br>
|
|
|
|
</body>
|
|
</html>
|