void ft_think(t_env *e, int l, int i) { int c; int rt; c = 0; rt = THINK_T * TT; e->i_state[i] = 0; e->state[i] = ft_strdup(THINK); while (c < rt) { if (ft_is_dead(e) != 0) e->roll = 3; if ((c % TT) == 0) e->hp[i] -= 1; if (e->i_state[NEXT(i)] == 1 || e->i_state[PREV(i)] == 1) { UNLOCK(&e->lock[l]); return (ft_rest(e, i, c)); } usleep(TS); if (e->roll == 3) break ; c++; } UNLOCK(&e->lock[l]); }
int get_next_line(int const fd, char **line) { static t_list *fds = NULL; t_fd *actual; int read; if (!line || fd < 0) return (-1); actual = actual_fd(fd, &fds); if (actual->rest && ft_rest(actual, line)) return (1); if ((read = ft_read(fd, actual, line)) != 0) return (read); if (actual->rest == NULL || actual->rest[0] == '\0') return (0); *line = actual->rest; actual->rest = NULL; return (1); }
static int ft_read(int fd, t_fd *actual, char **line) { char buf[BUFF_SIZE + 1]; int ret; while ((ret = read(fd, buf, BUFF_SIZE))) { if (ret == -1) return (-1); buf[ret] = '\0'; if (actual->rest) actual->rest = ft_strjoin_free(actual->rest, buf, 1); else actual->rest = ft_strdup(buf); if (ft_rest(actual, line)) return (1); } return (0); }
void ft_eat(t_env *e, int l, int r, int i) { int t; int c; c = 0; e->i_state[i] = 0; e->state[i] = ft_strdup(EAT); e->hp[i] = MAX_LIFE; while (c < EAT_T) { if (e->roll == 3) break ; t = time(NULL); ft_sleep(1, t); c++; } UNLOCK(&e->lock[l]); UNLOCK(&e->lock[r]); if (e->roll != 3) ft_rest(e, i, 0); }
void *ft_alg(void *p) { t_env *e; int i; e = (t_env *)p; i = e->id; while (e->roll != 3) { never_die(e, i); if (!ft_strcmp(e->state[NEXT(i)], THINK) && !ft_strcmp(e->state[PREV(i)], THINK)) try_both(e, i); else if (TRY(&e->lock[FI]) == 0) ft_try(e, FI, NI, i); else if (TRY(&e->lock[NI]) == 0) ft_try(e, NI, FI, i); else ft_rest(e, i, 0); } if (pthread_detach(e->th[i])) ft_putendl("Error while detaching thread"); return (p); }