Speedy TLS is a library for accessing large amounts of thread-local storage extremely quickly (single instruction). It leverages x86 memory segmentation by dedicating a different segment of memory for each thread's thread-local storage area.
Each thread must call speedy_tls_init passing the number of bytes desired for thread-local storage. The size is only limited by how much memory can be mapped into the process space, and it will allocate at least one page for thread-local data. Alternatively if you allocate the thread-local memory yourself, call speedy_tls_init_foraddr passing the address and number of bytes (must be a multiple of the page size).
Properly initialized, you can then use the macros in speedy-tls.h to get, set, and manipulate values in TLS in a single instruction:
speedy_tls_get_* speedy_tls_put_* speedy_tls_add_* speedy_tls_inc_* speedy_tls_dec_* speedy_tls_get_and_add_*
Since add, inc, dec, and get_and_add both get and change the TLS value in a single instruction, they are not safe for use in SMP. Variants of these macros are provided (speedy_tls_atomic_*) that use the lock instruction prefix so that they are atomic on SMP. Additionally, the library also provides an atomic get_and_add for use on local variables.
The library relies on exclusive use of either the FS register (32-bit) or GS register (64-bit). Make sure that GCC does not interfere (use the -mno-tls-direct-seg-refs GCC option).
An example program, test-driver.cpp, is included in the download demonstrating how to use the library in a program with pthreads.
The library is made available under a modified BSD-like license:
This is the license for speedy-tls, a set of assembly routines for extremely fast access to thread-local storage. Send comments and questions to KevinJohnHoffman@gmail.com. Version 1.0. The latest version is at: http://www.kevinjhoffman.com/ speedy-tls COPYRIGHT NOTICE, LICENSE AND DISCLAIMER. Copyright (C) 2008 Kevin Hoffman Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the intended use of this software is first emailed to KevinJohnHoffman@gmail.com, and provided that the name of the above copyright holders, or their entities, not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. The above copyright holders disclaim all warranties with regard to this software, including all implied warranties of merchantability and fitness. In no event shall the above copyright holders be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or performance of this software.
Download version 1.0 here. Tested platforms:
- Linux x86 (2.6 kernel, GCC 3.4.6)
- Linux x64 (2.6 kernel, GCC 3.4.6)
- OSX x86 (10.4, GCC 4.0.1)
Windows may be supported in the future, but will require some adaptation, as the FS or GS segment is already in use by Windows (more info).
2008-04-28: Initial version released.