Verified Commit c47d3e73 authored by Camil Staps's avatar Camil Staps 🚀

Make System._PointerWithFinalizer more general; move to System._Finalized

parent a2cde102
definition module System._Finalized
/**
* This module provides support for 'finalized' values. These are values that
* have a finalizer attached to them: a C function pointer and an integer
* argument for that function. When the value is discarded by the garbage
* collector, the C function will be called with that argument. Because garbage
* collection runs periodically, this may not be directly after all references
* to the value are lost (and may not happen at all, depending on the heap
* usage of the application).
*/
from System._Pointer import :: Pointer
:: Finalized a = Finalized !a !Finalizer
:: Finalizer = {finalizer_implementation :: !FinalizerT}
:: FinalizerT = DummyFinalizer !Int !Int !Int
/**
* Attach a finalizer to a value.
*
* @param The value
* @param A pointer to the finalizer C function
* @param An integer argument for the C function
* @result The value with a finalizer attached
*/
finalize :: a !Pointer !Int -> Finalized a
/**
* Perfoms an operation on a finalized value.
* All operations on a finalized value must use this function in order for the
* system to work (otherwise, a reference to the value may still exist while
* all references to the {{`Finalizer`}} are lost).
*
* @param The operation to perform
* @param The finalized value
* @result The operation's result
*/
withFinalizedValue :: !(a -> b) !(Finalized a) -> (!b, !Finalized a)
implementation module System._PointerWithFinalizer
import StdEnv
import Data.Func
import System._Pointer
:: PointerWithFinalizer = PointerWithFinalizer !Pointer !Finalizer
pointerWithFinalizer :: !Pointer !Pointer -> PointerWithFinalizer
pointerWithFinalizer ptr finalPtr = PointerWithFinalizer ptr $ make_finalizer finalPtr ptr
withPointer :: !(Pointer -> a) !PointerWithFinalizer -> (!a, !PointerWithFinalizer)
withPointer func fptr=:(PointerWithFinalizer ptr _) = (func ptr, fptr)
:: Finalizer = {finalizer_implementation :: !FinalizerT}
:: FinalizerT = DummyFinalizer !Int !Int !Int
make_finalizer :: !Pointer !Pointer -> Finalizer
make_finalizer f v = {finalizer_implementation = fst $ make_finalizer_c f v}
where
make_finalizer_c :: !Int !Int -> (!FinalizerT, !Int)
make_finalizer_c f v = code {
push_finalizers
push_a_b 0
pop_a 1
build_r e__system_kFinalizer 0 3 0 0
pop_b 3
set_finalizers
pushI 0
}
implementation module System._Finalized
import StdEnv
import Data.Func
import System._Pointer
finalize :: a !Pointer !Int -> Finalized a
finalize val ptr arg = Finalized val $ make_finalizer ptr arg
withFinalizedValue :: !(a -> b) !(Finalized a) -> (!b, !Finalized a)
withFinalizedValue func fin=:(Finalized x _) = (func x, fin)
make_finalizer :: !Pointer !Int -> Finalizer
make_finalizer f v = {finalizer_implementation = fst $ make_finalizer_c f v}
where
make_finalizer_c :: !Int !Int -> (!FinalizerT, !Int)
make_finalizer_c f v = code {
push_finalizers
push_a_b 0
pop_a 1
build_r e__system_kFinalizer 0 3 0 0
pop_b 3
set_finalizers
pushI 0
}
definition module System._PointerWithFinalizer
/**
* This module provides pointer to which finalizers are attached,
* which are called if no reference to the pointer is left.
*/
from System._Pointer import :: Pointer
:: PointerWithFinalizer
/**
* Creates a pointer with a finalizer which is called if no reference to the pointer is left.
*
* @param A pointer
* @param A pointer to the finalizer C function, which gets the pointer as argument
* @result The pointer with a finalizer attached
*/
pointerWithFinalizer :: !Pointer !Pointer -> PointerWithFinalizer
/**
* Perfoms an operation on the pointer.
* All operations on the pointer have to be evaluated within the function
* to make sure that no operations are performed on the pointer after the finalizer is called.
*
* @param The operation to perform
* @param The pointer with finalizer
* @result The operation's result
*/
withPointer :: !(Pointer -> a) !PointerWithFinalizer -> (!a, !PointerWithFinalizer)
......@@ -145,6 +145,7 @@ import qualified System.TTS
import qualified System.Time
import qualified System._Directory
import qualified System._FilePath
import qualified System._Finalized
import qualified System._Platform
import qualified System._Pointer
import qualified System._Posix
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment