Language Integrated STM in C# Using the Roslyn Compiler
Total Page:16
File Type:pdf, Size:1020Kb
class Account { public atomic long Balance { get; set; } { static void Main() { var salary = new Account(1000); new Account(-50000); var studentLoan = TransferMoney(1000, salary, studentLoan); Laurberg Kasper Breinholt Pørtner Karlsen Andreas UgleholdtHansen Tobias toLocking - AnAlternative Using theRoslynCompiler Language IntegratedSTMinC# } public Account(long balance) { Balance = balance; } public static void TransferMoney(long amount, Account from, Account to) { atomic { from.Subtract(amount); to.Add(amount); } } public void Add(long amount) { atomic { Balance += amount; } } public void Subtract(long amount) { atomic { if (Balance - amount >= 0) { Balance -= amount; } else { throw new Exception(”Insuffcient funds!”); } } } } Department of Computer Science Selma Lagerløfs Vej 300 DK-9220 Aalborg Ø http://www.cs.aau.dk Title: Abstract: Language Integrated STM in C# Us- ing the Roslyn Compiler - An Alter- This master thesis investigates native to Locking. whether language integrated STM is a valid alternative to locking in C# Project Period: in terms of usability, and provides ad- Spring Semester 2015 ditional benefits compared to library- based STM. To do so, an extension Project Group: of C# called AC# was implemented. dpt109f15 AC# provides integrated support for STM, including conditional synchro- Participants: nization using the retry and orelse Tobias Ugleholdt Hansen constructs, and nesting of transac- Andreas Pørtner Karlsen tions. AC# was implemented by ex- Kasper Breinholt Laurberg tending the open source Roslyn C# compiler. To power AC# a library- based STM system, based on the Supervisor: TLII algorithm, was implemented. Lone Leth Thomsen The extended compiler transforms AC# source code to regular C# code Page Numbers: 120 + 4 appendices which utilizes the STM library. For each concurrency approach: AC#, Date of Completion: library-based STM and locking in June 8, 2015 C#, four di↵erent concurrency prob- lems, representing di↵erent aspects of concurrency, were implemented. These implementations were ana- lyzed according to a set of usability characteristics, facilitating a conclu- sion upon the usability of language integrated STM. Our evaluation con- cludes that AC# is a valid alterna- tive to locking, and provides better usability than library-based STM. Preface This report documents the master thesis done by group dpt109f15 at the Department of Computer Science at Aalborg University. The thesis was written as part of the Computer Science (IT) study program in the spring of 2015 at the 10th semester. The first time an acronym is used it will appear in the format: Software Transactional Memory (STM). Inline quotations and names will appear in italics. The work presented in this report is based on work or results described in books, articles, video lectures, and research papers from outside sources. The full list of acronyms along with the bibliography, appendix, and summary can be found at the end of the report. We would like to give a special thanks to our supervisor Lone Leth Thomsen, from the Department of Computer Science, for her excellent guidance and immaculate attention to details. She supplied invaluable help throughout the project with professionalism and black humour. Her feedback has been indispensable and has significantly raised the quality of the final result. Her constructive criticism helped us to narrow down the subject and kept us motivated and enthusiastic about the project. The report is structured with dependencies between the chapters, and the following can be used as a reading guide: Chapter 1 “Introduction” presents the motivation for choosing the topic, • the related work, the scope, and the hypothesis. Lastly the method of evaluation is presented. Chapter 2 “Background Knowledge” establishes the term “locking”, • and the key concepts of Software Transactional Memory (STM). This knowledge is required in order to understand the remaining work. If the reader is familiar with these areas, this chapter can be skipped. Chapter 3 “Roslyn” outlines the structure of the Roslyn compiler, which • enables the integration of STM into C#. Chapter 4 “Requirements for AC#” analyze the requirements to the • STM system which executes transactions in AC#. Chapter 5 “Design and Integration” describes the decisions related to • designing and integrating AC# based on the requirements. i PREFACE ii Chapter 6 “STM Implementation” describes the implementation of the • STM system that powers AC#, and is based on the requirements and design choices. Chapter 7 “Roslyn Extension” describes how Roslyn is extended to • encompass language integrated STM, thus being a compiler for AC#. Chapter 8 “Evaluation of Characteristics” evaluates AC#, its associated • STM library and locking in C# according to the evaluation method described in Section 1.5. Based on this evaluation, a conclusion on the hypothesis is made in • Chapter 9 “Conclusion”. To reflect on the decisions made throughout the report, Chapter 10 • “Reflection” discusses the choices made and their consequences. Continuation of the work in the future and its potential is discussed in • Chapter 11 “Future Work”. PREFACE iii Tobias Ugleholdt Hansen Andreas Pørtner Karlsen [email protected] [email protected] Kasper Breinholt Laurberg [email protected] Contents Preface i 1 Introduction 1 1.1 Motivation . 1 1.2 RelatedWork............................ 2 1.3 Scope ................................ 4 1.4 ProblemStatement......................... 5 1.5 Evaluation Method . 6 2 Background Knowledge 8 2.1 LockinginC# ........................... 8 2.2 STMKeyConcepts......................... 10 3 Roslyn 14 3.1 Introduction............................. 14 3.2 Roslyn Architecture . 15 3.3 Compiler Phases . 17 3.4 SyntaxTrees ............................ 19 4 Requirements for AC# 26 4.1 Tracking Granularity . 26 4.2 Transactions & Variables . 28 4.3 Strong or Weak Atomicity . 29 4.4 Side-e↵ects ............................. 32 4.5 Conditional Synchronization . 33 4.6 Nesting ............................... 34 4.7 Opacity ............................... 34 4.8 Summary of Requirements . 36 5 Design and Integration 37 5.1 Transactional Blocks & Variables . 37 5.2 Transactional Parameters . 40 5.3 Example of AC# . 43 5.4 Conditional Synchronization . 45 5.5 Nesting ............................... 47 5.6 Summary of Design . 48 6 STM Implementation 50 6.1 Implementation Criteria . 50 6.2 Selection of Algorithm . 52 6.3 Library Interface . 56 iv CONTENTS v 6.4 Internal Details . 60 6.5 Testing ............................... 64 7 Roslyn Extension 67 7.1 Extension Strategy . 67 7.2 Lexing & Parsing Phases . 69 7.3 Syntax Tree Transformations . 73 7.4 Testing ............................... 84 7.5 Design and Integration Revisited . 86 8 Evaluation of Characteristics 90 8.1 ImplicitorExplicitConcurrency . 90 8.2 FaultRestrictiveorExpressive . 91 8.3 PessimisticorOptimistic. 91 8.4 Readability & Writability . 92 9 Conclusion 108 9.1 Problem Statement Questions Revisited . 109 9.2 Hypothesis Revisited . 109 10 Reflection 112 10.1 Preliminary Investigation . 112 10.2 Design . 113 10.3 STM Implementation . 115 10.4 Roslyn Extension . 115 10.5 C# 6.0 . 116 11 Future Work 118 11.1 Performance Test . 118 11.2 Integration into CLR . 119 11.3 Irreversible Actions . 119 Appendix 121 A Roslyn Compiler Call Chain 122 B Concurrency Problems 124 B.1 Dining Philosophers . 124 B.2 The Santa Claus Problem . 125 B.3 ConcurrentQueue ......................... 125 B.4 Concurrent HashMap . 125 C Evaluation Implementations 127 C.1 Lock-Based . 127 C.2 STM Library . 142 C.3 AC#.................................156 CONTENTS vi D Summary 172 List of Acronyms 173 Bibliography 174 1 Introduction This chapter describes the motivation behind this project in Section 1.1. Related work is presented in Section 1.2 and the scope of the project is addressed in Section 1.3. The hypothesis and problem statement questions are presented in Section 1.4. Finally, the project evaluation method is stated in Section 1.5. 1.1 Motivation Today the increase in CPU speed comes in the form of additional cores, as opposed to faster clock speed[1]. In order to utilize this increase in speed, many of the popular sequential programming languages, such as C, C++, Java, and C#, require changes or new additions in order to adapt[2, p. 56]. Our recent study[3], analyzed the runtime performance and characteristics of three di↵erent approaches to concurrent programming: Threads & Locks (TL), STM, and the Actor model. The study concluded that STM eliminates many of the risks related to programming in the context of shared-memory concurrency, notably deadlocks, leading to simpler reasoning and improved usability. Additionally, STM fits with the thread model used by sequential languages, and can thereby be applied to existing implementations, without requiring major rewrites. The runtime performance of STM is at a competitive level comparable to that of fine-grained locking[3]. The analysis uncovered one major caveat of STM,itis not orthogonal with side-e↵ects, such as Input/Output (IO) and exceptions[3]. STM has, as of the time of writing, seen only limited official language integra- tion, despite of the advantages STM could provide to sequential languages.To our knowledge STM has only been introduced as an official built-in language feature in Clojure1 and Haskell2. STM has been introduced as a library based solution in sequential languages. However to supply features such as static anal- ysis and syntax support require language integration.