https://eclecticlight.co/2024/06/10/multitasking-parallel-processing-and-concurrency-in-swift/ Skip to content [eclecticlight] The Eclectic Light Company Macs, painting, and more Main navigation Menu * Downloads * Freeware * M-series Macs * Mac Problems * Mac articles * Macs * Art hoakley June 10, 2024 Macs, Technology Multitasking, parallel processing, and concurrency in Swift It has been a very long time since computers have executed code in a simple, linear manner. Even back in the 1950s, most used a system of interrupts or traps to handle errors, input/output, and other features, although those weren't considered to be multitasking. In computers with a single processor core, multitasking was a way of cheating to give the impression that the processor was doing several things at once, when in fact all it was doing was switching rapidly between two or more different programs. There are two fundamental models for doing that: * cooperative multitasking, in which individual tasks yield to give others processing time; * preemptive multitasking, in which a scheduler switches between tasks at regular intervals. When a processor switches from one task to the next, the current task state must be saved so that it can be resumed later. Once that's complete, the next task is loaded to complete what's known as a context switch. That incurs overhead, both in terms of processing and in memory storage. Inevitably, switching between lightweight tasks has less overhead. There have been different strategies adopted to determine the optimum size of tasks and overhead imposed by context switching, and terminology differs between them, variously using words such as processes, threads and even fibres, which can prove thoroughly confusing. Preemptive multitasking was championed on larger computers and in Unix, but was seldom attempted in personal computers until the mid-1980s. When the Macintosh was released forty years ago, its Motorola 68000 processor had insufficient memory and was short of computing power to make it a good candidate for multitasking. Andy Hertzfeld wrote Switcher for Classic Mac OS in 1984-85 to enable the user to switch between apps, and that led to MultiFinder in 1987, bringing cooperative multitasking. At the same time, Apple developed a variant of the Macintosh II with a Paged Memory Management Unit (PMMU) to support virtual memory, and in February 1988 it released the first Unix for Macintosh, A/UX, complete with preemptive multitasking. But Mac OS didn't gain true preemptive multitasking until the release of Mac OS X 12 years later. Cooperative multitasking worked well much of the time, but let badly behaved tasks hog the processor and block other tasks from getting their fair share. It was greatly aided by the main event loop at the heart of Mac apps that waited for control input to direct the app to perform work for the user. If an app charged off to spend many seconds tackling a demanding task, without polling the main event loop, that app could lock the user out for what seemed like an age. As IBM was developing its first Personal Computer, and Apple its first Macintosh, on the other side of the Atlantic in England two engineers, David May and Robert Milne, were developing an alternative approach to computing based on a parallel architecture of multiple high-speed processors connected by high-speed serial links, the Transputer. In 1985, their company Inmos released its first usable chip, the T414 Transputer, a 32-bit parallel processor with 2 KB of on-board memory and running at up to 20 MHz, with four serial links at up to 20 Mb/s. Two years later, in 1987, their second-generation chip, the T800, started shipping in small quantities and at high cost because of initial low yields. Its major advance was a 64-bit floating-point unit implementing the new IEEE 7540-1985 standard for floating-point arithmetic. This also had a whole 4 KB of on-chip memory and could run at up to 25 MHz when you were lucky. For comparison, the single 68020 CPU in a Macintosh II of the same year ran at 16 MHz, and had a separate 68881 floating-point unit. The Transputer wasn't envisaged to be the sole processor type in a computer. Demonstration designs almost invariably interfaced them with an IBM PC, as an array of Transputers used as co-processors for computationally intensive tasks, of which the most popular was computing Mandelbrot sets for on-screen display. Matching the number of squares within an image to the number of T800 chips in an array enabled companies such as Meiko Scientific, Parsytec and Microway to enthuse potential customers. By the autumn of 1987, several major PC manufacturers were developing accelerator boards such as Commodore's for its Amiga A2000, which relied on a Motorola 68000 as the main CPU. Perhaps the biggest challenge for those intending to use Transputers was software support for these large farms of cores. David May and others at Inmos enlisted the help of Tony Hoare to develop a parallel programming language, occam (perversely named with a small o), forming the software heart of the Transputer Development System, TDS, inevitably nicknamed tedious. occam was different in that it required sequential instructions to be written explicitly, such as SEQ x := x + 10.5 y := x - 5.5 while those to be run in parallel were marked as PAR p() q() Because of occam's structure, its bundled code editor was probably the first to fold sections of code away to reveal its structure, in its spectacular folding editor. This took a similar approach to that now available in BBEdit when working with XML and similar documents. By 1990, the Transputer was falling behind mainstream processor development, but it provided extensive experience in running code in parallel across multiple processor cores that was later used in the design of computer clusters. Between 2003-2006, Apple shipped thousands of Xserves with PowerPC G4 and G5 processors to build supercomputer clusters. Apple's first Macs with dual processors came in 2000, with PowerPC 7400 (G4) chips in Power Mac G4 desktop systems. In 2005, the Power Mac G5 was the first Mac to use dual-core PowerPC G5 processors, then the iMac 17-inch of 2006 used Apple's first Intel Core Duo processor with two cores. Since Apple has been switching architecture to its own chips from 2020, every Mac comes with multiple CPU cores of two different types, and runs macOS with its support for both preemptive and cooperative multitasking. What we have now, though, is the inverse of where this all started, multiple processor cores on which threads are really executing simultaneously, and have no need to pretend. For those, Swift 6 supports structured concurrency to address many of the problems encountered in parallel processing and cooperative multitasking. For the sake of example, assume an app needs to access two types of remote information stored in Property Lists on a server somewhere. One type it parses into a list of Updates, the other into a list of Versions. Because those have to be fetched over the network, building those lists could take some seconds, and on occasion might fail altogether. One way to tackle this is with a function loadUpdatesAndVersions() shown below. swiftstrucconcurrency1 This first calls a child task asynchronously, which tries to load the Updates (shown in blue). As soon as that task is running, the main function calls a similar child task to load the Versions (blue). The main task is then suspended as it awaits the lists of Updates and Versions from those child tasks, in the last line of code, which returns the two lists to the rest of the app. This may appear trivially simple, but building it into the Swift language enables replacement of a lot of contorted code that had been used previously. In 40 years we have progressed from multitasking of single processors, and massively parallel computing, to cooperative multitasking of multiple cores. It has already been quite a journey. Share this: * Twitter * Facebook * Reddit * Pinterest * Email * Print * Like Loading... Related Posted in Macs, Technology and tagged INMOS, multitasking, Occam, parallel processing, Swift, Transputer, Xserve. Bookmark the permalink. 5Comments Add yours 1. 1 [048f4e3d4375] Duncan on June 10, 2024 at 2:35 pm Reply Excellent explanation. I wish my old textbooks were this readable. I remember back in the Mac 'Classic' days when the holy grails for Mac OS and Windows were true virtual memory and preemptive multitasking. Unix had it, and OS/2 was close, but we had to suffer total system stalls and crashes while the hardware struggled to provide the power necessary for such 'exotic' features (at least while also supporting a decent GUI). I'm glad those days are far in the rear-view mirror. LikeLiked by 1 person + 2 [87cc8acbb0b9] hoakley on June 10, 2024 at 3:31 pm Reply Thank you, Duncan. Howard. LikeLike 2. 3 [432a88bcfdf9] Dan Hellenberg on June 10, 2024 at 5:05 pm Reply I finished reading just as it turned 12:00 here. WWDC is about to start. LikeLiked by 1 person 3. 4 [3b34cb8abc6a] Alex on June 11, 2024 at 4:25 am Reply I know you focus a lot on the Virtualization aspects of macOS, hope this makes your day as much as it did for me. https://developer.apple.com/documentation/virtualization/ vzgenericplatformconfiguration/ 4360553-isnestedvirtualizationsupported?changes=latest_minor LikeLiked by 1 person + 5 [87cc8acbb0b9] hoakley on June 11, 2024 at 6:30 am Reply Thank you. But there's more too - support for Apple ID and iCloud access too! Howard. LikeLike Leave a comment Cancel reply [ ] [ ] [ ] [ ] [ ] [ ] [ ] D[ ] This site uses Akismet to reduce spam. Learn how your comment data is processed. Quick Links * Free Software Menu * System Updates * M-series Macs * Mac Troubleshooting Summary * Mac problem-solving * Painting topics * Painting * Long Reads Search Search for: [ ] [Search] Monthly archives * June 2024 (26) * May 2024 (79) * April 2024 (75) * March 2024 (81) * February 2024 (72) * January 2024 (78) * December 2023 (79) * November 2023 (74) * October 2023 (77) * September 2023 (77) * August 2023 (72) * July 2023 (79) * June 2023 (73) * May 2023 (79) * April 2023 (73) * March 2023 (76) * February 2023 (68) * January 2023 (74) * December 2022 (74) * November 2022 (72) * October 2022 (76) * September 2022 (72) * August 2022 (75) * July 2022 (76) * June 2022 (73) * May 2022 (76) * April 2022 (71) * March 2022 (77) * February 2022 (68) * January 2022 (77) * December 2021 (75) * November 2021 (72) * October 2021 (75) * September 2021 (76) * August 2021 (75) * July 2021 (75) * June 2021 (71) * May 2021 (80) * April 2021 (79) * March 2021 (77) * February 2021 (75) * January 2021 (75) * December 2020 (77) * November 2020 (84) * October 2020 (81) * September 2020 (79) * August 2020 (103) * July 2020 (81) * June 2020 (78) * May 2020 (78) * April 2020 (81) * March 2020 (86) * February 2020 (77) * January 2020 (86) * December 2019 (82) * November 2019 (74) * October 2019 (89) * September 2019 (80) * August 2019 (91) * July 2019 (95) * June 2019 (88) * May 2019 (91) * April 2019 (79) * March 2019 (78) * February 2019 (71) * January 2019 (69) * December 2018 (79) * November 2018 (71) * October 2018 (78) * September 2018 (76) * August 2018 (78) * July 2018 (76) * June 2018 (77) * May 2018 (71) * April 2018 (67) * March 2018 (73) * February 2018 (67) * January 2018 (83) * December 2017 (94) * November 2017 (73) * October 2017 (86) * September 2017 (92) * August 2017 (69) * July 2017 (81) * June 2017 (76) * May 2017 (90) * April 2017 (76) * March 2017 (79) * February 2017 (65) * January 2017 (76) * December 2016 (75) * November 2016 (68) * October 2016 (76) * September 2016 (78) * August 2016 (70) * July 2016 (74) * June 2016 (66) * May 2016 (71) * April 2016 (67) * March 2016 (71) * February 2016 (68) * January 2016 (90) * December 2015 (96) * November 2015 (103) * October 2015 (119) * September 2015 (115) * August 2015 (117) * July 2015 (117) * June 2015 (105) * May 2015 (111) * April 2015 (119) * March 2015 (69) * February 2015 (54) * January 2015 (39) Tags APFS Apple Apple silicon backup Big Sur Blake bug Catalina Consolation Console Corinth diagnosis Disk Utility Dore El Capitan extended attributes Finder firmware Gatekeeper Gerome High Sierra history of painting iCloud Impressionism landscape LockRattler log M1 Mac Mac history macOS macOS 10.12 macOS 10.13 macOS 10.14 macOS 10.15 macOS 11 macOS 12 macOS 13 macOS 14 malware Metamorphoses Mojave Monet Monterey Moreau MRT myth narrative OS X Ovid painting performance Pissarro Poussin privacy Renoir riddle Rubens Sargent scripting security Sierra SilentKnight Sonoma SSD Swift Time Machine Tintoretto Turner update upgrade Ventura xattr Xcode XProtect Statistics * 17,300,406 hits Blog at WordPress.com. Footer navigation * Free Software Menu * About & Contact * Macs * Painting * Downloads * Mac problem-solving * Extended attributes (xattrs) * Painting topics * SilentKnight, Skint, silnite, LockRattler, SystHist & Scrub * DelightEd & Podofyllin * xattred, Spotcord, Metamer & xattr tools * 32-bitCheck & ArchiChect * XProCheck, T2M2, Ulbow, Consolation and log utilities * Cirrus & Bailiff * Taccy, Signet, Precize, Alifix, UTIutility, Sparsity, alisma * Versatility & Revisionist * Text Utilities: Nalaprop, Dystextia and others * PDF * Keychains & Permissions * Updates * Spundle, Cormorant, Stibium, Dintch, Fintch and cintch * Long Reads * Mac Troubleshooting Summary * M-series Macs * Mints: a multifunction utility * VisualLookUpTest * Virtualisation on Apple silicon * System Updates * Saturday Mac Riddles * Last Week on My Mac * sysctl information Secondary navigation * Search Post navigation Heroines 4: Phaedra's shame and a chariot accident Solutions to Saturday Mac riddles 259 Search for: [ ] [Search] Begin typing your search above and press return to search. Press Esc to cancel. * Comment * Reblog * Subscribe Subscribed + [croppe] The Eclectic Light Company Join 4,952 other subscribers [ ] Sign me up + Already have a WordPress.com account? Log in now. * + [croppe] The Eclectic Light Company + Customize + Subscribe Subscribed + Sign up + Log in + Copy shortlink + Report this content + View post in Reader + Manage subscriptions + Collapse this bar Loading Comments... Write a Comment... [ ] Email (Required) [ ] Name (Required) [ ] Website [ ] [Post Comment] %d [b]