Ejemplo n.º 1
0
/*
 * Function name: fibonacci
 * Description: This function is called everytime the fibonacci of a number 
 * is to be computed.
 * p -> input : Number whose fibonacci is to be computed.
 * p -> output : Stores the result of the computation.
 */
void fibonacci(void* f) 
{
  Foo* foo = (Foo*)f;
  //fprintf(stderr, "I am thread %p, %ld, %x, RSP:%p\n", mythread_myid(), (long)foo->input, (unsigned int)pthread_self(),(void *)&foo);
  if(foo->input <= 2) {
    foo->output = 1;
  } 
  else {
  /* Allocation heap space for storing input and output fields 
   * of fib(f-1) and fib(f-2)
   */
    Foo* a = (Foo *)calloc(1,sizeof(Foo));
    Foo* b = (Foo *)calloc(1,sizeof(Foo));
    a->input = foo->input - 1;
    b->input = foo->input - 2;
    ThreadId id1;
    ThreadId id2;
    /* Spawning a new thread for computing fib(f-1) */
    mythread_create(&id1, NULL, fibonacci, (void *)a);
    /* Spawning a new thread for computing fib(f-2) */
    mythread_create(&id2, NULL, fibonacci, (void *)b);
    /* Parent waits till the children compute the fibonacci */
    mythread_join(id1);
    mythread_join(id2);
    /* fib(f) = fib(f-1) + fib(f-2) */
    foo->output = a->output + b->output;
    free((void *)a);
    free((void *)b);
	}
}
Ejemplo n.º 2
0
// Our operating system prototype
void prototype_os()
{
	nextBufferIndex = 0;
	full = mysem_create(0);
	empty = mysem_create(BUFFER_SIZE);
	mutex = mysem_create(1);
	int i = 0;
	running_thread[1] = NULL;
	TCB *threads[NUM_THREADS];
	for (i = 0; i < NUM_THREADS; i++)
	{
		// Here: call mythread_create so that the TCB for each thread is created
		TCB *tcb = (TCB *) malloc(sizeof(TCB));
		if (i % 2 == 0)
			mythread_create(tcb, &consumer, i);
		else
			mythread_create(tcb, &producer, i);
		threads[i] = tcb;
	}
	// Here: initialize the timer and its interrupt handler as is done in Project I
	alt_alarm * myAlarm;
	alt_alarm_start( &myAlarm, ALARMTICKS(QUANTUM_LENGTH), &mythread_handler, NULL);
	for (i = 0; i < NUM_THREADS; i++)
	{
		// Here: call mythread_join to suspend prototype_os
		mythread_join(i);
	}

	while (TRUE)
	{
		alt_printf ("This is the OS prototype for my exciting CSE351 course projects!\n");
		int j = 0;
		for (j = 0 ; j < MAX * 10; j++);
	}
}
Ejemplo n.º 3
0
int main(){
	mythread_init();
	struct mythread_t t1, t2;
	mythread_create(&t1, test, 0);
	mythread_create(&t2, test2, 0);
	fcfs();
	printf("all threads completed\n");
}
Ejemplo n.º 4
0
int main(){
	mythread_init();
	timer_init();
	struct mythread_t t1, t2;
	mythread_create(&t1, test, 0);
	mythread_create(&t2, test2, 0);
	roundrobin();
	printf("all threads completed\n");
}
Ejemplo n.º 5
0
int main()
{
    mythread_init();

    mythread_create(thread_test, (void *)5);
    mythread_create(thread_test, (void *)10);

    while (1)
    {
        mythread_yield();
    }

    printf("main is going away\n");

    return 0;
}
Ejemplo n.º 6
0
int main() {
	/*
	DNODE node1 = createNode();
	getMember(node1,threadId) = 1;
	qHead = node1;

	DNODE node2 = createNode();
	getMember(node2,threadId) = 2;
	enqueue(node2);	
		
	printQ();

	DNODE foundNode = search(2);
	printf("id of foundNode : %d \n" , getMember(foundNode,threadId));

	DNODE node3 = createNode();
	getMember(node3,threadId) = 3;
	getMember(node3,state) = RUNNABLE;
	enqueue(node3);

	foundNode  = searchRunnable();
	printf("id of runnable Node : %d \n" , getMember(foundNode,threadId));

	dequeue();
	printQ();

	dequeue();
	printQ();
	*/
	int tid1,tid2,tid3;
	mythread_create(&tid1,NULL,sayHello,NULL);
	waitpid(tid1,0,0);
	
}
Ejemplo n.º 7
0
int main(void)
{
  
  int err1, err2;
  int i;
    
  if (mythread_barrier_init(&mybarrier, NULL, 10) != 0){
    printf("Barrier Test Fail\n");
    return 1;
  }



  for(i=0;i<10;i++){
    err1 = mythread_create(&threadc[i],NULL,barrierTest,NULL);
    if (err1 != 0) printf("Barrier Test Fail\n");
  }
    

  for(i=0;i<10;i++){
    err1 = mythread_join(threadc[i],NULL);
    if (err1 != 0) printf("Barrier Test Fail\n");
  }
  
  if (mythread_barrier_destroy(&mybarrier) != 0){
        printf("Barrier Test Fail\n");
        return 1;
    }

  printf("Barrier Test PASS\n");

  return 0;

}
Ejemplo n.º 8
0
void *thread_function(void *arg)
{
	int rc, i;
	mythread_t thread,childthread ;
	struct threadValues *value;
	value = (struct threadValues *)arg;
        thread=mythread_self();
        printf("This is thread %d before yielding\n",thread.tid);

	for(i=0; i < MAX_KEYS; i++)
	{
		printf("\nSettig values %d %d for key %d by thread %d", value->v1, value->v2, i, thread.tid);
		rc = mythread_setspecific(keys[i], value);
		if(rc != 0)
		{
			printf("\nmythread_setspecific call error for thread = %d", keys[i]);
		}
	}
	rc=mythread_create(&childthread,NULL,&printingfunction,NULL);
	rc=mythread_yield();
	printf("this is thread %d after yield\n",thread.tid);
	//rc = mythread_yield();
   	getData();
	return NULL;
}
Ejemplo n.º 9
0
void
tree(void* p)
{
  int x = (int)p;
  int i;

  // msg
  for (i=0; i<x; i++) fputs(" ", stderr);
  fprintf(stderr, "Starting %p\n", mythread_myid());

  // spawn child
  if (x <3) {
    for (i=0; i<x; i++) fputs(" ", stderr);
    fprintf(stderr, "About to spawn child %p\n", mythread_myid());
    ThreadId tid;
    mythread_create(&tid, NULL, tree, (void*)(x+1));
    // wait for child
    ThreadId child = mythread_join(tid);
    for (i=0; i<x; i++) fputs(" ", stderr);
    fprintf(stderr, "Joined with child(%p) in %p\n", child, mythread_myid());
  }
  for (i=0; i<x; i++) fputs(" ", stderr);
  fprintf(stderr, "Exiting %p\n", mythread_myid());
  mythread_exit();
}
Ejemplo n.º 10
0
/*
 * Prototype OS that creates threads and semaphores.
 */
void os_primitive()
{
    unsigned int i;
    tcb *thread_pointer;

    // Create the semaphores
    full = mysem_create(0);
    empty = mysem_create(BUFFER_SIZE);
    mutex = mysem_create(1);

    // Create the threads
    for (i = 0; i < NUM_THREADS; i++)
    {
        thread_pointer = mythread_create(i, 4096, mythread);   // 4B * 4096 entries = 16KB
        mythread_start(thread_pointer);
        mythread_join(thread_pointer);
    }

    if ( start_alarm_succeed() )
        printf ("Start the alarm successfully\n");
    else
        printf ("Unable to start the alarm\n");

    /* an endless while loop */
    while (1)
    {
        printf ("This is the OS primitive for my exciting CSE351 course projects!\n");

        /* delay printf for a while */
        for (i = 0; i < 5*MAX; i++);
    }
}
int main()
{
    int i;
    mythread_t thread[NR_THREADS];
    mythread_setconcurrency(3);

    mythread_mutex_init(&mutex, NULL);
    mythread_cond_init(&okToProceed, NULL);

    for (i = 0; i < NR_THREADS; i++)
	mythread_create(&thread[i], NULL, runner, NULL);
    sleep(2);
    printf("Main: sending broadcast signal to all threads\n");
    fflush(stdout);
    mythread_cond_broadcast(&okToProceed);
    printf("Main: joining all threads\n");
    fflush(stdout);
    for (i = 0; i < NR_THREADS; i++)
	mythread_join(thread[i], NULL);
    printf("Main: about to exit\n");
    fflush(stdout);

    mythread_exit(NULL);

    mythread_cond_destroy(&okToProceed);
    mythread_mutex_destroy(&mutex);
    return 1;
}
Ejemplo n.º 12
0
int main()
{
    int i = 0;
    mythread_t tid[10];
    void *status;

    // initialize key
    mythread_key_create(&name_key, free);
    mythread_key_create(&id_key, free);

    for (i = 0; i < 10; i++) {
	int *temp = (int *) (malloc) (sizeof(int));
	*temp = i;
	mythread_create(&tid[i], NULL, hello, (void *) temp);
	mythread_yield();
    }

    printf("main thread is joining with child threads\n");
    for (i = 0; i < 10; i++) {
	mythread_join(tid[i], &status);
	printf("joined on thread:%d\n", tid[i]);
    }
    
    mythread_key_delete(id_key);
    mythread_key_delete(name_key);
    printf("main thread is exiting\n");
    fflush(NULL);
    mythread_exit(NULL);
    return 0;
}
Ejemplo n.º 13
0
int main()
{
    unsigned long thread_1;
    unsigned long thread_2;
    void *result;

    mythread_init();

    thread_1 = mythread_create(write_thread, (void *)1);
    mythread_detach(thread_1);
    thread_1 = mythread_create(write_thread, (void *)3);
    mythread_detach(thread_1);
    thread_2 = mythread_create(read_thread, (void *)10);

    mythread_join(thread_2, NULL);

    printf("main is going away\n");

    //mythread_cleanup();

    return 0;
}
Ejemplo n.º 14
0
int main()
{
    getcontext(&uctx_main);  //AMJAD: Added uctx_main
    int thread_num = 10;
    int j;
    char* thread_names[] = {
        "thread 0",
        "thread 1",
        "thread 2",
        "thread 3",
        "thread 4",
        "thread 5",
        "thread 6",
        "thread 7",
        "thread 8",
        "thread 9"
    };

    /* Initialize MyThreads library. */
    mythread_init();

    /* 250 ms */
    set_quantum_size(250);

    counter_mutex = create_semaphore(1);

    for(j=0; j<thread_num; j++)
    {
        mythread_create(thread_names[j], (void *) &handler, 6004);
    }

    /* Print threads informations before run */
    mythread_state();

    /* When this function returns, all threads should have exited. */
    runthreads();
    
    destroy_semaphore(counter_mutex);

    /* Print threads informations after run */
    mythread_state();

    printf("The counter is %d\n", counter);
    printf("The result is %f\n", result);

    if (counter == 50 &&
	(result - 151402.656521) < 0.000001)
      printf(">>> Thread library PASSED the Test 1\n");

    exit(0);
}
Ejemplo n.º 15
0
int
main(int argc, char** argv)
{
  mythreads_init();

  if (argc != 2) die("Need an argument: %d\n", argc);
  int nt = atoi(argv[1]);
  ThreadId tid;
  mythread_create(&tid, NULL, checkcollect, (void*)(nt));
  mythreads_start();
  //  exit(0);

  int i;
  int n = 5;
  for (i=0; i<n; i++) {
    ThreadId tid;
    mythread_create(&tid, NULL, tree, (void*)0);
  }
  mythreads_start();
  printf("After threads 1\n");

  mythread_create(&tid, NULL, checknull, (void*)(n*2));
  mythreads_start();
  printf("After threads 2\n");
  exit(0);

  ThreadId tid1;
  ThreadId tid2;
  mythread_create(&tid1, NULL, thread, (void*)1);
  fprintf(stderr, "Created thread: %d\n", (int)tid1);
  mythread_create(&tid2, NULL, thread, (void*)2);
  fprintf(stderr, "Created thread: %d\n", (int)tid2);
  mythreads_start();
  printf("After threads\n");
  exit(0);
}
Ejemplo n.º 16
0
void
checknull(void* p)
{
  int x = (int)p;
  int i;
  for (i=0; i<x; i++) {
    ThreadId tid;
    mythread_create(&tid, NULL, worker, (void*)(i));
  }
  for (i=0; i<x; i++) {
    ThreadId child = mythread_join(NULL);
    fprintf(stderr, "joined with %p\n", child);
  }
  mythread_exit();
}
Ejemplo n.º 17
0
int main(int argc, char **argv)
{
	mythread_t thread[MAX_THREADS];
	int rc=0, i;
	struct threadValues *value;

	for(i=0; i < MAX_KEYS; i++)
	{
		rc = mythread_key_create(&keys[i], NULL);
  		if(rc != 0)
			write(1,"\nmythread_key_create() failed\n",29);
   	}

  	printf("Create threads\n");

	for (i=0; i < MAX_THREADS; ++i)
	{
       		value = (struct threadValues *)malloc(sizeof(struct threadValues));
     		value->v1 = i;
     		value->v2 = i+10;
     		rc = mythread_create(&thread[i], NULL, thread_function, value);
     		if(rc != 0)
			printf("\nmythread_create() call error");
  	}

  	printf("Wait for half the threads to complete\n");
  	for (i=0; i < MAX_THREADS; ++i) {
  		rc = mythread_join(thread[i], NULL);
		if(rc != 0)
	     		printf("\nmythread_join() call error\n", rc);
  	}

	//delete half the keys
	for(i=0; i < MAX_KEYS; i++)
	{
		rc = mythread_key_delete(keys[i]);
		if(rc != 0)
	     		printf("\nmythread_delete() call error\n", rc);
	}
  	printf("Main before exiting\n");

        mythread_exit(0);

	//delete other half of the keys
	for(i=MAX_KEYS/2; i < MAX_KEYS; i++)
		mythread_key_delete(keys[i]);
  	printf("Main after exiting\n");
}
Ejemplo n.º 18
0
int
main(int argc, char** argv) 
{
  mythreads_init();
  if (argc != 3) {
    printf ("Arguments format: <program> <n> <Num_cores>\n");
    return 0;
  }
  long n = atoi(argv[1]);
  int num_cores= atoi(argv[2]); 

  /* Computing fibonacci(n) */ 
  mythread_create(NULL, NULL, testitout, (void*)n);
  mythreads_start(num_cores);
  return 0;
}
Ejemplo n.º 19
0
/*
 * Function name: testitout
 * Description: This function is called from the main for calculating the  
 * fibonacci of the number represented by the input argument p.
 */
void
testitout(void* p) 
{
  fprintf(stderr, "main thread %p, %ld %x\n", mythread_myid(), (long)p,(unsigned int)pthread_self());
  /* Allocation heap space for storing input and output fields of fib(p) */
  Foo* a = (Foo *)calloc(1,sizeof(Foo));
  a->input = (long)p;
  /* Spawning a new thread for computing fib(p) */
  ThreadId id;
  mythread_create(&id, NULL, fibonacci, (void *)a);
  mythread_join(id);
  free(a);
  fprintf(stderr,"parent is done %x\n",(unsigned int)pthread_self());
  fprintf(stderr,"result is %ld %x\n",a->output,(unsigned int)pthread_self());
  //exit(0);
}
Ejemplo n.º 20
0
void
thread(void* p)
{
  fputs("Entered\n", stderr);
  int x = (int)p;
  int y = 2;
  fprintf(stderr, "Entered thread %d with %d\n", (int)mythread_myid(), x);
  mythread_yield();
  fprintf(stderr, "back in thread %d with %d and %d\n", (int)mythread_myid(), x, y++);
  mythread_yield();
  fprintf(stderr, "back in thread %d with %d and %d\n", (int)mythread_myid(), x, y++);
  if (left) {
    ThreadId tid3;
    left--;
    mythread_create(&tid3, NULL, thread, (void*)left);
  }
  mythread_exit();
}
Ejemplo n.º 21
0
int main (void)
{
    int test;
    // initialize mutex, condition variable and barrier.   
    mythread_mutex_init (&mut, NULL);
    mythread_cond_init (&con, NULL);
    // 4 threads will be calling wait. 
    mythread_barrier_init (&barr, NULL, 4);

    mythread_setconcurrency (8);

    // create first 3 threads and assign them functions. 
    mythread_create (&t1, NULL, foo1, NULL);
    mythread_create (&t2, NULL, foo2, NULL);
    mythread_create (&t3, NULL, foo3, NULL);
    
    // stall main until all threads have finished. 
   if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) 
    
      PASS--;

   // after this stage, mutex and condition variables are not required, destroy them. 
   if ( mythread_cond_destroy (&con) != 0)
     PASS --;

   if ( mythread_mutex_destroy (&mut) != 0)
     PASS --;
  
    // create next 3 threads and assign functions. This is to test reusability of barriers. 
    mythread_create (&t4, NULL, foo3, NULL);
    mythread_create (&t5, NULL, foo4, NULL);
    mythread_create (&t6, NULL, foo5, NULL);
    
    // let main also wait on the barrier
   if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) 
     PASS--;
 
    // at this point, we are sure that all the threads have finished running. Now check value of PASS for any errors. 
    // If PASS is 0, means all function executed successfully. If PASS is less than 0, atleast one function returned error. 
    // print PASS or FAIL accordingly. 

    if (PASS == 0)
      printf ("PASS\n");
    else
      printf ("FAIL\n");

    //destroy the barrier and exit. 
    mythread_barrier_destroy (&barr);      
    mythread_exit(NULL);
    return 0;
}
Ejemplo n.º 22
0
mystatus_t myhtml_stream_create(myhtml_t* myhtml, mystatus_t* status, size_t count, size_t id_increase)
{
    if(count == 0) {
        myhtml->thread_stream = NULL;
        
        *status = MyHTML_STATUS_OK;
        return *status;
    }
    
    myhtml->thread_stream = mythread_create();
    if(myhtml->thread_stream == NULL)
        *status = MyCORE_STATUS_THREAD_ERROR_MEMORY_ALLOCATION;
    
    *status = mythread_init(myhtml->thread_stream, MyTHREAD_TYPE_STREAM, count, id_increase);
    
    if(*status)
        myhtml->thread_stream = mythread_destroy(myhtml->thread_stream, NULL, NULL, true);
    
    return *status;
}
Ejemplo n.º 23
0
int main(int argc, char **argv)
{	
	int i;
	char buffer[1024];
	futex_init(&printFutex, 1);
	mythread_t mythread1[NTHREADS];
	mythread_t signalingThread[NTHREADS/2];
	mythread_setconcurrency(4);
	mythread_mutex_init(&mutex,NULL);
	mythread_cond_init(&condition,NULL);
	mythread_barrier_init(&barrier,NULL,NTHREADS+1);
	
	/* Create Threads */
	for(i=0;i<NTHREADS;i++){
		sprintf(buffer, "Created thread : %d\n", i+1);
		printToConsole(buffer);
		mythread_create(&mythread1[i],NULL,&thread_func, NULL);
	}
	
	/*Signal threads waiting on cond var*/
	while(count<NTHREADS){
		/* Special case for testing broadcast*/
		if(count == NTHREADS/2){
			mythread_cond_broadcast(&condition);
		}else{
			mythread_cond_signal(&condition);
		}
	}
	
	/* Waiting on barrier. Last thread, or this main thread will unblock the barrier depending on the execution sequence */
	mythread_barrier_wait(&barrier);
	sprintf(buffer, "Out of barrier, main thread exiting..\n");
	printToConsole(buffer);
	/* Destroy mutex, barrier and cond*/
	mythread_cond_destroy(&condition);
        mythread_mutex_destroy(&mutex);
	mythread_barrier_destroy(&barrier);
	sprintf(buffer, "Finished Execution\n");
	printToConsole(buffer);
}
Ejemplo n.º 24
0
void prototype_os() {
	//thread initialized on creation..I want to bring back the initialize function
	ThreadControlBlock *newThread;
	initialize(mainThreadQueue);
	int i, j;
	for (i = 0; i < num_threads; i++) {
		// Here: do whatever you need
		// Here: call mythread_create so that the TCB for each thread is created
		//assembly calls
		newThread = mythread_create(i, 4096, mythread);
		// Here: call mythread_join to make each thread runnable/ready
		mythread_join(newThread);
	}
	// Here: initialize the timer and its interrupt handler as is done in Project I
	alt_alarm_start(&alarm, ALARMTICKS(x), mythread_handler, NULL);

	while (1) {
		alt_printf(
				"This is the OS prototype for my exciting CSE351 course projects!\n");
		for (j = 0; j < MAX; j++);
	}
}
Ejemplo n.º 25
0
void
checkcollect(void* p)
{
  int nt = (int)p;

  spawned = 0;
  collected = 0;

  mythread_seminit(&mutex, 1);  /* mutex = 1 */
  int i;
  for (i=0; i<nt; i++) {
    ThreadId tid1;
    mythread_create(&tid1, NULL, worker1, 0);
    spawned++;
    mythread_yield();
  }
  while ((collected < spawned)) {
    printf("%d spawned, %d collected\n", spawned, collected);
    mythread_yield();
  }
  printf("%d spawned, %d collected\n", spawned, collected);
  mythread_exit();
}
Ejemplo n.º 26
0
int main(void){
  
  int rc1, rc2;
  int i;
    
  mythread_setconcurrency(4);

	/*	Creating 10 threads */  
   for ( i = 0; i<10; i++)
    {
        rc1 = mythread_create(&threads[i], NULL, thread_function, (void *)NULL);
        if (rc1) ("ERROR; return code from pthread_create() is %d\n", rc1);
    }

   /* Main will now wait for 10 threads */
  for(i = 0; i<10; i++)
  {
   		rc2 = mythread_join(threads[i], NULL);
    	if (rc2) ("ERROR; return code from pthread_join() is %d\n", rc2);
  }
  
  return 0;

}
Ejemplo n.º 27
0
int main()
{
	int i;
	mythread_t thread[NTHREADS];
	mythread_setconcurrency(NTHREADS+1);
	mythread_mutex_init(&mut, NULL);
	mythread_cond_init(&cond, NULL);

	for(i = 0; i < NTHREADS; i++)
		mythread_create(&thread[i], NULL, fun, NULL);
	for(i = 0; i < INT_MAX/10; i++);

	printf("Broadcast\n"); fflush(stdout);
	mythread_cond_broadcast(&cond);
	printf("Now join\n"); fflush(stdout);
	for(i = 0; i < NTHREADS; i++)
		mythread_join(thread[i], NULL);	
	printf("main exit\n"); fflush(stdout);

	mythread_exit(NULL);
	mythread_cond_destroy(&cond);
	mythread_mutex_destroy(&mut);
	return 1;
}
Ejemplo n.º 28
0
myhtml_status_t myhtml_init(myhtml_t* myhtml, enum myhtml_options opt, size_t thread_count, size_t queue_size)
{
    myhtml_status_t status;
    
    myhtml_init_marker(myhtml);
    
    myhtml->async_incoming_buf = mcobject_async_create();
    if(myhtml->async_incoming_buf == NULL)
        return MyHTML_STATUS_ERROR_MEMORY_ALLOCATION;
    
    mcobject_async_status_t mcstatus = mcobject_async_init(myhtml->async_incoming_buf, 32, 1024, sizeof(myhtml_incoming_buffer_t));
    if(mcstatus != MCOBJECT_ASYNC_STATUS_OK)
        return MyHTML_STATUS_ERROR_MEMORY_ALLOCATION;
    
    // for tags node and other
    myhtml->mchar = mchar_async_create(64, 4096);
    
    myhtml->tag_index = mcobject_async_create();
    mcstatus = mcobject_async_init(myhtml->tag_index, 128, 1024, sizeof(myhtml_tag_index_node_t));
    
    if(mcstatus != MCOBJECT_ASYNC_STATUS_OK)
        return MyHTML_STATUS_TAGS_ERROR_MCOBJECT_CREATE;
    
    status = myhtml_tokenizer_state_init(myhtml);
    if(status) {
        myhtml->insertion_func = NULL;
        myhtml->thread = NULL;
        
        return status;
    }
    
    status = myhtml_rules_init(myhtml);
    if(status) {
        myhtml->thread = NULL;
        
        return status;
    }
    
    myhtml->opt = opt;
    myhtml->thread = mythread_create();
    
#ifdef MyHTML_BUILD_WITHOUT_THREADS
    
    status = mythread_init(myhtml->thread, NULL, thread_count, queue_size);
    
    if(status)
        return status;
    
#else /* MyHTML_BUILD_WITHOUT_THREADS */
    switch (opt) {
        case MyHTML_OPTIONS_PARSE_MODE_SINGLE:
            status = mythread_init(myhtml->thread, "lastmac", 0, queue_size);
            if(status)
                return status;
            
            break;
            
        case MyHTML_OPTIONS_PARSE_MODE_ALL_IN_ONE:
            status = mythread_init(myhtml->thread, "lastmac", 1, queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_worker_index_stream, &status);
            break;
            
        case MyHTML_OPTIONS_PARSE_MODE_WORKER_TREE:
            status = mythread_init(myhtml->thread, "lastmac", 2, queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_index, &status);
            myhread_create_stream(myhtml->thread, myhtml_parser_worker_stream, &status);
            break;
            
        case MyHTML_OPTIONS_PARSE_MODE_WORKER_INDEX:
            status = mythread_init(myhtml->thread, "lastmac", 2, queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_worker_index, &status);
            myhread_create_stream(myhtml->thread, myhtml_parser_stream, &status);
            break;
            
        case MyHTML_OPTIONS_PARSE_MODE_TREE_INDEX:
            if(thread_count == 0)
                thread_count = 1;
            
            status = mythread_init(myhtml->thread, "lastmac", (thread_count + 1), queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_stream_index, &status);
            myhread_create_batch(myhtml->thread, myhtml_parser_worker, &status, thread_count);
            break;
            
        default:
            // default MyHTML_OPTIONS_PARSE_MODE_SEPARATELY
            if(thread_count == 0)
                thread_count = 1;
            
            status = mythread_init(myhtml->thread, "lastmac", (thread_count + 2), queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_index, &status);
            myhread_create_stream(myhtml->thread, myhtml_parser_stream, &status);
            myhread_create_batch(myhtml->thread, myhtml_parser_worker, &status, thread_count);
            break;
    }
    
#endif /* MyHTML_BUILD_WITHOUT_THREADS */
    
    myhtml_clean(myhtml);
    
    return status;
}
/* mythread_create function creates a new thread using clone system call.
   It passes wrapper function to the clone system which invokes the user function.
   attr can also be passed, if not passed, then default attr will be used. 
 */
int mythread_create(mythread_t* new_thread,
		    mythread_attr_t * attr,
		    void *(*start_func) (void *), void *arg)
{
	/* Global thread queue would be empty on first mythread_create call. 
	   This would add the main thread in the queue and also an idle thread, 
 	   and then proceed with the new thread creation */

	if (mythread_queue_globalVar == NULL) {
		mythread_queue_globalVar = malloc(sizeof(struct mythread_queue));
		mythread_t *main_t_tcb = (mythread_t *) malloc(sizeof(mythread_t));

		/* Initializing TCB values for main thread */

		main_t_tcb->start_func = NULL; /* There is no explicit function to call for the main thread */
		main_t_tcb->state = READY;
		main_t_tcb->returnValue = NULL;
		main_t_tcb->arg = NULL;
		main_t_tcb->joinq.headNode = NULL;
		main_t_tcb->joinq.tailNode = NULL;
		main_t_tcb->tid = (pid_t) syscall(SYS_gettid);

		/* Initialize main thread futex */
		futex_init(&main_t_tcb->futex, 0);

		/* Adding main thread to the queue */
		queue_add_element(mythread_queue_globalVar, main_t_tcb);

		/* Initialise the yeild futex */
		futex_init(&yield_futex, 1);

		/* Recursive call to mythread_create for the idle thread creation*/
		mythread_create(&idle_tcb, NULL, idle_thread, NULL);
	}

	mythread_t *new_mythread_t = (mythread_t *) malloc(sizeof(mythread_t));
	pid_t tid;

	/* Clone flags to be passed, taken from pthread source code - CLONE_PTRACE is removed */
	int clone_flags = (CLONE_VM | CLONE_FS  
			   | CLONE_FILES | CLONE_SIGHAND 
			   | CLONE_PARENT_SETTID | CLONE_THREAD
			   | CLONE_CHILD_CLEARTID | CLONE_SYSVSEM);

	/* Check if attr is passed, if not, initialize with default attr */
	if (attr == NULL || (attr->stackbase == NULL && attr->stacksize != 0)){
		mythread_attr_t attr_new;
		mythread_attr_init(&attr_new);
		attr = &attr_new;
	}

	/* Initialize new thread's TCB */
	new_mythread_t->arg = arg;
	new_mythread_t->state = READY;
	new_mythread_t->start_func = start_func;
	new_mythread_t->joinq.headNode = NULL;
	new_mythread_t->joinq.tailNode = NULL;
	new_mythread_t->returnValue = NULL;

	/* Initialize the tcb's futex to zero. */
	futex_init(&new_mythread_t->futex, 0);

	/* Clone system call. This will invoke the wrapper_function. */
	tid = clone(wrapper_function, (char *)attr->stackbase, clone_flags,new_mythread_t);

	new_mythread_t->tid = tid;
	/* Returning the reference of the created TCB */
	*new_thread = *new_mythread_t;

	/* Adding the thread to the global TCB queue */
	queue_add_element(mythread_queue_globalVar, new_mythread_t);
	return 0;
}
Ejemplo n.º 30
0
myhtml_status_t myhtml_init(myhtml_t* myhtml, enum myhtml_options opt, size_t thread_count, size_t queue_size)
{
    myhtml_status_t status;
    
    myhtml->async_incoming_buf = mcobject_async_create();
    if(myhtml->async_incoming_buf == NULL)
        return MyHTML_STATUS_ERROR_MEMORY_ALLOCATION;
    
    mcobject_async_status_t mcstatus = mcobject_async_init(myhtml->async_incoming_buf, 32, 1024, sizeof(myhtml_incoming_buf_t));
    if(mcstatus != MCOBJECT_ASYNC_STATUS_OK)
        return MyHTML_STATUS_ERROR_MEMORY_ALLOCATION;
    
    myhtml->tags = myhtml_tag_create();
    if(myhtml->tags == NULL) {
        myhtml->parse_state_func = NULL;
        myhtml->insertion_func = NULL;
        myhtml->thread = NULL;
        
        return MyHTML_STATUS_TAGS_ERROR_MEMORY_ALLOCATION;
    }
    
    status = myhtml_tag_init(myhtml->tags);
    if(status) {
        myhtml->parse_state_func = NULL;
        myhtml->insertion_func = NULL;
        myhtml->thread = NULL;
        
        return status;
    }
    
    status = myhtml_tokenizer_state_init(myhtml);
    if(status) {
        myhtml->insertion_func = NULL;
        myhtml->thread = NULL;
        
        return status;
    }
    
    status = myhtml_rules_init(myhtml);
    if(status) {
        myhtml->thread = NULL;
        
        return status;
    }
    
    myhtml->opt = opt;
    myhtml->thread = mythread_create();
    
    switch (opt) {
        case MyHTML_OPTIONS_PARSE_MODE_SINGLE:
            status = mythread_init(myhtml->thread, "lastmac", 0, queue_size);
            if(status)
                return status;
            
            break;
            
        case MyHTML_OPTIONS_PARSE_MODE_ALL_IN_ONE:
            status = mythread_init(myhtml->thread, "lastmac", 1, queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_worker_index_stream, &status);
            break;
            
        case MyHTML_OPTIONS_PARSE_MODE_WORKER_TREE:
            status = mythread_init(myhtml->thread, "lastmac", 2, queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_index, &status);
            myhread_create_stream(myhtml->thread, myhtml_parser_worker_stream, &status);
            break;
            
        case MyHTML_OPTIONS_PARSE_MODE_WORKER_INDEX:
            status = mythread_init(myhtml->thread, "lastmac", 2, queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_worker_index, &status);
            myhread_create_stream(myhtml->thread, myhtml_parser_stream, &status);
            break;
            
        case MyHTML_OPTIONS_PARSE_MODE_TREE_INDEX:
            if(thread_count == 0)
                thread_count = 1;
            
            status = mythread_init(myhtml->thread, "lastmac", (thread_count + 1), queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_stream_index, &status);
            myhread_create_batch(myhtml->thread, myhtml_parser_worker, &status, thread_count);
            break;
            
        default:
            // default MyHTML_OPTIONS_PARSE_MODE_SEPARATELY
            if(thread_count == 0)
                thread_count = 1;
            
            status = mythread_init(myhtml->thread, "lastmac", (thread_count + 2), queue_size);
            if(status)
                return status;
            
            myhread_create_stream(myhtml->thread, myhtml_parser_index, &status);
            myhread_create_stream(myhtml->thread, myhtml_parser_stream, &status);
            myhread_create_batch(myhtml->thread, myhtml_parser_worker, &status, thread_count);
            break;
    }
    
    myhtml_clean(myhtml);
    
    return status;
}