Thread: Can a Locking mechanism be implemented stricly with software?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    Can a Locking mechanism be implemented stricly with software?

    Hi.
    I know it is not Linux related per seh, but I couldn't find any other suitable place for this question that bothers me for quite a lot, and I couldn't find the answer online.

    Suppose I want to implement a locking mechanism.
    Is there a way to do it without the help of hardware?
    For example, the Test-And-Set function, afaik, is implemented using hardware to allow atomicity.
    Is atomicity a necessary component for a locking mechanism?
    Because if it is, then I can't think of any way to implement it without the help of some designated hardware...

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Hardware locking is the only reliable way to guarantee atomicity. It's virtually impossible to implement a locking mechanism in software without guaranteed atomicity. Most modern processors support hardware locking of memory. The intel processors and their clones implement a "lock" prefix for certain instructions, which takes exclusive ownership of a cache line for the data referenced by the instruction that follows the lock prefix.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Absurd View Post
    Hi.
    I know it is not Linux related per seh, but I couldn't find any other suitable place for this question that bothers me for quite a lot, and I couldn't find the answer online.

    Suppose I want to implement a locking mechanism.
    Is there a way to do it without the help of hardware?
    For example, the Test-And-Set function, afaik, is implemented using hardware to allow atomicity.
    Is atomicity a necessary component for a locking mechanism?
    Because if it is, then I can't think of any way to implement it without the help of some designated hardware...
    I find the question a little odd... I mean, everything that software does is dependent on hardware support. When you add two integers it only works because there is hardware support.

    Suppose there was a way to implement locking "in software" without a dedicated instruction for atomic access to a memory location. How exactly would that operation (however it looked) be independent of the hardware? You seem to be drawing a completely arbitrary line and saying that things on this side of the line are "supported by hardware" and the things on the other side happen... through magic, I guess?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Efficient timer mechanism for many timers
    By ShwangShwing in forum C Programming
    Replies: 9
    Last Post: 07-28-2009, 11:52 AM
  2. Replies: 3
    Last Post: 03-28-2008, 09:09 PM
  3. The game I/O mechanism design
    By Mario F. in forum Game Programming
    Replies: 1
    Last Post: 11-11-2006, 05:25 AM
  4. Replies: 3
    Last Post: 06-22-2005, 07:27 AM
  5. callback method/mechanism
    By unregistered in forum C Programming
    Replies: 7
    Last Post: 11-20-2001, 11:44 AM