aboutsummaryrefslogtreecommitdiff
path: root/philo_one/main.c
blob: 94ac0f71d7dd62b3d26a3296d0321d2afa8c6085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/11/24 05:53:02 by cacharle          #+#    #+#             */
/*   Updated: 2020/02/14 01:41:21 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "philo_one.h"


int	main(int argc, char **argv)
{
	t_philo_args	philo_args;
	t_philo			*philos;
	t_fork			*forks;
	t_routine_arg	*routine_args;
	/* mutex_t			mutex_stdout; */

	/* pthread_mutex_init(mutex_stdout); */
	if (!parse_args(&philo_args, argc, argv))
		return (1);
	if ((forks = forks_new(philo_args.philo_num)) == NULL)
		return (1);
	if ((philos = philos_new(philo_args.philo_num)) == NULL)
	{
		free(forks);
		return (1);
	}
	if ((routine_args = forks_dispatch(philos, forks, &philo_args)) == NULL)
	{
		free(philos);
		free(forks);
		return (1);
	}
	if (!philos_start(philos, routine_args, philo_args.philo_num))
	{
		free(philos);
		free(forks);
		free(routine_args);
		return (1);
	}
	while (TRUE)
		if (philos_starved(philos, philo_args.philo_num))
			break;
	philos_join(philos, philo_args.philo_num);
	free(routine_args);
	free(philos);
	forks_destroy(forks, philo_args.philo_num);
	return (0);
}