Kevin van Vechten Core OS

Similar documents
Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Fall Stanford CS193p Fall 2010

Stanford CS193p. Developing Applications for ios. Spring Stanford CS193p. Spring 2012

Grand Central Dispatch and NSOperation. CSCI 5828: Foundations of Software Engineering Lecture 28 12/03/2015

ios Performance and Concurrency Patrick Thomson

Power, Performance, and Diagnostics

Grand Central Dispatch. Sri Teja Basava CSCI 5528: Foundations of Software Engineering Spring 10

ITP 342 Mobile App Dev

Mobile Application Development

Game Center Techniques, Part 1

Introducing the Photos Frameworks

Working Effectively with Objective-C on iphone OS. Blaine Garst Wizard of Runtimes

Advanced Memory Analysis with Instruments. Daniel Delwood Performance Tools Engineer

Finding Bugs Using Xcode Runtime Tools

Multitasking and Background Execution

Mastering Xcode for iphone OS Development Part 1. Todd Fernandez Sr. Manager, IDEs

Cross Platform Nearby Networking

Building a (Core) Foundation. Rob Napier

Using and Extending the Xcode Source Editor

What s New in the LLVM Compiler. Chris Lattner LLVM Chief Architect

Mastering Xcode for iphone OS Development Part 2. Marc Verstaen Sr. Manager, iphone Tools

Concurrency. CS 442: Mobile App Development Michael Saelee

ITP 342 Mobile App Dev. Animation

Advanced Debugging and the Address Sanitizer

Using HTML5 Offline Storage. Brady Eidson Safari and WebKit Engineer

E Event-based parser, XML, 180 Extended attributes, URLs, 118 API, 119 command line, 118 description, 118 NSURL category, 119

Seamless Linking to Your App

MRCP. Client Integration Manual. Developer Guide. Powered by Universal Speech Solutions LLC

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

What s New in Xcode App Signing

Concurrent Programming in Mac OS X and ios

Optimizing Swift Performance Session 409

Implementing UI Designs in Interface Builder

App Extension Best Practices

Introducing the Contacts Framework

Advanced Cocoa Text Tips and Tricks. Aki Inoue Cocoa Engineer

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Accessibility on ios. Developing for everyone. Frameworks #WWDC14. Session 210 Clare Kasemset ios Accessibility

Building Watch Apps #WWDC15. Featured. Session 108. Neil Desai watchos Engineer

ITP 342 Mobile App Dev. Animation

Grand Central Dispatch

Address Book for iphone

Short Notes of CS201

Accessibility on OS X

Intro to Native ios Development. Dave Koziol Arbormoon Software, Inc.

CS201 - Introduction to Programming Glossary By

Data Delivery with Drag and Drop

Chapter 4: Multi-Threaded Programming

CS333 Intro to Operating Systems. Jonathan Walpole

Editing Media with AV Foundation

C Blocks Block CPRE 388

Fix Bugs Faster Using Activity Tracing

Dotstack Porting Guide.

CS 4411 OS Practicum. Oliver Kennedy

Creating Extensions for ios and OS X, Part Two

What s New in Foundation for Swift Session 207

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Cross-compiling C++ to JavaScript. Challenges in porting the join.me common library to HTML5

Using the Camera with AV Foundation Overview and best practices. Brad Ford iphone Engineering

Introducing the Modern WebKit API

Introducing CloudKit. A how-to guide for icloud for your Apps. Frameworks #WWDC14. Session 208 Olivier Bonnet CloudKit Client Software

Creating Extensions for Safari

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Threads Tuesday, September 28, :37 AM

Chapter 4: Threads. Operating System Concepts 9 th Edition

Runtime Support for Algol-Like Languages Comp 412

Getting the Most Out of HealthKit

What s New in tvos #WWDC16. App Frameworks. Session 206. Hans Kim tvos Engineer

PusleIR Multitouch Screen Software SDK Specification. Revision 4.0

Creating Content with iad JS

Jython. An introduction by Thinh Le

Mysteries of Auto Layout, Part 1

Operating System: Chap13 I/O Systems. National Tsing-Hua University 2016, Fall Semester

CS193P - Lecture 10. iphone Application Development. Performance

Enhancing your apps for the next dimension of touch

Mobile Application Development

C++ Objects Overloading Other C++ Peter Kristensen

Lecture 3. Variables. Variables

Storyboards and Controllers on OS X

2. Classes: Abstraction & Encapsulation

COCOA WORKSHOP PART 1. Andreas Monitzer

ios 8 SDK Development

Developing Applications for ios

Creating Complications with ClockKit Session 209

Chapter 4: Threads. Operating System Concepts 9 th Edition

Swift API Design Guidelines

CS193P - Lecture 13. iphone Application Development. Address Book - Putting People in Your App

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Apple Watch Design Tips and Tricks

What s New in Testing

What s New in Core Data?

Mobile Application Development

SYNTHESIZING CONCURRENT SCHEDULERS FOR IRREGULAR ALGORITHMS. Donald Nguyen, Keshav Pingali

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Fundamentals of Programming Session 12

What s New in CloudKit

What's New in UIKit Dynamics and Visual Effects Session 229

Designing for Apple Watch

Objective-C ICT/7421ICTNathan. René Hexel. School of Information and Communication Technology Griffith University.

Transcription:

Kevin van Vechten Core OS 2

3

Bill Bumgarner 4

(lambda (a) (add a d)) 10 timesrepeat:[pen turn:d; draw] z.each { val puts(val + d.to_s)} repeat(10, ^{ putc('0'+ d); }); 5

6

7

8

^ 9

[myset objectspassingtest: ^(id obj, BOOL *stop) { return [obj testvalue: value]; }]; dispatch_async(main_queue, ^{ [viewcontroller displaynewstuff]; }); 10

^ BOOL (id item) { return [item length] > 20; } ^ void (id item) { [item doonegoodthing]; } ^ (id item) { [item doonegoodthing]; } ^ void (void) { [local dotwogoodthings]; } ^ { [local dotwogoodthings]; } 11

12

void (*callable)(void); 13

void (^callable)(void); 14

char *(^worker)(char *a, BOOL(^done)(int)); 15

typedef BOOL (^doneblk_t)(int); char *(^workb)(char *a, doneblk_t d); 16

17

typedef void (^workblk_t)(int i); 18

void typedef void (^workblk_t)(int i); repeat(int n, workblk_t ablock) { for (int i = 0; i < n; ++i) ablock(i); } 19

int d = 2; workblk_t w = ^(int i){ printf( %d\n", i * d); }; repeat(5, w); Output 20

Over 100 APIs Foundation Grand Central Dispatch 21

22

[myset objectspassingtest: ^(id obj, BOOL *stop) { return [obj testvalue: value]; }]; [adictionary enumeratekeysandobjectsusingblock: ^(id k, id v, BOOL *stop) { NSLog(@"%@ => %@", k, v); }]; 23

[application beginbackgroundtaskwithexpirationhandler: ^{ /* expiration handler callback code */ }]; [anoperation setcompletionblock: ^{ /* handle operation completion */ }]; 24

[operationqueue addoperationwithblock: ^{ /* hard work is hard */ }]; dispatch_async(main_queue, ^{ [viewcontroller displaynewstuff]; }); 25

// thread a dispatch_async(queue, ^{... }); // thread b dispatch_sync(queue, ^{... }); // main thread dispatch_async(queue, ^{... }); 26

27

Blocks are Objective-C objects Block objects start out on the stack Copied to the heap using [ablock copy] or Block_copy() Release with [ablock release] or Block_release() Blocks have private const copy of stack (auto) variables Object references are retained Mutable variables must be declared with block keyword Shared with all other blocks that use that variable Shared with the scope of declaration block Object references are not retained 28

29

block int shared; int captured = 10; block2: const int captured = 10; Stack block1: block1 const = ^{ int captured = 10; shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 30

block int shared; int captured = 10; block2: block1: block3: const int captured = 10; const int captured = 10; Stack block1 = ^{ shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 31

block int shared; int captured = 10; block1: block3: const int captured = 10; const int captured = 10; block2: const int captured = 10; Stack block1 = ^{ shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 32

block int shared; int captured = 10; block1: const int captured = 10; block2: const int captured = 10; block3: const int captured = 10; Stack block1 = ^{ shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 33

block int shared; int captured = 10; block1: const int captured = 10; block2: const int captured = 10; Stack block1 = ^{ shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 34

block int shared; int captured = 10; block1: const int captured = 10; block2 & block3: const int captured = 10; Stack block1 = ^{ shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 35

block int shared; int captured = 10; block1: const int captured = 10; block2 & block3: const int captured = 10; Stack block1 = ^{ shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 36

block int shared; int captured = 10; block1: const int captured = 10; Stack block1 = ^{ shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 37

block int shared; int captured = 10; block1: const int captured = 10; block2 & block3: const int captured = 10; Stack block1 = ^{ shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 38

block int shared; block2 & block3: const int captured = 10; Stack block1 = ^{ shared += captured; }; block2 = [block1 copy]; block3 = [block1 copy]; Function() Heap 39

Shiva Bhattacharjee Core OS 40

Threading is hard Using GCD makes it simple and fun No explicit thread management Extremely efficient under the hood GCD and Blocks are a powerful duo 41

Keeping your app responsive 42

Shiva Bhattacharjee 43

Do not block the main thread Move work to another thread Update UI back on main thread Text GCD 44

- (void)addtweetwithmsg:(nsstring*)msg url:(nsurl*)url { // Controller UI callback on main thread DTweet *tw = [[DTweet alloc] initwithmsg:msg]; [tweets addtweet:tw display:yes]; dispatch_async(image_queue, ^{ tw.img = [imagecache getimgfromurl:url]; dispatch_async(main_queue, ^{ [tweets updatetweet:tw display:yes]; }); }); [tw release]; } 45

46

Do not block the main thread Move work to another thread Update UI back on main thread Text dispatch_async() 47

Daniel Steffen Core OS 48

Lightweight list of blocks Enqueue/dequeue is FIFO Enqueue with dispatch_async() Dequeue by automatic thread or main thread 49

50

Executes blocks one at a time on main thread Cooperates with the UIKit main run loop dispatch_get_main_queue() 51

- (void)addtweetwithmsg:(nsstring*)msg url:(nsurl*)url { // Controller UI callback on main thread DTweet *tw = [[DTweet alloc] initwithmsg:msg]; [tweets addtweet:tw display:yes]; tw.img = [imagecache getimgfromurl:url]; [tweets updatetweet:tw display:yes]; [tw release]; } 52

- (void)showtweetwithmsg:(nsstring*)msg url:(nsurl*)url { // Controller networkio callback on background thread id d = [NSDictionary dictionarywithobjectsandkeys: msg, @"msg", url, @"url", nil]; [self performselectoronmainthread: @selector(updatetwdisplay:) withobject:d waituntildone:no]; } - (void)updatetwdisplay:(nsdictionary*)d { [self addtweetwithmsg:[d objectforkey:@"msg"] url:[d objectforkey:@"url"]]; } 53

- (void)showtweetwithmsg:(nsstring*)msg url:(nsurl*)url { // Controller networkio callback on background thread id dispatch_async(dispatch_get_main_queue(), d = [NSDictionary dictionarywithobjectsandkeys: ^{ msg, @"msg", url, @"url", nil]; [self }); performselectoronmainthread: @selector(updatetwdisplay:) withobject:d waituntildone:no]; } - (void)updatetwdisplay:(nsdictionary*)d { [self addtweetwithmsg: [d objectforkey:@" msg"] url: [d objectforkey:@" url "]];]; } 54

55

Execute blocks one at a time On automatic helper thread Queue up background work 56

dispatch_queue_t queue; queue = dispatch_queue_create("com.example.purpose", NULL); dispatch_release(queue); 57

But wait, there s even more 58

Enqueuing is thread-safe Execution is serial Protect access to shared data Queues are lightweight 59

queue = dispatch_queue_create( com.example.tweets, NULL); // Main Thread dispatch_async(queue, ^{ [tweets removelasttweet]; }); block NSArray *a; dispatch_sync(queue, ^{ a = [tweets copytweets]; }); // Background Thread dispatch_async(queue, ^{ [tweets addtweet:tw]; }); dispatch_release(queue); 60

61

62

Queues are reference counted dispatch_retain() / dispatch_release() GCD retains parameters to dispatch API as necessary Ensure correct queue lifetime across asynchronous operations 63

- (void)asyncparsedata:(nsdata *)data queue:(dispatch_queue_t)queue block:(void (^)(id result))block { dispatch_retain(queue); dispatch_async(self.parse_queue, ^{ id result = [self parsedata:data]; dispatch_async(queue, ^{ block(result); }); dispatch_release(queue); }); } 64

Ensure objects captured by blocks are valid when blocks are executed Objective-C objects are auto-retained/released Other objects must be retained by your code CFRetain() / CFRelease() 65

66

One queue per task or subsystem Communicate with dispatch_async() Queues are lightweight and efficient Automatic thread recycling 67

Demo app tasks 1. Receive and parse network stream 2. Maintain message history 3. Fetch and cache images 4. Display user interface 68

dispatch_async(network_queue, network_queue ^{ NSData *d = [twitterstream receivedata]; DTweet *tw = [[DTweet alloc] initwithdata:d]; dispatch_async(tweets_queue, tweets_queue ^{ [tweets addtweet:tw]; dispatch_async(main_queue, ^{ [viewcontroller displaytweet:tw]; }); dispatch_async(image_queue, image_queue ^{ tw.img = [imagecache getimgfromurl:tw.url]; dispatch_async(main_queue, ^{ [viewcontroller updatetweetdisplay:tw]; }); }); }); [tw release]; }); 69

Pitfalls Avoid blocking per-subsystem queues Be careful when waiting Blocked worker threads consume resources 70

71

72

73

Dispatch sources Monitor external events Files, Network Sockets, Directories, Timers Event handlers can be delivered to any queue Use sources to replace polling or blocking API calls See session: Simplifying iphone App Development with Grand Central Dispatch 74

dispatch_async(network_queue, dispatch_source_set_event_handler(network_source, ^{ ^{ NSData *d = [twitterstream receivedata]; DTweet *tw = [[DTweet alloc] initwithdata:d]; dispatch_async(tweets_queue, ^{ [tweets addtweet:tw]; dispatch_async(main_queue, ^{ [viewcontroller displaytweet:tw]; }); dispatch_async(image_queue, ^{ tw.img = [imagecache getimgfromurl:tw.url]; dispatch_async(main_queue, ^{ [viewcontroller updatetweetdisplay:tw]; }); }); }); [tw release]; }); 75

76

GCD is part of libsystem.dylib Available to all apps #include <dispatch/dispatch.h> Open Source http://libdispatch.macosforge.org/ libdispatch-dev@lists.macosforge.org 77

Michael Jurewitz Developer Tools and Performance Evangelist jurewitz@apple.com Documentation Concurrency Programming Guide http://developer.apple.com Open Source Mac OS Forge > libdispatch http://libdispatch.macosforge.org Apple Developer Forums http://devforums.apple.com 78

Simplifying iphone App Development with Grand Central Dispatch Working Effectively with Objective-C on iphone OS Advanced Objective-C and Garbage Collection Techniques Mission Friday 10:15AM Pacific Heights Wednesday 9:00AM Pacific Heights Friday 11:30AM 79

Objective-C and Garbage Collection Lab Grand Central Dispatch Lab Grand Central Dispatch Lab Grand Central Dispatch Lab Application Frameworks Lab B Wednesday 2:00PM Core OS Lab A Wednesday 4:30PM Core OS Lab A Thursday 9:00AM Core OS Lab A Friday 11:30AM 80

81

82