|
| 1 | +# This file is a part of Julia. License is MIT: https://julialang.org/license |
| 2 | + |
| 3 | +export increment_pin_count!, decrement_pin_count!, |
| 4 | + increment_tpin_count!, decrement_tpin_count!, |
| 5 | + get_pin_count, get_tpin_count |
| 6 | + |
| 7 | +""" |
| 8 | + increment_pin_count!(obj) |
| 9 | +
|
| 10 | +Increment the pin count of `obj` to preserve it beyond a lexical scope. |
| 11 | +
|
| 12 | +This ensures that `obj` is not moved or collected by the garbage collector. |
| 13 | +It is crucial for safely passing references to foreign code. |
| 14 | +
|
| 15 | +Each call increments the pin count by one. The object remains pinned and |
| 16 | +alive until the count is decremented to zero via `decrement_pin_count!`. |
| 17 | +
|
| 18 | +# Examples |
| 19 | +
|
| 20 | +```julia |
| 21 | +x = SomeObject() |
| 22 | +increment_pin_count!(x) # x is now pinned (count = 1) |
| 23 | +
|
| 24 | +increment_pin_count!(x) # pin count is now 2 |
| 25 | +""" |
| 26 | +function increment_pin_count!(obj) |
| 27 | + ccall(:jl_increment_pin_count, Cvoid, (Any,), obj) |
| 28 | +end |
| 29 | + |
| 30 | +""" |
| 31 | + decrement_pin_count!(obj) |
| 32 | +
|
| 33 | +Decrement the pin count of `obj`. |
| 34 | +
|
| 35 | +When the count drops to zero, the object is no longer pinned and may be |
| 36 | +moved (or collected by the garbage collector, if no other references |
| 37 | +exist). |
| 38 | +
|
| 39 | +This is necessary to release objects that were previously preserved for |
| 40 | +foreign code. |
| 41 | +
|
| 42 | +# Examples |
| 43 | +
|
| 44 | +```julia |
| 45 | +x = SomeObject() |
| 46 | +increment_pin_count!(x) # x is now pinned (count = 1) |
| 47 | +
|
| 48 | +increment_pin_count!(x) # pin count is now 2 |
| 49 | +
|
| 50 | +decrement_pin_count!(x) # reduces pin count to 1 |
| 51 | +
|
| 52 | +decrement_pin_count!(x) # count is 0; x may now be collected |
| 53 | +""" |
| 54 | +function decrement_pin_count!(obj) |
| 55 | + ccall(:jl_decrement_pin_count, Cvoid, (Any,), obj) |
| 56 | +end |
| 57 | + |
| 58 | +""" |
| 59 | + increment_tpin_count!(obj) |
| 60 | +
|
| 61 | +Increment the transitive pin count of `obj` to preserve it beyond a |
| 62 | +lexical scope. |
| 63 | +
|
| 64 | +This ensures that `obj` and any other objects reachable from it are not |
| 65 | +moved or collected by the garbage collector. This is crucial for safely |
| 66 | +passing references to foreign code. |
| 67 | +
|
| 68 | +Each call increments the transitive pin count by one. The object remains |
| 69 | +transitively pinned and alive until the count is decremented to zero via |
| 70 | +`decrement_tpin_count!`. |
| 71 | +
|
| 72 | +# Examples |
| 73 | +
|
| 74 | +```julia |
| 75 | +x = SomeObject() |
| 76 | +increment_tpin_count!(x) # x is now transitively pinned (count = 1) |
| 77 | +
|
| 78 | +increment_tpin_count!(x) # transitive pin count is now 2 |
| 79 | +""" |
| 80 | +function increment_tpin_count!(obj) |
| 81 | + ccall(:jl_increment_tpin_count, Cvoid, (Any,), obj) |
| 82 | +end |
| 83 | + |
| 84 | +""" |
| 85 | + decrement_tpin_count!(obj) |
| 86 | +
|
| 87 | +Decrement the transitive pin count of `obj`. |
| 88 | +
|
| 89 | +When the count drops to zero, `obj` and any objects reachable from it are |
| 90 | +no longer pinned and may be moved (if no other pins exist) or collected by |
| 91 | +the garbage collector (if no other references exist). |
| 92 | +
|
| 93 | +This is necessary to release object graphs that were previously preserved |
| 94 | +for foreign code. |
| 95 | +
|
| 96 | +# Examples |
| 97 | +
|
| 98 | +```julia |
| 99 | +x = SomeObject() |
| 100 | +increment_tpin_count!(x) # pins x and reachable objects (count = 1) |
| 101 | +
|
| 102 | +decrement_tpin_count!(x) # reduces transitive pin count to 0 |
| 103 | + # objects may now be collected |
| 104 | +""" |
| 105 | +function decrement_tpin_count!(obj) |
| 106 | + ccall(:jl_decrement_tpin_count, Cvoid, (Any,), obj) |
| 107 | +end |
| 108 | + |
| 109 | +""" |
| 110 | + get_pin_count(obj) |
| 111 | +
|
| 112 | +Return the current pin count of `obj`. |
| 113 | +
|
| 114 | +This indicates how many times `obj` has been explicitly pinned via |
| 115 | +`increment_pin_count!`. A nonzero count means the object is currently |
| 116 | +pinned and will not be moved or collected by the garbage collector (GC). |
| 117 | +
|
| 118 | +# Examples |
| 119 | +
|
| 120 | +```julia |
| 121 | +x = SomeObject() |
| 122 | +increment_pin_count!(x) |
| 123 | +get_pin_count(x) # returns 1 |
| 124 | +
|
| 125 | +increment_pin_count!(x) |
| 126 | +get_pin_count(x) # returns 2 |
| 127 | +
|
| 128 | +decrement_pin_count!(x) |
| 129 | +get_pin_count(x) # returns 1 |
| 130 | +""" |
| 131 | +function get_pin_count(obj) |
| 132 | + c = ccall(:jl_get_pin_count, Csize_t, (Any,), obj) |
| 133 | + return Int64(c) |
| 134 | +end |
| 135 | + |
| 136 | +""" |
| 137 | + get_tpin_count(obj) |
| 138 | +
|
| 139 | +Return the current transitive pin count of `obj`. |
| 140 | +
|
| 141 | +This indicates how many times `obj` has been explicitly transitively pinned |
| 142 | +via `increment_tpin_count!`. A nonzero count means `obj` and all objects |
| 143 | +reachable from it are currently pinned and will not be moved or collected |
| 144 | +by the garbage collector (GC). |
| 145 | +
|
| 146 | +# Examples |
| 147 | +
|
| 148 | +```julia |
| 149 | +x = SomeObject() |
| 150 | +increment_tpin_count!(x) |
| 151 | +get_tpin_count(x) # returns 1 |
| 152 | +
|
| 153 | +increment_tpin_count!(x) |
| 154 | +get_tpin_count(x) # returns 2 |
| 155 | +
|
| 156 | +decrement_tpin_count!(x) |
| 157 | +get_tpin_count(x) # returns 1 |
| 158 | +""" |
| 159 | +function get_tpin_count(obj) |
| 160 | + c = ccall(:jl_get_tpin_count, Csize_t, (Any,), obj) |
| 161 | + return Int64(c) |
| 162 | +end |
0 commit comments