-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecution-scheduler.ts
More file actions
132 lines (118 loc) · 2.8 KB
/
execution-scheduler.ts
File metadata and controls
132 lines (118 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Execution Scheduler for Dual-Engine Coordination
// Prevents GPU and CPU engines from competing for resources
export class ExecutionScheduler {
private gpuLock = false;
private cpuLock = false;
private gpuQueue: Array<() => void> = [];
private cpuQueue: Array<() => void> = [];
/**
* Acquire GPU lock
* Waits if GPU is already in use
*/
async acquireGPU(): Promise<void> {
if (!this.gpuLock) {
this.gpuLock = true;
console.log('🔒 GPU lock acquired');
return;
}
// Wait for lock to be released
console.log('⏳ Waiting for GPU lock...');
return new Promise((resolve) => {
this.gpuQueue.push(resolve);
});
}
/**
* Release GPU lock
* Processes next item in queue if any
*/
releaseGPU(): void {
console.log('🔓 GPU lock released');
const next = this.gpuQueue.shift();
if (next) {
console.log('▶️ Processing next GPU task from queue');
next();
} else {
this.gpuLock = false;
}
}
/**
* Acquire CPU lock
* Waits if CPU is already in use
*/
async acquireCPU(): Promise<void> {
if (!this.cpuLock) {
this.cpuLock = true;
console.log('🔒 CPU lock acquired');
return;
}
// Wait for lock to be released
console.log('⏳ Waiting for CPU lock...');
return new Promise((resolve) => {
this.cpuQueue.push(resolve);
});
}
/**
* Release CPU lock
* Processes next item in queue if any
*/
releaseCPU(): void {
console.log('🔓 CPU lock released');
const next = this.cpuQueue.shift();
if (next) {
console.log('▶️ Processing next CPU task from queue');
next();
} else {
this.cpuLock = false;
}
}
/**
* Execute with GPU lock (auto acquire/release)
*/
async withGPULock<T>(fn: () => Promise<T>): Promise<T> {
await this.acquireGPU();
try {
return await fn();
} finally {
this.releaseGPU();
}
}
/**
* Execute with CPU lock (auto acquire/release)
*/
async withCPULock<T>(fn: () => Promise<T>): Promise<T> {
await this.acquireCPU();
try {
return await fn();
} finally {
this.releaseCPU();
}
}
/**
* Get current lock status
*/
getStatus(): {
gpuLocked: boolean;
cpuLocked: boolean;
gpuQueueLength: number;
cpuQueueLength: number;
} {
return {
gpuLocked: this.gpuLock,
cpuLocked: this.cpuLock,
gpuQueueLength: this.gpuQueue.length,
cpuQueueLength: this.cpuQueue.length,
};
}
/**
* Reset all locks (use with caution)
*/
reset(): void {
console.warn('⚠️ Resetting all locks');
this.gpuLock = false;
this.cpuLock = false;
this.gpuQueue = [];
this.cpuQueue = [];
}
}
// Singleton instance
export const executionScheduler = new ExecutionScheduler();