Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements...

84
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 1 Project Loom June 2018 Ron Pressler, Alan Bateman

Transcript of Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements...

Page 1: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !1

Project Loom

June 2018Ron Pressler, Alan Bateman

Page 2: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !2

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Safe Harbor Statement

Page 3: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !3

Project Loom

• Continuations • Fibers • Tail-calls

Page 4: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !4

Why Fibers

Today, developers are forced to choose between

App

Connections

simple (blocking / synchronous), but less scalable code (with threads)

App

Connections

complex, non-legacy-interoperable, but scalable code (asynchronous)

and

Page 5: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !5

Why Fibers

With fibers, devs have both: simple, familiar, maintainable, interoperable code, that is also scalable

App

Connections

Fibers make even existing server applications consume fewer machines (by increasing utilization), significantly reducing costs

Page 6: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !6

Continuations: The User Perspective

Page 7: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !7

What

A continuation (precisely: delimited continuation) is a program object representing a computation that may be suspended and resumed (also, possibly, cloned or even serialized).

Page 8: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !8

Continuations: User Perspective

package java.lang;

public class Continuation implements Runnable {public Continuation(ContinuationScope scope, Runnable body);

public final void run();public static void yield(ContinuationScope scope);public boolean isDone();

protected void onPinned(Reason reason) { throw new IllegalStateException("Pinned: " + reason); }}

Page 9: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !9

Continuations: User Perspective

Continuation cont = new Continuation(SCOPE, () -> { while (true) { System.out.println("before"); Continuation.yield(SCOPE); System.out.println("after"); }});

while (!cont.isDone()) { cont.run();}

Page 10: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !10

Fibers

Page 11: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !11

• A light weight or user mode thread, scheduled by the Java virtual machine, not the operating system

• Fibers are low footprint and have negilgible task-switching overhead. You can have millions of them!

What is a fiber?

Page 12: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !12

• The runtime is well positioned to manage and schedule application threads, esp. if they interleave computation and I/O and interact very often (exactly how server threads behave)

• Make concurrency simple again

Why fibers?

Page 13: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !13

fiber = continuation + scheduler

Page 14: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !14

• A fiber wraps a task in a continuation

• The continuation yields when the task needs to block

• The continuation is continued when the task is ready to continue

• Scheduler executes tasks on a pool of carrier threads

• java.util.concurrent.Executor in the current prototype

• Default/built-in scheduler is a ForkJoinPool

fiber = continuation + scheduler

Page 15: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !15

• Current focus is on the control flow and concepts, not the API

• Minimal java.lang.Fiber in current prototype that supports

1. Starting a fiber to execute a task

2. Parking/unparking

3. Waiting for a fiber to terminate

User facing API

Page 16: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !16

Implementing Fibers

Page 17: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !17

• A fiber wraps a user’s task in a continuation

• The fiber task is submited to the scheduler to start or continue the continuation, essentially:

mount();try { cont.run();} finally { unmount();}

Page 18: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !18

Page 19: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !19

LockSupport.park

ForkJoinPool.runWorker

ForkJoinWorkerThread.run

Unsafe.park

Carrier thread waiting for work

Page 20: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !20

Fiber.runContinuation

ForkJoinTask$RunnableExecuteAction.exec

:

ForkJoinWorkerThread.runA fiber is scheduled on the carrier thread. The fiber task runs.

Page 21: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !21

Main.lambda$main$0

Continuation.enter0

Continuation.enter

Continuation.run

Fiber.runContinuation

ForkJoinTask$RunnableExecuteAction.exec

:

ForkJoinWorkerThread.run

The fiber runs the continuation to run the user’s task.

Page 22: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !22

Contuation.yield

Fiber.park

java.util.concurrent.LockSupport.park

:

java.util.concurrent.ReentrantLock.lock

Main.lambda$main$0

Continuation.enter0

Continuation.enter

Continuation.run

Fiber.runContinuation

ForkJoinTask$RunnableExecuteAction.exec

:

ForkJoinWorkerThread.run

The task attempts acquire a lock which leads to the continuation yielding

Page 23: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !23

Fiber.runContinuation

ForkJoinTask$RunnableExecuteAction.exec

:

ForkJoinWorkerThread.run

The continuation stack is saved and control returns to the fiber’s task at the instruction following the call to Continuation.run

Page 24: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !24

LockSupport.park

ForkJoinPool.runWorker

ForkJoinWorkerThread.run

Unsafe.park

The fiber task terminates. The carrier thread goes back to waiting for work.

Page 25: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !25

The owner of the lock releases it. This unparks the Fiber waiting to acquire the lock by scheduling its task to run again.

ReentrantLock.unlock

LockSupport.unpark

Fiber.unpark

ForkJoinPool.execute

Page 26: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !26

Fiber.runContinuation

ForkJoinTask$RunnableExecuteAction.exec

:

ForkJoinWorkerThread.run

The fiber task runs again, maybe on a different carrier thread

Page 27: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !27

Fiber.runContinuation

ForkJoinTask$RunnableExecuteAction.exec

:

ForkJoinWorkerThread.run

The fiber task invokes Continuation run (again) to continue it

Continuation.run

Page 28: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !28

java.util.concurrent.ReentrantLock.lock

Fiber.park

java.util.concurrent.LockSupport.park

:

Main.lambda$main$0

Continuation.enter0

Continuation.enter

Continuation.run

Fiber.runContinuation

ForkJoinTask$RunnableExecuteAction.exec

:

ForkJoinWorkerThread.run

The stack is restored and control continues at the instruction following the call to Continuation.yield

Page 29: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !29

Continuation.enter

Main.lambda$main$0

Continuation.enter0

Continuation.run

Fiber.runContinuation

ForkJoinTask$RunnableExecuteAction.exec

:

ForkJoinWorkerThread.run

The user’s task continues.

Page 30: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. !30

Fiber.runContinuation

ForkJoinTask$RunnableExecuteAction.exec

:

ForkJoinWorkerThread.run

The user’s task completes and the continuation terminates. Control returns to the fiber’s task at the instruction following the call to Continuation.run

Page 31: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !31

• A big question, lots of trade-offs

• Do we completely re-imagine threads?

• Do we attempt to allow all existing code to run in the context of a fiber?

• Likely to wrestle with this topic for a long time

• Current prototype can run existing code

… but with some limitations, as we will see

How much existing code can fibers run?

Page 32: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !32

• Example uses Jetty and Jersey

Example using existing code/libraries

Page 33: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !33

• Assume servlet or REST service that spends a long time waiting

Example with existing code/libraries

assume this takes 100ms

Page 34: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !34

Default configuration (maxThreads = 200), load = 5000 HTTP request/s

Page 35: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !35

maxThreads = 400, load = 5000 HTTP request/s

Page 36: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !36

fiber per request, load = 5000 HTTP request/s

Page 37: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !37

• Can’t yield with native frames on continuation stack

native method

may park/yield

Limitations

Page 38: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !38

• Can’t yield while holding or waiting for a monitor

may park carrier thread

Limitations

may park carrier thread

Page 39: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !39

• Current limitations

• Can’t yield with native frames on continuation stack

• Can’t yield while holding or waiting for a monitor

• In both cases, parking may pin the carrier thread

• What about the existing Thread API and Thread.currentThread() ?

Limitations

Page 40: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !40

Relationship between Fiber and Thread in current prototype

Strand

Thread Fiber

Page 41: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !41

• Current prototype

• First use of Thread.currentThread() in a fiber creates a shadow Thread

• “unstarted” Thread from perspective of VM, no VM meta data

• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers

• Thread locals become fiber local (for now)

• ThreadLocal and the baggage that is InheritableThreadLocal, context ClassLoader, ..

• Special case ThreadLocal for now to avoid needing Thread object

Thread.currentThread() and Thread API in current prototype

Page 42: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !42

Thread Locals

• Spectrum of uses

• Container managed cache of connection or credentials context

• Approximating processor/core local in lower level libraries

• …

• Significant topic for later

Page 43: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !43

Footprint

• Thread

• Typically 1MB reserved for stack + 16KB of kernel data structures

• ~2300 bytes per started Thread, includes VM meta data

• Fiber

• Continuation stack: hundreds of bytes to KBs

• 200-240 bytes per fiber in current prototype

Page 44: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !44

• Thread sleep, join

• java.util.concurrent and LockSupport.park

• I/O

• Networking I/O: socket read/write/connect/accept

• File I/O

• Pipe I/O

APIs that potentially park

Page 45: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !45

• Current prototype executes tasks as Runnable. Easy to use CompletableFuture too.

• j.u.concurrent just works so can share objects or share by communicating

• Not an explicit goal at this time to introduce new concurrency APIs but new APIs may emerge

Communication between fibers

Page 46: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !46

Implementing Continuations

Page 47: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !47

We need:

• Millions of continuations (=> low RAM overhead) • Fast task-switching (=> no stack copying)

Page 48: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !48

Native Stack Continuation

stack refStackrun

Page 49: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !49

Native Stack Continuation

stack refStack

Entry

run

enter

Page 50: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !50

Native Stack Continuation

stack refStack

Entry

run

yieldYield

A

B

C

enter

Page 51: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !51

Native Stack Continuation

stack refStack

Entry

run

yieldYield

A

B

C

enter

freeze

Page 52: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !52

Native Stack Continuation

stack refStack

Entry

run

yieldYield

A

B

C

enter

yield

freeze

Page 53: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !53

Native Stack Continuation

stack refStack

Entry

run

yieldYield

A

B

C

enter

yield

Examine the frame for pinning

freeze

Page 54: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !54

Native Stack Continuation

stack refStack

Entry

run

yieldYield

A

B

C

enter

yield

C“Raw” copy

freeze

Page 55: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !55

Native Stack Continuation

stack refStack

Entry

run

yieldYield

A

B

C

enter

yield

CExtract oops

freeze

Page 56: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !56

Native Stack Continuation

stack refStack

Entry

run

yieldYield

A

B

C

enter

freeze

yield

A

B

C

Page 57: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !57

Native Stack Continuation

stack refStackrun

yield

A

B

C

Page 58: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !58

Native Stack Continuation

stack refStack

Entry

run

enter

doContinue

yield

A

B

C

Page 59: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !59

Native Stack Continuation

stack refStack

Entry

run

enter

yield

A

B

C

thaw

Page 60: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !60

Native Stack Continuation

stack refStack

Entry

run

enter

yield

A

B

C

A

“Raw” copy

thaw

Page 61: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !61

Native Stack Continuation

stack refStack

Entry

run

enter

yield

A

B

C

ARestore oops

thaw

Page 62: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !62

Native Stack Continuation

stack refStack

Entry

run

enter

yield

A

B

C

APatch

thaw

Page 63: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !63

Native Stack Continuation

stack refStack

Entry

run

enter

yield

A

B

C

A

yield

B

C

Yieldthaw

Page 64: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !64

Native Stack Continuation

stack refStack

Entry

run

enter

A

yield

B

C

Yield

Page 65: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !65

Native Stack Continuation

stack refStack

Entry

run

enter

yield

A

B

C

Lazy copy

Page 66: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !66

Native Stack Continuation

stack refStack

Entry

run

enter

yield

A

B

C

doContinue

Page 67: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !67

Native Stack Continuation

stack refStack

Entry

run

enter

yield

A

B

C

thaw

Page 68: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !68

Native Stack Continuation

stack refStack

Entry

run

enter

yield

A

B

Cyield

C

thaw

Page 69: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !69

Native Stack Continuation

stack refStack

Entry

run

enter A

B

yield

C

thaw

Page 70: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !70

Native Stack Continuation

stack refStack

Entry

run

enter A

B

yield

CInstall return barrier (if there are more frozen frames)

thaw

Page 71: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !71

Native Stack Continuation

stack refStack

Entry

run

enter A

B

yield

C

Yield

Page 72: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !72

Native Stack Continuation

stack refStack

Entry

run

enter A

BC

Page 73: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !73

Native Stack Continuation

stack refStack

Entry

run

enter A

B

thaw

Page 74: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !74

Native Stack Continuation

stack refStack

Entry

run

enter A

BB

thaw

Page 75: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !75

Native Stack Continuation

stack refStack

Entry

run

enter A

B

thaw

Page 76: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !76

Native Stack Continuation

stack refStack

Entry

run

enter A

BInstall return barrier

thaw

Page 77: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !77

Native Stack Continuation

stack refStack

Entry

run

enter A

B

yield

D

Yield

Page 78: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !78

Native Stack Continuation

stack refStack

Entry

run

enter A

B

yield

D

B

yield

D

freeze

Yield

Page 79: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !79

Native Stack Continuation

stack refStackrun

A

B

yield

D

Page 80: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !80

Epilogue

Page 81: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !81

Features not in current prototype

• Serialization and cloning • JVM TI and debugging support for fibers • Tail calls

Page 82: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !82

Next Steps

• Design behavior and API • Add missing features • Improve performance

Page 83: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. !83

More information

• Project Loom page: http://openjdk.java.net/projects/loom/

• Mailing list: [email protected]

• Repo: http://hg.openjdk.java/net/loom/loom

Page 84: Project Loom - Oraclecr.openjdk.java.net/~rpressler/loom/JVMLS2018.pdf• Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers •