Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,42 @@ Lunatik **modifies** [luaL\_openlibs](https://www.lua.org/manual/5.4/manual.html

Lua APIs are documented thanks to [LDoc](https://stevedonovan.github.io/ldoc/). This documentation can be read here: https://luainkernel.github.io/lunatik/, and in the source files.

## Lunatik Objects

Lunatik exposes kernel facilities to Lua through *objects*. A Lunatik object is a Lua userdata that represents a kernel-resident resource and is shared between Lua and C code.

Internally, each Lunatik object combines:
- a Lua userdata
- a kernel lock (mutex or spinlock)
- a reference counter

This allows safe access to kernel resources from both Lua scripts and kernel code while respecting Linux kernel execution constraints.

### Object Lifetime

Lunatik objects are typically created by Lua APIs (e.g. `runtime`, `device`, `probe`) or by the [C API](<README#C API>). Once created, an object may be referenced by:
- Lua variables
- kernel subsystems
- other Lunatik objects

The lifetime of an object is not tied solely to Lua garbage collection. An object remains alive as long as at least one reference exists.

Some objects expose an explicit *stop* or *close* operation, which releases internal resources (such as Lua states or kernel hooks) before the object itself is freed.

### Reference Counting

Each Lunatik object maintains an internal reference counter. References are incremented and decremented automatically by Lunatik, and may also be managed explicitly from C code using `lunatik_getobject()` and `lunatik_putobject()`.

When the reference counter reaches zero, the object’s resources and memory are released.

Lua scripts do not manage locking directly; synchronization is handled internally by Lunatik.

### Interaction with Lua Garbage Collection

Lua garbage collection may release Lua references to an object, but this does not necessarily destroy the underlying kernel object. Garbage collection typically results in a reference counter decrement.

Kernel-held references keep the object alive even after all Lua references are gone. For deterministic cleanup, objects should be explicitly stopped or unloaded when appropriate.

## Lunatik C API

```C
Expand Down