Files
mac_linuxgpu/linuxu/tests/test_workqueue.c
T
geramyloveless 953cf0149c mac_linuxgpu: upstream Linux amdgpu on macOS
Runs the unmodified upstream Linux amdgpu and amdkfd drivers on Apple
Silicon Macs inside a DriverKit system extension, for AMD GPUs attached
over Thunderbolt. The Linux sources come from a pinned submodule and are
compiled against linuxu, a userspace implementation of the Linux kernel
APIs they use. Includes the HSA runtime, the host installer app, the
setup and firmware fetch scripts, and the offline test suite.
2026-10-02 18:49:17 -07:00

85 lines
2.0 KiB
C

/* test_workqueue.c — workqueue (REAL): schedule_work runs the
* worker; a delayed work respects its jiffy delay (>= delay-2 jiffies);
* cancel_delayed_work_sync prevents a queued-but-not-fired work.
* HZ=100 (shared jiffies contract: 10ms per jiffy). */
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <linux/workqueue.h>
#include <linux/jiffies.h>
#define EXPECT(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
return 1; \
} else { \
fprintf(stderr, "ok: %s\n", #cond); \
} \
} while (0)
static int ran = 0;
static int delayed_ms = -1;
static int cancel_ran = 0;
static int64_t now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
static int64_t start_ms = 0;
static void work_fn(struct work_struct *work)
{
(void)work;
ran = 1;
}
static void delayed_fn(struct work_struct *work)
{
(void)work;
delayed_ms = (int)(now_ms() - start_ms);
}
static void cancel_fn(struct work_struct *work)
{
(void)work;
cancel_ran = 1;
}
int main(void)
{
struct work_struct work;
struct delayed_work dwork;
struct delayed_work cwork;
/* 1. immediate work runs */
INIT_WORK(&work, work_fn);
schedule_work(&work);
usleep(50 * 1000);
EXPECT(ran == 1);
/* 2. delayed work: deadline is 50ms at the shared HZ=100. */
INIT_DELAYED_WORK(&dwork, delayed_fn);
start_ms = now_ms();
schedule_delayed_work(&dwork, msecs_to_jiffies(50));
usleep(200 * 1000);
/* Absolute jiffies are quantized at 10ms; a deadline may land
* just under the requested wall-clock duration. */
EXPECT(delayed_ms >= 40);
EXPECT(delayed_ms < 150);
/* 3. cancel_delayed_work_sync: queue a long-delay work, cancel
* before it fires; it must never run */
INIT_DELAYED_WORK(&cwork, cancel_fn);
schedule_delayed_work(&cwork, msecs_to_jiffies(4000));
EXPECT(cancel_delayed_work_sync(&cwork) == true);
usleep(80 * 1000);
EXPECT(cancel_ran == 0);
fprintf(stderr, "PASS test_workqueue\n");
return 0;
}