* Create the lock file with our PID already in it, so no reader ever finds * it empty: write a private file, then hard-link it into place, which fails * if the lock exists. Where hard links are missing, fall back to an * exclusive create, whose PID lands a moment later (see * EMPTY_LOCK_
()
| 542 | * EMPTY_LOCK_GRACE_MS). False when the lock already exists. |
| 543 | */ |
| 544 | private create(): boolean { |
| 545 | const pid = String(process.pid); |
| 546 | const tmp = `${this.lockPath}.${process.pid}.${threadId}.tmp`; |
| 547 | try { |
| 548 | fs.writeFileSync(tmp, pid); |
| 549 | fs.linkSync(tmp, this.lockPath); |
| 550 | return true; |
| 551 | } catch (err) { |
| 552 | if ((err as NodeJS.ErrnoException).code === 'EEXIST') return false; |
| 553 | // No hard links here (or no private file): create in place instead. |
| 554 | } finally { |
| 555 | try { fs.unlinkSync(tmp); } catch { /* never written */ } |
| 556 | } |
| 557 | try { |
| 558 | fs.writeFileSync(this.lockPath, pid, { flag: 'wx' }); |
| 559 | return true; |
| 560 | } catch (err) { |
| 561 | if ((err as NodeJS.ErrnoException).code === 'EEXIST') return false; |
| 562 | throw err; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | private readHolder(): LockHolder { |
| 567 | let content: string; |