Overview
LibCore is SerenityOS’s core utilities library built on top of AK. It provides essential abstractions for event-driven programming, file I/O, process management, timers, and system integration. LibCore is the foundation for both GUI applications (via LibGUI) and command-line utilities.LibCore provides cross-platform abstractions with platform-specific implementations for SerenityOS, Linux, and macOS.
Event Loop
EventLoop
Core event loop for asynchronous programming.Event Loop Features
Event Loop Features
- Deferred invocations: Run callbacks asynchronously
- Timers: Periodic or one-shot timer events
- Filesystem notifications: Watch for file changes
- POSIX signals: Handle signals in event loop
- Coroutine support: Async/await style programming
- Nested event loops: Support for modal dialogs
int()
Pump the event loop until exit is requested. Returns exit code.
size_t(WaitMode)
Process events once. Returns number of events processed.
void(int)
Request event loop exit with given exit code.
void(Function<void()>)
Schedule callback to run on next event loop iteration.
void(Function<bool()>)
Pump event loop until condition becomes true.
EventReceiver
Base class for objects that receive events.File I/O
File
Modern file I/O using streams.OpenMode
Open for reading
OpenMode
Open for writing
OpenMode
Open for reading and writing
OpenMode
Append to end of file
OpenMode
Truncate file to zero length
OpenMode
Fail if file already exists
Directory & DirIterator
Directory operations and iteration.MappedFile
Memory-mapped file I/O for efficient access.Timers
Timer
Schedule periodic or one-shot timer events.int
Timer interval in milliseconds
bool
If true, timer fires once then stops. If false, repeats indefinitely.
Function<void()>
Callback invoked when timer fires
Process Management
Process
Spawn and manage child processes.Network I/O
Socket
TCP/UDP socket abstraction.TCPServer & UDPServer
Server socket implementations.Configuration
ConfigFile
INI-style configuration file handling.Environment
Environment variable access.System Utilities
DateTime
Date and time handling.ArgsParser
Command-line argument parsing.ArgsParser Features
ArgsParser Features
- Positional arguments
- Boolean flags (—flag, -f)
- Value options (—option=value, -o value)
- Automatic —help generation
- Required vs optional arguments
- Type-safe parsing
ElapsedTimer
High-resolution timing.System
System information and utilities.Promises & Async
Promise
Asynchronous operation results.File Watching
FileWatcher
Monitor file system changes.Best Practices
Event Loop Usage
Event Loop Usage
- Create one EventLoop per thread
- Use
deferred_invokefor async callbacks - Don’t block the event loop with long operations
- Use timers for periodic work
Error Handling
Error Handling
Resource Management
Resource Management
- Use RAII: Resources automatically cleaned up
- Files close on destruction
- Timers stop on destruction
- Sockets disconnect on destruction
