예제 #1
0
/* 
 * Similar algorithm for the delete, find, and insert:
 * Lock the first two elements (locking each before getting the copy of the element)
 * then unlock previous, keep ownership of the current, and lock next in a loop.
 */
sval_t
lockc_delete(intset_l_t *set, skey_t key)
{
  PARSE_TRY();
  UPDATE_TRY();

  node_l_t *curr, *next;
  sval_t res = 0;
	
  GL_LOCK(set->lock);		/* when GL_[UN]LOCK is defined the [UN]LOCK is not ;-) */
  LOCK(ND_GET_LOCK(set->head));
  curr = set->head;
  LOCK(ND_GET_LOCK(curr->next));
  next = curr->next;
	
  while (next->key < key) 
    {
      UNLOCK(ND_GET_LOCK(curr));
      curr = next;
      LOCK(ND_GET_LOCK(next->next));
      next = next->next;
    }

  if (key == next->key)
    {
      res = next->val;
      curr->next = next->next;
      UNLOCK(ND_GET_LOCK(next));
      node_delete_l(next);
      UNLOCK(ND_GET_LOCK(curr));
    } 
  else 
    {
      UNLOCK(ND_GET_LOCK(curr));
      UNLOCK(ND_GET_LOCK(next));
    }  
  GL_UNLOCK(set->lock);

  return res;
}
예제 #2
0
/* 
 * Similar algorithm for the delete, find, and insert:
 * Lock the first two elements (locking each before getting the copy of the element)
 * then unlock previous, keep ownership of the current, and lock next in a loop.
 */
int lockc_delete(intset_l_t *set, val_t val) {
	node_l_t *curr, *next;
	int found;
	
	LOCK(&set->head->lock);
	curr = set->head;
	LOCK(&curr->next->lock);
	next = curr->next;
	
	while (next->val < val) {
		UNLOCK(&curr->lock);
		curr = next;
		LOCK(&next->next->lock);
		next = next->next;
	}
	found = (val == next->val);
	if (found) {
		curr->next = next->next;
	}
	UNLOCK(&curr->lock);
	UNLOCK(&next->lock);
	node_delete_l(next);
	return found;
}