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

Stabilize lock statement #357

Closed
jclark opened this issue Oct 6, 2019 · 4 comments
Closed

Stabilize lock statement #357

jclark opened this issue Oct 6, 2019 · 4 comments
Labels
Area/Lang Relates to the Ballerina language specification status/proposed Design is proposed but not yet agreed Type/Improvement Enhancement to language design
Milestone

Comments

@jclark
Copy link
Collaborator

jclark commented Oct 6, 2019

Semantics of experimental implementation are described in #346. We need to decide how we want to do this in the spec. I think we should have semantics that more focused on providing a model for the programmer that is easy to understand and reason about and that does not expose implementation details.

I think it unlikely that the user will want semantics where the effects of multiple lock statements appear to be interleaved. Accordingly, I propose that the semantics be that outermost lock statements are not interleaved. All outermost lock blocks in the program execute in a single total order. Nested lock blocks do nothing. The effect is as if all lock statements execute under a single, global, recursive lock.

This is similar to "atomic sections", which are often provided by language interfaces to software transactional memory e.g. C++. But several kinds of implementation are possible:

  1. naive implementation using a single, global, recursive lock
  2. transactional memory
  3. compile-time lock inference

For Ballerina, we could start with 1 and then do 3. Papers on 3 include:

We also need to decide on the keyword. Possibilities are:

  • lock
  • synchronized
  • atomic

This also relates to the pure function idea #145. We might say that a pure function can access module-level variables only within a lock statement.

@jclark jclark added Type/Improvement Enhancement to language design status/proposed Design is proposed but not yet agreed Area/Lang Relates to the Ballerina language specification labels Oct 6, 2019
@sanjiva
Copy link
Contributor

sanjiva commented Oct 6, 2019

Ref keyword - we did consider all these options when doing this originally.

I prefer to keep lock given we've already used it and the others didn't seem to have any more clarity for the developer. I don't think that has changed.

@jclark
Copy link
Collaborator Author

jclark commented Oct 6, 2019

I am OK with lock: it's a good fit for the semantics I've proposed.

I think atomic is not right for this semantics, since the statement is not executed atomically.

I would also be OK with synchronized. It is better on our familiarity design principle, but it is a harder, more computer-sciencey word.

@jclark jclark added this to the 2020R1 milestone Oct 6, 2019
@vinok88
Copy link

vinok88 commented Dec 20, 2019

It's better to define the semantics related to object and record fields within lock blocks as well. AFAIR, it was not clear on following areas in the previous design,

  • Should we allow a parallel update of record/object reference while its field is accessed within a lock?
  • When it comes to nested record field access, are we locking all the fields or only the leaf node?
    Eg:- Consider the field access of person.address.street within a lock
    should we lock access to both the address field of person and street field of address?

@jclark
Copy link
Collaborator Author

jclark commented Dec 20, 2019

Nothing special about record or object references: lock behaves as if it acquires a single, global, recursive mutex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Lang Relates to the Ballerina language specification status/proposed Design is proposed but not yet agreed Type/Improvement Enhancement to language design
Projects
None yet
Development

No branches or pull requests

3 participants