Await_capsuleSourceinclude module type of struct include Portable_kernel.Capsule endmodule Blocking_sync = Portable_kernel.Capsule.Blocking_syncmodule Prim = Portable_kernel.Capsule.Priminclude module type of struct include Prim.Extended endmodule Access = Portable_kernel.Capsule.AccessAccess defines an access token to inspect and use data within a capsule
module Data = Portable_kernel.Capsule.DataData defines pointers to some data within a capsule
Meaningful access to the value within a ('a, 'k) Data.t pointer requires an uncontended or shared 'k Access.t. An 'k Access.t can be synchronized using Sync.Mutex.with_lock from the Await library.
Alternatively, the three following modules Frozen, Owned and Scoped offer practical wrappers for common patterns when synchronizing capsule access.
Frozen represents a capsule whose contents have been permanently frozen: nobody may write to it, so everyone may read from it without synchronization.
Owned uses aliasing as a proxy for contention; owning a capsule uniquely is sufficient to access a capsule, since uniqueness guarantees that only one thread has access to it.
Scoped uses 'k Capsule.Password.t -- a token that is only ever available locally -- to grant permission for the current thread to have uncontended access to the capsule 'k for the duration of the current region. Locality guarantees that a password is not shared with other existing threads. Unlike Owned and the more direct Access.t, a Scoped.t crosses contention, and can hence be accessed from a different capsule.
A Scoped.Shared represents a temporary freeze: the capsule may only be read from for the duration of the local 'k Capsule.Password.Shared.t
Each wrapper offers various trade-offs; when making a choice over which kind of wrapper to use, it can be useful to observe whether a piece of data follows a common pattern:
Frozen using Frozen.create.Scoped must be used.Data.get_id.Frozen, Owned and Scoped can each be synchronized with a mutex or reader-writer lock in the Await or Parallel libraries
module Isolated = Portable_kernel.Capsule.Isolatedmodule Frozen = Portable_kernel.Capsule.Frozenmodule Owned = Portable_kernel.Capsule.Ownedmodule Scoped = Portable_kernel.Capsule.Scopedmodule Initial = Portable_kernel.Capsule.InitialLocks protecting explicit capsules which take a Sync.t, and may only be held for a bounded period of time.