Ejemplo n.º 1
0
void wrapper(void* (*start_routine)(void*), void *arg)
{
    void *retval;
    retval = (void *) start_routine(arg);  
    gtthread_exit(retval);
    return;
}
Ejemplo n.º 2
0
/*
  The gtthread_cancel() function is analogous to pthread_cancel,
  allowing one thread to terminate another asynchronously.
 */
int gtthread_cancel(gtthread_t thread)
{
    /* if a thread cancel itself */
    if (gtthread_equal(current->tid, thread))
        gtthread_exit(0);

    sigprocmask(SIG_BLOCK, &vtalrm, NULL);
    thread_t* t = thread_get(thread);
    if (t == NULL)
    {
        sigprocmask(SIG_UNBLOCK, &vtalrm, NULL);    
        return -1;
    }
    if (t->state == GTTHREAD_DONE)
    {
        sigprocmask(SIG_UNBLOCK, &vtalrm, NULL); 
        return -1;
    }
    if (t->state == GTTHREAD_CANCEL)
    {
        sigprocmask(SIG_UNBLOCK, &vtalrm, NULL);  
        return -1;
    }
    else
        t->state = GTTHREAD_CANCEL;

    free(t->ucp->uc_stack.ss_sp);
    free(t->ucp);
    t->ucp = NULL;
    t->joining = 0;
    steque_enqueue(&zombie_queue, t);
    sigprocmask(SIG_UNBLOCK, &vtalrm, NULL);
    return 0;
}
Ejemplo n.º 3
0
void run_thread(void *(*start_routine)(void *), void *arg)
{
	void *rv; 
	sigprocmask(SIG_UNBLOCK, &alrm, NULL);

	rv = start_routine(arg);
	gtthread_exit(rv);
}
Ejemplo n.º 4
0
int main() {
  printf("test4c. Should print 'Success'.\n");

  gtthread_init(50000L);
  gtthread_create( &t, thr, NULL);
  gtthread_exit(NULL);
  return EXIT_SUCCESS;
}
Ejemplo n.º 5
0
static void start_wrapper(void *(*start_routine)(void *), void *arg) {
  void *retval;

  /* Unblock alarms */
  sigprocmask(SIG_UNBLOCK, &vtalrm, NULL);

  retval = start_routine(arg);
  gtthread_exit(retval);
}
Ejemplo n.º 6
0
void main () {
	int i;
	gtthread_init(100000);
	for (i=0; i<5; i++) {
		gtthread_mutex_init(&chopstick[i]);
		gtthread_create(&philosopher[i], (void *)dining, (int *)(i+1));
	}
	gtthread_exit(NULL);
}
Ejemplo n.º 7
0
/* 
 * A wrapper function to start a routine.
 * The reason we need this is because we need to call gtthread_exit
 * when the thread finish executing.
 */
void gtthread_start(void* (*start_routine)(void*), void* args)
{
    /* unblock signal comes from gtthread_create */
    sigprocmask(SIG_UNBLOCK, &vtalrm, NULL);

    /* start executing the start routine*/
    current->retval = (*start_routine)(args);

    /* when start_rountine returns, call gtthread_exit*/
    gtthread_exit(current->retval);
}
Ejemplo n.º 8
0
void thread_handler(void *(*thread_func)(void *), void *arg){

  void *retval;
  gtthread_blk_t *cur;
  cur = get_cur_thread();
  DEBUG_MSG("thread_handler: thread ID: # %ld\n", cur->tID);
  
  retval = (*thread_func)(arg);
  gtthread_exit(retval);
  

}
Ejemplo n.º 9
0
void *thrX(void *in)
{
    int ndx;
    printf("thrX: enter\n"); fflush(stdout);
    for(ndx=0; ndx<5; ++ndx) {
        usleep(10*1000);
        puts("X");
        gtthread_yield();
    }
    printf("thrX: exit\n"); fflush(stdout);
    gtthread_exit(in);
    return in;
}
Ejemplo n.º 10
0
int main()
{
	gtthread_t th1;

	gtthread_init(1000);

	gtthread_create(&th1, worker, NULL);

	printf("main exit\n");

	gtthread_exit(NULL);
	return 0;
}
Ejemplo n.º 11
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.º 12
0
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;
}
Ejemplo n.º 13
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.º 14
0
void *thrX(void *in)
{
    int ndx;
    int th = (int)(long)in;
    int dur = (int)hold[th];
    printf("thrX: enter\n"); fflush(stdout);
    for(ndx=0; ndx<10; ++ndx) {
        gtthread_mutex_lock(&m[0]);
printf("thrX: lock %d\n",th); fflush(stdout);
        gtthread_yield();
        usleep(dur*1000);
printf("thrX: unlock %d\n",th); fflush(stdout);
        gtthread_mutex_unlock(&m[0]);
        gtthread_yield();
    }
    printf("thrX: exit\n"); fflush(stdout);
    gtthread_exit(in);
    return in;
}
main()  
{
  int i;
  gtthread_init(100);
  gtthread_t philosopher_thread[CHOPSTICKS]; 
  int dn[CHOPSTICKS];
  void *diner();
  // intializing all the locks
  for (i=0;i<CHOPSTICKS;i++){
    gtthread_mutex_init(&chopstick_mutex[i]);
    philosopher_status[i]=INITIAL;
  }
    // creating the philosphers
  	for (i=0;i<CHOPSTICKS;i++){
      dn[i] = i;
      gtthread_create(&philosopher_thread[i],diner,&dn[i]);
  	}
   gtthread_exit(0);
  //	while(1)
}
void *diner(int *i)
{
int philId, eat, think, tTime, eTime;
//int eating = 0;
printf("I'm Philosopher %d\n",*i);
philId = *i;
int forwardCycle=0;
if(philId%2==0){
	forwardCycle=1;
}

while (1) {
   philosopher_status[philId]=THINKING;
   printf("Philosopher %d is thinking\n", philId);
   think=rand()%10000000;
   for(tTime=0;tTime<think;tTime++);
   philosopher_status[philId]=HUNGRY;
   printf("Philosopher %d is hungry\n", philId);
   while(philosopher_status[philId+CHOPSTICKS-1]==EATING || philosopher_status[philId+1]==EATING);
	   if(forwardCycle){
		   gtthread_mutex_lock(&chopstick_mutex[philId]);
		   gtthread_mutex_lock(&chopstick_mutex[(philId+1)%CHOPSTICKS]);
	   } else{
		   gtthread_mutex_lock(&chopstick_mutex[(philId+1)%CHOPSTICKS]);
		   gtthread_mutex_lock(&chopstick_mutex[philId]);
	   }
	   philosopher_status[philId]=EATING;
	   printf("Philosopher %d is eating\n", philId);
   eat=rand()%10000000;
   for(eTime=0;eTime<eat;eTime++);
   printf("Philosopher %d is done eating\n", philId);
	   if(forwardCycle){
		    gtthread_mutex_unlock(&chopstick_mutex[(philId+1)%CHOPSTICKS]);
		    gtthread_mutex_unlock(&chopstick_mutex[philId]);
		} else{
			gtthread_mutex_unlock(&chopstick_mutex[philId]);
			gtthread_mutex_unlock(&chopstick_mutex[(philId+1)%CHOPSTICKS]);
	   }
   }
gtthread_exit(NULL);
}
Ejemplo n.º 17
0
/*
  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;
}
Ejemplo n.º 18
0
int main() {
	printf("gtthread_init\n");
	fflush(stdout);
  	gtthread_t t1,t2,t3;
	fflush(stdout);
  	gtthread_init(1);
	int a = 1;
	int b = 2;
	int c = 3;
  	gtthread_create( &t1, thr1, &a);
	gtthread_create( &t2, thr1, &b);
	gtthread_create( &t3, thr1, &c);
	printf("create finish\n");
	//fflush(stdout);
	int j = 0;
	long i;
	while(j != 10){
		i = 0;
		while(i<100000){
			i++;
			//printf("***\n");
		}
		printf ("main\n");
		fflush(stdout);
		//printf("self id = %d\n",gtthread_self().id);
		j++;
		//gtthread_yield();	
		
	}
	gtthread_cancel(t1);
	printf("main finish\n");
	gtthread_exit(0);
  //gtthread_yield();

  //return EXIT_SUCCESS;
}
Ejemplo n.º 19
0
/**
 * Applies the function pointed to by func to arg, and stores the return
 * value in retval. Inspired by the apply() method in Python.
 */
static void apply(void *(*func) (void *), void *arg, gtthread_t *runner_thread) {
  runner_thread->retval = (void *) func(arg);
  gtthread_exit(NULL);
}
Ejemplo n.º 20
0
void* worker(void* arg)
{
	long i = (long) arg;
	gtthread_exit((void*) g_return_values[i]);
	return NULL;
}