/*-*- 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.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#define EZBENCH_COUNT 10000
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/timespec.h"
#include "libc/intrin/atomic.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/spawn.h"
#include "libc/thread/thread.h"
#include "third_party/nsync/mu.h"

int THREADS = 16;
int ITERATIONS = 100;

int count;
atomic_int started;
atomic_int finished;
pthread_mutex_t lock;
pthread_mutexattr_t attr;

FIXTURE(pthread_mutex_lock, normal) {
  ASSERT_EQ(0, pthread_mutexattr_init(&attr));
  ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
  ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
  ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
}

FIXTURE(pthread_mutex_lock, recursive) {
  ASSERT_EQ(0, pthread_mutexattr_init(&attr));
  ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
  ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
  ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
}

FIXTURE(pthread_mutex_lock, errorcheck) {
  ASSERT_EQ(0, pthread_mutexattr_init(&attr));
  ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
  ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
  ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
}

////////////////////////////////////////////////////////////////////////////////
// TESTS

int MutexWorker(void *p, int tid) {
  int i;
  ++started;
  for (i = 0; i < ITERATIONS; ++i) {
    pthread_mutex_lock(&lock);
    ++count;
    sched_yield();
    pthread_mutex_unlock(&lock);
  }
  ++finished;
  return 0;
}

TEST(pthread_mutex_lock, contention) {
  int i;
  struct spawn *th = gc(malloc(sizeof(struct spawn) * THREADS));
  pthread_mutexattr_init(&attr);
  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
  pthread_mutex_init(&lock, &attr);
  pthread_mutexattr_destroy(&attr);
  count = 0;
  started = 0;
  finished = 0;
  for (i = 0; i < THREADS; ++i) {
    ASSERT_SYS(0, 0, _spawn(MutexWorker, (void *)(intptr_t)i, th + i));
  }
  for (i = 0; i < THREADS; ++i) {
    ASSERT_SYS(0, 0, _join(th + i));
  }
  EXPECT_EQ(THREADS, started);
  EXPECT_EQ(THREADS, finished);
  EXPECT_EQ(THREADS * ITERATIONS, count);
  EXPECT_EQ(0, pthread_mutex_destroy(&lock));
}

////////////////////////////////////////////////////////////////////////////////
// BENCHMARKS

void BenchSpinUnspin(pthread_spinlock_t *s) {
  pthread_spin_lock(s);
  pthread_spin_unlock(s);
}

void BenchLockUnlock(pthread_mutex_t *m) {
  pthread_mutex_lock(m);
  pthread_mutex_unlock(m);
}

void BenchLockUnlockNsync(nsync_mu *m) {
  nsync_mu_lock(m);
  nsync_mu_unlock(m);
}

BENCH(pthread_mutex_lock, bench_uncontended) {
  {
    pthread_spinlock_t s = {0};
    EZBENCH2("spin 1x", donothing, BenchSpinUnspin(&s));
  }
  {
    nsync_mu m = {0};
    EZBENCH2("nsync 1x", donothing, BenchLockUnlockNsync(&m));
  }
  {
    pthread_mutex_t m;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
    pthread_mutex_init(&m, &attr);
    EZBENCH2("normal 1x", donothing, BenchLockUnlock(&m));
  }
  {
    pthread_mutex_t m;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&m, &attr);
    EZBENCH2("recursive 1x", donothing, BenchLockUnlock(&m));
  }
  {
    pthread_mutex_t m;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
    pthread_mutex_init(&m, &attr);
    EZBENCH2("errorcheck 1x", donothing, BenchLockUnlock(&m));
  }
}

struct SpinContentionArgs {
  pthread_spinlock_t *spin;
  atomic_char done;
  atomic_char ready;
};

int SpinContentionWorker(void *arg, int tid) {
  struct SpinContentionArgs *a = arg;
  while (!atomic_load_explicit(&a->done, memory_order_relaxed)) {
    pthread_spin_lock(a->spin);
    atomic_store_explicit(&a->ready, 1, memory_order_relaxed);
    pthread_spin_unlock(a->spin);
  }
  return 0;
}

struct MutexContentionArgs {
  pthread_mutex_t *mutex;
  atomic_char done;
  atomic_char ready;
};

int MutexContentionWorker(void *arg, int tid) {
  struct MutexContentionArgs *a = arg;
  while (!atomic_load_explicit(&a->done, memory_order_relaxed)) {
    if (pthread_mutex_lock(a->mutex)) notpossible;
    atomic_store_explicit(&a->ready, 1, memory_order_relaxed);
    if (pthread_mutex_unlock(a->mutex)) notpossible;
  }
  return 0;
}

struct NsyncContentionArgs {
  nsync_mu *nsync;
  atomic_char done;
  atomic_char ready;
};

int NsyncContentionWorker(void *arg, int tid) {
  struct NsyncContentionArgs *a = arg;
  while (!atomic_load_explicit(&a->done, memory_order_relaxed)) {
    nsync_mu_lock(a->nsync);
    atomic_store_explicit(&a->ready, 1, memory_order_relaxed);
    nsync_mu_unlock(a->nsync);
  }
  return 0;
}

BENCH(pthread_mutex_lock, bench_contended) {
  struct spawn t;
  {
    pthread_spinlock_t s = {0};
    struct SpinContentionArgs a = {&s};
    _spawn(SpinContentionWorker, &a, &t);
    while (!a.ready) sched_yield();
    EZBENCH2("spin 2x", donothing, BenchSpinUnspin(&s));
    a.done = true;
    _join(&t);
  }
  {
    nsync_mu m = {0};
    struct NsyncContentionArgs a = {&m};
    _spawn(NsyncContentionWorker, &a, &t);
    while (!a.ready) sched_yield();
    EZBENCH2("nsync 2x", donothing, BenchLockUnlockNsync(&m));
    a.done = true;
    _join(&t);
  }
  {
    pthread_mutex_t m;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
    pthread_mutex_init(&m, &attr);
    struct MutexContentionArgs a = {&m};
    _spawn(MutexContentionWorker, &a, &t);
    while (!a.ready) sched_yield();
    EZBENCH2("normal 2x", donothing, BenchLockUnlock(&m));
    a.done = true;
    _join(&t);
  }
  {
    pthread_mutex_t m;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&m, &attr);
    struct MutexContentionArgs a = {&m};
    _spawn(MutexContentionWorker, &a, &t);
    while (!a.ready) sched_yield();
    EZBENCH2("recursive 2x", donothing, BenchLockUnlock(&m));
    a.done = true;
    _join(&t);
  }
  {
    pthread_mutex_t m;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
    pthread_mutex_init(&m, &attr);
    struct MutexContentionArgs a = {&m};
    _spawn(MutexContentionWorker, &a, &t);
    while (!a.ready) sched_yield();
    EZBENCH2("errorcheck 2x", donothing, BenchLockUnlock(&m));
    a.done = true;
    _join(&t);
  }
}
