Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:LMCache LMCache TTL Lock Impl

From Leeroopedia
Revision as of 15:25, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/LMCache_LMCache_TTL_Lock_Impl.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Concurrency, Storage Management
Last Updated 2026-02-09 00:00 GMT

Overview

Implements the TTLLock class methods providing a thread-safe lock with automatic expiration based on a time-to-live (TTL) mechanism.

Description

This file contains the implementation of the TTLLock class declared in ttl_lock.h. The lock uses atomic compare-and-swap (CAS) loops for thread safety without traditional mutexes. The lock() method either resets the counter to 1 if the TTL has expired or increments it otherwise, always refreshing the expiration timestamp. The unlock() method decrements the counter using a CAS loop that prevents the counter from going below zero. The is_locked() method checks both the counter and the TTL expiration to determine if the lock is held.

Usage

Use this lock in the storage manager layer when cache entries need to be protected from eviction for a bounded period. The TTL ensures that stale locks are automatically released even if a client fails to call unlock, preventing permanent resource leaks.

Code Reference

Source Location

Signature

TTLLock::TTLLock(uint32_t ttl_sec);
void TTLLock::lock();
void TTLLock::unlock();
bool TTLLock::is_locked() const;
void TTLLock::reset();
int64_t TTLLock::now_ms();
int64_t TTLLock::to_ms(const TimePoint& tp);

Import

#include "ttl_lock.h"

I/O Contract

Inputs

Name Type Required Description
ttl_sec uint32_t No (default 300) TTL duration in seconds before the lock auto-expires

Outputs

Name Type Description
is_locked bool Returns true if counter > 0 AND TTL has not expired

Usage Examples

#include "ttl_lock.h"
using namespace lmcache::storage_manager;

// Create a lock with 60-second TTL
TTLLock lock(60);

// Acquire the lock (increments counter, sets expiration)
lock.lock();

// Check status
if (lock.is_locked()) {
    // ... perform protected operation ...
}

// Release the lock (decrements counter)
lock.unlock();

// Force reset to initial state
lock.reset();

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment