/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2022 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
#include "libc/runtime/internal.h"
#include "libc/stdio/lock.internal.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#include "third_party/nsync/mu.h"

int Ai;
const char *A[16];

// clang-format off
void prepare1(void) { A[Ai++] = "prepare1"; }
void prepare2(void) { A[Ai++] = "prepare2"; }
void parent1(void) { A[Ai++] = "parent1"; }
void parent2(void) { A[Ai++] = "parent2"; }
void child1(void) { A[Ai++] = "child1"; }
void child2(void) { A[Ai++] = "child2"; }
// clang-format on

void *ForceThreadingMode(void *arg) {
  return 0;
}

TEST(pthread_atfork, test) {
  int pid;
  __enable_threads();
  SPAWN(fork);
  ASSERT_EQ(0, pthread_atfork(prepare1, parent1, child1));
  ASSERT_EQ(0, pthread_atfork(prepare2, parent2, child2));
  flockfile(stdout);
  SPAWN(fork);
  flockfile(stdout);
  ASSERT_STREQ("prepare2", A[0]);
  ASSERT_STREQ("prepare1", A[1]);
  ASSERT_STREQ("child1", A[2]);
  ASSERT_STREQ("child2", A[3]);
  funlockfile(stdout);
  EXITS(0);
  funlockfile(stdout);
  ASSERT_STREQ("prepare2", A[0]);
  ASSERT_STREQ("prepare1", A[1]);
  ASSERT_STREQ("parent1", A[2]);
  ASSERT_STREQ("parent2", A[3]);
  EXITS(0);
}

pthread_mutex_t mu;

void mu_lock(void) {
  pthread_mutex_lock(&mu);
}

void mu_unlock(void) {
  pthread_mutex_unlock(&mu);
}

void mu_funlock(void) {
  pthread_mutex_init(&mu, 0);
}

void *Worker(void *arg) {
  for (int i = 0; i < 20; ++i) {
    mu_lock();
    usleep(20);
    mu_unlock();
    SPAWN(fork);
    mu_lock();
    usleep(1);
    mu_unlock();
    mu_lock();
    mu_unlock();
    EXITS(0);
    mu_lock();
    mu_unlock();
  }
  return 0;
}

TEST(pthread_atfork, torture) {
  if (IsWindows()) return;  // TODO(jart): why do we get EBADF? it worked before
  pthread_mutex_init(&mu, 0);
  pthread_atfork(mu_lock, mu_unlock, mu_funlock);
  int i, n = 4;
  pthread_t *t = _gc(malloc(sizeof(pthread_t) * n));
  for (i = 0; i < n; ++i) {
    ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0));
  }
  for (i = 0; i < n; ++i) {
    ASSERT_EQ(0, pthread_join(t[i], 0));
  }
}
