Ejemplo n.º 1
3
int traverseBFS(Graph graph, int start, Dllist close) {
	Dllist node, queue;
	JRB visited;
	int *output;
	int temp;

	int i, n, counter = 0;

	visited = make_jrb();
	queue = new_dllist();
	dll_append(queue, new_jval_i(start));

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			counter++;
			// reportFunc(temp);
			jrb_insert_int(visited, temp, new_jval_i(temp));
			
			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_append(queue, new_jval_i(output[i]));
				}
			}
		}
	}

	return counter;
}
Ejemplo n.º 2
0
Jval* deQueue(Queue q) {
	Jval* v;
	if (dll_empty(q) || dll_empty(dll_first(q))) v = NULL;
	else {
		v = (Jval*)malloc(sizeof(Jval));
		memcpy(v,&dll_first(q)->val, sizeof(Jval));
		dll_delete_node(dll_first(q));
	}
	return v;
}
Ejemplo n.º 3
0
int main (int argc, char **argv)
{
  struct msgbuff{
    long mtype;
    pid_t pid;
  }message;

  pid_t pid, npid;
  int i;

  int           msqid;
  key_t         keyx;
  struct msqid_ds msq;

  Dllist q = new_dllist();
  Dllist n;

  keyx = ftok(KEYFILE_PATH, (int)ID);

  msqid = msgget(keyx, 0666 | IPC_CREAT);

  if (msqid == -1) {
    perror("msgget");
    exit(1);
  }

  while(1) { 
    if((msgrcv(msqid, &message, sizeof(pid_t), 1, 0)) ==
       MSGQ_NG){
      perror("msgrcv");
      exit(1);
    }
    pid = message.pid;
    if(pid != 1) {
      if(dll_empty(q)) kill(pid, SIGUSR1);
      else {
        n = dll_first(q);
        npid = jval_i(dll_val(n));
        if(pid == npid) { 
          kill(pid, SIGUSR1);
          dll_delete_node(n);
        } else dll_append(q, new_jval_i(pid));
      }
    } else {
      if(!dll_empty(q)) { 
        n = dll_first(q);
        npid = jval_i(dll_val(n));
        kill(npid, SIGUSR1);
        dll_delete_node(n);
      }
    }
  }

  return 0;
}
Ejemplo n.º 4
0
void
stress_test_dll(int amt)
{
	dll l1;
	dll l2;
	gendata x, y;
	int i;

	l1 = dll_create();
	l2 = dll_create();
	assert(dll_empty(l1));
	assert(dll_empty(l2));

	printf("Filling two dlls with 2 * %d items...\n", amt);
	for (i = 0; i < amt; ++i) {
		x.num = i;
		l1 = dll_prepend_head(l1, x);
		l2 = dll_prepend_head(l2, x);
		assert(!dll_empty(l1));
		assert(!dll_empty(l2));
		l1 = dll_append_head(l1, x);
		l2 = dll_append_head(l2, x);
		assert(!dll_empty(l1));
		assert(!dll_empty(l2));
	}

	/* Do some funky inserting at a `random' position. */
	dll_append_head(dll_forward(l1, 1), x);
	assert(x.num == dll_get_data(dll_forward(l1, 1)).num);
	dll_remove_head(dll_forward(l1, 2), NULL);

	l1 = dll_append(l1, l2);
	assert(dll_count(l1) == (unsigned int)(4 * amt));

	/* From now on, l2 is `invalid' */

	printf("Removing 2 * 2 * %d items from the appended dll...\n", amt);
	for (i = 0; i < (2 * amt); ++i) {
		assert(!dll_empty(l1));
		x = dll_get_data(l1);
		l1 = dll_remove_head(l1, NULL);
		assert(!dll_empty(l1));
		y = dll_get_data(l1);
		l1 = dll_remove_head(l1, NULL);

		/*
		 * We have to count backwards in this check, since we're
		 * using the list like a stack, prepending all the time.
		 */
		assert(x.num == amt - (i % amt) - 1);
		assert(x.num == y.num);
	}
	assert(dll_empty(l1));
	dll_destroy(l1, NULL);
}
Ejemplo n.º 5
0
void free_dllist(Dllist l)
{
  while (!dll_empty(l)) {
    dll_delete_node(dll_first(l));
  }
  free(l);
}
Ejemplo n.º 6
0
void scheduler() {

  // Init no longer has children. Needs to close.
  if (jrb_empty(init->children)) {
    SYSHalt();
  }

  if(dll_empty(readyQ)) {
    noop_flag = 1;
    noop();
  } else {
    PCB *process;
    Dllist node;
    Jval registers;
    
    noop_flag = 0;
    process = (PCB *) malloc(sizeof(PCB));
    node = dll_first(readyQ);       // Get next node in queue
    dll_delete_node(node);          // Node in memory. Delete off readyQ
    
    // Get registers from node
    registers = dll_val(node);
    process = (PCB *)jval_v(registers);
    
    // Update current process
    currentProcess = process;
    User_Base = process->base;
    User_Limit = process->limit;
    
    run_user_code(process->registers);
  }
}
Ejemplo n.º 7
0
void scheduler()
{

	//readyq = new_dllist();

	if(dll_empty(readyq))
	{
		//printf("empty ready q\n");
		current = NULL;
		noop();
	}


	//takes the first PCB off the readyq and calls run_user_code() on its registers
	else
	{
		// pop off and delete from readyq
		printf("non-empty q\n");
		current = (PCB *) jval_v(dll_val(dll_first(readyq)));
		printf("non-empty q\n");
		dll_delete_node(dll_first(readyq));
		printf("non-empty q\n");
		run_user_code(current->registers);
	}
}
Ejemplo n.º 8
0
int solution(Graph graph, Jval start, Jval stop, Dllist stackVisit, Jval (*cloneFunc)(Jval),
	int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {

	Dllist stackRes;
	Dllist node;
	Jval nowNode, temp;
	int counter = 0;

	stackRes = new_dllist();
	node = dll_first(stackVisit);
	nowNode = cloneFunc(node->val);
	dll_delete_node(node);
	dll_prepend(stackRes, nowNode);

	while(!dll_empty(stackVisit)) {
		if(isAdjacent(graph, nowNode, start, compare)) {
			dll_prepend(stackRes, start);
			counter++;
			break;
		}

		do {
			node = dll_first(stackVisit);
			temp = cloneFunc(node->val);
			dll_delete_node(node);	

			if(isAdjacent(graph, nowNode, temp, compare)) {
				dll_prepend(stackRes, temp);
				nowNode = temp;
				counter++;
				break;
			}
		} while(!dll_empty(stackVisit));
	}

	printf("Solution: The shortest path between two node: \n");

	while(!dll_empty(stackRes)) {
		node = dll_first(stackRes);
		reportFunc(node->val);
		dll_delete_node(node);
	}

	free_dllist(stackVisit);

	return counter;
}
Ejemplo n.º 9
0
void initialize_simulation(Elevator_Simulation *es)
{
    Queue *list = mmalloc(Queue, 1);
    list->passengers = new_dllist();
    dll_empty(list->passengers);
    list->cond = mmalloc(pthread_cond_t, 1);
    pthread_cond_init(list->cond, NULL);
    list->count = 0;
    (*es).v = list; 
}
Ejemplo n.º 10
0
/*
 * insert bid in descending cost
 */
void insert_bid(char *msg)
{
  char *p;
  Bid *b, *tb;
  int i, id;
  Dllist ptr;

  // create a new bid
  b = (Bid *) malloc(sizeof(Bid));
  p = strstr(msg, "B");
  p++;
  b->cost = atoi(p);
  p = strstr(p, "$");
  p++;
  b->numrobot = atoi(p);
  b->coalition = new_dllist();
  for (i = 0; i < b->numrobot; i++) {
    p = strstr(p, "$");
    p++;
    id = atoi(p);
    if (dll_empty(b->coalition)) b->leaderID = id;
    dll_append(b->coalition, new_jval_i(id)); 
  }

  printf("insert bid: %s\n", msg);
  // insert the bid to bidList, increasing cost
  if (dll_empty(bidList)) {
    dll_append(bidList, new_jval_v(b));
  } else {
    dll_traverse(ptr, bidList) {
      tb = (Bid *) jval_v(dll_val(ptr));
      if (tb->cost > b->cost) {
        dll_insert_b(ptr, new_jval_v(b));
        break;
      }
      if (ptr == dll_last(bidList)) {// append it to the last
        dll_append(bidList, new_jval_v(b));
        break;
      } 
    }
  }
Ejemplo n.º 11
0
void
FreeFinishedThreads()
{
	Dllist curr;

        while (!dll_empty(ktFree_me)) 
        {
                curr = dll_first(ktFree_me);
		FreeKThread((K_t)curr->val.v);
		dll_delete_node(curr);
        }
        return;
}
Ejemplo n.º 12
0
//Each elevator is a while loop. 
//Check the global list and if it’s empty, block on the condition variable for blocking elevators. 
//When the elevator gets a person to service, it moves to the appropriate flo0or and opens its door. 
//It puts itself into the person’s e field, then signals the person and blocks until the person wakes it up. 
//When it wakes up, it goes to the person’s destination floor, opens its door, signals the person and blocks. 
//When the person wakes it up, it closes its door and re-executes its while loop.
//• Your elevator’s should call open door(), close door() from move to floor() appropriately. 
//All sleeping and printing should be done in elevator skeleton.c. 
//Thus, your final program that you hand in should not do any sleeping or any printing.
void *elevator(void *arg)
{
    Person *person_in_transit;
    for(;;)
    {

        pthread_mutex_lock(((Elevator*)arg)->es->lock);
        while (!((Queue*)((Elevator*)arg)->es->v)->count)
            pthread_cond_wait(((Queue*)((Elevator*)arg)->es->v)->cond, ((Elevator*)arg)->es->lock);
            if(!dll_empty(((Queue*)((Elevator*)arg)->es->v)->passengers)){
             person_in_transit = (Person*)jval_v(dll_val(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers)));
             dll_delete_node(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers));
            }
        // unlock the critial section.
        pthread_mutex_unlock(((Elevator*)arg)->es->lock);
       //move the elevator.
        --((Queue*)((Elevator*)arg)->es->v)->count;
        person_in_transit->from == ((Elevator*)arg)->onfloor?:move_to_floor(((Elevator*)arg), person_in_transit->from);
        //open the door
        open_door(((Elevator*)arg));
        //add the people to the elevator
        person_in_transit->e = ((Elevator*)arg); 
        //signal the person
        pthread_mutex_lock(person_in_transit->lock);
        pthread_cond_signal(person_in_transit->cond);
        pthread_mutex_unlock(person_in_transit->lock);
        // have the elevator wait
        pthread_mutex_lock(((Elevator*)arg)->lock);
        pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock);
        pthread_mutex_unlock(((Elevator*)arg)->lock);
        // close door 
        close_door(((Elevator*)arg));
        // elevator moves
        move_to_floor(person_in_transit->e,person_in_transit->to);
        // open the door once move has completed.
        open_door(((Elevator*)arg));
        // signal the person
        pthread_mutex_lock(person_in_transit->lock);
        pthread_cond_signal(person_in_transit->cond);
        pthread_mutex_unlock(person_in_transit->lock);
        // block the elevator
        pthread_mutex_lock(((Elevator*)arg)->lock);
        pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock);
        pthread_mutex_unlock(((Elevator*)arg)->lock);
        // close the door
        close_door(((Elevator*)arg));
        // restart. 
    }
}
Ejemplo n.º 13
0
int UShortestPath(Graph graph, Jval start, Jval stop, 
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {

	Dllist node, queue, stackVisit;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n;

	visited = make_jrb();
	queue = new_dllist();
	stackVisit = new_dllist();
	dll_append(queue, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return 0;
	}

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			jrb_insert_gen(visited, temp, temp, compare);
			dll_prepend(stackVisit, temp);
			
			if(compare(temp, stop) == 0) {
				return solution(graph, start, stop, stackVisit, cloneFunc, compare, reportFunc);
			}

			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_append(queue, output[i]);
				}
			}
		}
	}

	return -1;
}
Ejemplo n.º 14
0
void DFS(Graph graph, Jval start, Jval stop, 
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {
	Dllist node, stack;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n;

	visited = make_jrb();
	stack = new_dllist();
	dll_prepend(stack, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return;
	}

	while(!dll_empty(stack)) {
		node = dll_first(stack);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			reportFunc(temp);
			jrb_insert_gen(visited, temp, temp, compare);
			
			if(compare(temp, stop) == 0) {
				jrb_free_tree(visited);
				free_dllist(stack);
				free(output);
				return;
			}

			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_prepend(stack, output[i]);
				}
			}
		}
	}
}
Ejemplo n.º 15
0
int deepFirstSearch(Graph graph, int start, int stop, Dllist close) {
	Dllist node, stack;
	JRB visited;
	int output[100];
	int temp;

	int i, n;

	visited = make_jrb();
	stack = new_dllist();
	dll_prepend(stack, new_jval_i(start));

	while(!dll_empty(stack)) {
		node = dll_first(stack);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			// reportFunc(temp);
			dll_append(close, new_jval_i(temp));
			jrb_insert_int(visited, temp, new_jval_i(temp));
			
			if(compare(temp, stop) == 0) {
				jrb_free_tree(visited);
				free_dllist(stack);
				return 1;
			}

			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_prepend(stack, new_jval_i(output[i]));
				}
			}
		}
	}

	jrb_free_tree(visited);
	free_dllist(stack);

	return 0;
}
Ejemplo n.º 16
0
int dtq_delete(int queue_id)
{
	TIMR_ENT *queue_head, *entry;

	DISABLE_AST
	queue_head = timer_queues[queue_id].queue_head;
	if(queue_head)
	{
		while(!dll_empty((DLL *)queue_head))
		{
			entry = queue_head->next;
			dll_remove(entry);
			free(entry);
		}
		free(queue_head);
		timer_queues[queue_id].queue_head = 0;
	}
	ENABLE_AST
	return(1);			
}
void BFS_Visit(Graph_Struct Graph,char start[])
{
	Graph_Symbol_Table *Table;
	char *u,*v;
	JRB node_rbt,tree,ele;
	Dllist node_queue,queue = new_dllist();
	printf("%s ",start);
	
	u = malloc(MAX_LENGTH_STRING);
	v = malloc(MAX_LENGTH_STRING);
	
	Table = Search_On_Table(Graph,start);
	Table->colour = 'g';
	
	dll_append(queue, new_jval_s(start));
	while(!dll_empty(queue))
	{
		node_queue = dll_first(queue);
		u = jval_s(node_queue->val);
		dll_delete_node(node_queue);
		node_rbt = jrb_find_str(Graph.Edges,u);
		if(node_rbt != NULL)
		{
			tree = (JRB)jval_v(node_rbt->val);
			jrb_traverse(ele,tree)
			{
				v = jval_s(ele->key);
				Table = Search_On_Table(Graph,v);
				if(Table->colour == 'w')
				{
					printf("%s ",v);
					Table->colour = 'g';
					if(Table->previous == NULL)
					{
						Table->previous = malloc(MAX_LENGTH_STRING);
					}
					strcpy(Table->previous,u);
					dll_append(queue, new_jval_s(v));
				}
			}
		}
Ejemplo n.º 18
0
int BFStraverse(Graph graph, Jval start,
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {
	
	Dllist node, queue;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n, counter = 0;

	visited = make_jrb();
	queue = new_dllist();
	dll_append(queue, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return counter;
	}

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			counter++;
			reportFunc(temp);
			jrb_insert_gen(visited, temp, temp, compare);
			
			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_append(queue, output[i]);
				}
			}
		}
	}

	return counter;
}
Ejemplo n.º 19
0
int UShortestPath(Graph graph, int start, int stop, Dllist close) {
	Dllist node, queue, stackVisit;
	JRB visited;
	int output[100];
	int temp;

	int i, n;

	visited = make_jrb();
	queue = new_dllist();
	stackVisit = new_dllist();
	dll_append(queue, new_jval_i(start));

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			jrb_insert_int(visited, temp, new_jval_i(temp));
			dll_prepend(stackVisit, new_jval_i(temp));
			
			if(temp == stop) {
				return solution(graph, start, stop, stackVisit, close);
			}

			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_append(queue, new_jval_i(output[i]));
				}
			}
		}
	}

	return -1;
}
Ejemplo n.º 20
0
void *elevator(void *arg){
    Elevator *e = (Elevator *) arg; 
    Dllist item, next, pickup;
    Person *p;
    int direction = 1;
    pickup = new_dllist();

    while(1){

        if(e -> onfloor >= top) direction = -1;
        else if(e -> onfloor <= 1) direction = 1;
        //printf("\tElevator[%i] on floor %i going %s:\n",
        //        e -> id, e -> onfloor, direction == 1 ? "up": "down"); 

        /* pick people up */
        pthread_mutex_lock(lock);

        item = dll_first(people);
        while(!dll_empty(people) && item != dll_nil(people)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            //printf("\t\tShould I get %s %s going from %i to %i? ",
            //        p -> fname, p -> lname, p -> from, p -> to);

            if(e -> onfloor == p -> from){
                if(p -> to > e -> onfloor && direction == 1 || 
                   p -> to < e -> onfloor && direction == -1){
                    dll_append(pickup, item -> val);
                    dll_delete_node(item);
                    //printf("yes!\n");

                }
                //else printf("no!\n");
            }
            //else printf("no!\n");

            item = next;
        }

        pthread_mutex_unlock(lock);

        item = dll_first(pickup);
        while(!dll_empty(pickup) && item != dll_nil(pickup)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            if(!e -> door_open) open_door(e);
            pthread_mutex_lock(p -> lock);

            p -> e = e;

            pthread_cond_signal(p -> cond);
            pthread_mutex_lock(e -> lock);
            pthread_mutex_unlock(p -> lock);

            pthread_cond_wait(e -> cond, e -> lock);
            pthread_mutex_unlock(e -> lock);

            dll_delete_node(item);
            item = next;
        }
        if(e -> door_open) close_door(e);

        move_to_floor(e, e -> onfloor + direction);

        /* drop people off */
        item = dll_first(e -> people);
        while(!dll_empty(e -> people) && item != dll_nil(e -> people)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            if(p -> to == e -> onfloor){
               if(!e -> door_open) open_door(e);
                
               pthread_mutex_lock(p -> lock);
               pthread_cond_signal(p -> cond);
               pthread_mutex_lock(e -> lock);
               pthread_mutex_unlock(p -> lock);

               pthread_cond_wait(e -> cond, e -> lock);
               pthread_mutex_unlock(e -> lock);
            }

            item = next;
        }
        //if(e -> door_open) close_door(e);

    }

    return NULL;
}
Ejemplo n.º 21
0
int main ( void ) {
    DlList_T myList;

    myList = dll_create();
    if( myList == 0 ) {
        fputs( "Cannot create list!\n", stderr );
        return( 1 );
    }
    printf( "Initial list is %s\n", 
        dll_empty( myList ) ? "empty" : "not empty" );

    char* one = (char*)malloc( 11 * sizeof(char) );
    char* two = (char*)malloc( 12 * sizeof(char) );
    char* three = (char*)malloc( 11 * sizeof(char) );
    strcpy( one, "First Line" );
    strcpy( two, "Second Line" );
    strcpy( three, "Third Line" );

    printf( "Checking cursor initialized null...\n");
    if( dll_has_next( myList ) ) {
        printf( "Your possition is valid\n" );
    } else {
        printf( "Your possition is NOT valid\n" );
    }

    // Test append
    printf( "List size: %d\n", dll_size( myList ) );

    printf( "Adding \"%s\"\n", one );
    dll_append( myList, one );
    printf( "List size: %d\n", dll_size( myList ) );

    printf( "Adding \"%s\"\n", two );
    dll_append( myList, two );
    printf( "List size: %d\n", dll_size( myList ) );

    printf( "Adding \"%s\"\n", three );
    dll_append( myList, three );
    printf( "List size: %d\n", dll_size( myList ) );

    printf( "Checking cursor fixed with appends...\n");
    if( dll_has_next( myList ) ) {
        printf( "Your possition is valid\n" );
    } else {
        printf( "Your possition is NOT valid\n" );
    }

    printf( "Test cursor movement...\n" );
    if( dll_move_to( myList, 3 ) ) {
        printf( "You moved to an index you shouldn't be able to\n" );
    } else {
        printf( "You can't move the cursor to 3\n" );
    }

    if( dll_move_to( myList, 2 ) ) {
        printf( "moved to the last index\n" );
    } else { 
        printf( "movement problem to index 2\n" );
    }

    if( dll_move_to( myList, 0 ) ) {
        printf( "moved to the first index\n" );
    } else { 
        printf( "movement problem to index 0\n" );
    }

    printf( "Checking cursor still valid...\n" );
    if( dll_has_next( myList ) ) {
        printf( "Your possition is valid\n" );
    } else {
        printf( "Your possition is NOT valid\n" );
    }

    printf( "Print state and test dll_next:\n" );
    void* data = dll_next( myList );
    int index = 0;
    // Index 0
    printf( "[%d] \"%s\"\n", index, (char*)data );
    data = dll_next( myList );
    index++;
    // Index 1
    printf( "[%d] \"%s\"\n", index, (char*)data );
    data = dll_next( myList );
    index++;
    // Index 2
    printf( "[%d] \"%s\"\n", index, (char*)data );
    data = dll_next( myList );
    index++;
    // Index 3 (Should be the same as index 2 as it should not exist)
    printf( "[%d] \"%s\"\n", index, (char*)data );
    data = dll_next( myList );

    printf( "Lets work backwards:\n" );
    data = dll_prev( myList );
    index = dll_size( myList ) - 1;
    // Index 2
    printf( "[%d] \"%s\"\n", index, (char*)data );
    data = dll_prev( myList );
    index--;
    // Index 1
    printf( "[%d] \"%s\"\n", index, (char*)data );
    data = dll_prev( myList );
    index--;
    // Index 0
    printf( "[%d] \"%s\"\n", index, (char*)data );
    data = dll_prev( myList );
    index--;
    // Index -1 (Should be same as index 0 as it should not exist)
    printf( "[%d] \"%s\"\n", index, (char*)data );
    data = dll_prev( myList );

    char* four = (char*)malloc( 12 * sizeof(char) );
    char* five = (char*)malloc( 11 * sizeof(char) );
    char* six = (char*)malloc( 11 * sizeof(char) );
    char* seven = (char*)malloc( 13 * sizeof(char) );
    char* eight = (char*)malloc( 12 * sizeof(char) );
    strcpy( four, "Fourth Line" );
    strcpy( five, "Fifth Line" );
    strcpy( six, "Sixth Line" );
    strcpy( seven, "Seventh Line" );
    strcpy( eight, "Eighth Line" );

    printf( "Testing inserts\n" );
    dll_insert_at( myList, 0, six );
    printf( "List size: %d\n", dll_size( myList ) );

    dll_insert_at( myList, 2, seven );
    printf( "List size: %d\n", dll_size( myList ) );

    dll_insert_at( myList, 4, eight );
    printf( "List size: %d\n", dll_size( myList ) );

    printf( "Test full print and check inserts\n" );
    index = 0;
    data = dll_get( myList, index );
    while( data != NULL ) {
        printf( "[%d] \"%s\"\n", index, (char*)data );
        index++;
        data = dll_get( myList, index );
    }

    printf( "Test Sets\n" );
    data = dll_set( myList, 0, five );
    printf( "Switched \"%s\" with \"%s\"\n", (char*)data, five );
    free( data );

    data = dll_set( myList, 2, four );
    printf( "Switched \"%s\" with \"%s\"\n", (char*)data, four );
    free( data );

    printf( "Test full print and check sets\n" );
    index = 0;
    data = dll_get( myList, index );
    while( data != NULL ) {
        printf( "[%d] \"%s\"\n", index, (char*)data );
        index++;
        data = dll_get( myList, index );
    }
    
    printf( "Testing popping\n" );
    data = dll_pop( myList, dll_size( myList ) -1 );
    printf( "Last element is: \"%s\"\n", (char*)data );
    free( data );

    data = dll_pop( myList, 2 );
    printf( "Third element is: \"%s\"\n", (char*)data );
    free( data );

    printf( "Poping the rest...\n");
    index = 0;
    data = dll_pop( myList, 0 );
    while( data != NULL ) {
        printf( "[%d] \"%s\"\n", index, (char*)data );
        free( data );
        index++;
        data = dll_pop( myList, 0 );
    }

    printf( "Destroying\n" );
    dll_destroy( myList );
}
Ejemplo n.º 22
0
static UINT32 wdbHwBpAdd
    (
    WDB_EVTPT_ADD_DESC *	pBreakPoint,	/* breakpoint to add */
    UINT32 *			pId		/* breakpoint ID */
    )
    {
    BRKPT *	pBp;
    dll_t *	pDll;
    int		status;
    DBG_REGS	dbgRegs;		/* debug registers */
    int		contextId;		/* context ID */
    UINT32	addr;			/* breakpoint address */
    UINT32	count = 0;		/* breakpoint count */
    int		type = DEFAULT_HW_BP;	/* hardware type */

    switch (pBreakPoint->numArgs)
	{
	default:
	case 3:
	    type = pBreakPoint->args[2];
	    /* FALL THROUGH */
	case 2:
	    count = pBreakPoint->args[1];
	    /* FALL THROUGH */
	case 1:
	    addr = pBreakPoint->args[0];
	    break;
	case 0:
	    return (WDB_ERR_INVALID_PARAMS);
	}

    /* check validity of hardware breakpoint address */

    if (wdbDbgHwAddrCheck (addr, type, (FUNCPTR) pWdbRtIf->memProbe) != OK)
        return (WDB_ERR_MEM_ACCES);

    /* check the agent mode */

    switch (pBreakPoint->context.contextType)
	{
	case WDB_CTX_SYSTEM:
	    if (!wdbIsNowExternal())
		return (WDB_ERR_AGENT_MODE);
	    break;

	default:
	    if (!wdbIsNowTasking())
		return (WDB_ERR_AGENT_MODE);
	}

    /* set the context ID */

    switch (pBreakPoint->context.contextType)
        {
        case WDB_CTX_SYSTEM:
            contextId = BP_SYS;
            break;
        case WDB_CTX_ANY_TASK:
            contextId = BP_ANY_TASK;
            break;
        case WDB_CTX_TASK:
        default:
            contextId = pBreakPoint->context.contextId;
        }

    /* clean dbgRegs structure */

    memset (&dbgRegs, 0, sizeof (DBG_REGS));

    /* fill dbgRegs structure with all hardware breakpoints */

    wdbTaskLock ();	/* disable task switching */
    for (pDll = dll_head(&bpList); pDll != dll_end(&bpList);
		pDll = dll_next(pDll))
	{
	pBp = BP_BASE(pDll);

	/* check if found breakpoint is applicable to new breakpoint context */

	if (((contextId == BP_SYS) && (pBp->bp_task == BP_SYS)) ||
		((contextId == BP_ANY_TASK) && (pBp->bp_task != BP_SYS)) ||
		((contextId != BP_SYS) && (pBp->bp_task == BP_ANY_TASK)))
	    {
	    if (pBp->bp_flags & BRK_HARDWARE)
		{
		if ((status = wdbDbgHwBpSet (&dbgRegs, 
				pBp->bp_flags & BRK_HARDMASK, 
				(UINT32) pBp->bp_addr)) != OK)
		    {
		    wdbTaskUnlock ();	/* re-enable task switching */
		    return (status);
		    }
		}
	    }
	}
    wdbTaskUnlock ();	/* re-enable task switching */

    if ((status = wdbDbgHwBpSet (&dbgRegs, type, addr)) != OK)
	return (status);

    if (dll_empty (&bpFreeList))
	return (WDB_ERR_EVENTPOINT_TABLE_FULL);

    wdbTaskLock ();		/* disable task switching */
    pBp = BP_BASE(dll_tail (&bpFreeList));
    dll_remove (&pBp->bp_chain);
    wdbTaskUnlock ();	/* re-enable task switching */

    pBp->bp_flags = BP_HOST | type | BRK_HARDWARE;
    pBp->bp_addr = (INSTR *)addr;
    pBp->bp_action = pBreakPoint->action.actionType;
    pBp->bp_count = count;
    pBp->bp_callRtn = (void (*)())pBreakPoint->action.callRtn;
    pBp->bp_callArg = pBreakPoint->action.callArg;
    pBp->bp_task = contextId;

    /* 
     * XXX - MS hack because host tools pass wrong info.
     * XXX - DBT This has been corrected in tornado 2.0 host tools but we
     * must keep this hack for backward compatibility.
     */

    if ((pBp->bp_action == 0) || (pBp->bp_action == WDB_ACTION_STOP))
	pBp->bp_action = WDB_ACTION_STOP | WDB_ACTION_NOTIFY;

    wdbTaskLock ();		/* disable task switching */
    dll_insert(&pBp->bp_chain, &bpList);
    wdbTaskUnlock ();		/* re-enable task switching */

    if (pBreakPoint->context.contextType != WDB_CTX_SYSTEM)
	if (_wdbTaskBpAdd != NULL)
	    _wdbTaskBpAdd (pBreakPoint);

    *pId = (UINT32)pBp;
    return (WDB_OK);
    }
Ejemplo n.º 23
0
static UINT32 wdbBpAdd
    (
    WDB_EVTPT_ADD_DESC *	pBreakPoint,
    UINT32 *			pId
    )
    {
    INSTR	val;
    BRKPT *	pBp;
    INSTR *	addr;

    switch (pBreakPoint->numArgs)
	{
	default:
	case 1:
#if ((CPU_FAMILY == ARM) && ARM_THUMB)
	    addr = (INSTR *) ((UINT32)((pBreakPoint->args[0]) & ~1));
#else /* CPU_FAMILY == ARM */
	    addr = (INSTR *) pBreakPoint->args[0];
#endif /* CPU_FAMILY == ARM */
	    break;
	case 0:
	    return (WDB_ERR_INVALID_PARAMS);
	}

    /* check validity of breakpoint address */

    TEXT_UNLOCK(addr);

    if (((*pWdbRtIf->memProbe) ((char *)addr, VX_READ,
				sizeof(INSTR), (char *)&val) != OK) ||
        ((*pWdbRtIf->memProbe) ((char *)addr, VX_WRITE,
				sizeof(INSTR), (char *)&val) != OK))
	{
	TEXT_LOCK(addr);
        return (WDB_ERR_MEM_ACCES);
	}

    TEXT_LOCK(addr);

    /* check the agent mode */

    switch (pBreakPoint->context.contextType)
	{
	case WDB_CTX_SYSTEM:
	    if (!wdbIsNowExternal())
		return (WDB_ERR_AGENT_MODE);
	    break;
	default:
	    if (!wdbIsNowTasking())
		return (WDB_ERR_AGENT_MODE);
	}



    if (dll_empty (&bpFreeList))
	return (WDB_ERR_EVENTPOINT_TABLE_FULL);

    wdbTaskLock ();	/* disable task switching */
    pBp = BP_BASE(dll_tail (&bpFreeList));
    dll_remove (&pBp->bp_chain);
    wdbTaskUnlock ();	/* re-enable task switching */

    pBp->bp_flags = BP_HOST;
    pBp->bp_addr = addr;
    pBp->bp_action = pBreakPoint->action.actionType;
#if ((CPU_FAMILY == ARM) && ARM_THUMB)
    pBp->bp_callRtn = (void (*)())((UINT32)pBreakPoint->action.callRtn | 1);
#else /* CPU_FAMILY == ARM */
    pBp->bp_callRtn = (void (*)())pBreakPoint->action.callRtn;
#endif /* CPU_FAMILY == ARM */
    pBp->bp_callArg = pBreakPoint->action.callArg;
    pBp->bp_instr = *(INSTR *)addr;

    if (pBreakPoint->numArgs > 1)	/* second argument is count */
   	pBp->bp_count = pBreakPoint->args[1];
    else
   	pBp->bp_count = 0;

    /* XXX - hack because host tools pass wrong info */

    if ((pBp->bp_action == 0) || (pBp->bp_action == WDB_ACTION_STOP))
	pBp->bp_action = WDB_ACTION_STOP | WDB_ACTION_NOTIFY;

    /* set the context ID */

    switch (pBreakPoint->context.contextType)
        {
        case WDB_CTX_SYSTEM:
	    pBp->bp_task = BP_SYS;
            break;
        case WDB_CTX_ANY_TASK:
	    pBp->bp_task = BP_ANY_TASK;
            break;
        case WDB_CTX_TASK:
        default:
	    pBp->bp_task = pBreakPoint->context.contextId;
        }

    wdbTaskLock ();		/* disable task switching */
    dll_insert(&pBp->bp_chain, &bpList);
    wdbTaskUnlock ();		/* re-enable task switching */

    if (pBreakPoint->context.contextType != WDB_CTX_SYSTEM)
	if (_wdbTaskBpAdd != NULL)
	    _wdbTaskBpAdd (pBreakPoint);

    *pId = (UINT32)pBp;
    return (WDB_OK);
    }
Ejemplo n.º 24
0
int emptyStack(Stack s) {

	return dll_empty(s);
}
Ejemplo n.º 25
0
int emptyQueue(Queue q) {
	return dll_empty(q);
}
Ejemplo n.º 26
0
void
KtSched()
{
	K_t kt;
	JRB jb;
	unsigned int sp;
	unsigned int now;
        JRB tmp;
        Dllist dtmp;

	/*
	 * start by recording the current stack contents in case
	 * I'm descheduled
	 *
	 * this is where I return to when I'm rescheduled
	 */
	if(setjmp(ktRunning->jmpbuf) != 0)
	{
		FreeFinishedThreads();
		/*
		 * if we are being killed by another thread, jump through
		 * the exitbuf
		 */
		if(ktRunning->die_now)
		{
                        /* Jim: This used to longjmp to the exitbuf,
                                but I changed it for two reasons:  
                                1. It wasn't being removed from ktActive
                                2. Hell will be paid if it is ktOriginal.
                                I believe kt_exit() is cleaner.  I
                                have not tested it.  I should.  */
			kt_exit();
			/* not reached */
		}
		return;
	}


start:

        if (!jrb_empty(ktSleeping)) {
  	  now = time(0);
	  while(!jrb_empty(ktSleeping))
	  {
	  	kt = (K_t) jval_v(jrb_val(jrb_first(ktSleeping)));
		if(kt->wake_time > now)
		{
			break;
		}
		WakeKThread(kt);
          }
	}
			


	/*
	 * if there is nothing left to run, exit.  However, if there 
         * are sleepers or a joinall, deal with them appropriately
	 */
	if(dll_empty(ktRunnable))
	{

		/*
		 * first, check for sleepers and deal with them
		 */
		if(!jrb_empty(ktSleeping))
		{
			kt = jval_v(jrb_val(jrb_first(ktSleeping)));
			sleep(kt->wake_time - now);
			goto start;
		}

		/*
		 * next, see if there is a joinall thread waiting
		 */
		jb = jrb_find_int(ktBlocked,0);
		if(jb != NULL)
		{
			WakeKThread((K_t)jval_v(jrb_val(jb)));
			goto start;
		}

		if(!jrb_empty(ktBlocked)) 
		{
			if(Debug & KT_DEBUG)
			{
				fprintf(stderr,
					"All processes blocked, exiting\n");
				fflush(stderr);
			}
			exit(1);
		} else {
			if(Debug & KT_DEBUG)
			{
				fprintf(stderr,
					"No runnable threads, exiting\n");
				fflush(stderr);
			}
			exit(0);
                }

                fprintf(stderr, "We shouldn't get here\n");
                exit(1);
	}

        /* Grab the first job of the ready queue */

        dtmp = dll_first(ktRunnable);
        kt = (K_t) dtmp->val.v;
	dll_delete_node(dtmp);

        /* If it is runnable, run it */

	if(kt->state == RUNNABLE) {

		ktRunning = kt;
		ktRunning->state = RUNNING;
		longjmp(ktRunning->jmpbuf,1);
                /* This doesn't return */
	}

	/*
	 * if we have never run before, set up initial stack and go
	 */
	if(kt->state == STARTING) {
		if(setjmp(kt->jmpbuf) == 0)
		{
			/*
			 * get double word aligned SP -- stacks grow from high
			 * to low
			 */
			sp = (unsigned int)&((kt->stack[kt->stack_size-1]));
			while((sp % 8) != 0)
				sp--;
#ifdef LINUX
			/*
			 * keep double word aligned but put in enough
			 * space to handle local variables for KtSched
			 */
			kt->jmpbuf->__jmpbuf[JB_BP] = (int)sp;
			kt->jmpbuf->__jmpbuf[JB_SP] = (int)sp-1024;
			PTR_MANGLE(kt->jmpbuf->__jmpbuf[JB_SP]);
#endif

#ifdef SOLARIS
			/*
			 * keep double word aligned but put in enough
			 * space to handle local variables for KtSched
			 */
			kt->jmpbuf[JB_FP] = (int)sp;
			kt->jmpbuf[JB_SP] = (int)sp-1024;
#endif
			/*
			 * set ktRunning while we still have local variables
			 */
			kt->state = RUNNING;
			ktRunning = kt;
			/*
			 * now jump onto the new stack
			 */
			longjmp(kt->jmpbuf,1);
		}
		else
		{
			/*
			 * here we are on a new, clean stack -- touch nothing,
			 * set the state, and call
			 *
			 * ktRunning is global so there is no local variable
			 * problem
			 *
			 * borrow this stack to try and free the last thread
			 * if there was one
			 */

			FreeFinishedThreads();

			if(setjmp(ktRunning->exitbuf) == 0)
			{
				/*
				 * if we were killed before we ran, skip the
				 * function call
				 */
				if(ktRunning->die_now == 0)
				{
					ktRunning->func(ktRunning->arg);
				}
			}


			/*
			 * we are back and this thread is done
			 *
			 * make it inactive
			 */

			jb = jrb_find_int(ktActive,ktRunning->tid);
			if(jb == NULL)
			{
				if(Debug & KT_DEBUG)
				{
					fprintf(stderr,
				"KtSched: panic -- inactive return\n");
					fflush(stderr);
				}
				exit(1);
			}
			jrb_delete_node(jb);

			/*
			 * look to see if there is a thread waiting for this
			 * one to exit -- careful with locals
			 */
			jb = jrb_find_int(ktBlocked,ktRunning->tid);
			if(jb != NULL)
			{
				WakeKThread((K_t)jval_v(jrb_val(jb)));
			}


			/*
			 * all we can do now is to commit suicide
			 *
			 * don't touch the locals;
			 *
			 * and don't free the stack we are running on
			 */
			FreeFinishedThreads();
                        ktRunning->state = DEAD;
			dll_append(ktFree_me,new_jval_v(ktRunning));
			ktRunning = NULL;
			goto start;
		}
	}

        /* The only way we get here is if there was a thread on the
           runnable queue whose state was not RUNNABLE or STARTING.
           Flag that as an error */

        fprintf(stderr, 
                "Error: non-STARTING or RUNNABLE thread on the ready queue\n");
        exit(1);
}