xref: /openbmc/linux/lib/is_single_threaded.c (revision 6cc88bc45ce8043171089c9592da223dfab91823)
1*6cc88bc4SDavid Howells /* Function to determine if a thread group is single threaded or not
2*6cc88bc4SDavid Howells  *
3*6cc88bc4SDavid Howells  * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4*6cc88bc4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
5*6cc88bc4SDavid Howells  * - Derived from security/selinux/hooks.c
6*6cc88bc4SDavid Howells  *
7*6cc88bc4SDavid Howells  * This program is free software; you can redistribute it and/or
8*6cc88bc4SDavid Howells  * modify it under the terms of the GNU General Public Licence
9*6cc88bc4SDavid Howells  * as published by the Free Software Foundation; either version
10*6cc88bc4SDavid Howells  * 2 of the Licence, or (at your option) any later version.
11*6cc88bc4SDavid Howells  */
12*6cc88bc4SDavid Howells 
13*6cc88bc4SDavid Howells #include <linux/sched.h>
14*6cc88bc4SDavid Howells 
15*6cc88bc4SDavid Howells /**
16*6cc88bc4SDavid Howells  * is_single_threaded - Determine if a thread group is single-threaded or not
17*6cc88bc4SDavid Howells  * @p: A task in the thread group in question
18*6cc88bc4SDavid Howells  *
19*6cc88bc4SDavid Howells  * This returns true if the thread group to which a task belongs is single
20*6cc88bc4SDavid Howells  * threaded, false if it is not.
21*6cc88bc4SDavid Howells  */
22*6cc88bc4SDavid Howells bool is_single_threaded(struct task_struct *p)
23*6cc88bc4SDavid Howells {
24*6cc88bc4SDavid Howells 	struct task_struct *g, *t;
25*6cc88bc4SDavid Howells 	struct mm_struct *mm = p->mm;
26*6cc88bc4SDavid Howells 
27*6cc88bc4SDavid Howells 	if (atomic_read(&p->signal->count) != 1)
28*6cc88bc4SDavid Howells 		goto no;
29*6cc88bc4SDavid Howells 
30*6cc88bc4SDavid Howells 	if (atomic_read(&p->mm->mm_users) != 1) {
31*6cc88bc4SDavid Howells 		read_lock(&tasklist_lock);
32*6cc88bc4SDavid Howells 		do_each_thread(g, t) {
33*6cc88bc4SDavid Howells 			if (t->mm == mm && t != p)
34*6cc88bc4SDavid Howells 				goto no_unlock;
35*6cc88bc4SDavid Howells 		} while_each_thread(g, t);
36*6cc88bc4SDavid Howells 		read_unlock(&tasklist_lock);
37*6cc88bc4SDavid Howells 	}
38*6cc88bc4SDavid Howells 
39*6cc88bc4SDavid Howells 	return true;
40*6cc88bc4SDavid Howells 
41*6cc88bc4SDavid Howells no_unlock:
42*6cc88bc4SDavid Howells 	read_unlock(&tasklist_lock);
43*6cc88bc4SDavid Howells no:
44*6cc88bc4SDavid Howells 	return false;
45*6cc88bc4SDavid Howells }
46