From d7aea773431926cefb6430b948329da1662d1dee Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 30 Sep 2020 07:59:27 +0200 Subject: Refactoring common lib --- philo_one/Makefile | 18 ++++++--- philo_one/src/args.c | 32 --------------- philo_one/src/helper.c | 99 ----------------------------------------------- philo_one/src/io.c | 34 ++++++++-------- philo_one/src/philo_one.h | 50 +++++++----------------- 5 files changed, 43 insertions(+), 190 deletions(-) delete mode 100644 philo_one/src/args.c delete mode 100644 philo_one/src/helper.c (limited to 'philo_one') diff --git a/philo_one/Makefile b/philo_one/Makefile index da3f82e..091bd56 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,15 +6,15 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/09/29 11:30:07 by cacharle ### ########.fr # +# Updated: 2020/09/30 07:45:51 by cacharle ### ########.fr # # # # **************************************************************************** # RM = rm -f CC = gcc -CCFLAGS = -Wall -Wextra #-Werror -LDFLAGS = -lpthread +CCFLAGS = -I$(COMMONDIR) -Wall -Wextra #-Werror +LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon NAME = philo_one @@ -24,7 +24,7 @@ OBJDIR = obj SRC = $(shell find $(SRCDIR) -type f -name '*.c') OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) -all: prebuild $(NAME) +all: prebuild common_all $(NAME) prebuild: @mkdir -pv $(OBJDIR) @@ -35,7 +35,7 @@ $(NAME): $(OBJ) $(OBJDIR)/%.o: $(SRCDIR)/%.c $(CC) $(CCFLAGS) -c -o $@ $< -clean: +clean: common_clean $(RM) $(OBJ) fclean: clean @@ -43,4 +43,10 @@ fclean: clean re: fclean all -.PHONY: all prebuild clean fclean re +common_all: + $(MAKE) -C $(COMMONDIR) all + +common_clean: + $(MAKE) -C $(COMMONDIR) clean + +.PHONY: all prebuild clean fclean re common_all common_clean diff --git a/philo_one/src/args.c b/philo_one/src/args.c deleted file mode 100644 index b7277dd..0000000 --- a/philo_one/src/args.c +++ /dev/null @@ -1,32 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* args.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/09/29 11:08:06 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "philo_one.h" - -bool parse_args(t_philo_conf *philo_args, int argc, char **argv) -{ - if (argc != 5 && argc != 6) - return h_err(false, "Usage: %s philosophers_num death_timeout eat_timeout sleep_timeout [meal_num]", argv[0]); - if ((philo_args->philo_num = h_atou_strict(argv[1])) == -1 - || (philo_args->timeout_death = h_atou_strict(argv[2])) == -1 - || (philo_args->timeout_eat = h_atou_strict(argv[3])) == -1 - || (philo_args->timeout_sleep = h_atou_strict(argv[4])) == -1) - return (false); - if (argc == 6) - { - if ((philo_args->meal_num = h_atou_strict(argv[5])) == -1) - return (false); - } - else - philo_args->meal_num = 1; - return (true); -} diff --git a/philo_one/src/helper.c b/philo_one/src/helper.c deleted file mode 100644 index fa4c7ff..0000000 --- a/philo_one/src/helper.c +++ /dev/null @@ -1,99 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* helper.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2020/09/29 14:15:50 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "philo_one.h" - -static int h_strlen(char *s) -{ - int counter; - - counter = 0; - while (s[counter]) - counter++; - return (counter); -} - -long int h_atou_strict(char *s) -{ - long int num; - char *origin; - - origin = s; - if (*s < '0' || *s > '9') - return (h_err(-1, "Error: %s: is not a number", origin)); - num = 0; - while (*s >= '0' && *s <= '9') - { - num *= 10; - if (num > UINT_MAX) - return (h_err(-1, "Error: %s: is too big", origin)); - num += *s - '0'; - if (num > UINT_MAX) - return (h_err(-1, "Error: %s: is too big", origin)); - s++; - } - if (*s != '\0') - return (h_err(-1, "Error: %s: is not a number", origin)); - return (num); -} - -void h_putnbr(unsigned long int num) -{ - if (num > 9) - h_putnbr(num / 10); - h_putchar(num % 10 + '0'); -} - -void h_putchar(char c) -{ - write(STDOUT_FILENO, &c, 1); -} - -void h_putstr(char *s) -{ - write(STDOUT_FILENO, s, h_strlen(s)); -} - -int h_err(int ret, const char *format, ...) -{ - char *str; - va_list ap; - - va_start(ap, format); - while (*format != '\0') - { - if (format[0] == '%' && format[1] == 's') - { - str = va_arg(ap, char*); - if (str != NULL) - write(STDERR_FILENO, str, h_strlen(str)); - format += 2; - } - else - { - write(STDERR_FILENO, format, 1); - format++; - } - } - va_end(ap); - write(STDERR_FILENO, "\n", 1); - return (ret); -} - -t_time h_time_now(void) -{ - struct timeval tv; - - if (gettimeofday(&tv, NULL) == -1) - return (-1); - return (tv.tv_sec * 1000 + tv.tv_usec / 1000); -} diff --git a/philo_one/src/io.c b/philo_one/src/io.c index 96a8f07..6fb6424 100644 --- a/philo_one/src/io.c +++ b/philo_one/src/io.c @@ -6,28 +6,28 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2020/09/29 14:55:21 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 07:49:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -static void philo_put(int id, t_philo_event event) -{ - h_putnbr(h_time_now()); - h_putchar(' '); - h_putnbr(id); - if (event == EVENT_FORK) - h_putstr(" has taken fork\n"); - else if (event == EVENT_EAT) - h_putstr(" is eating\n"); - else if (event == EVENT_SLEEP) - h_putstr(" is sleeping\n"); - else if (event == EVENT_THINK) - h_putstr(" is thinking\n"); - else if (event == EVENT_DIE) - h_putstr(" died\n"); -} +/* static void philo_put(int id, t_philo_event event) */ +/* { */ +/* h_putnbr(h_time_now()); */ +/* h_putchar(' '); */ +/* h_putnbr(id); */ +/* if (event == EVENT_FORK) */ +/* h_putstr(" has taken fork\n"); */ +/* else if (event == EVENT_EAT) */ +/* h_putstr(" is eating\n"); */ +/* else if (event == EVENT_SLEEP) */ +/* h_putstr(" is sleeping\n"); */ +/* else if (event == EVENT_THINK) */ +/* h_putstr(" is thinking\n"); */ +/* else if (event == EVENT_DIE) */ +/* h_putstr(" died\n"); */ +/* } */ void io_eat(t_routine_arg *arg) { diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index e92fdcd..2c04316 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2020/09/29 14:15:47 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 07:48:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,24 +21,22 @@ # include # include -typedef long int t_time; +# include "common.h" -typedef enum -{ - EVENT_FORK, - EVENT_EAT, - EVENT_SLEEP, - EVENT_THINK, - EVENT_DIE -} t_philo_event; +// typedef long int t_time; + +// typedef enum +// { +// EVENT_FORK, +// EVENT_EAT, +// EVENT_SLEEP, +// EVENT_THINK, +// EVENT_DIE +// } t_philo_event; typedef struct { - int philo_num; - t_time timeout_death; - t_time timeout_eat; - t_time timeout_sleep; - int meal_num; + t_philo_args args; bool all_alive; pthread_mutex_t mutex_stdout; pthread_mutex_t mutex_all_alive; @@ -82,7 +80,7 @@ bool philos_start( t_philo *philos, t_routine_arg *routine_args, int num); -void philos_join(t_philo *philos, int num); +void philos_detach(t_philo *philos, int num); /* ** routine.c @@ -100,24 +98,4 @@ void io_think(t_routine_arg *arg); void io_sleep(t_routine_arg *arg); void io_die(t_routine_arg *arg); -/* -** common.c -*/ - -bool parse_args( - t_philo_conf *philo_args, - int argc, - char **argv); - -/* -** helper.c -*/ - -long int h_atou_strict(char *s); -void h_putnbr(unsigned long num); -void h_putchar(char c); -void h_putstr(char *s); -int h_err(int ret, const char *format, ...); -t_time h_time_now(void); - #endif -- cgit