|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
310c69 |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
310c69 |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
310c69 |
* of the License, or (at your option) any later version.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
310c69 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
310c69 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
310c69 |
* GNU General Public License for more details.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
310c69 |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
310c69 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
310c69 |
* 02110-1301, USA.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* $Id: //eng/vdo-releases/aluminum/src/c++/vdo/kernel/threadRegistry.c#1 $
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "threadRegistry.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include <linux/gfp.h>
|
|
Packit Service |
310c69 |
#include <linux/slab.h>
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "permassert.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* We need to be careful when using other facilities that may use
|
|
Packit Service |
310c69 |
* threadRegistry functions in their normal operation. For example,
|
|
Packit Service |
310c69 |
* we do not want to invoke the logger while holding a lock.
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void registerThread(ThreadRegistry *registry,
|
|
Packit Service |
310c69 |
RegisteredThread *newThread,
|
|
Packit Service |
310c69 |
const void *pointer)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
INIT_LIST_HEAD(&newThread->links);
|
|
Packit Service |
310c69 |
newThread->pointer = pointer;
|
|
Packit Service |
310c69 |
newThread->task = current;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
bool foundIt = false;
|
|
Packit Service |
310c69 |
RegisteredThread *thread;
|
|
Packit Service |
310c69 |
write_lock(®istry->lock);
|
|
Packit Service |
310c69 |
list_for_each_entry(thread, ®istry->links, links) {
|
|
Packit Service |
310c69 |
if (thread->task == current) {
|
|
Packit Service |
310c69 |
// This should not have been there.
|
|
Packit Service |
310c69 |
// We'll complain after releasing the lock.
|
|
Packit Service |
310c69 |
list_del_init(&thread->links);
|
|
Packit Service |
310c69 |
foundIt = true;
|
|
Packit Service |
310c69 |
break;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
list_add_tail(&newThread->links, ®istry->links);
|
|
Packit Service |
310c69 |
write_unlock(®istry->lock);
|
|
Packit Service |
310c69 |
ASSERT_LOG_ONLY(!foundIt, "new thread not already in registry");
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void unregisterThread(ThreadRegistry *registry)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
bool foundIt = false;
|
|
Packit Service |
310c69 |
RegisteredThread *thread;
|
|
Packit Service |
310c69 |
write_lock(®istry->lock);
|
|
Packit Service |
310c69 |
list_for_each_entry(thread, ®istry->links, links) {
|
|
Packit Service |
310c69 |
if (thread->task == current) {
|
|
Packit Service |
310c69 |
list_del_init(&thread->links);
|
|
Packit Service |
310c69 |
foundIt = true;
|
|
Packit Service |
310c69 |
break;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
write_unlock(®istry->lock);
|
|
Packit Service |
310c69 |
ASSERT_LOG_ONLY(foundIt, "thread found in registry");
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void initializeThreadRegistry(ThreadRegistry *registry)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
INIT_LIST_HEAD(®istry->links);
|
|
Packit Service |
310c69 |
rwlock_init(®istry->lock);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
const void *lookupThread(ThreadRegistry *registry)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
const void *result = NULL;
|
|
Packit Service |
310c69 |
read_lock(®istry->lock);
|
|
Packit Service |
310c69 |
RegisteredThread *thread;
|
|
Packit Service |
310c69 |
list_for_each_entry(thread, ®istry->links, links) {
|
|
Packit Service |
310c69 |
if (thread->task == current) {
|
|
Packit Service |
310c69 |
result = thread->pointer;
|
|
Packit Service |
310c69 |
break;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
read_unlock(®istry->lock);
|
|
Packit Service |
310c69 |
return result;
|
|
Packit Service |
310c69 |
}
|