コード例 #1
0
ファイル: test-mutex.c プロジェクト: joaomlneto/so-sthreads
int main(int argc, char **argv)
{
	int checks;
	
	printf("Testing sthread_mutex_*, impl: %s\n",
		   (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
	sthread_init();
	
	mutex = sthread_mutex_init();
	sthread_mutex_lock(mutex);
	
	if (sthread_create(thread_start, (void*)1, 1) == NULL) {
		printf("sthread_create failed\n");
		exit(1);
	}
	
	/* Wait until the other thread has at least started,
	 * to give it a chance at getting through the mutex incorrectly. */
	while (ran_thread == 0) {
		sthread_yield();
	}

   sthread_dump();
	
	/* The other thread has run, but shouldn't have been
	 * able to affect counter (note that this is not a great test
	 * for preemptive scheduler, since the other thread's sequence
	 * is not atomic). */   
	assert(counter == 0);
	
	/* This should let the other thread run at some point. */
	sthread_mutex_unlock(mutex);
	
	/* Allow up to 100 checks in case the scheduler doesn't
	 * decide to run the other thread for a really long time. */
	checks = 100;
	while (checks > 0) {
		sthread_mutex_lock(mutex);
		if (counter != 0) {
			/* The other thread ran, got the lock,
			 * and incrmented the counter: test passes. */
			checks = -1;
		} else {
			checks--;
		}
		sthread_mutex_unlock(mutex);
		
		/* Nudge the scheduler to run the other thread: */
		sthread_yield();
	}
	
	if (checks == -1) {
		printf("sthread_mutex passed\n");
	} else {
		printf("*** sthread_mutex failed\n");
	}
	sthread_mutex_free(mutex);
	return 0;
}
コード例 #2
0
ファイル: 10monitor05.c プロジェクト: joaomlneto/so-sthreads
int main(int argc, char **argv) {
  int arg = 1;
  printf("Testing sthread_mutex_*, impl: %s\n",
      (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
  
  sthread_init();

  mon = sthread_monitor_init();
  
  sthread_monitor_enter(mon);
  if (sthread_create(thread_start, (void*)&arg, 1) == NULL) {
    printf("sthread_create failed\n");
    exit(1);
  }
  printf("thread principal vai bloquear-se\n");
  sthread_monitor_wait(mon);
  printf("thread principal desbloqueada\n");
  i = 2;
  printf("i=2\n");
  sthread_monitor_exit(mon);
  if (i==2) {
    printf("\nSUCESSO!\n");
  } else {
    printf("\nteste falhado...\n");
  }
  return 1;
}
コード例 #3
0
int main(int argc, char **argv)
{
  printf("Testing sthread_create, impl: %s\n",
	 (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
  sthread_init();
    
  if (sthread_create(thread1, (void*)1, 10) == NULL) {
    printf("sthread_create failed\n");
    exit(-1);
  }
    
  if (sthread_create(thread2, (void*)1, 10) == NULL) {
    printf("sthread_create failed\n");
    exit(-1);
  }

  printf("in main\n");
  sthread_sleep(10000);
  
  printf("\ntwo threads runqueue active in priority 10\n");
  sthread_dump();
  sthread_sleep(10000);

  printf("\ntwo threads runqueue active in priority 10\n");
  sthread_dump();

  printf("out main\n");

  return 0;
}
コード例 #4
0
int main(int argc, char **argv)
{
  int j;

   int count = argc > 1 ? atoi(argv[1]) : 100;
   printf("Testing time slice and mutexes, impl: %s\n",
      (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");

   sthread_init();

   if (sthread_create(thread_1, (void*)count, 1) == NULL) {
      printf("sthread_create failed\n");
      exit(1);
   }

   if (sthread_create(thread_2, (void*)count, 1) == NULL) {
      printf("sthread_create failed\n");
      exit(1);
   }
   printf("created two threads\n");

   printf("if this is the last line of output, time slices aren't working!\n");

   while(!t1_complete || !t2_complete);

   printf("PASSED\n");
   return 0;
}
コード例 #5
0
int main(int argc, char **argv)
{
  void *ret;
  int i;

  printf("Testing sthreads, impl: %s\n",
	 (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
  sthread_init();

  mon1 = sthread_monitor_init();
  mon2 = sthread_monitor_init();

    
  if (sthread_create(thread0, (void*)1, 10) == NULL) {
    printf("sthread_create failed\n");
    exit(-1);
  }

  sthread_monitor_enter(mon1);
  for (i = 0; i < NUMBER; i++){
    if ((thr[i] = sthread_create(thread1, (void*)i, 10)) == NULL) {
      printf("sthread_create failed\n");
      exit(-1);
    }
    sthread_yield();
  }
  for (i = 0; i < NUMBER; i++){
    sthread_monitor_wait(mon1);
  }
    
  printf("in main\n");
  
  sthread_monitor_exit(mon1);
  
  sthread_sleep(10000);

  sthread_monitor_enter(mon2);
  sthread_monitor_signalall(mon2);
  sthread_monitor_exit(mon2);
  
  for (i = 0; i < NUMBER; i++){
    sthread_join(thr[i], &ret);
  }
  printf("\nSUCCESS in creating %i threads\n", NUMBER);
  printf("out main\n");

  return 0;
}
コード例 #6
0
int main(int argc, char **argv) {
    sthread_t thread = NULL;
    int * res = (int *) malloc(sizeof(int));

    printf("Testing sthread_mutex_*, impl: %s\n",
           (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");

    sthread_init();
    if ((thread = sthread_create(thread_start, (void *) NULL, 1)) == NULL) {
        printf("sthread_create failed\n");
        exit(1);
    }
    sthread_join(thread, (void**)&res);
    printf("\nteste concluido: consultar dump*.tsv\n");
    return 1;
}
コード例 #7
0
ファイル: test-crash.c プロジェクト: joaomlneto/so-sthreads
int main(int argc, char **argv)
{
  int i;

  printf("Testing sthread_sleep, impl: %s\n",
	 (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
  
  sthread_init();
  
  for (i = 1; i <= 15; i++){
    sthread_sleep(1000000);
    printf("elapsed %i secs\n", i);
  }

  return 0;
}
コード例 #8
0
ファイル: test-join.c プロジェクト: Nesokas/sampleProject
int main(int argc, char **argv)
{
  sthread_t thr;
  void *ret;
  int i;

	printf("Testing sthread_join, impl: %s\n",
		   (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
	sthread_init();
    
	if ((thr = sthread_create(thread_start, (void*)1, 1)) == NULL) {
		exit(-1);
	}
    
	sthread_join(thr, &ret);
	printf("back in main\n");
	return 0;
}
コード例 #9
0
ファイル: 02mutex02.c プロジェクト: joaomlneto/so-sthreads
int main(int argc, char **argv) {
  sthread_mutex_t mutex = NULL;
  int i = 0;

  printf("Testing sthread_mutex_*, impl: %s\n",
     (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
 
  sthread_init();

  mutex = sthread_mutex_init();
  while (i < 10) {
    sthread_mutex_lock(mutex);
    printf("i=%i\n",i);
    i++;
    sthread_yield();
    sthread_mutex_unlock(mutex);
  }
  printf("\nSUCESSO!\n");
  return 1;
}
コード例 #10
0
ファイル: test-join.c プロジェクト: Bredgren/CSE451
int main(int argc, char **argv) {
  int n;
  int *arg[MAXTHREADS];
  int *retptr;
  sthread_t child[MAXTHREADS];

  printf("Testing sthread_create, impl: %s\n",
         (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");

  if ( argv[1] ) nThreads = atoi(argv[1]);
  if ( nThreads > MAXTHREADS ) nThreads = MAXTHREADS;
  printf("Creating %d threads\n", nThreads);

  sthread_init();

  for (n = 0; n < nThreads; ++n) {
    arg[n] = malloc(sizeof(arg[n]));
    if (!arg[n]) {
      printf("malloc() failed\n");
      exit(1);
    }
    *(arg[n]) = n;

    child[n] = sthread_create(thread_start, (void*) arg[n], 1);
    if (child[n] == NULL) {
      printf("sthread_create failed\n");
      exit(1);
    }
  }

  for (n = nThreads - 1; n >= 0; --n) {
    retptr = (int *)sthread_join(child[n]);
    printf("main: joined with %d: %d\n", n, *retptr);
    free(arg[n]);
  }

  return 0;
}
コード例 #11
0
ファイル: test-create.c プロジェクト: kunw/P2
int main(int argc, char **argv) {
	int i;
	int *arg = (int *)malloc(sizeof(int));
	if (!arg) {
		printf("error: malloc failed\n");
		exit(1);
	}
	*arg = 1;

	printf("Testing sthread_create, impl: %s\n",
			(sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");

	sthread_init();

	if (sthread_create(thread_start, (void *)arg, 0) == NULL) {
		printf("sthread_create failed\n");
		exit(1);
	}

	/* Without using other thread primitives (which we don't want to
	 * rely on for this first test), we can't know for sure that the
	 * child thread runs and completes if we yield just once (in fact,
	 * with x86_64 pthreads the child almost never completes after the
	 * main thread yields just once). So, we yield an arbitrary number
	 * of times here before exiting (100 isn't always enough, but 1000
	 * seems to be). Even if the child thread doesn't finish running by
	 * the time we're done looping, the success/fail of this test shouldn't
	 * change, but the output may appear in an unexpected order. */
	for (i = 0; i < 1000; i++) {
		sthread_yield();
	}
	printf("back in main\n");

	free(arg);
	return 0;
}
コード例 #12
0
ファイル: test-cond.c プロジェクト: kunw/P2
int main(int argc, char **argv) {
	int sent, checks, i;
	sthread_t child[MAXTHREADS];


	printf("Testing sthread_cond_*, impl: %s\n",
			(sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	assert(num_threads <= MAXTHREADS);

	sthread_init();

	mutex = sthread_mutex_init();
	avail_cond = sthread_cond_init();

	sthread_mutex_lock(mutex);

	for (i = 0; i < num_threads; i++) {
		child[i] = sthread_create(thread_start, NULL, 1);
		if (child[i] == NULL) {
			printf("sthread_create %d failed\n", i);
			exit(1);
		}
	}

	assert(transfered == 0);

	/* This should let the other thread run at some point. */
	sthread_mutex_unlock(mutex);

	/* Send a bunch of things for the other threads to take */
	sent = 0;
	while (sent < max_transfer) {
		sthread_mutex_lock(mutex);
		waiting++;
		sent++;
		sthread_cond_signal(avail_cond);
		sthread_mutex_unlock(mutex);
		sthread_yield();
	}

	printf("Sent %d\n", sent);

	/* Now give the other threads 100 tries to get
	 * them all across. We assume that's enough
	 * for the sake of not running this test forever. */
	checks = 10000;  //arbitrary??
	while (checks > 0) {
		sthread_mutex_lock(mutex);
		if (transfered != max_transfer)
			checks--;
		else {
			/* broadcast to let the consumers know we've
			 * finished, so they can exit
			 * (othrewise, they may still be holding the lock
			 * when we try to free it below) */
			sthread_cond_broadcast(avail_cond);
			checks = -1;
		}
		sthread_mutex_unlock(mutex);
		sthread_yield();
	}

	if (checks == -1) {
		/* Wait for child threads to finish, otherwise we could try to
		 * free the mutex before they've unlocked it! */
		printf("joining on children\n");
		for (i = 0; i < num_threads; i++) {
			sthread_join(child[i]);
			printf("joined with child %d\n", i);
		}
		printf("sthread_cond passed\n");
	} else {
		printf("*** sthread_cond failed\n");
		/* If we failed, don't bother joining on threads. */
	}

	sthread_mutex_free(mutex);
	sthread_cond_free(avail_cond);
	return 0;
}