aboutsummaryrefslogtreecommitdiff
path: root/philo_one/philo.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-10 00:34:47 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-10 01:30:53 +0100
commit1a7c6c5a0ed9a5aae1fe45c3af335c120c2dc642 (patch)
tree965aa21be5411d2a9e1156013efc1980bca29f90 /philo_one/philo.c
parentdfbafce6506fe5c47b0e60289a9d4629e502c9ac (diff)
downloadphilosophers-1a7c6c5a0ed9a5aae1fe45c3af335c120c2dc642.tar.gz
philosophers-1a7c6c5a0ed9a5aae1fe45c3af335c120c2dc642.tar.bz2
philosophers-1a7c6c5a0ed9a5aae1fe45c3af335c120c2dc642.zip
WIP: philo one
Diffstat (limited to 'philo_one/philo.c')
-rw-r--r--philo_one/philo.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/philo_one/philo.c b/philo_one/philo.c
new file mode 100644
index 0000000..7cc3ca4
--- /dev/null
+++ b/philo_one/philo.c
@@ -0,0 +1,85 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* philo.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */
+/* Updated: 2020/02/10 01:29:25 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "philo_one.h"
+
+t_philo *philos_new(int num)
+{
+ int i;
+ t_philo *philos;
+
+ if (num < 0)
+ return (NULL);
+ if ((philos = (t_philo*)h_calloc(num + 1, sizeof(t_philo))) == NULL)
+ return (NULL);
+ i = -1;
+ while (++i < num)
+ {
+ philos[i].id = i + 1;
+ philos[i].alive = FALSE;
+ }
+ return (philos);
+}
+
+void philos_destroy(t_philo *philos, int num)
+{
+ (void)num;
+ if (philos == NULL)
+ return ;
+ free(philos);
+}
+
+t_bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num)
+{
+ t_routine_arg *routine_arg;
+ int i;
+
+ i = -1;
+ while (++i < num)
+ {
+ philos[i].alive = TRUE;
+ if (pthread_create(philos[i].thread, NULL, philo_one_routine, (void*)(routine_args + i)) == -1)
+ return (FALSE);
+ }
+ return (TRUE);
+}
+
+void philos_join(t_philo *philos, int num)
+{
+ int i;
+
+ i = -1;
+ while (++i < num)
+ {
+ if (philos[i].alive)
+ {
+ philos[i].alive = FALSE;
+ pthread_join(philos[i].thread, NULL);
+ }
+ }
+}
+
+t_bool philos_starved(t_philo *philos, int num)
+{
+ int i;
+
+ i = -1;
+ while (++i < num)
+ if (!philos[i].alive)
+ {
+ i = -1;
+ while (++i < num)
+ philos[i].alive = FALSE;
+ return (TRUE);
+ }
+ return (FALSE);
+}