Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
spinlock.C
Package: threads-2.0.tar.gz [view]
Upload User: shtangtang
Upload Date: 2007-01-04
Package Size: 167k
Code Size: 1k
Category:
Linux-Unix program
Development Platform:
Unix_Linux
- //
- // struct spinlock
- //
- // this structure is a primitive semaphore. It simple controls
- // the situation of a single integer... with an on/off value.
- #include <thread_spinlock.h>
- extern "C" {
- # include <sched.h>
- };
- //
- // testandset
- //
- // test if the spinlock is clear, and set it in one atomic
- // instruction. Ensuring it is owned by only one process.
- int spinlock::testandset()
- {
- int ret;
- __asm__ __volatile__("xchgl %0, %1"
- : "=r"(ret), "=m"(s_spinlock)
- : "0"(1), "m"(s_spinlock));
- return ret;
- }
- //
- // acquire
- //
- // continue to try and set the spinlock, until successfull. Yielding
- // to other processes in the scheduling while waiting.
- void spinlock::acquire()
- {
- while (testandset())
- __sched_yield();
- }
- //
- // release
- //
- // release the spinlock, by setting it to 0.
- void spinlock::release()
- {
- #ifndef RELEASE
- s_spinlock = 0;
- #else
- RELEASE(&s_spinlock);
- #endif
- }