export default class ReadWriteLock {
    constructor() {
        this.w3 = 0;
        this.writer = false;
        this.y3 = [];
        this.z3 = [];
    }
    async read(x7) {
        return new Promise((z7) => {
            const a8 = () => {
                this.w3 += 1;
                x7();
                z7();
                this.w3 -= 1;
                this.a4();
            };
            if (this.writer || this.z3.length > 0) {
                this.y3.push(a8);
            }
            else {
                a8();
            }
        });
    }
    async write(s7) {
        return new Promise((u7) => {
            const v7 = () => {
                this.writer = true;
                s7();
                u7();
                this.writer = false;
                this.a4();
            };
            if (this.w3 > 0 || this.writer) {
                this.z3.push(v7);
            }
            else {
                v7();
            }
        });
    }
    a4() {
        if (this.writer || this.w3 > 0) {
            return;
        }
        if (this.z3.length > 0) {
            const r7 = this.z3.shift();
            if (r7) {
                r7();
            }
        }
        else if (this.y3.length > 0) {
            while (this.y3.length > 0) {
                const q7 = this.y3.shift();
                if (q7) {
                    q7();
                }
            }
        }
    }
}
