aboutsummaryrefslogtreecommitdiffstats
path: root/src/splat/splat-thread.c
blob: 0741db1fa201da65b4d9bc79421ad3cc0c474aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <sys/zfs_context.h>
#include <sys/splat-ctl.h>

#define KZT_SUBSYSTEM_THREAD		0x0600
#define KZT_THREAD_NAME			"thread"
#define KZT_THREAD_DESC			"Kernel Thread Tests"

#define KZT_THREAD_TEST1_ID		0x0601
#define KZT_THREAD_TEST1_NAME		"create"
#define KZT_THREAD_TEST1_DESC		"Validate thread creation and destruction"

#define KZT_THREAD_TEST_MAGIC            0x4488CC00UL

typedef struct thread_priv {
        unsigned long tp_magic;
        struct file *tp_file;
        spinlock_t tp_lock;
        wait_queue_head_t tp_waitq;
	int tp_rc;
} thread_priv_t;


static void
kzt_thread_work(void *priv)
{
	thread_priv_t *tp = (thread_priv_t *)priv;

	spin_lock(&tp->tp_lock);
	ASSERT(tp->tp_magic == KZT_THREAD_TEST_MAGIC);
	tp->tp_rc = 1;

	spin_unlock(&tp->tp_lock);
	wake_up(&tp->tp_waitq);

	thread_exit();
}

static int
kzt_thread_test1(struct file *file, void *arg)
{
	thread_priv_t tp;
        DEFINE_WAIT(wait);
	kthread_t *thr;
	int rc = 0;

	tp.tp_magic = KZT_THREAD_TEST_MAGIC;
	tp.tp_file = file;
        spin_lock_init(&tp.tp_lock);
	init_waitqueue_head(&tp.tp_waitq);
	tp.tp_rc = 0;

	spin_lock(&tp.tp_lock);

	thr = (kthread_t *)thread_create(NULL, 0, kzt_thread_work, &tp, 0,
			                 (proc_t *) &p0, TS_RUN, minclsyspri);
	/* Must never fail under Solaris, but we check anyway so we can
	 * report an error when this impossible thing happens */
	if (thr == NULL) {
		rc = -ESRCH;
		goto out;
	}

        for (;;) {
                prepare_to_wait(&tp.tp_waitq, &wait, TASK_UNINTERRUPTIBLE);
                if (tp.tp_rc)
                        break;

                spin_unlock(&tp.tp_lock);
                schedule();
                spin_lock(&tp.tp_lock);
        }

        kzt_vprint(file, KZT_THREAD_TEST1_NAME, "%s",
	           "Thread successfully started and exited cleanly\n");
out:
	spin_unlock(&tp.tp_lock);

	return rc;
}

kzt_subsystem_t *
kzt_thread_init(void)
{
        kzt_subsystem_t *sub;

        sub = kmalloc(sizeof(*sub), GFP_KERNEL);
        if (sub == NULL)
                return NULL;

        memset(sub, 0, sizeof(*sub));
        strncpy(sub->desc.name, KZT_THREAD_NAME, KZT_NAME_SIZE);
        strncpy(sub->desc.desc, KZT_THREAD_DESC, KZT_DESC_SIZE);
        INIT_LIST_HEAD(&sub->subsystem_list);
        INIT_LIST_HEAD(&sub->test_list);
        spin_lock_init(&sub->test_lock);
        sub->desc.id = KZT_SUBSYSTEM_THREAD;

        KZT_TEST_INIT(sub, KZT_THREAD_TEST1_NAME, KZT_THREAD_TEST1_DESC,
                      KZT_THREAD_TEST1_ID, kzt_thread_test1);

        return sub;
}

void
kzt_thread_fini(kzt_subsystem_t *sub)
{
        ASSERT(sub);
        KZT_TEST_FINI(sub, KZT_THREAD_TEST1_ID);

        kfree(sub);
}

int
kzt_thread_id(void) {
        return KZT_SUBSYSTEM_THREAD;
}