ta0kira / zeolite

Zeolite is a statically-typed, general-purpose programming language.
Apache License 2.0
18 stars 0 forks source link

Add condition support to `lib/thread`. #142

Closed ta0kira closed 3 years ago

ta0kira commented 3 years ago

This doesn't need to have the same calling convention as pthread_cond_wait, but the semantics should roughly be this:

  1. One or more threads block on the condition.
  2. A separate thread triggers the condition.
  3. All threads continue.

Since pthread_cond_wait requires the caller to both lock and unlock the mutex, this can be built into the "wait" call in the implementation. This means that the mutex can be entirely encapsulated in the implementation.

ta0kira commented 3 years ago

Actually, the condition needs to expose some mutex logic so that the thread doing the triggering can wait for all of the threads to respond.

ta0kira commented 3 years ago

Some questions that need to be answered:

All of these questions have a common theme: Is it a general requirement that there be some sort of state that is checked or updated before/after a wait call?


An example use-case:

Data is read by one thread and is processed by others.

Processing thread:

lock mutex

while data not present
  wait condition

take data
unlock mutex
process data

Read thread:

read data
lock mutex
append data
unlock mutex
continue condition

In the processing thread, multiple things happen while the lock is held, in addition to wait.


So, it might be useful to have the condition expose mutex logic in addition to wait/continue, then have wrappers for use-cases such as the example above.