|
Locks & Hardware
In computer science, a lock is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are many threads of execution. Locks are one way of enforcing concurrency control policies. more...
Home
ATV Parts
Apparel & Merchandise
Automotive Tools
Aviation Parts
Boat Parts
Car & Truck Parts
Air Conditioning and Heat
Air Intake & Fuel Delivery
Brakes
Charging & Starting Systems
Computer, Chip, Cruise...
Cooling System
Decals, Emblems, & Detailing
Emission System
Engines & Components
Exhaust
Exterior
Air Dams
Antennas
Body Kits
Bras
Bug Shields
Bumpers
Car Cover
Doors & Door Handles
Fenders
Grilles
Hoods
Locks & Hardware
Mirrors
Mouldings & Trim
Nerf Bars & Running Boards
Other
Racks
Snow Plows & Parts
Splash Guards / Mud Flaps
Spoilers & Wings
Sunroof, Convertible &...
Tailgates & Liftgates
Towing & Hauling
Truck Bed Accessories
Trunk Lids & Parts
Windshield Wiper System
Filters
Gaskets
Gauges
Glass
Ignition System
Interior
Lighting & Lamps
Other Parts
Safety & Security
Salvage Parts Cars
Suspension & Steering
Transmission & Drivetrain
Turbos, Nitrous,...
Wheels, Tires & Parts
Car Audio, Video
Manuals & Literature
Motorcycle Parts
Other Vehicle Parts
Personal Watercraft Parts
Racing Parts
Services & Installation
Snowmobile Parts
Vintage Car & Truck Parts
Wholesale Lots
Types
Generally, locks are advisory locks, where each thread cooperates by acquiring the lock before accessing the corresponding data. Some systems also implement mandatory locks, where attempting unauthorized access to a locked resource will force an exception in the entity attempting to make the access.
A (binary) semaphore is the simplest type of lock. In terms of access to the data, no distinction is made between shared (read only) or exclusive (read and write) modes. Other schemes provide for a shared mode, where several threads can acquire a shared lock for read-only access to the data. Other modes such as exclusive, intend-to-exclude and intend-to-upgrade are also widely implemented.
Independent of the type of lock chosen above, locks can be classified by what happens when the lock strategy prevents progress of a thread. Most locking designs block the execution of the thread requesting the lock until it is allowed to access the locked resource. A spinlock is a lock where the thread simply waits ("spins") until the lock becomes available. It is very efficient if threads are only likely to be blocked for a short period of time, as it avoids the overhead of operating system process re-scheduling. It is wasteful if the lock is held for a long period of time.
Implementation
Locks typically require hardware support for efficient implementation. This usually takes the form of one or more atomic instructions such as "test-and-set", "fetch-and-add" or "compare-and-swap". These instructions allow a single process to test if the lock is free, and if free, acquire the lock in a single atomic operation.
Uniprocessor architectures have the option of using uninterruptable sequences of instructions, using special instructions or instruction prefixes to disable interrupts temporarily, but this technique does not work for multiprocessor shared-memory machines. Proper support for locks in a multiprocessor environment can require quite complex hardware and/or software support, with substantial synchronization issues.
The reason an atomic operation is required is because of concurrency, where more than one task executes the same logic. For example, consider the following C code:
The above example does not guarantee that the task has the lock, since more than one task can be testing the lock at the same time. Since both tasks will detect that the lock is free, both tasks will attempt to set the lock, not knowing that the other task is also setting the lock. Dekker's or Peterson's algorithm are possible substitutes if atomic locking operations are not available.
Read more at Wikipedia.org
|
|