Hi,
Below is the atomic function of compare and swap instruction (CAS) written in
assembly
code.
The following is general description of the function for
CAS(int *p,int n,int k)
which does if(*p == n)
{
*p = k;
return 1;
}
else
{
return 0;
}
This operation we have to perform atomically, the following is assembly code
for that one
temp is the global variable which preserves atomicity.
My question is that is there any flaw the violates atomicity principle ? Can
anyone who
worked before on this please guide us.
retr = cas(p,i,j,&temp);
int cas(int *p,int i,int j,int *t){
// local parameters are accessed in this way
// 8(%ebp) ---------- first parameter
// 12(%ebp)----------- second parameter
// 16(%ebp)----------- third parameter
// 20(%ebp)------------Fourthe parameter
// Fourth parameter is only used for performing the assembly level
instructions atomically
asm(" movl 20(%ebp),%edx"); //moving fourth parameter into the edx
Remember
// the fourth parameter consists of
address
// performing bit test and set atomically using lock
// intially the (edx) contains zero value at bit 0 A process when executes
the moves this
// bit 0 to carry flag and set the corresponding bit in (edx) to 1
// The first which entering the code only the value of 0 in the carry flag
the remaining process
// will see a value of 1 in the carry flag.so jc implies already some other
process is there in the
// code
asm("lock bts $0,(%edx)");
asm(" jc label2");
//moving of first and second pararmeters into the registers
asm(" movl 8(%ebp),%ebx");
asm(" movl 12(%ebp),%ecx"); //after performing CAS operation
// Comparing if (*p) == i
asm(" cmp %ecx,(%ebx)");
asm(" je label1");
asm(" movl $0,%eax");
asm(" movl $0,(%edx)");
asm(" leave");
asm(" ret ");
// set (*p) = j and set the temp variable to 0 so that another
// may perform the operation
asm("label1: ");
asm(" movl 16(%ebp),%ecx");
asm(" movl %ecx ,(%ebx)");
asm(" movl $0,(%edx)");
// we are setting the return value in the register %eax
asm(" movl $1,%eax");
asm(" leave");
asm(" ret");
asm("label2: ");
asm(" movl $0,%eax");
asm(" leave");
asm(" ret");
}//end of cas
Rama
This message posted from opensolaris.org
_______________________________________________
perf-discuss mailing list
[email protected]