MCPcopy Create free account
hub / github.com/FastLED/FastLED / try_acquire

Method try_acquire

ci/util/lock_database.py:72–179  ·  view source on GitHub ↗

Atomic lock acquisition via BEGIN IMMEDIATE. Read lock: succeeds if no OTHER PID holds write lock. Write lock: succeeds if no OTHER PID holds any lock. Re-entrancy: same PID always succeeds (upgrade read->write if sole holder). Args: lock_name: Name iden

(
        self,
        lock_name: str,
        owner_pid: int,
        hostname: str,
        operation: str,
        mode: str = "write",
    )

Source from the content-addressed store, hash-verified

70 conn.close()
71
72 def try_acquire(
73 self,
74 lock_name: str,
75 owner_pid: int,
76 hostname: str,
77 operation: str,
78 mode: str = "write",
79 ) -> bool:
80 """Atomic lock acquisition via BEGIN IMMEDIATE.
81
82 Read lock: succeeds if no OTHER PID holds write lock.
83 Write lock: succeeds if no OTHER PID holds any lock.
84 Re-entrancy: same PID always succeeds (upgrade read->write if sole holder).
85
86 Args:
87 lock_name: Name identifying the lock
88 owner_pid: PID of the process acquiring the lock
89 hostname: Hostname of the machine
90 operation: Description of the operation
91 mode: Lock mode - 'read' or 'write'
92
93 Returns:
94 True if lock was acquired, False if blocked
95 """
96 conn = self._get_connection()
97 try:
98 cursor = conn.cursor()
99 cursor.execute("BEGIN IMMEDIATE")
100
101 cursor.execute(
102 "SELECT owner_pid, lock_mode FROM lock_holders WHERE lock_name = ?",
103 (lock_name,),
104 )
105 holders = cursor.fetchall()
106
107 my_rows = [h for h in holders if h[0] == owner_pid]
108 other_rows = [h for h in holders if h[0] != owner_pid]
109
110 if mode == "read":
111 # Blocked by other writer
112 if any(h[1] == "write" for h in other_rows):
113 conn.rollback()
114 return False
115 # Already hold read or write - re-entrant
116 if my_rows:
117 conn.commit()
118 return True
119 # Insert new read lock
120 cursor.execute(
121 "INSERT INTO lock_holders (lock_name, owner_pid, lock_mode, operation, hostname, acquired_at) "
122 "VALUES (?, ?, 'read', ?, ?, ?)",
123 (lock_name, owner_pid, operation, hostname, time.time()),
124 )
125 conn.commit()
126 return True
127
128 if mode == "write":
129 # Blocked by other holders

Calls 8

_get_connectionMethod · 0.95
anyFunction · 0.85
executeMethod · 0.80
commitMethod · 0.80
timeMethod · 0.80
debugMethod · 0.80
closeMethod · 0.45