Debian Codegraph @

src/libsysprof/sysprof-thread-info.c

raw
1 /* sysprof-thread-info.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include "sysprof-thread-info.h"
24
25 struct _SysprofThreadInfo
26 {
27 GObject parent_instance;
28 SysprofDocumentProcess *process;
29 int thread_id;
30 };
31
32 enum {
33 PROP_0,
34 PROP_PROCESS,
35 PROP_THREAD_ID,
36 PROP_IS_MAIN_THREAD,
37 N_PROPS
38 };
39
40 G_DEFINE_FINAL_TYPE (SysprofThreadInfo, sysprof_thread_info, G_TYPE_OBJECT)
41
42 static GParamSpec *properties [N_PROPS];
43
44 static void
45 sysprof_thread_info_finalize (GObject *object)
46 {
47 SysprofThreadInfo *self = (SysprofThreadInfo *)object;
48
49 g_clear_object (&self->process);
50
51 G_OBJECT_CLASS (sysprof_thread_info_parent_class)->finalize (object);
52 }
53
54 static void
55 sysprof_thread_info_get_property (GObject *object,
56 guint prop_id,
57 GValue *value,
58 GParamSpec *pspec)
59 {
60 SysprofThreadInfo *self = SYSPROF_THREAD_INFO (object);
61
62 switch (prop_id)
63 {
64 case PROP_IS_MAIN_THREAD:
65 g_value_set_boolean (value, sysprof_thread_info_is_main_thread (self));
66 break;
67
68 case PROP_PROCESS:
69 g_value_set_object (value, sysprof_thread_info_get_process (self));
70 break;
71
72 case PROP_THREAD_ID:
73 g_value_set_int (value, sysprof_thread_info_get_thread_id (self));
74 break;
75
76 default:
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
78 }
79 }
80
81 static void
82 sysprof_thread_info_set_property (GObject *object,
83 guint prop_id,
84 const GValue *value,
85 GParamSpec *pspec)
86 {
87 SysprofThreadInfo *self = SYSPROF_THREAD_INFO (object);
88
89 switch (prop_id)
90 {
91 case PROP_PROCESS:
92 self->process = g_value_dup_object (value);
93 break;
94
95 case PROP_THREAD_ID:
96 self->thread_id = g_value_get_int (value);
97 break;
98
99 default:
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
101 }
102 }
103
104 static void
105 sysprof_thread_info_class_init (SysprofThreadInfoClass *klass)
106 {
107 GObjectClass *object_class = G_OBJECT_CLASS (klass);
108
109 object_class->finalize = sysprof_thread_info_finalize;
110 object_class->get_property = sysprof_thread_info_get_property;
111 object_class->set_property = sysprof_thread_info_set_property;
112
113 properties[PROP_IS_MAIN_THREAD] =
114 g_param_spec_boolean ("is-main-thread", NULL, NULL,
115 FALSE,
116 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
117
118 properties[PROP_PROCESS] =
119 g_param_spec_object ("process", NULL, NULL,
120 SYSPROF_TYPE_DOCUMENT_PROCESS,
121 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
122
123 properties[PROP_THREAD_ID] =
124 g_param_spec_int ("thread-id", NULL, NULL,
125 G_MININT, G_MAXINT, 0,
126 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
127
128 g_object_class_install_properties (object_class, N_PROPS, properties);
129 }
130
131 static void
132 sysprof_thread_info_init (SysprofThreadInfo *self)
133 {
134 }
135
136 /**
137 * sysprof_thread_info_get_process:
138 * @self: a #SysprofThreadInfo
139 *
140 * Gets the process that owns the thread info.
141 *
142 * Returns: (transfer none): a #SysprofDocumentProcess
143 */
144 SysprofDocumentProcess *
145 sysprof_thread_info_get_process (SysprofThreadInfo *self)
146 {
147 g_return_val_if_fail (SYSPROF_IS_THREAD_INFO (self), NULL);
148
149 return self->process;
150 }
151
152 /**
153 * sysprof_thread_info_get_thread_id:
154 * @self: a #SysprofThreadInfo
155 *
156 * Gets the thread identifier.
157 *
158 * This typically matches what `gettid()` syscall returns on Linux.
159 *
160 * Returns: an integer containing the thread-id
161 */
162 int
163 sysprof_thread_info_get_thread_id (SysprofThreadInfo *self)
164 {
165 g_return_val_if_fail (SYSPROF_IS_THREAD_INFO (self), -1);
166
167 return self->thread_id;
168 }
169
170 /**
171 * sysprof_thread_info_is_main_thread:
172 * @self: a #SysprofThreadInfo
173 *
174 * Checks if the thread is the main thread for a process.
175 *
176 * Returns: %TRUE if the thread-id is the main thread.
177 */
178 gboolean
179 sysprof_thread_info_is_main_thread (SysprofThreadInfo *self)
180 {
181 g_return_val_if_fail (SYSPROF_IS_THREAD_INFO (self), FALSE);
182
183 return self->thread_id == sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (self->process));
184 }