Abstract:
In accordance with some embodiments, software transactional memory may be used for both managed and unmanaged environments. If a cache line is resident in a cache and this is not the first time that the cache line has been read since the last write, then the data may be read directly from the cache line, improving performance. Otherwise, a normal read may be utilized to read the information. Similarly, write performance can be accelerated in some instances to improve performance.

Description:
BACKGROUND 
       [0001]    This relates to computer systems with transactional memory. 
         [0002]    Transactional memory (TM) eliminates many of the problems associated with locks and enables developers to compose multi-threaded applications safely. TM constructs (in the form of an atomic block, or transaction) have been added to new or existing languages. TM is a concurrency control mechanism for controlling access to shared memory in concurrent computing. Software transactional memory (STM) is a transactional memory implemented in software. 
     
    
     
       BRIEF DESCRIPTION OF THE DRAWINGS 
         [0003]      FIG. 1  is a flow chart for a hardware assisted strong atomicity read according to one embodiment; 
           [0004]      FIG. 2  is a flow chart for a hardware assisted strong atomicity write according to one embodiment; 
           [0005]      FIG. 3  is a flow chart for a hardware assisted transactional write in a transactional memory system with write buffering according to one embodiment; and 
           [0006]      FIG. 4  is an architectural depiction of one embodiment. 
       
    
    
     DETAILED DESCRIPTION 
       [0007]    With transactional memories, the programmer declares an atomic code block. The underlying system guarantees atomicity and isolation during execution, giving the illusion that the block executes in an atomic step with respect to other concurrently executing operations. In software transactional memory systems, a pointer sized transaction record tracks the state of each object accessed inside a transaction. 
         [0008]    There are two types of transactional memory implementations. Transactional memory that provides weak atomicity isolates transactions only from other transactions. Software implementation of weak atomicity may insert barriers only into the transactional code. Transactional memory that provides strong atomicity isolates transactions from both transactional and non-transactional code. Software implementation of strong atomicity inserts barriers into both transactional and non-transactional code. 
         [0009]    In software transactional memory, for each memory location there is an associated transaction record. Multiple memory locations can map to the same transaction record. For example, software transactional memory for unmanaged languages, like C++, may use cache-line based conflict resolution, where a cache line may be mapped into one transaction record. Software transactional memory for managed languages, like Java, may use object based conflict resolution, where all fields of an object may be mapped into one transaction record. 
         [0010]    The transaction record represents the state of data at a memory location. A transaction record can be in the shared state, which allows read only access by any number of transactions, or the exclusive state, which allows read-write access by the single transaction that owns the record. In the shared state, the record contains a version number used for optimistic read concurrency. In the exclusive state, the record contains a pointer to the owning transaction descriptor. The exclusive anonymous state indicates that some thread owns the object exclusively for non-transactional read-write access, but the record does not indicate which thread owns the record. 
         [0011]    Enforcing memory ordering and isolation between transactional and non-transactional threads involves read and write isolation barriers in code that executes outside of atomic blocks. 
         [0012]    A system that provides strong atomicity ensures isolation, even in the presence of threads that access shared data outside transactions. A strong atomic system also orders transactions with conflicting non-transactional memory operations in a consistent manner. 
         [0013]    In a software transactional memory with cache line based conflict resolution, the software transactional memory maintains a table of transaction records. Given an address, the software transactional memory may use a hashing function to find an index of the corresponding transaction record in the table. 
         [0014]    In a software transactional memory with object based conflict resolution, the transaction records are allocated in the object headers. In such a system, the software transactional memory computes the address of the transaction record by adding a fixed offset to the object address. 
         [0015]    For writes, the software transactional memory may use a two-phase locking and acquire ownership of the transaction record by atomically setting it to the transaction descriptor. In an in-place update scheme, the software transactional memory logs the old value before updating the memory location. In a write buffering scheme, the software transactional memory writes the new value in a commit log and copies values from the commit log into the memory locations when transaction commits. For reads, the software transactional memory may use the transaction record as a version number. Before reading a datum, the software transactional memory logs the version number of that datum and validates at intervals that the version number has remained the same. The software transactional memory may use even numbers for transaction descriptors and odd numbers for version numbers, or some other mechanism that allows it to detect if a transaction record represents transaction descriptor or version number. 
         [0016]    The software transactional memory requires that all accesses to shared data be mediated through read and write barriers which enforce the software transactional memory access protocol. In strongly atomic software transactional memory system the code outside the transaction also is instrumented to preserve true isolation and ordering of the transactions. 
         [0017]    Thus, in a pure software path code sequence for strong atomicity reads, called a normal read herein, initially a transaction record is read and then a check determines whether any thread owns the memory location to be read. In other words, a determination is made as to whether the record is in the shared state, which can be detected by checking if it represents a version number. If the check fails, then the data cannot be read because another thread has an exclusive access to the data. A contention algorithm is called to resolve the situation of contention for the data. If the check succeeds, then the read can be done. Next, a comparison ensues that the version numbers did not change. If the comparison fails, then a contention algorithm is called and the result of the previous read is not used. 
         [0018]    In a software code sequence for strong atomicity writes, called a normal write herein, the transaction record is locked, a write is implemented, and then the lock is released. To perform a non-transaction write, the transaction record must be set to an anonymous exclusive state. Initially, there is an attempt to atomically change the value of a transaction record to indicate this state. This may be referred to as establishing a lock. 
         [0019]    If this cannot be done (e.g., because the transaction is not in a shared state), then there is a contention and a contention algorithm is called, because another entity is also writing to the same memory location. If the lock succeeds, then the data may be written. The last thing that is done is to release the lock and set the transaction record to the shared state with an incremented version number. 
         [0020]    With a hardware assisted software transactional memory, both the read and the write barriers can be made faster. The hardware allows the transactional memory to detect if the memory location was not changed since the last read or write. In this case the data can be read without performing additional checks or written without acquiring the lock on the transaction record. 
         [0021]    The hardware provides two special bits per transaction record associated with the data residing in the processor cache—a read monitor bit and a write monitor bit. It uses these bits to track if the data is written by another processor. In this case the hardware resets the read monitor bit and the write monitor bit. A transactional memory system with cache line based conflict detection may use one read monitor bit and one write monitor bit per cache line (because there is one transaction record per cache line). Transactional memory system with object based conflict detection may use multiple read and write monitor bits per cache line. The number of bits depends on how many objects can be located in the same cache line and should be sufficient to have one read monitor bit and one write monitor bit per object. 
         [0022]    In the case of the read, as shown in  FIG. 1 , a load and check instruction at block  12  reads the data. The load and check instruction loads the data from memory and sets a flag if the data is in the cache and a corresponding read monitor bit is set. In one embodiment, a carry flag is set. If the data is in the cache (diamond  14 ) and the read monitor bit is set (diamond  15 ), then the faster read is done. Otherwise, the normal read, described above, is implemented, as indicated at block  16 . Then, the read monitor flag is set in block  17 . 
         [0023]    As an example, in some embodiments, a cache line may be mapped to the transactional record. Initially, the read monitor bit may be zero in one embodiment. In a cache line example, the read monitor bit may be associated with a cache line. The read monitor bit may be set to one after the first time that a normal read is completed. The load and check operation then determines if the data is both in the cache and the read monitor bit is set to one, indicating that one normal read has already been completed and no other thread has changed the data. 
         [0024]    The first time the cache line is accessed, the read monitor bit is zero. The load and check operation fails and a normal read is done at block  16 . After the completion of the normal read, the read monitor bit is set to one, as indicated at block  17 . 
         [0025]    The second time the cache line is read, if no thread has written the data in the interim, the read monitor bit is set and the data is in the cache so the load and check succeeds and the faster read operation is done, without having to complete all the steps of a normal read. 
         [0026]    The benefit of the monitor bit is that it enables one to avoid using the normal read when unnecessary, thereby improving performance. In one embodiment, the fast path for the normal read requires six instructions while the fast path for the hardware assisted read requires only two instructions—a load and check followed by a conditional jump. 
         [0027]    For the store, the hardware assisted software transactional memory store and check code  18  checks, at oval  18 , that the data is in the cache and the corresponding write monitor bit is set. If these conditions are met, the store is done and a flag is set to one. In one embodiment, a carry flag may be set. If the data is not in the cache or the monitor bit is not set, the store is not performed and the flag is reset to zero. The checks and the store are performed atomically, that is, no write from another processor can occur between the checks and the write. If the data is in the cache, as determined at diamond  20 , and the write monitor bit is set (diamond  25 ) then the faster write is done. If not, then a normal software assisted store (described above) can be done, as indicated in block  22 . A write monitor bit is then set to one at block  23 . 
         [0028]    In one embodiment, the fast path for the normal write uses four instructions while the fast path for the hardware assisted read uses only two instructions—a store and check followed by a conditional jump. Thus, the cost of a strong atomicity write can come down from four to two instructions without executing a lock instruction in the fast path. 
         [0029]    An implementation of software transactional memory can be classified into two kinds depending on how it performs transactional writes. An in-place update software transactional memory updates a value in memory immediately. If a thread writes X to 1, then X immediately becomes 1 and all threads that can read X see that change. In contrast, in a write buffering software transactional memory, the write goes to a buffer first and is only copied to a memory location when the transaction completes successfully. 
         [0030]    The read and write routines, shown in  FIGS. 1 and 2 , work for an in place update software transactional memory with no modifications required on transactional code. 
         [0031]    The hardware assisted strongly atomic software transactional memory also works for a write buffering scheme. As shown in  FIG. 3 , a transactional write takes ownership of the transaction record corresponding to an address of the written memory location, as indicated in block  24 . The new value is stored into a commit log, as indicated in block  26 . The old value is read and stored back into the memory location, as indicated in blocks  28  and  30 . By storing the old value back into the memory location, the transaction is forced to take exclusive ownership of the cache line. This results in resetting of the corresponding read monitor bit and write monitor bit allowing cache reuse to be leveraged in non-transactional threads. 
         [0032]    This algorithm works with encounter time locking. Some write buffering algorithms use commit order locking. In such cases, the transaction has to read the old value and write it back into the memory location atomically. This guarantees that no other thread could change that value in between of the read and the write. 
         [0033]    The problem that arises with the algorithm set forth in  FIGS. 1 and 2 , in the case of write buffering, may be understood from an example. Assume that X is read non-transactionally the first time using the algorithm of  FIG. 1 . Then a transaction executed by another thread writes X. If the write is an in-place update, the read monitor bit is set to zero and the next time there is a read to that memory location, the load and check ( FIG. 1 ) fails. However, if write buffering was used to write the data, the write goes to a buffer, without resetting the monitor bit, so the load and check would not fail when, in fact, it should. 
         [0034]    By writing the old value at the pertinent memory location back to the same memory location, the read monitor bit is set to the proper state without changing anything at the memory location. At the memory location, the old data is simply rewritten to the same location, the new data remains in the write buffer, and all that has been achieved is to set the read monitor bit to the correct state. 
         [0035]    While examples were given above with respect to unmanaged environments using software languages such as C++, the algorithms set forth above also operate in managed environments, such as Java environments, as well. In the managed environments, more read and write monitor bits may be used per cache line. In the managed environment, the algorithms still work in the same way and there need be no changes in the algorithms already described in connection with the unmanaged environments. 
         [0036]    A software program compilation and execution system  100  includes a compiler  120  that compiles source code  110  into an intermediate language code  130 , as shown in  FIG. 3 . The source code  110  may be, for example, Java. The intermediate language code  130  may be, for example, Java byte-code or Common Intermediate Language code. The compiler  120  may be a software system that is run on a computer system and the intermediate language code  130  is stored in a memory of the computer system, a hard drive, or downloaded from an external source. 
         [0037]    The software program compilation and execution system  100  includes a virtual machine  140  and a hardware platform  150 . The virtual machine  140  further compiles the intermediate language code  130  into native code. The virtual machine  140  may be implemented as a software system. In this embodiment, the virtual machine  140  runs on the hardware platform  150 . The virtual machine  140  may be, for example, a Java virtual machine, a small talk runtime system, or other runtime system. Alternatively, the virtual machine  140  may be implemented using other techniques (e.g., as a firmware system). 
         [0038]    The hardware platform  150  executes the native code compiled by the virtual machine  140 . The hardware platform  150  may be implemented, for example, by a personal computer, a personal digital assistant, a network computer, a server computer, a notebook computer, a workstation, a mainframe computer, or a supercomputer. 
         [0039]    With commit order, locking transaction attempts to atomically execute boxes  28  and  30  in  FIG. 3 . This operation succeeds if the old value has not changed between the read and the store. 
         [0040]    While the present invention has been described with respect to a limited number of embodiments, those skilled in the art will appreciate numerous modifications and variations therefrom. It is intended that the appended claims cover all such modifications and variations as fall within the true spirit and scope of this present invention.