<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="215051" data-post-id="215051">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dimitarvp" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/120/38664_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dimitarvp
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="ityonemo" data-post="16" data-topic="39724">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/48/11341_2.png" class="avatar"> ityonemo:</div>
<blockquote>
<p>Rust will happily panic on if you buffer overflow or have an error on malloc, or a ton of other things, because it’s priority is to prevent creating side channels that can be used to exfiltrate data, and for that security model, taking down the entire process is preferable to security problems.</p>
</blockquote>
</aside>
<p>There is a way to write programs in Rust that would make panics 99% less likely, namely using functions that return <code>Result&lt;T&gt;</code> and not using functions that can panic – this is very similar to Elixir APIs that return <code>:ok</code> / <code>:error</code> tuples vs. those whose names end with <code>!</code> and can raise an exception. I don’t have <em>huge</em> production Rust experience but practically every Rust dev I was working with was following the practice of matching on returned errors.</p>
<p>Speaking of those “ton of other things”, we must be realistic that most programming languages and runtimes can’t recover from an out-of-memory condition or a kernel error so I don’t think that’s a fair criticism to Rust – or any other language. Operating under the assumption that you do in fact have some free memory and your physical devices aren’t bombarding the kernel with fatal errors is reasonable.</p>
<p>Answering <a class="mention" href="/u/amnu3387" rel="nofollow">@amnu3387</a> here, I believe that what you are looking for here is a close tie between Rust and Zig. The answer is 99% in the “pick your poison” territory. Haskell could work too if that’s your jam.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="215051" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/creating-long-running-nifs-or-something-else/39724/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-215051" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="215051"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="215088" data-post-id="215088">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ityonemo" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/120/11341_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ityonemo
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>beware that “yielding nif” API has changed a bit (it’s in the docs under guides) and there are several conveniences that people have asked for that I’m punting to 0.8.0 release, which will happen this summer, probably sometime in july.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="215088" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/creating-long-running-nifs-or-something-else/39724/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-215088" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="215088"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="215111" data-post-id="215111">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dimitarvp" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/120/38664_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dimitarvp
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m very interested in how you do yielding, when you release it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="215111" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/creating-long-running-nifs-or-something-else/39724/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-215111" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="215111"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #23"></div>
  </section>
</div>
    <div class="postbit" id="215114" data-post-id="215114">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ityonemo" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/120/11341_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ityonemo
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>yielding currently works as of 0.7.2, . the api changes are between 0.7.0 and 0.7.1, and the updated niceties coming in 0.8.0 are separate things.  I don’t expect the yielding API to change moving forward, except to support interrupting threaded nifs too.  If you’re curious as to how it works:</p>
<p><a href="https://github.com/ityonemo/zigler/blob/master/lib/zig/nif/yielding.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/ityonemo/zigler/blob/master/lib/zig/nif/yielding.ex</a></p>
<p>The entrypoint functions start at line 105, the async function is line 159</p>
<p>Ability to seamlessly swap between yielding, sync, and threaded depends on the fact that zig has colorless async functions.</p>
<p><code>beam.yield</code> function is here:  <a href="https://github.com/ityonemo/zigler/blob/master/priv/beam/beam.zig" class="inline-onebox" rel="noopener nofollow ugc">zigler/priv/beam/beam.zig at main · E-xyza/zigler · GitHub</a></p>
<p>tl;dr - launcher function sets up a frame (heap-allocated memory space for an async function + its stack) inside of a BEAM resource, then it launches the function, inside of an event loop, watching for the 1 ms time limit to elapse.  If you get close to exhausting the event loop’s 1ms requirement, intead of resuming the async function, it calls the BEAM nif ABI “reschedule a tail-call” function, passing the resource along as an artifact.  On reentry, it accesses the resource and obtains the frame pointer and is able to resume the async function.  If you kill the parent process, it triggers garbage collection of the resource, which is set up with a hook to resume at the beam.yield point, except signalling an error, which triggers any defer statements in the zig as usual.</p>
<p>Also importantly, strategic use of threadlocal variables are necessary to allow for ‘global’ information exchange.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="215114" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/creating-long-running-nifs-or-something-else/39724/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-215114" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="215114"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>