1/* uisthread.c
2 *
3 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT.  See the GNU General Public License for more
15 * details.
16 */
17
18/* @ALL_INSPECTED */
19#include <asm/processor.h>
20#include <linux/signal.h>
21#include <linux/sched.h>
22#include <linux/kthread.h>
23#include "uisutils.h"
24#include "uisthread.h"
25
26/* this is shorter than using __FILE__ (full path name) in
27 * debug/info/error messages
28 */
29#define CURRENT_FILE_PC UISLIB_PC_uisthread_c
30#define __MYFILE__ "uisthread.c"
31
32/*****************************************************/
33/* Exported functions                                */
34/*****************************************************/
35
36/* returns 0 for failure, 1 for success */
37int
38uisthread_start(struct uisthread_info *thrinfo,
39		int (*threadfn)(void *), void *thrcontext, char *name)
40{
41	/* used to stop the thread */
42	init_completion(&thrinfo->has_stopped);
43	thrinfo->task = kthread_run(threadfn, thrcontext, name);
44	if (IS_ERR(thrinfo->task)) {
45		thrinfo->id = 0;
46		return 0;	/* failure */
47	}
48	thrinfo->id = thrinfo->task->pid;
49	return 1;
50}
51EXPORT_SYMBOL_GPL(uisthread_start);
52
53void
54uisthread_stop(struct uisthread_info *thrinfo)
55{
56	int stopped = 0;
57
58	if (thrinfo->id == 0)
59		return;		/* thread not running */
60
61	kthread_stop(thrinfo->task);
62	/* give up if the thread has NOT died in 1 minute */
63	if (wait_for_completion_timeout(&thrinfo->has_stopped, 60 * HZ))
64		stopped = 1;
65
66	if (stopped)
67		thrinfo->id = 0;
68}
69EXPORT_SYMBOL_GPL(uisthread_stop);
70