Exemplo n.º 1
0
int dispatch_add_edge(char *params, network *net) {
	char *first_param = NULL, *second_param = NULL, *third_param;
	if (params[0] == '\0')
		send_error(24);
	else if (!isspace(params[0]))
		send_error(3);
	else if (check_and_get_three_params(params + 1, &first_param, &second_param,
			&third_param) == FALSE) {
		send_error(24);
		return 1;
	} else if ((check_string_is_non_negative_integer(first_param) == FALSE)
			|| (check_string_is_non_negative_integer(second_param) == FALSE)) {
		send_error(5);
		return 1;
	} else if (!check_string_is_double(third_param)) {
		send_error(-4);
		return 1;
	} else {
		int first_id = atoi(first_param);
		int second_id = atoi(second_param);
		double weight = strtod(third_param, NULL);
		if ((is_legal_id(first_id, net) == FALSE)
				|| (is_legal_id(second_id, net) == FALSE)) {
			send_error(7);
			return 1;
		} else if (first_id == second_id) {
			send_error(8);
			return 1;
		} else {
			return add_edge(first_id, second_id, weight, net);
		}
	}

	return 1;
}
Exemplo n.º 2
0
int
binary_semaphore_create(int initial_value)
{
	if(!initialized)
		init_bsemaphores();
	
	if(!is_legal_id(next_bsemaphore))
		panic("binary semaphore overflow!");
	
	acquire(&list_lock);
	struct binary_semaphore *s = &bsemaphores[next_bsemaphore];
	
	initlock(&s->lock, "bsemaphore_lock");
	acquire(&s->lock);
	s->id=next_bsemaphore;
	int id=next_bsemaphore;
	if(initial_value<=0)
		s->value=0;
	else
		s->value=1;
	init_queue(&s->waiting);
	release(&s->lock);
	
	next_bsemaphore++;
	release(&list_lock);
	
	return id;
}
Exemplo n.º 3
0
int
binary_semaphore_down(int s_id)
{
	acquire(&list_lock);
	if(!is_legal_id(s_id) || !is_semaphore_init(s_id))
		return -1;
	
	struct binary_semaphore *s = &bsemaphores[s_id];
	
	acquire(&s->lock);
	
	if(s->value <= 0){
		// Already locked
		if(enque(proc, &s->waiting)<0){
			// enque fails
			release(&s->lock);
			release(&list_lock);
			//cprintf("[err %d %d]", proc->pid, s_id);
			return -1;
		}

		//release(&s->lock);
		//cprintf("[sleep %d %d]", proc->pid, s_id);
		release(&list_lock);
		sleep(proc, &s->lock);
		release(&s->lock);
		return 0;
	}
	//cprintf("[lock %d %d]", proc->pid, s_id);
	s->value=0;
	release(&s->lock);
	release(&list_lock);
	return 0;
}
Exemplo n.º 4
0
int
binary_semaphore_up(int s_id)
{
	acquire(&list_lock);
	if(!is_legal_id(s_id) || !is_semaphore_init(s_id))
		return -1;
	
	struct binary_semaphore *s = &bsemaphores[s_id];
	
	acquire(&s->lock);
	
	void *p=0;
	if(deque(&s->waiting, &p)<0){
		// no waiting procs
		//cprintf("[unlk %d %d]", proc->pid, s_id);
		s->value=1;
		release(&s->lock);
		release(&list_lock);
		return 0;
	}
	
	if(((struct proc*) p)->tid == proc->tid){
		s->value=1;
		//cprintf("[self %d %d]", proc->pid, s_id);
		release(&s->lock);
		release(&list_lock);
		return binary_semaphore_up(s_id);
	}
	//release(&s->lock);
	s->value=0;
	//cprintf("[wake %d %d]", ((struct proc*) p)->pid, s_id);
	wakeup((struct proc*) p);
	release(&s->lock);
	release(&list_lock);
	return 0;
}