Giter Site home page Giter Site logo

lemmy / blockingqueue Goto Github PK

View Code? Open in Web Editor NEW
483.0 12.0 21.0 18.68 MB

Tutorial "Weeks of debugging can save you hours of TLA+". Each git commit introduces a new concept => check the git history!

Home Page: https://youtu.be/wjsI0lTSjIo

License: MIT License

TLA 72.15% Java 10.51% Makefile 0.42% C 5.08% R 5.18% Objective-C 1.95% Shell 3.07% Rust 1.63%
tlaplus model-checking java fifo fifo-queue specification tlaps tla

blockingqueue's Introduction

💻 Working as a Software Engineer at Microsoft on TLA+.

Anurag's github stats

blockingqueue's People

Contributors

lemmy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

blockingqueue's Issues

Polish and extend tutorial

Polish

  • Starvation -> NoStarvation
  • Invariant -> NoDeadlock or DeadlockFree (proof has theorem DeadlockFreedom)
  • Java take vs. TLA get action names
  • Align bound variable x in \E x \in waitSet... in Notify with Wait and Next, i.e. t
  • Inconsistent terminology around processes and threads
  • Comment and spec contradictory (conjunct even seems superfluous):
    \* ...there a fewer Poison messages in the buffer than (non-terminated)
    \* Consumers.
    \/ Cardinality(cons) < Cardinality({i \in DOMAIN buffer: buffer[i]=Poison})

Extend

Code pointers

No deadlock on macOS with POSIX threads

TLDR: On macOS, don't lock a mutex that has not been initialized, don't wait on a condition that has not been initialized.

Everything that is written below is super interesting and plausible. However, it is all wrong! Spurious wakeups are a red herring, and the real problem is the way more mundane/trivial coding bug below that only manifests on macOS.
The missing initialization would have been detectable on macOS with a test that asserts the consistency of the elements that are enqueued and dequeued. It is also obvious, that trace validation would have spotted the bug immediately.

diff --git a/impl/producer_consumer.c b/impl/producer_consumer.c
index bf7efda..46cfdfe 100644
--- a/impl/producer_consumer.c
+++ b/impl/producer_consumer.c
@@ -73,6 +73,9 @@ int main(int argc, char * argv[]) {
 
        printf("Buffer size = %d, # Producers = %d, # Consumers = %d\n", buff_size, numProducers, numConsumers);
 
+       pthread_mutex_init(&mutex, NULL);
+       pthread_cond_init(&modify, NULL);
+
        /* Allocate space for the buffer */
        buffer = malloc(sizeof(int) * buff_size);
        pthread_t prods[numProducers], cons[numConsumers];

Works with:

markus@avocado:~$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

Fails with:

markus@armbook ~ % gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: arm64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Red herring: spurious wake-ups

The C code (POSIX) deadlocks on Windows and Linux but doesn't deadlocked on Intel (MacBook Pro Retina Early 2015) or Apple Silicon M1 mac. However, the Cocoa variant below deadlocks as expected. This seemingly has to do with the likelihood of pthread_cond.c causing spurious wake-ups (signal is effectively broadcast?) on macOS, which POSIX generally allows for performance reasons. Also see a related SO discussion.


(list copied from a Rust issue at rust-lang/rust#62291)

why bounded queues don't play well

\* on the same page that "Poison pills work reliably only with unbounded queues."

I think that this is because of an (unstated?) presumption that producers are unwilling to block forever, so with a bounded queue you would need a PutFail action that loses a poison pill.

Thanks for this tutorial, it has been great fun to work through!

Questions about "Weeks of Debugging Can Save You Hours of TLA+"

Hi, Kuppe:
I have read your great talk presentation from here:
https://bitbucket.org/lemmster/blockingqueue

But I have a little confused about the state graph in “BlockingQueue in PlusCal” section,as attached.
image

Highlighted in red box.
If the "p1" is already in the waitset, the 'put' action should not occur because we have only one producer and it is blocked now.
PS: the state graph in this git project has no such action.

I’m not sure if my understanding is correct or if that PDF is out of date.
Please help. Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.