Exemplo n.º 1
0
/* Find the node with key name and return a result or error string in result.
 * Result must have space for len characters. */
void query(char *name, char *result, int len) 
{
	node_t *parent;
    node_t *target;

    //Parent will be locked afte this
    target = searchQ(name, &head, &parent);

    if (!target) 
    {
		strncpy(result, "not found", len - 1);
		//Release the parent lock
		pthread_rwlock_unlock(&(parent->mutex_node_lock));
		return;
    } 
    else 
    {   	
	    //The only critical section for the read	
		strncpy(result, target->value, len - 1);
		//Unlock any of the locks
		pthread_rwlock_unlock(&(target->mutex_node_lock));
		pthread_rwlock_unlock(&(parent->mutex_node_lock));
		return;
    }
}
Exemplo n.º 2
0
		// search for a key
		// Pre-condition: given the key to find
		//                given a subscript by reference
		//                given the type of search desired as a char, either
		//                        - 's' for simple lookups            or
		//                        - 'i' for insertion
		// Post-condition: true is returned if key is found
		//                 false is returned if key is not found
		//                 for type 's': 
		//                     subscript is set to position of the key or the first NULL encountered
		//                 for type 'i':
		//                     subscript is set to position of the key or the first tombstone encountered
		// Note: the search function has been modified to take three arguments instead of two
		//       as instructed in the assignment description
    bool search(std::string key, int& subscript, char type)
		{
			if (probeType == 'd')
				return searchD(key, subscript, type); //helper for double hash
			else 
				return searchQ(key, subscript, type); //helper for quad hash
		}
Exemplo n.º 3
0
 //USed for query
node_t *searchQ(char *name, node_t * parent, node_t ** parentpp) {

    node_t *next;
    node_t *result;

    //Lock the parent as you traverse down
    pthread_rwlock_rdlock(&(parent->mutex_node_lock));

    if (strcmp(name, parent->name) < 0) 
    {
    	next = parent->lchild;
    }
    else 
    {
    	next = parent->rchild;
    }

    //pthread_rwlock_lock(&(next->mutex_node_lock));

    if (next == NULL) 
    {
		result = NULL;
    } 
    else 
    {
		if (strcmp(name, next->name) == 0) 
		{
		    /* Note that this falls through to the if (parentpp .. ) statement
		     * below. */
			//Found it lock the Read
		    result = next;
		} 
		else 
		{
		    /* "We have to go deeper!" This recurses and returns from here
		     * after the recursion has returned result and set parentpp */
			//pthread_rwlock_unlock(&(next->mutex_node_lock));
			//Release the parentbefore recursing down
			pthread_rwlock_unlock(&(parent->mutex_node_lock));
		    result = searchQ(name, next, parentpp);
		    return result;
		}
    }

    /* record a parent if we are looking for one */
    if (parentpp != 0) 
    {
    	*parentpp = parent;
    	
    }

    //pthread_rwlock_unlock(&(parent->mutex_node_lock));
    if(result != NULL)
    {
    	pthread_rwlock_rdlock(&(result->mutex_node_lock));
    }

    return (result);
}