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 RWLock

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


Knowledge Sources
Domains Concurrency, Thread Safety
Last Updated 2026-02-09 00:00 GMT

Overview

Provides thread synchronization primitives including a read-write lock and a fast lock, both with timeout support, for thread-safe operations in the LMCache cache controller.

Description

This module implements two lock classes for thread-safe operations. RWLockWithTimeout is a classic readers-writer lock allowing multiple concurrent readers but exclusive writer access, with optional timeout on both acquire operations. It includes context managers (read_lock and write_lock) for safe usage. FastLockWithTimeout is an optimized mutex for high-frequency operations on small critical sections, using a non-blocking fast path to minimize context switch overhead. Both locks raise RWLockTimeoutError when acquisition fails within the specified timeout. Neither lock is reentrant.

Usage

Use RWLockWithTimeout when protecting shared data structures that are read frequently but written infrequently, such as the registry tree in the cache controller. Use FastLockWithTimeout for high-frequency, short critical sections such as per-worker node state updates where lock contention is expected to be minimal.

Code Reference

Source Location

Signature

class RWLockTimeoutError(Exception): ...

class RWLockWithTimeout:
    def __init__(self) -> None: ...
    def acquire_read(self, timeout: Optional[float] = None) -> bool: ...
    def release_read(self) -> None: ...
    def acquire_write(self, timeout: Optional[float] = None) -> bool: ...
    def release_write(self) -> None: ...
    def read_lock(self, timeout: Optional[float] = None): ...   # context manager
    def write_lock(self, timeout: Optional[float] = None): ...  # context manager

class FastLockWithTimeout:
    def __init__(self) -> None: ...
    def acquire(self, timeout: Optional[float] = None) -> bool: ...
    def release(self) -> None: ...
    def __enter__(self) -> "FastLockWithTimeout": ...
    def __exit__(self, exc_type, exc_val, exc_tb) -> None: ...

Import

from lmcache.v1.cache_controller.locks import (
    RWLockWithTimeout,
    RWLockTimeoutError,
    FastLockWithTimeout,
)

I/O Contract

Inputs

Name Type Required Description
timeout Optional[float] No Timeout in seconds for lock acquisition; None means wait indefinitely

Outputs

Name Type Description
acquired bool Whether the lock was successfully acquired (for acquire_read/acquire_write/acquire methods)
RWLockTimeoutError Exception Raised by context managers when timeout expires before lock acquisition

Usage Examples

from lmcache.v1.cache_controller.locks import RWLockWithTimeout, FastLockWithTimeout

# Read-write lock with context manager
rw_lock = RWLockWithTimeout()

with rw_lock.read_lock(timeout=5.0):
    # Multiple threads can read concurrently
    data = shared_dict.get("key")

with rw_lock.write_lock(timeout=10.0):
    # Only one thread can write at a time
    shared_dict["key"] = new_value

# Fast lock for high-frequency operations
fast_lock = FastLockWithTimeout()

with fast_lock:
    # Short critical section with non-blocking fast path
    counter += 1

Page Connections

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