// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Various unit tests for the "ntsync" synchronization primitive driver.
*
* Copyright (C) 2021-2022 Elizabeth Figura <zfigura@codeweavers.com>
*/
#define _GNU_SOURCE
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <linux/ntsync.h>
#include "kselftest_harness.h"
static int read_sem_state(int sem, __u32 *count, __u32 *max)
{
struct ntsync_sem_args args;
int ret;
memset(&args, 0xcc, sizeof(args));
ret = ioctl(sem, NTSYNC_IOC_SEM_READ, &args);
*count = args.count;
*max = args.max;
return ret;
}
#define check_sem_state(sem, count, max) \
({ \
__u32 __count, __max; \
int ret = read_sem_state((sem), &__count, &__max); \
EXPECT_EQ(0, ret); \
EXPECT_EQ((count), __count); \
EXPECT_EQ((max), __max); \
})
static int release_sem(int sem, __u32 *count)
{
return ioctl(sem, NTSYNC_IOC_SEM_RELEASE, count);
}
static int read_mutex_state(int mutex, __u32 *count, __u32 *owner)
{
struct ntsync_mutex_args args;
int ret;
memset(&args, 0xcc, sizeof(args));
ret = ioctl(mutex, NTSYNC_IOC_MUTEX_READ, &args);
*count = args.count;
*owner = args.owner;
return ret;
}
#define check_mutex_state(mutex, count, owner) \
({ \
__u32 __count, __owner; \
int ret = read_mutex_state((mutex), &__count, &__owner); \
EXPECT_EQ(0, ret); \
EXPECT_EQ((count), __count); \
EXPECT_EQ((owner), __owner); \
})
static int unlock_mutex(int mutex, __u32 owner, __u32 *count)
{
struct ntsync_mutex_args args;
int ret;
args.owner = owner;
args.count = 0xdeadbeef;
ret = ioctl(mutex, NTSYNC_IOC_MUTEX_UNLOCK