summaryrefslogtreecommitdiffstats
path: root/drivers/staging/csr/oska/thread.c
blob: f680cef709e716db4f879c9548e7f77f6bf954d3 (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
/*
 * Linux thread functions.
 *
 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
 *
 * Refer to LICENSE.txt included with this source code for details on
 * the license terms.
 */
#include <linux/module.h>

#include "thread.h"

static int thread_func(void *data)
{
    os_thread_t *thread = data;

    thread->func(thread->arg);

    /*
     * kthread_stop() cannot handle the thread exiting while
     * kthread_should_stop() is false, so sleep until kthread_stop()
     * wakes us up.
     */
    set_current_state(TASK_INTERRUPTIBLE);
    if (!kthread_should_stop())
        schedule();

    return 0;
}

int os_thread_create(os_thread_t *thread, const char *name, void (*func)(void *), void *arg)
{
    thread->func = func;
    thread->arg = arg;

    thread->stop = 0;

    thread->task = kthread_run(thread_func, thread, name);
    if (IS_ERR(thread->task)) {
        return PTR_ERR(thread->task);
    }
    return 0;
}
EXPORT_SYMBOL(os_thread_create);

void os_thread_stop(os_thread_t *thread, os_event_t *evt)
{
    /*
     * Stop flag must be set before the event is raised so
     * kthread_should_stop() cannot be used.
     */
    thread->stop = 1;

    if (evt) {
        os_event_raise(evt, ~0);
    }

    kthread_stop(thread->task);
}
EXPORT_SYMBOL(os_thread_stop);

int os_thread_should_stop(os_thread_t *thread)
{
    return thread->stop;
}
EXPORT_SYMBOL(os_thread_should_stop);