-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e04562
commit 418ffe8
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
hide: | ||
- toc | ||
--- | ||
|
||
# FFI Global Initializer | ||
|
||
## Problem | ||
|
||
You are working with a library via [Pony's C-FFI](https://tutorial.ponylang.io/c-ffi/) and need to initialize the library before you can use it. And you'd like to do this initialization once and only once. | ||
|
||
## Solution | ||
|
||
Pony's primitives can serve a variety of design purposes. Here we will use a primitive to initialize our imaginary C library. | ||
|
||
```pony | ||
use @magic_global_initialization[None]() | ||
primitive LibraryInitializer | ||
fun _init() => | ||
@magic_global_initialization() | ||
``` | ||
|
||
## Discussion | ||
|
||
Only a single instance of a Pony `primitive` will exist in our binary. User defined primitives are singletons. We can combine this with the fact that primitives have an `_init` function that is called when the primitive is created. | ||
|
||
Wrapping our "should only be done once" initialization code in a primitive's `_init` method is a good way to ensure that the initialization code is only run once. It is important to note that this approach doesn't protect you from someone mistakenly calling the initializer via C-FFI directly again, | ||
|
||
If we need to teardown the library, we can add a `_final` method to `LibraryInitializer` which will be executed on program shutdown. | ||
|
||
```pony | ||
use @magic_global_initialization[None]() | ||
use @magic_global_shutdown[None]() | ||
primitive LibraryInitializer | ||
fun _init() => | ||
@magic_global_initialization() | ||
fun _final() => | ||
@magic_global_shutdown() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters