/*-*- 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/struct/timespec.h"
#include "libc/errno.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#include "libc/thread/thread2.h"

// TODO(jart): Can we make this test go faster on NetBSD?

int pos;
int count;
int limit;
long data[1000];
pthread_mutex_t mu;
pthread_cond_t non_full;
pthread_cond_t non_empty;

int Put(long v, struct timespec *abs_deadline) {
  int err, added = 0, wake = 0;
  pthread_mutex_lock(&mu);
  while (count == limit) {
    if ((err = pthread_cond_timedwait(&non_full, &mu, abs_deadline))) {
      ASSERT_EQ(ETIMEDOUT, err);
      ASSERT_NE(NULL, abs_deadline);
    }
  }
  if (count != limit) {
    int i = pos + count;
    if (limit <= i) i -= limit;
    data[i] = v;
    if (count == 0) wake = 1;
    count++;
    added = 1;
  }
  pthread_mutex_unlock(&mu);
  if (wake) pthread_cond_broadcast(&non_empty);
  return added;
}

long Get(struct timespec *abs_deadline) {
  long err, v = 0;
  pthread_mutex_lock(&mu);
  while (!count) {
    if ((err = pthread_cond_timedwait(&non_empty, &mu, abs_deadline))) {
      ASSERT_EQ(ETIMEDOUT, err);
      ASSERT_NE(NULL, abs_deadline);
    }
  }
  if (count) {
    v = data[pos];
    data[pos] = 0;
    if (count == limit) {
      pthread_cond_broadcast(&non_full);
    }
    pos++;
    count--;
    if (pos == limit) {
      pos = 0;
    }
  }
  pthread_mutex_unlock(&mu);
  return v;
}

#define N 1000

void *Producer(void *arg) {
  for (int i = 0; i < N; i++) {
    ASSERT_EQ(1, Put((i + 1) * 3, 0));
  }
  return 0;
}

void *Consumer(void *arg) {
  for (int i = 0; i < N; i++) {
    ASSERT_EQ((i + 1) * 3, Get(0));
  }
  return 0;
}

TEST(cond, test) {
  pthread_t t;

  limit = 1;
  ASSERT_EQ(0, pthread_create(&t, 0, Producer, 0));
  Consumer(0);
  ASSERT_EQ(0, pthread_join(t, 0));

  limit = 10;
  ASSERT_EQ(0, pthread_create(&t, 0, Producer, 0));
  Consumer(0);
  ASSERT_EQ(0, pthread_join(t, 0));

  limit = 100;
  ASSERT_EQ(0, pthread_create(&t, 0, Producer, 0));
  Consumer(0);
  ASSERT_EQ(0, pthread_join(t, 0));

  limit = 1000;
  ASSERT_EQ(0, pthread_create(&t, 0, Producer, 0));
  Consumer(0);
  ASSERT_EQ(0, pthread_join(t, 0));
}
