Skip to content

Add threadLock keyword -- locks mutex, evals code, then unlocks #3749

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

Draft
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

d-torrance
Copy link
Member

We add a threadLock keyword as suggested in #3739 (comment) to help write thread-safe code. For example:

i1 : x = new MutableList;

i2 : y = new MutableList;

i3 : scan(1000, i -> schedule(() -> (x##x = null; threadLock y##y = null)))

i4 : #x

o4 = 180

i5 : #y

o5 = 1000

TODO:

  • Add documentation
  • Deal with any potential issues (e.g., what happens if we cancel a task that locked the mutex before it unlocks it?)

mutex := newMutex;
threadLock(c:Code):Expr := (
r := 0;
while (r = trylock(mutex); r == Ccode(int, "EBUSY")) do (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not good. I think you want pthread_cond_wait or similar.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I've read, pthread_mutex_trylock is the only way to check if a mutex is locked. pthread_cond_wait blocks until some condition is met, and we don't want to block since we want to check for interrupts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely think we need pthread conditions, because you can signal only one or all threads using pthread_cond_signal and pthread_cond_broadcast that an event has changed. This is how tasks are implemented too. e.g. this is how taskResult blocks:

lock_guard<pthreadMutex> lock(m_Mutex);
while(!m_Done && m_KeepRunning)
{
pthread_cond_wait(&m_FinishCondition,&m_Mutex.m_Mutex);
}
return m_Result;

In fact, I suspect if we change interrupt_handler in main.cpp to also signal the current blocking task, it would interrupt just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants