xref: /openbmc/qemu/migration/threadinfo.c (revision 136cb9cc)
1 /*
2  *  Migration Threads info
3  *
4  *  Copyright (c) 2022 HUAWEI TECHNOLOGIES CO., LTD.
5  *
6  *  Authors:
7  *  Jiang Jiacheng <jiangjiacheng@huawei.com>
8  *
9  *  This work is licensed under the terms of the GNU GPL, version 2 or later.
10  *  See the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu/queue.h"
15 #include "qemu/lockable.h"
16 #include "threadinfo.h"
17 
18 QemuMutex migration_threads_lock;
19 static QLIST_HEAD(, MigrationThread) migration_threads;
20 
21 static void __attribute__((constructor)) migration_threads_init(void)
22 {
23     qemu_mutex_init(&migration_threads_lock);
24 }
25 
26 MigrationThread *migration_threads_add(const char *name, int thread_id)
27 {
28     MigrationThread *thread =  g_new0(MigrationThread, 1);
29     thread->name = name;
30     thread->thread_id = thread_id;
31 
32     WITH_QEMU_LOCK_GUARD(&migration_threads_lock) {
33         QLIST_INSERT_HEAD(&migration_threads, thread, node);
34     }
35 
36     return thread;
37 }
38 
39 void migration_threads_remove(MigrationThread *thread)
40 {
41     QEMU_LOCK_GUARD(&migration_threads_lock);
42     if (thread) {
43         QLIST_REMOVE(thread, node);
44         g_free(thread);
45     }
46 }
47 
48 MigrationThreadInfoList *qmp_query_migrationthreads(Error **errp)
49 {
50     MigrationThreadInfoList *head = NULL;
51     MigrationThreadInfoList **tail = &head;
52     MigrationThread *thread = NULL;
53 
54     QEMU_LOCK_GUARD(&migration_threads_lock);
55     QLIST_FOREACH(thread, &migration_threads, node) {
56         MigrationThreadInfo *info = g_new0(MigrationThreadInfo, 1);
57         info->name = g_strdup(thread->name);
58         info->thread_id = thread->thread_id;
59 
60         QAPI_LIST_APPEND(tail, info);
61     }
62 
63     return head;
64 }
65