Inside core.async Channels. Rich Hickey

Similar documents
System Architecture Directions for Networked Sensors[1]

Synchronising Threads

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

com.walmartlabs/lacinia-pedestal Documentation

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

05-concurrency.txt Tue Sep 11 10:05: , Fall 2012, Class 05, Sept. 11, 2012 Randal E. Bryant

Synchronizing the Asynchronous

CS11 Java. Fall Lecture 7

Ideas to improve glibc and Kernel interaction. Adhemerval Zanella

So far, we've seen situations in which locking can improve reliability of access to critical sections.

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Programming Safe Agents in Blueprint. Alex Muscar University of Craiova

Chapter 6: Process Synchronization

Deterministic Futexes Revisited

Typed Assembly Language for Implementing OS Kernels in SMP/Multi-Core Environments with Interrupts

Advance Operating Systems (CS202) Locks Discussion

CS 450 Exam 2 Mon. 4/11/2016

JS Event Loop, Promises, Async Await etc. Slava Kim

A mutex is a global object that allows you to control code execution order and to prevent conflicts in a multi-thread application. Basically, a mutex

INTERFACING REAL-TIME AUDIO AND FILE I/O

Questions from last time

C# Asynchronous Programming Model

Reminder from last time

Abstract. Introduction

Lecture 32: Volatile variables, Java memory model

Asynchronous Interactions and Managing Modeless UI with Autodesk Revit API

Java ReentrantLock (Part 1)

Condition Variables & Semaphores

Batches and Commands. Overview CHAPTER

Locks and semaphores. Johan Montelius KTH

June 27, 2014 EuroClojure 2014 Krakow, Poland. Components. Just Enough

This is a talk given by me to the Northwest C++ Users Group on May 19, 2010.

Deviations are things that modify a thread s normal flow of control. Unix has long had signals, and these must be dealt with in multithreaded

recap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a

Lecture 21: Transactional Memory. Topics: consistency model recap, introduction to transactional memory

Multithreading and Interactive Programs

CPS 310 second midterm exam, 11/14/2014

Project 2: Part 1: RPC and Locks

Symbols. abstractions, implementing, 270 through indirection, 77 with macros, 183 abstract syntax tree (AST), 149

Vulkan: Scaling to Multiple Threads. Kevin sun Lead Developer Support Engineer, APAC PowerVR Graphics

Programming with the Service Control Engine Subscriber Application Programming Interface

PROCESS SYNCHRONIZATION

Stuart

Programming with the Service Control Engine Subscriber Application Programming Interface

Intertask Communication

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Java Threads. COMP 585 Noteset #2 1

Groking the Linux SPI Subsystem FOSDEM Matt Porter

C++ Threading. Tim Bailey

Chapter 6: Process Synchronization. Module 6: Process Synchronization

A Non-Blocking Concurrent Queue Algorithm

Transactifying Apache s Cache Module

Discussion CSE 224. Week 4

Locks and semaphores. Johan Montelius KTH

CS 241 Honors Concurrent Data Structures

College of Computer & Information Science Spring 2010 Northeastern University 26 January 2010

EECS 482 Introduction to Operating Systems

CS 167 Final Exam Solutions

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Lecture 15: I/O Devices & Drivers

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

What's wrong with Semaphores?

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

TRANSACTIONS AND ABSTRACTIONS

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Monitors; Software Transactional Memory

Adaptive algorithm for High-Performance FPGA Cores

Algorithms for Scalable Synchronization on Shared Memory Multiprocessors by John M. Mellor Crummey Michael L. Scott

Concurrency Bugs in the Network Stack of NetBSD

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Big Data Technology Incremental Processing using Distributed Transactions

Relaxed Memory-Consistency Models

Reminder from last time

Design of Thread-Safe Classes

Non-blocking Array-based Algorithms for Stacks and Queues. Niloufar Shafiei

Multithreading and Interactive Programs

Actors in the Small. Making Actors more Useful. Bill La

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Pebbles Kernel Specification September 26, 2004

CSE 153 Design of Operating Systems

A Sense of Time for JavaScript and Node.js

ActiveVOS Dispatch Service. ActiveVOS 9.0+

ApacheCon NA How to Avoid Common Mistakes in OFBiz Development Presented by Adrian Crum

GStreamer Element States How do they work in detail?

Foundations of the C++ Concurrency Memory Model

Lecture 7: Transactional Memory Intro. Topics: introduction to transactional memory, lazy implementation

Chapter 7: Process Synchronization!

Open Nesting in Software Transactional Memory. Paper by: Ni et al. Presented by: Matthew McGill

PROJECT 6: PINTOS FILE SYSTEM. CS124 Operating Systems Winter , Lecture 25

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

RE-ENTRANCY IN OPERATING SYSTEM. - By Surya Seetharaman

Concurrent Objects and Linearizability

Linux Driver and Embedded Developer

IV. Process Synchronisation

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Actor-Based Concurrency: Implementation and Comparative Analysis With Shared Data and Locks Alex Halter Randy Shepherd

Concurrent and Distributed Systems Introduction

CS Introduction to Data Structures How to Parse Arithmetic Expressions

Transcription:

Inside core.async s Rich Hickey

Warning! Implementation details Subject to change

The Problems Single channel implementation For use from both dedicated threads and go threads simultaneously, on same channel alt and atomicity multi-read/write concurrency

API >! <! >!! put! <!! take! alt! alt!

SPI >! <! >!! put! impl/put! [val handler] impl/take! [handler] <!! take! alt! alt!

Anatomy user-specified buffer buffer() internal linked queues of handlers

Invariants Never pending puts and takes Never takes and anything in buffer Never puts and room in buffer take! and put! use channel mutex no global mutex

put! buffer() completes handler impl/put! [val handler] buffer() buffer()

put! - windowed buffers sliding buffer() X impl/put! [val handler] dropped dropping buffer() dropped

take! buffer() buffer() impl/take! [handler] completes handler buffer()

close! all pending takes complete with nil (closed) subsequent puts complete with nil (already closed) subsequent takes consume ordinarily until empty any pending puts complete with true takes then complete with nil

Queue Limits puts and takes queues are not unbounded 1024 pending ops limit will throw if exceeded not for buffering, use buffers/windowing

alt(s!!) attempts more than one op on more than one channel without global mutex nor multi-channel locks exactly one op can succeed

alt implications registration of handlers is not atomic completion might occur before registrations are finished or any time thereafter completion of one alternative must disable the others atomically cleanup

Handlers Wrapper around a callback SPI active? commit -> callback-fn lock-id -> unique-id java.util.concurrent.locks.lock: lock, unlock

take/put handlers simple wrapper on callback lock is no-op lock-id is 0 active? always true commit -> the callback

alt handlers each op handler wraps its own callback, but delegates rest to shared flag handler flag handler has lock a boolean active? flag that starts true and makes one-time atomic transition commit transitions shared flag and returns callback must be called under lock

alt handlers alt handler shared flag callback alt handler shared flag callback alt handler shared flag mutex lock-id flag shared flag callback

alt concurrency no global or multi-channel locking but channel does multi-handler locking some ops commit both a put and take lock-ids used to ensure consistent lock acquisition order

alt cleanup disabled handlers will still be in queues channel ops purge buffer() buffer()

SPI revisited handler callback only invoked on async completion only 2 scenarios when not parked, op happens immediately callback is not used non-nil return value is op return

put! buffer() completes handler impl/put! [val handler] buffer() buffer()

take! buffer() buffer() impl/take! [handler] completes handler buffer()

Wiring!/!! blocking ops (!!) create promise callback delivers only deref promise on nil return from op parking go ops (!) IOC state machine code is callback

Summary You don t need to know any of this But understanding the machine can help you make good decisions