Example #1
0
/* thread routine */
void *t_routine(void *arg)
{
    int li, sum;
    SortedListElement_t *elements = (SortedListElement_t *)arg;

    // hash and insert elements into a sublist
    for (int k = 0; k < iter_count; k++) {
        li = hash_element(&elements[k]) % list_count; //get list index

        lock_sublist(li);
        SortedList_insert(&sublist[li], &elements[k]);
        unlock_sublist(li);
    }

    // lock and enumerate all lists
    sum = 0;
    for (int k = 0; k < list_count; k++) {
        lock_sublist(k);
        sum += SortedList_length(&sublist[k]);
        unlock_sublist(k);
    }

    // hash and remove deleted 
    for (int k = 0; k < iter_count; k++) {
        li = hash_element(&elements[k]) % list_count; //get list index

        lock_sublist(li);
        SortedList_delete(SortedList_lookup(&sublist[li], elements[k].key));
        unlock_sublist(li);
    }

    return NULL;
}
Example #2
0
void test_insert()
{
	SortedList_t list;
	SortedListElement_t element[100];
	list_init(&list);

	for (int i = 0; i < 100; i++) {
		element_set_randkey(&element[i]);
		SortedList_insert(&list, &element[i]);
		//TEST_CHECK(is_sorted(&list));
	}
}
Example #3
0
void test_length()
{
	SortedList_t list;
	SortedListElement_t element[100];
	list_init(&list);

	for (int i = 0;; i++) {
		TEST_CHECK(SortedList_length(&list) == i);
		if (i >= 100)
			break;

		element_set_randkey(&element[i]);
		SortedList_insert(&list, &element[i]);
	}
}
Example #4
0
void test_corrupt_length()
{
	SortedList_t list;
	SortedListElement_t element[100];
	list_init(&list);

	for (int i = 0; i < 100; i++) {
		element_set_randkey(&element[i]);
		SortedList_insert(&list, &element[i]);
	}

	element[99].next = element[99].prev;
	// TODO: setting to NULL causes segfault

	TEST_CHECK(SortedList_length(&list) == -1);
}
Example #5
0
void test_lookup()
{
	SortedList_t list;
	list_init(&list);

	SortedListElement_t elements[26];

	// make a SortedList with keys ordered from lowercase a to lowercase z
	for (int i = 0; i < 26; i++) {
		char c[] = { (char)(i + 97), 0 };
		element_set_key(&elements[i], c);
		SortedList_insert(&list, &elements[i]);
	}

	TEST_CHECK(SortedList_lookup(&list, "z") == &elements[25]);
	TEST_CHECK(SortedList_lookup(&list, "") == NULL);
}
Example #6
0
void test_delete()
{
	SortedList_t list;
	list_init(&list);

	SortedListElement_t elements[26];
	for (int i = 0; i < 26; i++) {
		char c[] = { (char)(i+97), 0 };
		element_set_key(&elements[i], c);
		SortedList_insert(&list, &elements[i]);
	}

	TEST_CHECK(SortedList_delete(&elements[25]) == 0);
	TEST_CHECK(SortedList_length(&list) == 25);
	//TEST_CHECK(is_sorted(&list));

	SortedListElement_t *e = list.next;
	while (e != &list) {
		TEST_CHECK(strcmp(e->key, elements[25].key) != 0);
		e = e->next;
	}
}
Example #7
0
void* threadfunc(void* ind){
	int index = *((int*) ind);
	free(ind);
	int length;
	int i;

	// insert each element 
	for(i = 0; i < iterations; ++i) {
		int j = hash(elements[index*(iterations) + i].key);
		// printf("j=%d\n", j);
		if(MUTEX){
			int ret = pthread_mutex_lock(&(mutex[j]));
			SortedList_insert(&list[j], &elements[index*(iterations) + i]);
			// printf("releasing mutex for insert, mutex=%d\n", mutex);
			pthread_mutex_unlock(&(mutex[j]));
		}
		else if(SPIN){
			while(__sync_lock_test_and_set(&(spinlock[j]),1));
			SortedList_insert(&list[j], &elements[index*(iterations) + i]);
			// SortedList_display(list);	
			__sync_lock_release(&(spinlock[j]));
		}else{
			SortedList_insert(&list[j], &elements[index*(iterations) + i]);
		}		
	}

	// get length
	if(MUTEX){
		// lock before entering "locking" process
		pthread_mutex_lock(&len_mutex);
		for(i = 0; i != nLists; i++)
			pthread_mutex_lock(&(mutex[i]));
		
		// critical section
		length = SortedList_length(list);

		for(i = 0; i != nLists; i++)
			pthread_mutex_unlock(&(mutex[i]));
		
		pthread_mutex_unlock(&len_mutex);
	}else if(SPIN){
		// lock before entering "locking" process
		while(__sync_lock_test_and_set(&len_spinlock,1));
		
		for(i = 0; i != nLists; i++)
			while(__sync_lock_test_and_set(&(spinlock[i]),1));
		
		length = SortedList_length(list);	

		for(i = 0; i != nLists; i++)
			__sync_lock_release(&(spinlock[i]));

		__sync_lock_release(&len_spinlock);
	}else{
		length = SortedList_length(list);
	}
	// printf("length = %d\n", length);
	// look up/ delete
	for(i = 0; i < iterations; ++i) {
		int j = hash(elements[index*(iterations) + i].key);
		SortedListElement_t* target;
		if(MUTEX){
			pthread_mutex_lock(&(mutex[j])); 	
			target = SortedList_lookup(&list[j], elements[index*(iterations) + i].key);
			if(target == NULL){
				printf("should never be here.\b");
				pthread_mutex_unlock(&(mutex[j]));
				continue;
			}
			SortedList_delete(target);
			pthread_mutex_unlock(&(mutex[j]));
		}else if(SPIN){
			while(__sync_lock_test_and_set(&(spinlock[j]),1));
			target =  SortedList_lookup(&list[j], elements[index*iterations + i].key);
			if(target == NULL){
				printf("should never be here.\b");
				__sync_lock_release(&(spinlock[j]));
				continue;
			}
			SortedList_delete(target);
			__sync_lock_release(&(spinlock[j]));
		}else{
			// printf("lookup\n");
			// printf("j=%d, key=%s\n", j,elements[index*(iterations) + i].key);
			target=  SortedList_lookup(&list[j], elements[index*iterations + i].key);	
			// printf("\tDONE loopup\n");
			if(target == NULL){
				printf("should never be here.\b");
				continue;
			}
			// printf("delete\n");
			SortedList_delete(target);
			// printf("\tDONE delete\n");
		}

	}

	// SortedList_display(list);
	// length = SortedList_length(list);
}
Example #8
0
void* thread_function(void* arg){
  SortedListElement_t* node = (SortedListElement_t*) arg;

  // Do the insertion
  for(int i=0; i<iterations; i++){
    switch(sync_mechanism){
    case 'n':
      SortedList_insert(head, node + i);
      break;
    case 'm':
      pthread_mutex_lock(&mutex);
      SortedList_insert(head, node + i);
      pthread_mutex_unlock(&mutex);
      break;
    case 's':
      while(__sync_lock_test_and_set(&lock, 1) == 1);
      SortedList_insert(head, node + i);
      __sync_lock_release(&lock);
    default:
      break;
    }
  }

  // Get the list length
  for(int i=0; i<iterations; i++){
    switch(sync_mechanism){
    case 'n':
      SortedList_length(head);
      break;
    case 'm':
      pthread_mutex_lock(&mutex);
      SortedList_length(head);
      pthread_mutex_unlock(&mutex);
      break;
    case 's':
      while(__sync_lock_test_and_set(&lock, 1) == 1);
      SortedList_length(head);
      __sync_lock_release(&lock);
    default:
      break;
    }
  }

  // Looks up and deletes each of the keys it has previously inserted
  for(int i=0; i<iterations; i++){
    SortedListElement_t* temp;
    switch(sync_mechanism){
    case 'n':
      temp = SortedList_lookup(head, node[i].key);
      SortedList_delete(node+i);
      break;
    case 'm':
      pthread_mutex_lock(&mutex);
      temp = SortedList_lookup(head, node[i].key);
      SortedList_delete(node+i);
      pthread_mutex_unlock(&mutex);
      break;
    case 's':
      while(__sync_lock_test_and_set(&lock, 1) == 1);
      temp = SortedList_lookup(head, node[i].key);
      SortedList_delete(node+i);
      __sync_lock_release(&lock);
    default:
      break;
    }
  }
  return NULL;
}
Example #9
0
void thread_routine(void * arg)
{	
	long long threadID = (long long) arg;
	//printf("%i\n", threadID);
	int k;
	
	
	
	//insert
	if(which_lock == 'm')
	{
		for (k = 0; k != numiterations; k++)
		{
			long long index = (threadID * numiterations) + k;
			int sublistindex = getSubListIndex(&elements[index]);
			pthread_mutex_lock(&my_mutex[sublistindex]);
			SortedList_insert(&head[sublistindex], &elements[index]);
			pthread_mutex_unlock(&my_mutex[sublistindex]);
		}
	}
	else if(which_lock == 's')
	{
		for (k = 0; k != numiterations; k++)
		{
			long long index = (threadID * numiterations) + k;
			int sublistindex = getSubListIndex(&elements[index]);
			while(__sync_lock_test_and_set(&spinlock[sublistindex], 1));
			SortedList_insert(&head[sublistindex], &elements[index]);
			__sync_lock_release(&spinlock[sublistindex]);
		}
	}
	else if(which_lock == 0)
	{
		//insert set of pre-allocated elements into shared list
		for (k = 0; k != numiterations; k++)
		{
			long long index = (threadID * numiterations) + k;
			int sublistindex = getSubListIndex(&elements[index]);
			SortedList_insert(&head[sublistindex], &elements[index]);
		}
	}
	else
	{
		perror("shouldn't get here"); exit(1);
	}
	
	
	int listlen = 0;
	//get the sum of the lengths of all sublists = the length of the ENTiRE list at THIS moment
	if(which_lock == 'm')
	{
		for(k = 0; k != numlists; k++)
		{
			pthread_mutex_lock(&my_mutex[k]);
			listlen += SortedList_length(&head[k]);
			pthread_mutex_unlock(&my_mutex[k]);
		}
	}
	else if(which_lock == 's')
	{
		for(k = 0; k != numlists; k++)
		{
			while(__sync_lock_test_and_set(&spinlock[k], 1));
			listlen += SortedList_length(&head[k]);
			__sync_lock_release(&spinlock[k]);
		}
	}
	else if(which_lock == 0)
	{
		//get list length
		for(k = 0; k != numlists; k++)
			listlen += SortedList_length(&head[k]);
	}
	else
	{
		perror("shouldn't get here"); exit(1);
	}
	
	//lookup + delete
	if(which_lock == 'm')
	{
		for(k = 0; k != numiterations; k++)
		{
			long long index = (threadID*numiterations) + k;
			int sublistindex = getSubListIndex(&elements[index]);
			pthread_mutex_lock(&my_mutex[sublistindex]);
			//looks up
			SortedListElement_t* node = SortedList_lookup(&head[sublistindex], keys[index]);
			//deletes
			int ret = SortedList_delete(node);
			pthread_mutex_unlock(&my_mutex[sublistindex]);
		}
	}
	else if(which_lock == 's')
	{
		for(k = 0; k != numiterations; k++)
		{
			SortedListElement_t* node;
			long long index = (threadID*numiterations) + k;
			int sublistindex = getSubListIndex(&elements[index]);
			while(__sync_lock_test_and_set(&spinlock[sublistindex], 1));
			//looks up
			node = SortedList_lookup(&head[sublistindex], keys[index]);
			//deletes
			int ret = SortedList_delete(node);
	
			__sync_lock_release(&spinlock[sublistindex]);
		}
	}
	else if(which_lock == 0)
	{
			//looks up and deletes each of the prior keys entered
		for(k = 0; k != numiterations; k++)
		{
			long long index = (threadID*numiterations) + k;
			int sublistindex = getSubListIndex(&elements[index]);
			//looks up
			SortedListElement_t* node = SortedList_lookup(&head[sublistindex], keys[index]);
			//deletes
			int ret = SortedList_delete(node);
		}
	}
	else
	{
		perror("shouldn't get here"); exit(1);
	}
	
	pthread_exit(NULL);
}
Example #10
0
void *bthread(void *void_ptr) {
	struct thread_info ptr = *((struct thread_info*)void_ptr);
	size_t i = (ptr.thread_number)*(ptr.itr);
	while (i < (ptr.thread_number + 1)*(ptr.itr)) {
		if (sync_flag == '_') {
			SortedList_insert(ptr.head, &(ptr.sortedarray[i]));
		}
		else if (sync_flag == 'm') {
			pthread_mutex_lock(&mutex);
			SortedList_insert(ptr.head, &(ptr.sortedarray[i]));
			pthread_mutex_unlock(&mutex);
		}
		else if (sync_flag == 's') {
			while (__sync_lock_test_and_set(&locker, 1) == 1);
			SortedList_insert(ptr.head, &(ptr.sortedarray[i]));
			__sync_lock_release(&locker);
		}
		i++;
	}

	if (sync_flag == '_') {
		if (SortedList_length(ptr.head) < 0)
			exit(1);
	}
	else if (sync_flag == 'm') {
		pthread_mutex_lock(&mutex);
		if (SortedList_length(ptr.head) < 0)
			exit(1);
		pthread_mutex_unlock(&mutex);
	}
	else if (sync_flag == 's') {
		while (__sync_lock_test_and_set(&locker, 1) == 1);
		if (SortedList_length(ptr.head) < 0)
			exit(1);
		__sync_lock_release(&locker);
	}

	size_t j = (ptr.thread_number)*(ptr.itr);
	while (j < (ptr.thread_number + 1)*(ptr.itr)) {
		if (sync_flag == '_') {
			if (SortedList_lookup(ptr.head, ptr.sortedarray[j].key) == 0)
				exit(2);
			if (SortedList_delete(&(ptr.sortedarray[j])) == 1)
				exit(2);
		}
		else if (sync_flag == 'm') {
			pthread_mutex_lock(&mutex);
			if (SortedList_lookup(ptr.head, ptr.sortedarray[j].key) == 0)
				exit(2);
			if (SortedList_delete(&(ptr.sortedarray[j])) == 1)
				exit(2);
			pthread_mutex_unlock(&mutex);
		}
		else if (sync_flag == 's') {
			while (__sync_lock_test_and_set(&locker, 1) == 1);
			if (SortedList_lookup(ptr.head, ptr.sortedarray[j].key) == 0)
				exit(2);
			if (SortedList_delete(&(ptr.sortedarray[j])) == 1)
				exit(2);
			__sync_lock_release(&locker);
		}
		j++;
	}
	return NULL;
}
Example #11
0
void* thread_func(void* argc)
{
  int i;
  //insert 
  //printf("%d\n", *(int*)argc);
  for (i = *(int*)argc; i < operations; i += num_thread) 
  {

    if (sync_s == 'm')
    {
      pthread_mutex_lock(&lock);
      SortedList_insert(&list[hash_key(element[i].key)], &element[i]);
      pthread_mutex_unlock(&lock);
    }
    else if (sync_s=='s')
    {
      while (__sync_lock_test_and_set(&locker, 1));
        SortedList_insert(&list[hash_key(element[i].key)], &element[i]);
        __sync_lock_release(&locker);
    }
    else
    {
      SortedList_insert(&list[hash_key(element[i].key)], &element[i]);
    }
  }
  int b;
  //get the length
  if (sync_s=='m')
  {
    pthread_mutex_lock(&lock);

    for (b=0; b<num_list; b++)
    {
      SortedList_length(&list[b]);
      //printf("%d\n",SortedList_length(&list[b]) );
    }
    pthread_mutex_unlock(&lock);
  }
  else if (sync_s=='s')
  {
    while (__sync_lock_test_and_set(&locker, 1));
    for (b=0; b<num_list; b++)
    {
      SortedList_length(&list[b]);
    }
    __sync_lock_release(&locker);
  }
  else
  {
    for (b=0; b<num_list; b++)
    {
      SortedList_length(&list[b]);
    }
  }

  //lookup and delete.
  SortedListElement_t* node_deleted;
  for ( i = *(int *)argc; i < operations; i += num_thread) 
    {

      if (sync_s=='m')
      {
        pthread_mutex_lock(&lock);
        node_deleted = SortedList_lookup(&list[hash_key(element[i].key)], element[i].key);
        SortedList_delete(node_deleted);
        pthread_mutex_unlock(&lock);
      } 
      else if (sync_s=='s')
      {
        while (__sync_lock_test_and_set(&locker, 1));
        node_deleted = SortedList_lookup(&list[hash_key(element[i].key)], element[i].key);
        SortedList_delete(node_deleted);
        __sync_lock_release(&locker);
      }
      else
      {
        node_deleted = SortedList_lookup(&list[hash_key(element[i].key)], element[i].key);
        
        SortedList_delete(node_deleted);
      }
    } 
}