Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on SharedMutex & SharedTimedMutex #36

Closed
samchon opened this issue May 11, 2019 · 2 comments
Closed

Error on SharedMutex & SharedTimedMutex #36

samchon opened this issue May 11, 2019 · 2 comments
Labels
bug Something isn't working enhancement New feature or request help wanted Extra attention is needed question Further information is requested

Comments

@samchon
Copy link
Owner

samchon commented May 11, 2019

Summary

  • TSTL Version: 2.1.3
  • Expected behavior: Writing and reading locks are not mutual exclusive.
  • Actual behavior: Both of them must be mutual exclusive.

Code occuring bug

import std = require("tstl");

const enum Status
{
    START_READING = "Start Reading",
    END_READING = "End Reading",
    START_WRITING = "Start Writing",
    END_WRITING = "End Writing"
}
const MAGNIFIER: number = 3;

async function write(mutex: std.SharedTimedMutex, statusList: std.Pair<Status, number>[]): Promise<void>
{
    for (let i: number = 0; i < MAGNIFIER * 10; ++i)
    {
        // JUST DELAY FOR SAFETY
        await std.sleep_for(100);
        let time: number = Date.now();

        // DO WRITE
        await mutex.lock();
        {
            let now: number = Date.now();
            statusList.push(new std.Pair(Status.START_WRITING, now - time));
            
            await std.sleep_for(50);
            statusList.push(new std.Pair(Status.END_WRITING, Date.now() - now));
        }
        await mutex.unlock();
    }
}

async function read(mutex: std.SharedTimedMutex, statusList: std.Pair<Status, number>[]): Promise<void>
{
    for (let i: number = 0; i < MAGNIFIER * 100; ++i)
    {
        let time: number = Date.now();

        // DO READ
        await mutex.lock_shared();
        {
            let now: number = Date.now();
            statusList.push(new std.Pair(Status.START_READING, now - time));

            await std.sleep_for(10);
            statusList.push(new std.Pair(Status.END_READING, Date.now() - now));
        }
        await mutex.unlock_shared();
    }
}

async function main(): Promise<void>
{
    let mutex: std.SharedTimedMutex = new std.SharedTimedMutex();
    let statusList: std.Pair<Status, number>[] = [];

    try
    {
        let promises: Promise<void>[] = [];
        for (let i: number = 0; i < 25; ++i)
            promises.push(read(mutex, statusList));
        promises.push(write(mutex, statusList));

        await Promise.all(promises);

        let reading: number = 0;
        let writing: number = 0;
        
        for (let i: number = 0; i < statusList.length; ++i)
        {
            let status: Status = statusList[i].first;

            if (status === Status.START_READING)
                ++reading;
            else if (status === Status.START_WRITING)
                ++writing;
            else if (status === Status.END_READING)
                --reading;
            else
                --writing;
            
            if (writing > 0 && reading > 0)
                throw new Error(`Error on SharedTimeMutex; reading and writing at the same time at ${i}`);
        }
    }
    catch (exp)
    {
        for (let pair of statusList)
            console.log(pair.first, pair.second);
        throw exp;
    }
}
main();
@samchon samchon added the bug Something isn't working label May 11, 2019
@samchon samchon added this to To do in v2.1 Update via automation May 11, 2019
@samchon samchon added this to To do in v2.2 Update via automation May 11, 2019
@samchon samchon moved this from To do to In progress in v2.2 Update May 23, 2019
@samchon samchon moved this from To do to Patch in v2.1 Update Jun 14, 2019
@samchon samchon moved this from In progress to Done in v2.2 Update Jun 15, 2019
@samchon samchon changed the title Error on mutexes Error on SharedMutex & SharedTimedMutex Jun 15, 2019
@samchon
Copy link
Owner Author

samchon commented Jun 15, 2019

Until now, I'd implemented mutexes for one by one.

However, from now on, I'll implement only SharedTimedMutex class and let other mutexes to capsulize the SharedTimedMutex instance like below:

export class TimedMutex
{
    private mtx_: SharedTimedMutex = new SharedTimedMutex();

    public lock(): Promise<void>
    {
        return this.mtx_.lock();
    }
    public unlock(): Promise<void>
    {
        return this.mtx_.unlock();
    }

    public try_lock(): Promise<boolean>
    {
        return this.mtx_.try_lock();
    }
    public try_lock_for(ms: number): Promise<boolean>
    {
        return this.mtx_.try_lock_for(ms);
    }
    public try_lock_until(at: Date): Promise<boolean>
    {
        return this.mtx_.try_lock_until(at);
    }
}

@samchon
Copy link
Owner Author

samchon commented Jun 15, 2019

Have fixed until v2.1.6 patch and v2.2 update.

From the implementation, I can assure that writing and reading locks are mutual exclusive, but I can't sure that timed locks are working correctly. If someone can help me to implement the timed lock, or provide me the teseting code for the timed locks, then please help me.

@samchon samchon added enhancement New feature or request help wanted Extra attention is needed question Further information is requested labels Jun 15, 2019
@samchon samchon moved this from Done to In progress in v2.2 Update Jun 15, 2019
@samchon samchon moved this from Patch to In progress in v2.1 Update Jun 15, 2019
@samchon samchon moved this from In progress to Patch in v2.1 Update Jun 16, 2019
@samchon samchon moved this from In progress to Done in v2.2 Update Jun 16, 2019
@samchon samchon closed this as completed Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request help wanted Extra attention is needed question Further information is requested
Projects
No open projects
v2.1 Update
  
Patch
v2.2 Update
  
Done
Development

No branches or pull requests

1 participant