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

Threads and mutexes libraries bugs #1472

Open
DanyDollaro opened this issue Sep 21, 2024 · 0 comments
Open

Threads and mutexes libraries bugs #1472

DanyDollaro opened this issue Sep 21, 2024 · 0 comments
Assignees
Labels
Stdlib Standard library issue

Comments

@DanyDollaro
Copy link
Contributor

DanyDollaro commented Sep 21, 2024

In the STD the following files: pool.c3, thread.c3 are missing important functions implementations that also involves their underlying libraries such as thread_win32.c3, specifically:

  1. The Mutex class in thread.c3 does not check if a thread is trying to acquire it twice, deadlocking it.
fn void main()
{
    Mutex m;
    m.init()!!;

    m.lock()!!;
    m.lock()!!; // <- deadlock
}

In this situation it would be preferable to return a fault.

  1. Always the Mutex class lack of release controls
fn void main()
{
    Mutex m;
    m.init()!!;

    if (try m.unlock())
    {
        io::printn("unlock succeeded");
    }

    if (try m.unlock())
    {
        io::printn("unlock succeeded");
    }
}

since this code prints the following

unlock succeeded
unlock succeeded
  1. The actualt ThreadPool implementation make use of a fixed array to store the functions to be called
struct ThreadPool
{
	Mutex mu;
	QueueItem[SIZE] queue;
	usz qindex;
	// ...

Are there any particular reasons for this choice? Since a list or even a queue would be much better.

  1. The following test function is acquiring and releasing the work_done mutex using two different threads, the main thread and the one taken from the pool, wich is effectively wrong and worked only due to the lack of checks of the mutex implementation.

In this issue I mainly talked about some bugs in the Windows implementation, not sure about the POSIX one. In case I find out something new, I will add it here, since I have not yet delved into the other mutex types.

@DanyDollaro DanyDollaro changed the title Threads and mutexes libreries auditing Threads and mutexes libraries auditing Sep 21, 2024
@DanyDollaro DanyDollaro changed the title Threads and mutexes libraries auditing Threads and mutexes libraries bugs Sep 21, 2024
@lerno lerno self-assigned this Sep 21, 2024
@lerno lerno added the Stdlib Standard library issue label Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stdlib Standard library issue
Projects
None yet
Development

No branches or pull requests

2 participants