Esempio n. 1
0
int ta_waitall() {
	struct node *mainthread_node = list_pop(ready);//The last item of the ready queue holds the currently running thread. Since the main thread is running and we wish it to go to sleep, we need to "pop" it off the ready queue, and give the CPU to the next thread in queue.
	if(list_empty(ready) && list_sem_all_empty(sem_list)){
		return 0;
	}
	
	if(list_empty(ready) && !list_sem_all_empty(sem_list)){
		return -1;
	}

	while(!list_empty(ready)){
		swapcontext(&mainthread, list_last(ready)->threadContext);
		list_delete(ready); 
	}

	list_append_node(mainthread_node, ready);//main thread is back in ready queue and running

	list_clear(ready);
	free(ready);

	if(list_sem_all_empty(sem_list)){
		list_sem_destroy_list(sem_list);
		return 0;
	}//end if
	else{
		list_sem_destroy_list(sem_list);
		return -1;
	}//end else
	
}
Esempio n. 2
0
void ta_signal(tacond_t *cond) {

	if(!list_empty((cond->partner_lock->binary_sem).queue)){
		struct node *waken = list_pop( (cond->partner_lock->binary_sem).queue );
		assert(waken != NULL);
		list_append_node(waken, ready);
	}//end if

}//end method
Esempio n. 3
0
void list_insert_node_sorted(list_t *list, list_node_t *node) {
	list_node_t *each_node;
	
	if ( list->count == 0 ) {
		list_append_node(list, node);
	}
	else {
		for ( each_node = list->first; each_node; each_node = each_node->next) {
			if ( each_node->count >= node->count ) {
				list_insert_node_before_node(list, node, each_node);
				
				return;
			}
		}
		
		list_append_node(list, node);
	}
}
Esempio n. 4
0
void ta_sem_post(tasem_t *sem) {

	(sem->value)++; //increment the semaphore value by 1
	if(!list_empty(sem->queue)){
		struct node *waken = list_pop(sem->queue);
		assert(waken != NULL);
		list_append_node(waken, ready);
	}//end if
	
}
Esempio n. 5
0
void wakeup_queue(struct list_node *queue)
{
	struct list_node *ctr;
	/* move each element on the wait queue to the run queue */
	/* FIXME: this can probably be optimized */
	while((ctr = queue->next) != queue) {
		list_del_node(ctr);
		list_append_node(&run_list, ctr);
	}
	schedule();	
}
Esempio n. 6
0
void ta_yield() {

	struct node *yielded = list_pop(ready);

	if(yielded == NULL){
		return;
	}

	if(list_empty(ready)){//if there is no thread waiting to run, the calling thread Of ta_yield() continues running 
		list_append_node(yielded, ready);
		return;
	}

	yielded->threadContext->uc_link = &mainthread;
	list_append_node(yielded, ready);
	assert(!list_empty(ready));
	swapcontext(yielded->threadContext, list_last(ready)->threadContext);

    return;
}
Esempio n. 7
0
/**
 * Try to get "desired_ntokens" number of tokens from the given
 * filestream f.  Store them (destructively) in the given list
 * "tokens" of dynamically created extstring_t objects.  Store in
 * "gotten_ntokens" the actual number of tokens that we got. 
 */
int
bebop_get_tokens (list_t* tokens, 
		 int* gotten_ntokens, 
		 FILE* f,
		 const int desired_ntokens,
		 const char delimiters[],
		 const int ndelimiters)
{
  list_t current = *tokens;
  list_t outlist = *tokens;

  if (desired_ntokens < 0)
    {
      bebop_error ("bebop_get_tokens", 
		  "desired_ntokens = %d < 0", 
		  desired_ntokens);
      return -1;
    }
  for (*gotten_ntokens = 0; 
       *gotten_ntokens < desired_ntokens;
       (*gotten_ntokens)++)
    {
      extstring_t* token = NULL;
      int result = 0;

      if (list_empty_p (current))
	  /* We're out of nodes to reuse, so we have to create a new
	     extstring_t */
	token = extstring_create (0);
      else
	token = list_car (current);

      /* Try to get the next token */
      result = bebop_next_token (token, f, delimiters, ndelimiters);
      if (result != 0)
	{
	  extstring_destroy (token);
	  break;
	}
      else
	{
	  if (list_empty_p (current))
	    outlist = list_append_node (outlist, token);
	  else
	    {
	      list_set_car (current, token);
	      current = list_cdr (current);
	    }
	}
    }
  return 0;
}
Esempio n. 8
0
int
list_append_data(struct list *l, void *data)
{
	assert(l);
	assert(data);

	struct node *n = node_new(data);
	if (NULL == n) return -1;

	list_append_node(l, n);

	return 0;
}
Esempio n. 9
0
void ta_wait(talock_t *mutex, tacond_t *cond) {

	assert(mutex != NULL);
	assert(cond != NULL);

	cond->partner_lock = mutex;
	struct node *current_thread = list_pop(ready);
	assert(current_thread != NULL);
	list_append_node(current_thread, mutex->binary_sem.queue);
	ta_unlock(mutex);

	if(!list_empty(ready)){
		swapcontext(current_thread->threadContext, list_last(ready)->threadContext);
	}
	else{
		swapcontext(current_thread->threadContext, &mainthread);
	}
	ta_lock(mutex);
}
Esempio n. 10
0
void ta_sem_wait(tasem_t *sem) {

	(sem->value)--;
	if(sem->value >= 0){
		// if the semaphore's value is greater or equal to 0 after decrementing, do nothing and allow the thread to keep running
	}//end if
	else{ //else, put the thread to sleep
		struct node *sleep = list_pop(ready);
		assert (sleep != NULL);
		list_append_node(sleep, sem->queue);
		ucontext_t current_context;
		if(!list_empty(ready)){ // if there is a thread waiting to be run, run that thread
			swapcontext(&current_context, list_last(ready)->threadContext);
		}//end inner if
		else{ //there is no thread to be run, go back to main thread
			swapcontext(&current_context, &mainthread);
		}//end inner else
	}//end else

}