Localizing Globals and Statics to Make C Programs Thread-Safe

Localizing Globals and Statics to Make C Programs Thread-Safe

Localizing Globals and Statics to Make C Programs Thread-Safe Adam R. Smith∗ Prasad A. Kulkarni University of Kansas Department of Electrical Engineering and Computer Science Lawrence, Kansas, USA [email protected]; [email protected] ABSTRACT General Terms Challenges emerging from the exponential growth in CPU power Performance, Languages dissipation and chip hot spots with increasing clock frequencies have forced manufacturers to employ multicore processors as the Keywords ubiquitous platform in all computing domains. Embedded mo- bile devices are increasingly adopting multicore processors to im- Thread-safe, Globals prove program performance and responsiveness at low power lev- els. However, harnessing these performance and power benefits 1. INTRODUCTION requires the construction of parallel programs, a task significantly A microprocessor operating at a higher clock frequency con- more complex than writing sequential code. Parallel code develop- sumes more power. The power wall, which is a limit to the amount ment is also made more difficult by differences in the use of sev- of power that a microprocessor chip can dissipate without failing, eral programming language constructs. Therefore, it is critical to proved to be an impregnable barrier to the further scaling of mi- provide programmers with tools to ease the formidable task of par- croprocessor clock frequencies, effectively ending the era of high- allelizing existing sequential code or developing new parallel code performance uniprocessor chips in the server and desktop PC do- for multicore processors. mains. Chip manufactures now exploit the continuous growth in In this work we focus on the use of static and global variables chip transistor count enabled by Moore’s law by placing multi- that are commonly employed in C/C++ programs, the languages ple simpler reduced clock-rate cores on a single processor die [2]. of choice for developing embedded systems applications. Unpro- Advanced embedded applications are rapidly becoming more com- tected use of such variables produces functions that are not thread- plex, and high-end embedded processors are facing the same power safe, thereby preventing the program from being parallelized. Man- constraints that were earlier seen in PC processors. Consequently, ually eliminating global and static variables from existing sequen- several embedded processor manufacturers and application devel- tial code is tedious, time-consuming and highly error-prone. While opers are advocating the use of symmetric multiprocessing (SMP) no good solution to this problem currently exists, researchers have systems to realize the scalable performance and power require- proposed partial mitigation techniques that require massive changes ments of existing and future embedded applications [11, 34]. to linkers and runtime systems. In this work we study the character- Unfortunately, scalable performance on multicore processors can istics and effects of static and global variables in traditional bench- only be achieved by parallel applications distributing their tasks mark programs, and propose, construct, and explore a compiler- across multiple threads. Consequently, the migration to multicore based, semi-automatic and interactive technique to handle such vari- processors requires software developers to address two formidable ables and generate thread-safe code for parallel programs. tasks: (a) generate new parallel applications, and (b) convert exist- ing sequential programs to use multiple threads. Unfortunately, it has been observed that writing scalable and error-free parallel code Categories and Subject Descriptors is often substantially more difficult, time-consuming, and costly D.3.4 [Programming Languages]: Processors—Compilers; D.2.3 as compared to writing an equivalent serial program [20]. Fur- [Software Engineering]: Coding Tools and Techniques thermore, programming language constructs that are routinely em- ployed during sequential coding may not be available or may re- quire special handling during the generation of parallel code. There- ∗ Currently, a graduate student in the department of Computer Sci- fore, software tools that can automatically manage such common ence at the University of Illinois, Urbana, IL programming obstacles can greatly ease the task of producing multi- threaded parallel applications for software developers. In this work we focus on the use of static and global variables in imperative language programs like C/C++. Global variables pro- Permission to make digital or hard copies of all or part of this work for vide a convenient (albeit, occasionally error-prone) mechanism to personal or classroom use is granted without fee provided that copies are share information between multiple functions in the same program. not made or distributed for profit or commercial advantage and that copies Static variables are also typically used to hold global state, but bear this notice and the full citation on the first page. To copy otherwise, to the compiler restricts their visibility to only the function or file republish, to post on servers or to redistribute to lists, requires prior specific where they are defined. Static and global variables are an indis- permission and/or a fee. CASES’11, October 9–14, 2011, Taipei, Taiwan. pensable programming construct employed by C/C++ developers Copyright 2011 ACM X-XXXXX-XX-X/XX/XX ...$10.00. in programs of all sizes. Indeed, the lack of these constructs will make it substantially more cumbersome and tedious to program in with the compiler transformation. such languages. By virtue of residing in the data region of the process address 2. RELATED WORK space, only a single copy of each global and static variable is shared Increasing availability of multicore/multiprocessor machines and among all threads in a multi-threaded process. Consequently, de- the growing demand for multi-threaded programs has resulted in pending on their purpose and use, each static or global variable several approaches to ease the task of writing parallel applications. in a multi-threaded program needs to be assigned separate thread- In this section we describe previous efforts for providing thread- local storage, or its use needs to be guarded (typically by using local storage, specifications for parallel languages, and other li- semaphores or mutexes) to enforce synchronized and atomic access brary, tool and runtime level support for parallel programming. by each thread. However, manually eliminating or synchronizing Several high-level languages provide standard or non-standard the use of statics and globals is extremely hard, time-consuming support for thread-local variants of traditional global and static vari- and tedious. At the same time, several programming idioms can ables. For example, Delphi provides the variable specifier thread- be naturally and more efficiently expressed using static and global var [27], GNU C/C++ provides the __thread specifier [33], the variables. Therefore, researchers are exploring various mechanisms unofficial new standard for C++ (C++0x) provides the thread_local to appropriately manage static and global variables in multi-threaded specifier [14], and C# allows variables to be marked with the Thread- programs without substantially restricting their use or significantly Static attribute [13] to specify thread-local storage for global data. changing their semantics. Most of these mechanisms require ma- In contrast, our interactive approach does not require any changes jor changes to existing threading libraries and the runtime system to the language specification. Additionally, more than the language to provide a new and distinct global storage area for each thread design, in this paper we focus on the implementation aspects of in the process address space [33, 14, 13]. Additionally, in most providing thread-local storage for static and global variables. cases the user still needs to use special keywords to help the com- Library and compiler implementation support is also available to piler/runtime place static and global variables in their appropriate create thread-specific data. Thus, the POSIX thread interface [10], storage locations. Windows libraries [4], and the boost C++ libraries [3] provide API In this paper we explore a new compiler-based semi-automatic functionality to create and manage thread-local storage for statics and interactive transformation to sanitize the use of static and global and globals. Apart from learning a new API, such implementation variables in multi-threaded C applications. Our approach is semi- approaches are often cumbersome to use [14] and require signifi- automatic since, similar to existing techniques, the user is required cant support from the linker, system libraries and the runtime sys- to indicate the category of each static or global variable as thread- tem [33]. Our tool employs a complementary and novel compiler- local, shared, or other. These categories are explained in more based approach that not only provides thread-local storage without detail in section 4. We develop an interactive framework to sim- any modifications to the library, linker, or runtime support, but can plify this categorization process. To handle variables in the thread- also automatically synchronize access for shared global variables. local category, our compiler-based transformation constructs the Researchers have also developed new programming languages program call-graph, finds the definitions and uses of each vari- and extensions to existing languages to enable easier specification

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us