Beispiel #1
0
void *func4(void *arg) {
	gtthread_t *t3_self = (gtthread_t*) arg;
	gtthread_t gtt = gtthread_self();
	int res = gtthread_equal(gtt, *t3_self);
	printf("Fourth test is not equal: %s\n", !res ? "correct" : "incorrect");
	return NULL;
}
int gtthread_join(gtthread_t thread, void **status){
  int i = 0;
  steque_node_t* current;
  //TODO: No need to block and unblock signals?
  if (gtthread_equal(thread, gtthread_self())){
    printf("cannot join same thread\n");
    return -1;
  }

  //Searching for node with gtthread_t thread
  current = q->front;
  for (i = 0; (i < q->N); i++) {
    if(gtthread_equal(thread, ((node_t*)current -> item) -> id)) {
      break;
    }
    current = current -> next;
  }

  //Checking for canceled thread, blocking otherwise
  while (1){
    if (((node_t*)current -> item) -> canceled == 1){
      if(status != NULL){
        *status = ((node_t*)current -> item) -> returns;
      }
      break;
    }
    gtthread_yield();
  }     

  return 0;
}
Beispiel #3
0
int  gtthread_create(gtthread_t *thread, void *(*start_routine)(void *), void *arg)
{
  if (init != 1)
  {
 	/*printf("Error - gtthread not initialized. \n");*/
	exit(-1);
  }
  else
  {
	gtthread *newThread, *current;
	sigset_t oldset;

	/* Block alarms */
	sigprocmask(SIG_BLOCK, &alrm, &oldset);
	/* sigprocmask(SIG_BLOCK, &alrm, NULL); */

	/* do malloc for new thread and init its attributes */
	if((newThread = malloc(sizeof(gtthread))) != NULL)
	{
		newThread -> id = thread_num++;
		newThread -> finished = 0;
		newThread -> cancel = 0;
		/* store thread id */
		*thread = newThread -> id;
		
		/* get thread at front of currently running queue*/
		current = (gtthread *) gtthread_self();

		/* getcontext() */
		if ( getcontext(&newThread -> uctx) == 0)
		{
			newThread -> uctx.uc_stack.ss_sp = (char*) malloc(SIGSTKSZ);
			newThread -> uctx.uc_stack.ss_size = SIGSTKSZ;	
			/* set its successor context */
			newThread -> uctx.uc_link = &current -> uctx;
		}
	
		/* init join queue for the new thread */
		steque_init(&newThread -> joining);
		
		makecontext(&newThread -> uctx, (void (*)(void)) run_thread, 2, start_routine, arg);


		/* Add new thread to back of queue */
		steque_enqueue(&globalQ, newThread);
		steque_enqueue(&currently_running, newThread);
	
		sigprocmask(SIG_UNBLOCK, &alrm, NULL);
  	}	
  	else
  	{
		/*printf("Thread creation malloc failed. Exiting.\n");*/
		sigprocmask(SIG_UNBLOCK, &alrm, NULL);
		return 1;/* ? */
  	}

  	return 0;
  }
}
Beispiel #4
0
void* thr2(void *in) {
  if(gtthread_equal(gtthread_self(), t1)) {
    printf("Failure.\n");
  } else {
    printf("Success\n");
  }
  return NULL;
}
Beispiel #5
0
void* thr(void *in) {
  if(gtthread_equal(gtthread_self(), t)) {
    printf("Success");
  } else {
    printf("Failure");
  }
  return NULL;
}
Beispiel #6
0
void* worker(void* arg)
{
	gtthread_t tid = gtthread_self();

	if(gtthread_equal(g_th2, tid))
		printf("same\n");
	else
		printf("diff\n");
}
Beispiel #7
0
void *func3(void *arg) {
	gtthread_t gtt = gtthread_self();
	gtthread_yield(); // ensure that the global is set first
	gtthread_yield();
	gtthread_yield();
	int res = gtthread_equal(g_gtt, gtt);
	printf("Third test is equal:      %s\n", res ? "correct" : "incorrect");
	gtthread_t *ret = malloc(sizeof(gtt));
	memcpy(ret, &gtt, sizeof(gtt));
	return ret;
}
Beispiel #8
0
void thread2fn()
{
  
  if(gtthread_equal(*thread1, gtthread_self()) != 0)
  {
  	fprintf(stderr, "\nSame");
  }
  else
  {
 	fprintf(stderr, "\nDiff");
  }
}
Beispiel #9
0
int main() {
  printf("test4b. Should print 'Success'.\n");

  gtthread_init(50000L);
  gtthread_t t;
  gtthread_create( &t, thr, NULL);
  if(gtthread_equal(gtthread_self(), t)) {
    printf("Failure.");
  } else {
    printf("Success");
  }
  return EXIT_SUCCESS;
}
Beispiel #10
0
char* thr1(void *in) {
int i; 
int j = 0;

int *a = (int*)in; 
int k = 2000000;


//for(i = 0; i<3; i++){
	//printf("Exiting asd: %d\n", gtthread_equal(t2, gtthread_self()));
	//while(k--);
	//printf("Exiting Thread 1\n");
	//fflush(stdout);
    //fflush(stdout);
	//gtthread_cancel(1);
	//}	
//gtthread_exit("Asd");
//for (i = 0; i< 20; i++){
	//gtthread_mutex_lock(&g_mutex);
	//gtthread_mutex_unlock(&g_mutex);
	//while(1){
	//if (gtthread_self()==1)
   	//gtthread_create( &t2, thr2, NULL);
	for (i = 0 ; i <20; i++){
		//gtthread_mutex_lock(&g_mutex);
		printf("Exiting %d\n", gtthread_self()); 
		//gtthread_mutex_unlock(&g_mutex);
		while(k--);
k = 2000000;
	//gtthread_yield();
	}
	//	printf("Exiting %d\n", gtthread_self()); 
//gtthread_join(t2, NULL);
//gtthread_yield();
//k = 1000000;
//}
	/*for (i = 0; i< 5; i++){

		printf("Exiting %d\n", 1); 
	while(k--);
k = 1000000;
}*/

const char quote[50] = "asdaa asd as dasd sdsdasdasd\0";
//gtthread_exit(quote);

    return (char*)quote;
}
Beispiel #11
0
/* will swap out currently running thread for next thread in the queue*/
void alrm_handler(int sig)
{

	gtthread *currentThread;
	/* block signals */
	sigset_t oldset;
	
	sigprocmask(SIG_BLOCK, &alrm, &oldset);
	currentThread = (gtthread *) gtthread_self();
    /* currently running thread has reached the end of its time slice */
	/* push this thread to the back of the queue */
	steque_cycle(&currently_running); 
	scheduleNextAndSwap(currentThread);
	
	sigprocmask(SIG_UNBLOCK, &alrm, NULL);
}
int main() {
  printf("main: entering\n");

  gtthread_init(100);

  main_thread = gtthread_self();

  gtthread_t t1;
  gtthread_create(&t1, thr1, NULL);

  gtthread_exit(100);

  printf("main: exiting\n");
  fflush(stdout);
  return 0;
}
Beispiel #13
0
/* Tests creation.
   Should print "Hello World!" */
void thr2(void *in) {
int i;
int j = 0;
char * a;
int k = 1000000;
//int *k = (int*) in;
	for (i = 0 ; i <10; i++){
		//gtthread_mutex_lock(&g_mutex);
		printf("Exiting %d\n", gtthread_self()); 
		//gtthread_mutex_unlock(&g_mutex);
		while(k--);
k = 2000000;
	//gtthread_yield();
	}
    return;
}
Beispiel #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;
}
/*
  The gtthread_cancel() function is analogous to pthread_cancel,
  allowing one thread to terminate another asynchronously.
 */
int  gtthread_cancel(gtthread_t thread){
  int i = 0;
  steque_node_t* current = q->front;
  block_signal();
  if (gtthread_equal(thread, gtthread_self())){
    gtthread_exit((void*)GTTHREAD_CANCELED);
  }
  for (i = 0; (i < q->N); i++) {
    if(gtthread_equal(thread, ((node_t*)current -> item) -> id)) {
      if (((node_t*)current -> item) -> canceled == 1){
        return ESRCH;
      }
      else{
        ((node_t*)current -> item) -> canceled = 1;
        ((node_t*)current -> item) -> returns = (void*)GTTHREAD_CANCELED;
        break;
      }      
    }
    current = current -> next;
  }
  unblock_signal();
  return 0;
}
Beispiel #16
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;
}
Beispiel #17
0
void* thr1(void *in) {
    gtthread_cancel(gtthread_self());
    printf("Failure. This line should not be reached.\n");
    fflush(stdout);
    return NULL;
}
Beispiel #18
0
/*
 The gtthread_cancel() function is analogous to pthread_cancel,
 allowing one thread to terminate another asynchronously.
*/
int gtthread_cancel(gtthread_t thread) {
  steque_enqueue(&g_cancelatorium, (void *) thread.id);
  if(gtthread_equal(thread, gtthread_self()))
    alarm_safe_yield();
  return 0;
}