Explain Atomic Instructions and TSL test and set lock

Posted on 2004-11-09
Last Modified: 2007-12-19
does anyone knows a clear explanation about Atomic Instructions and TSL test and set lock, I mean theorical explanation of this two concepts that i can have a better understanding with what is going on

Question by:jsctechy
    LVL 5

    Expert Comment

    LVL 11

    Expert Comment


    a short explantion of atomic is..."from the point of view of other transactions, it either happens completely or not at all."

    So basically you are not allowed to do only "half" a transaction... Like Crediting my checking account but forgetting to deduct the money from your account.. Both actions need to happen. It cant be that you credit my account and then figure out you dont have the balance to transfer the money to my account and abort then. (I prefer this example since here only the bank looses money and i get a few bucks extra ;) )

    LVL 1

    Author Comment

    thanks for that clear explanation about atomic that really make a bit of sense to me better than having tons of websites telling me source codes where i can follow what are they trying to achive. Now there is an easy explanation for TSL

    Thanks for that comment

    LVL 11

    Expert Comment


    Atomic is only one part of a transaction. An transaction needs to follow the acronym "ACID" ACID stands for "atomicity, consistency, isolation, and durability"

    The other words make sense i hope if not ask again :)=

    LVL 1

    Accepted Solution

    TSL is a instruction (usually implemented on some CPU:s) that is used to implement the critical section problem (critical sections are used to implement atomic actions, in turn). What it does is, it sets a variable to the value you pass to it, and returns the old one you replaced - both atomically. TSL means Test and Set Lock. Imagine you have two threads that you want synchronized, meaning that you don´t want a piece of code in both of them to be able to run at once:

    Thread1 {

     int currentMoney = get amount of money in account...


     set account balance to currentMoney - spentMoney;

    Thread2 {
     int currentMoney = get amount of money in account


     set account balance to currentMoney + earnedMoney;

    Now imagine that Thread1 stops where the three dots are (...) and Thread2 runs, and completes. Then thread1 still has the OLD value of the account balance, and will update the balance to what it remembered, causing a nasty bug and either you or the bank will lose money without a trace.

    What you do in a situation like this, is you encluse the important code in critical sections, making the critical sections totally atomic in regard to eachother. This is done using mutual exclusion and lock variables, this is where TSL is used. A CSEntry function could look like: (CSEntry = Critical section entry):

    CSEntry(lockVar) {

     while(TSL(lockVar) == 1) sleep();

    And equally simple, CSEntry:

    CSEntry() {

     lockvar = 0;

    Hope this helps, and good luck ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    A short article about a problem I had getting the GPS LocationListener working.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now