Information Technology Reference
In-Depth Information
int
RCULL::search(intkey,int*ret1,int*ret2){
rcuLock.ReadLock();
intret=0;
Element*current=head;
while(current!=NULL){
if(current->key==key){
*ret1=current->val1;
*ret2=current->val2;
ret=1;
current=NULL; //breakoutofloop
}
}
rcuLock.ReadUnlock();
returnret;
}
Figure6.18: Implementation of a read-only method for a linked list that uses
RCU for synchronization.
void
RCULL::remove(intkey){
//Onewriteatatime
rcuLock.WriteLock();
void
RCULL::insert(intkey,intval1,intval2){
//Onewriteatatime
rcuLock.WriteLock();
intfound=0;
Element*prev=NULL;
Element*current=head;
while(!found&&current!=NULL){
if(current->key==key){
found=1;
//Publishupdatetoreaders
if(prev!=NULL){
rcuLock.Publish(&(prev->next),
current->next);
//Initializeitem
Element*item;
item=(Element*)malloc(sizeof(Element));
item->key=key;
item->val1=val1;
item->val2=val2;
item->next=head;
}
else{
rcuLock.Publish(&head,
current->next);
//Atomicallyupdatelist
rcuLock.Publish(&head,item);
}
//Allowotherwritestoproceed
rcuLock.WriteUnlock();
}
}
//Allowotherwritestoproceed
rcuLock.WriteUnlock();
if(found){
//Waituntilnoreaderhasoldversion
rcuLock.Synchronize();
free(current);
//Onreturn,noreaderhasoldversion
rcuLock.Synchronize();
}
}
}
Figure6.19: Implementation of two updating methods for a linked list that
uses RCU for synchronization.
Search WWH ::




Custom Search