Example #1
0
File: msg.c Project: herumi/kernel
void sys_send (struct msg *m, options_t options){

  if(get_current_tid() == Posts[post].owner){

  if(Threads[Posts[m->dest].owner].vm == Threads[Posts[m->from].owner].vm){
    if(m->dest > POST_NUM_MAX || Posts[m->dest].owner == 0){
      // TODO: send a error msg
    }

    prepend_to_list(Posts[m->dest].received, m);
    wake_thread(Posts[m->dest].owner);

  }else{
    // TODO: move a page of a message to dest
    if(Posts[m->dest].handler == NULL){
      // TODO: prepend_to_list(Posts[m->dest].received, m);
    }else{
      // TODO: create thread and call handler
    }
  }
}


struct msg *sys_recv (post_id_t post, options_t options){
  struct msg *m;

  if(get_current_tid() != Posts[post].owner)
    return NULL;

  if(is_list_empty(Posts[post].received)){
    if(options & MSG_NOWAIT)
      return NULL;

    // wait for a message
    sleep_thread(Posts[m->dest].owner);
  }

  Posts[post].received = pop_from_list(Posts[post].received, (void *) &m);
  return m;
}


void sys_await (post_id_t post, void (*handler)(struct msg *m), uintmax_t max_thread_num){

  if(get_current_tid() != Posts[post].owner)
    return;

  Posts[post].handler = handler;
  Posts[post].max_thread_num = max_thread_max;
}


struct msg *compose (size_t body_size){

  return allocate_memory_block(sizeof(struct msg) + body_size);
}


void discard (struct msg *m){

  free_memory_block(m);
}
Example #2
0
void* tty2_thread()
{
	while(1)
	{
		FILE* f;
		char fileName[30];
	
		int numlines = 0;
		int i; //Standard iterator.

		fprintf(tty2, "in: ");
		fscanf(tty2, "%s", fileName);
	
		f = fopen(fileName, "r");

		if(!f)
		{
			printf("Arquivo inválido!\n");
			exit(-1);
		}
	
		p2->tty = tty2;
		p2->ready = 0;
		
		pthread_mutex_lock(&allocatorMutex);
			if(allocate_block() == OUT_OF_MEMORY)
			{
				printf("SEM MEMÓRIA\n");
				continue;
			}
		pthread_mutex_unlock(&allocatorMutex);
			
		//Carregar programa na memória.
		while(!feof(f))
		{
			fscanf(f, "%d %d", &mem[numlines + p2->cs].inst, &mem[numlines + p2->cs].op);
			printf("inst= %d\t\top= %d\n", mem[numlines + p2->cs].inst, mem[numlines + p2->cs].op);
			numlines++;
		}
		
		p2->ready = 1;
		printf("Rodando!\n\n");
	
/*		while(1)
		{
			//MUTEX
			pthread_mutex_lock(&runningMutex);
				if(running == p2)
				{
					printf("PC: %d\t CS: %d\t DS: %d\t ACC: %d\n", running->pc, running->cs, running->ds, running->acc);
					if(run_line() == PROG_END) break;
					running->pc++;
				}
			pthread_mutex_unlock(&runningMutex);
			//MUTEX END
		}
		//Terminou execução
*/
		while(p2->ready == 1) {} //Fica parado enquanto o processo está em execução.
		
		pthread_mutex_lock(&allocatorMutex);
			if(p2->ready == 0)
			{
				if(free_memory_block(running->block) == UNALLOCATED_MEM)
				{
					printf("Memória já foi desalocada. Algo muito errado aconteceu.\n");
					continue;
				}
			}
		pthread_mutex_unlock(&allocatorMutex);
	}
}