Ejemplo n.º 1
0
int main() {
	gtthread_t t1, t2, t3, t4, t5;
	gtthread_init(1000);

	gtthread_create(&t1, func1, &t1);
	g_gtt = t1;
	gtthread_join(t1, NULL);

	gtt_tuple tuple;
	gtthread_create(&t2, func2, &tuple);
	tuple = (gtt_tuple) {
		.gtt1 = t1,
		.gtt2 = t2
	};
	gtthread_join(t2, NULL);

	gtthread_create(&t3, func3, NULL);
	g_gtt = t3;
	void *t3_self;
	gtthread_join(t3, &t3_self);

	gtthread_create(&t4, func4, t3_self);
	gtthread_join(t4, NULL);
	free(t3_self);

	gtthread_create(&t5, func5, NULL);
	volatile int x = 100000000;
	while(--x);
	void *msg;
	gtthread_join(t5, &msg);
	printf("%s\n", (char*) msg);

	return 0;
}
Ejemplo n.º 2
0
int main() {
	gtthread_t t1, t2, t3, t4, t5;

	gtthread_init(1000);
	gtthread_mutex_init(&gttm);

	gtthread_create(&t1, func1, (void*) 1);
	gtthread_create(&t2, func1, (void*) 2);
	gtthread_join(t1, NULL);
	gtthread_join(t2, NULL);

	gtthread_mutex_lock(&gttm);
	gtthread_create(&t3, func2, (void*) 3);
	gtthread_create(&t4, func2, (void*) 4);
	printf("This should print before threads 3 and 4 finish\n");
	gtthread_mutex_unlock(&gttm);
	gtthread_join(t3, NULL);
	gtthread_join(t4, NULL);

	printf("Testing yield with one schedulable thread... ");
	gtthread_yield();
	gtthread_yield();
	gtthread_yield();
	printf("done!\n");

	printf("Deadlocking with thread 5...\n");
	gtthread_mutex_lock(&gttm);
	gtthread_create(&t5, func3, NULL);
	gtthread_join(t5, NULL);
	gtthread_mutex_unlock(&gttm);
	printf("!!! ERROR !!! Failed to deadlock with thread 5\n");

	return 0;
}
Ejemplo n.º 3
0
int main() {
  int i;
  gtthread_t t1, t2, t3, t6;
  void *ret;

  gtthread_mutex_init(&count_lock);

  gtthread_init(5);
  gtthread_create(&t1, thr1, (void *) 1);
  gtthread_create(&t2, thr2, (void *) 2);
  gtthread_create(&t3, thr3, (void *) 3);
  gtthread_create(&t6, thr6, (void *) 6);

  for (i = 0; i < LOOP_MAX; i++) {
    printf("Main thread!\n");
    fflush(stdout);

    gtthread_mutex_lock(&count_lock);
    count++;
    gtthread_mutex_unlock(&count_lock);

    if (i % YIELD_VAL == 0) {
      gtthread_yield();
    }
  }

  printf("main waiting for thr1...\n");
  fflush(stdout);
  gtthread_join(t1, &ret);
  printf("thr1 joined main with value %d!\n", (int) ret);
  fflush(stdout);

  gtthread_cancel(t2);
  printf("thr2 cancelled!\n");
  fflush(stdout);

  printf("main waiting for thr3...\n");
  fflush(stdout);
  gtthread_join(t3, &ret);
  printf("thr3 joined main with value %d!\n", (int) ret);
  fflush(stdout);

  printf("main waiting for thr6...\n");
  fflush(stdout);
  gtthread_join(t6, &ret);
  printf("thr6 joined main with value %d!\n", (int) ret);
  fflush(stdout);

  gtthread_mutex_lock(&count_lock);
  printf("Final count value: %lu\n", count);
  fflush(stdout);
  gtthread_mutex_unlock(&count_lock);

  gtthread_mutex_destroy(&count_lock);
  return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
int main() {
    printf("test4d. Should print 'Success' three times.\n");

    gtthread_init(50000L);
    gtthread_create( &t1, thr1, NULL);
    gtthread_create( &t2, thr2, NULL);
    gtthread_join(t2, NULL);
    gtthread_join(t1, NULL);
    printf("Success\n");
    return EXIT_SUCCESS;
}
Ejemplo n.º 5
0
int main()
{
	int rc;

	gtthread_init(1000);

	rc = gtthread_create(&g_th1, worker, NULL);
	rc = gtthread_create(&g_th2, worker, NULL);

	gtthread_join(g_th1, NULL);
	gtthread_join(g_th2, NULL);
	return 0;
}
Ejemplo n.º 6
0
void *thr3(void *in) {
  int i, j = 0;
  void *ret;
  gtthread_t t4;

  printf("thr3 started with parameter %d\n", (int) in);
  fflush(stdout);

  gtthread_create(&t4, thr4, (void *) 4);

  for (i = 0; i < LOOP_MAX; i++) { 
    printf("thr3!\n");
    fflush(stdout);
    j++;

    if (i % YIELD_VAL == 0) {
      gtthread_yield();
    }

  }

  gtthread_join(t4, &ret);
  printf("thr4 joined thr3 with value %d!\n", (int) ret);
  fflush(stdout);

  printf("thr3 finished with parameter %d\n", (int) in);
  fflush(stdout);

  return (void *) j + 3;
}
Ejemplo n.º 7
0
void *thr2(void *in) {
  int i, j = 0;
  void *ret;
  gtthread_t t5;

  printf("thr2 started with parameter %d\n", (int) in);
  fflush(stdout);

  gtthread_create(&t5, thr5, NULL);

  for (i = 0; i < (LOOP_MAX + (LOOP_MAX / 2)); i++) { 
    printf("thr2!\n");
    fflush(stdout);
    j++;
    if (i % YIELD_VAL == 0) {
      gtthread_yield();
    }
  }

  gtthread_join(t5, NULL);
  printf("thr5 joined thr2 with value %d!\n", (int) ret);
  fflush(stdout);

  printf("thr2 finished with parameter %d\n", (int) in);
  fflush(stdout);

  return (void *) j + 2;
}
Ejemplo n.º 8
0
int main()
{
        printf("Should not print any errors.\n");
	long i;
	gtthread_t threads[NUM_THREADS];

	gtthread_init(1000);

	gtthread_mutex_init(&g_mutex);

	for (i = 0; i < NUM_THREADS; ++i) {
		gtthread_create(&threads[i], worker, (void*) i);
	}

	for (i = 0; i < NUM_THREADS; ++i) {
		gtthread_join(threads[i], NULL);
	}

	if (g_num != NUM_THREADS) {
		fprintf(stderr, 
				"!ERROR! Wrong result! %d != %d\n",
				g_num, NUM_THREADS);
	}
	return 0;
}
Ejemplo n.º 9
0
int main()
{
    int ndx, tdx;
    void* status = (void*)100;
    int result;
    printf("test thread create N, join N\n");
    printf("main: enter\n");
    gtthread_init(period);
    gtthread_yield();
    for(tdx=0; tdx<1000; ++tdx) {
        gtthread_create( &t[tdx], thrX, (void*)tdx);
        //gtthread_create( &t[tdx], thrR, (void*)tdx);
        //gtthread_create( &t[tdx], thrY, (void*)tdx);
    }
    puts(">");
    gtthread_yield();
    for(tdx=0; tdx<1000; ++tdx) {
        result = gtthread_join( t[tdx], &status);
        if(result) printf("main.join %d\n",result);
        printf("thread %d return %d\n",tdx,(long)status);
    }
    printf("main: exit\n");
    fflush(stdout);
    return EXIT_SUCCESS;
}
Ejemplo n.º 10
0
int main()
{
    int ndx, odx;
    void* status = (void*)100;
    printf("test thread create, cancel, join\n");
    printf("main: enter\n");
    gtthread_init(period);
    gtthread_yield();
    for(odx=0; odx<10; ++odx) {
        //gtthread_create( &t[odx], thrX, (void*)odx);
        //gtthread_create( &t[odx], thrR, (void*)odx);
        gtthread_create( &t[odx], thrY, (void*)odx);
    }
    gtthread_yield();
    for(ndx=0; ndx<20; ++ndx) {
            usleep(100*1000);
            puts(">");
            gtthread_yield();
    }
    for(odx=0; odx<10; ++odx) {
        gtthread_join( t[odx], &status);
        printf("thread %d return %d\n",odx,(long)status);
    }
    printf("main: exit\n");
    fflush(stdout);
    return EXIT_SUCCESS;
}
Ejemplo n.º 11
0
int main()
{
	gtthread_t threads[NUM_THREADS];
	void* rets[NUM_THREADS];
	int i = 0;

	srand(time(NULL));

	// Randomly assign return values
	for (i = 0; i < NUM_THREADS; ++i) {
		g_return_values[i] = rand();
	}

	gtthread_init(1000);

	for (i = 0; i < NUM_THREADS; ++i) {
		gtthread_create(&threads[i], worker, (void*) i);
	}

	for (i = 0; i < NUM_THREADS; ++i) {
		gtthread_join(threads[i], &rets[i]);
		printf("Join on Thread[%d]", i);
	}

	// Check return values
	for (i = 0; i < NUM_THREADS; ++i) {
		if ((long) rets[i] != g_return_values[i]) {
			fprintf(stderr, 
					"!ERROR! %dth return value is wrong! %d, %d\n",
					i, rets[i], g_return_values[i]);
		}
	}
printf("Main exit\n");
	return 0;
}
Ejemplo n.º 12
0
int main() {
    printf("test6b. Should print 'main hello' then 'thr1 hello' and 'thr2 hello' for 5 times interleaving (but no mixed up prints e.g. 'th1 thr2 hello hello')\n");

    gtthread_init(50000L);
    gtthread_mutex_init(&g_mutex);
    gtthread_mutex_lock(&g_mutex);

    gtthread_create( &t1, thr1, NULL);
    gtthread_create( &t2, thr2, NULL);
    gtthread_yield();
    printf("main hello\n");
    gtthread_mutex_unlock(&g_mutex);
    gtthread_join(t2, NULL);
    gtthread_join(t1, NULL);
    return EXIT_SUCCESS;
}
Ejemplo n.º 13
0
int main()
{
        printf("Should print 'thread 1 (3x) then thread 0 (3x).\n");
	gtthread_t th1, th2;

	gtthread_init(1000);

	gtthread_mutex_init(&g_mutex);

	gtthread_create(&th1, worker, (void*)1);
	gtthread_create(&th2, worker, (void*)2);

	gtthread_join(th1, NULL);
	gtthread_join(th2, NULL);
	return 0;
}
Ejemplo n.º 14
0
int main() {
	gtthread_t t1;
	gtthread_t t2;
	gtthread_t t3;
	double sum_thread1 = 0;
	double *p1 = &sum_thread1;
	double **p2 = &p1;

	gtthread_mutex_init(&g_mutex);

	gtthread_init(10000);

	gtthread_create(&t1, thr1, NULL );

	gtthread_create(&t2, thr2, NULL );

	gtthread_create(&t3, thr3, NULL );

	printf("id of thread1 is %d\n", t1.id);
	printf("id of thread1 is %d\n", t2.id);
	// get self id
	gtthread_t t0;
	t0 = gtthread_self();
	printf("The main thread is %d \n", t0.id);

	//gtthread_yield();
	//gtthread_cancel(t2);

	gtthread_join(t1, (void **) p2);
	printf("Returned Value from thread 1 = %f\n", **p2);

	gtthread_join(t2, NULL );
	gtthread_join(t3, NULL );



	printf("returning to main thread!\n");

	// printf("returning to main thread again!\n");
	gtthread_mutex_destroy(&g_mutex);

    printf("\n....Exiting the Main!....\n");

	return EXIT_SUCCESS;
}
Ejemplo n.º 15
0
void *thr1(void *in) {
  printf("thread 1: entering\n");

  void * ret_val;
  gtthread_join( main_thread, &ret_val);

  printf("return from main: %d\n", ret_val);

  printf("thread 1: exiting\n");
  fflush(stdout);
  return 0;
}
Ejemplo n.º 16
0
int main() {
  gtthread_init(1000);

  void * r1, * r2, * r3;
  gtthread_t t1, t2, t3;

  gtthread_create(&t1, thr1, 1);
  gtthread_join(t1, &r1);

  gtthread_create(&t2, thr1, r1);
  gtthread_join(t2, &r2);

  gtthread_create(&t3, thr1, r2);
  gtthread_join(t3, &r3);
  
  printf("return value: %d\n", r3);
  fflush(stdout);
  return 0;

  //return value should be 8
}
Ejemplo n.º 17
0
/* join specified thread */
void *thrJ(void *in)
{
    int result;
    void* status = (void*)in;
    long th = (long)in;
    printf("thrJ: enter %ld\n",th); fflush(stdout);
    result = gtthread_join(t[th],&status);
    if(result) printf("thrJ.join %d %d\n",th,result);
    printf("thread %d return %d\n",th,(long)status);
    printf("thrJ: exit\n"); fflush(stdout);
    gtthread_exit(status);
    return status;
}
Ejemplo n.º 18
0
int main()
{
    gtthread_t t1;
    gtthread_t t2;
    gtthread_t t3;
    double sum_thread1 = 0;
    double *p1 = &sum_thread1;
    double **p2 = &p1;

    gtthread_mutex_init(&g_mutex);
    gtthread_init(1000);
    gtthread_create(&t1, thr1, NULL );
    printf("thread1 id %ld\n", t1); //t1.id);
    gtthread_create(&t2, thr2, NULL );
    printf("thread2 id %ld\n", t2); //t2.id);
    gtthread_create(&t3, thr3, NULL );
    printf("thread3 id %ld\n", t3); //t2.id);

    // get self id
    gtthread_t t0;
    t0 = gtthread_self();
    printf("main thread id %ld \n", t0); //t0.id);
    //gtthread_yield();
    gtthread_cancel(t2);
    //gtthread_join(t1, NULL);
    gtthread_join(t1, p2);
    printf("thread1 return = %f\n", **p2);
    gtthread_join(t2, NULL );
    gtthread_join(t3, NULL );

    printf("returning to main thread!\n");
    // printf("returning to main thread again!\n");
    gtthread_mutex_destroy(&g_mutex);
    printf("\n....Exiting the Main!....\n");
    return EXIT_SUCCESS;
}
Ejemplo n.º 19
0
int main() {
	gtthread_t t1, t2, t3;
	gtthread_init(1000);

	gtthread_create(&t1, func1, NULL);
	gtthread_create(&t2, func2, NULL);
	gtthread_create(&t3, func3, NULL);

	void *msg;
	gtthread_join(t2, &msg);
	printf("%s\n", (char*) msg);
	free(msg);

	printf("main thread exiting...\n");
	gtthread_exit(NULL);
	return 0;
}
Ejemplo n.º 20
0
int
main (int argn,
  char **argv)
{
	int i;
	gtthread_init( 10 );
	if (argn == 2)
		sleep_seconds = atoi (argv[1]);

	gtthread_mutex_init (&food_lock);
	for (i = 0; i < PHILOS; i++)
		gtthread_mutex_init (&chopstick[i]);
	for (i = 0; i < PHILOS; i++)
		gtthread_create (&philo[i], philosopher, (void *)i);
	for (i = 0; i < PHILOS; i++)
		gtthread_join (philo[i], NULL);
	return 0;
}
Ejemplo n.º 21
0
int main()
{
    void* status = (void*)100;
    printf("test mutex lock, unlock\n");
    printf("main: enter\n");
    gtthread_init(period);
    gtthread_mutex_init(period);
    for( ndx=0; ndx<5; ++ndx ) {
        gtthread_create( &t[ndx], thrX, status);
        gtthread_create( &t[ndx+10], thrR, status);
    }
    for( ndx=0; ndx<5; ++ndx ) {
        gtthread_join( t[1], &status);
    }
    printf("main: exit\n");
    fflush(stdout);
    return EXIT_SUCCESS;
}
Ejemplo n.º 22
0
int main() {
    printf("Thread cancels itself and is joined afterwards. Should print 'Success'.\n");
    gtthread_t t1;

    gtthread_init(50000L);
    gtthread_create( &t1, thr1, NULL);
    void* result = NULL;
    if(gtthread_join(t1, &result)) {
      printf("Failure. Could not join canceled thread.\n");
    } else {
      if(result == PTHREAD_CANCELED) {
        printf("Success\n");
      } else {
        printf("Failure. Canceled threads join result was not as expected.\n");
      }
    }

    return EXIT_SUCCESS;
}
Ejemplo n.º 23
0
int main()
{
    gtthread_t th1;
    void* ret;

    gtthread_init(1000);

    gtthread_create(&th1, worker, (void*) g_str);

    gtthread_join(th1, &ret);

    if ((long) ret != g_ret) {
        fprintf(stderr,
                "!ERROR! Wrong return! %x, %x\n",
                ret, g_ret);
    }

    return 0;
}
Ejemplo n.º 24
0
int main()
{
      int i, o[num_threads];
      int a;
      int *rt[num_threads];
      for (i=0; i<num_threads; i++) {
          rt[i] = (int *)malloc(sizeof(int));
          o[i] = i;
      }
      puts("[main] started");
      gtthread_init(period);
      gtthread_t th[num_threads];

      for (i=0; i<num_threads; ++i) {
          gtthread_create(&th[i], thread1, (void*)&o[i]);
      }
      for (i=0; i<num_threads; ++i) {
          gtthread_join(th[i], (void*)&rt[i]);
      }
      for (i=0; i<num_threads; ++i)
          printf("[main] rt[%d]: %d\n", i, *rt[i]);
      puts("[main] finished");
      return 0;
}
Ejemplo n.º 25
0
int main()
{
  printf("test return values from repeated threads\n");
  printf("main create\n");
  gtthread_init(1000);

  void * r[10]; //* r2, * r3;
  gtthread_t t[10];
  int ndx;

  r[1-1] = (void*)1;
  gtthread_create(&t[1], thr1, r[1-1]);
  gtthread_join(t[1], &r[1]);

  gtthread_create(&t[2], thr1, r[2-1]);
  gtthread_join(t[2], &r[2]);

  gtthread_create(&t[3], thr1, r[3-1]);
  gtthread_join(t[3], &r[3]);

  gtthread_create(&t[4], thr1, r[4-1]);
  gtthread_join(t[4], &r[4]);

  gtthread_create(&t[5], thr1, r[5-1]);
  gtthread_join(t[5], &r[5]);

  printf("return value: %d\n", r[5]);

  for( ndx=1; ndx<10; ++ndx ) {
  gtthread_create(&t[5], thr1, r[5-1]);
  gtthread_join(t[5], &r[5]);
  printf("return value: [%d] %d\n", ndx, r[ndx]);
  }

  printf("main end\n");
  fflush(stdout);
  return 0;
}