1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2022 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "gmemorymonitor.h"
25 #include "ginitable.h"
26 #include "giomodule-priv.h"
28 #include "glib/gstdio.h"
29 #include "gcancellable.h"
33 #define G_TYPE_MEMORY_MONITOR_WIN32 (g_memory_monitor_win32_get_type ())
34 G_DECLARE_FINAL_TYPE (GMemoryMonitorWin32, g_memory_monitor_win32, G, MEMORY_MONITOR_WIN32, GObject)
36 #define G_MEMORY_MONITOR_WIN32_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable))
38 static void g_memory_monitor_win32_iface_init (GMemoryMonitorInterface *iface);
39 static void g_memory_monitor_win32_initable_iface_init (GInitableIface *iface);
41 struct _GMemoryMonitorWin32
43 GObject parent_instance;
50 G_DEFINE_TYPE_WITH_CODE (GMemoryMonitorWin32, g_memory_monitor_win32, G_TYPE_OBJECT,
51 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
52 g_memory_monitor_win32_initable_iface_init)
53 G_IMPLEMENT_INTERFACE (G_TYPE_MEMORY_MONITOR,
54 g_memory_monitor_win32_iface_init)
55 _g_io_modules_ensure_extension_points_registered ();
56 g_io_extension_point_implement (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
62 g_memory_monitor_win32_init (GMemoryMonitorWin32 *win32)
67 watch_handler (gpointer user_data)
69 GMemoryMonitorWin32 *win32 = user_data;
71 g_signal_emit_by_name (win32, "low-memory-warning",
72 G_MEMORY_MONITOR_WARNING_LEVEL_LOW);
74 return G_SOURCE_REMOVE;
77 /* Thread which watches for win32 memory resource events */
79 watch_thread_function (LPVOID parameter)
81 GWeakRef *weak_ref = parameter;
82 GMemoryMonitorWin32 *win32 = NULL;
83 HANDLE handles[2] = { 0, };
85 BOOL low_memory_state;
87 win32 = g_weak_ref_get (weak_ref);
91 if (!DuplicateHandle (GetCurrentProcess (),
97 DUPLICATE_SAME_ACCESS))
101 emsg = g_win32_error_message (GetLastError ());
102 g_debug ("DuplicateHandle failed: %s", emsg);
107 if (!DuplicateHandle (GetCurrentProcess (),
109 GetCurrentProcess (),
113 DUPLICATE_SAME_ACCESS))
117 emsg = g_win32_error_message (GetLastError ());
118 g_debug ("DuplicateHandle failed: %s", emsg);
123 g_clear_object (&win32);
127 if (!QueryMemoryResourceNotification (handles[1], &low_memory_state))
131 emsg = g_win32_error_message (GetLastError ());
132 g_debug ("QueryMemoryResourceNotification failed: %s", emsg);
137 win32 = g_weak_ref_get (weak_ref);
141 if (low_memory_state)
143 g_idle_add_full (G_PRIORITY_DEFAULT,
145 g_steal_pointer (&win32),
147 /* throttle a bit the loop */
148 g_usleep (G_USEC_PER_SEC);
152 g_clear_object (&win32);
154 result = WaitForMultipleObjects (G_N_ELEMENTS (handles), handles, FALSE, INFINITE);
157 case WAIT_OBJECT_0 + 1:
164 emsg = g_win32_error_message (GetLastError ());
165 g_debug ("WaitForMultipleObjects failed: %s", emsg);
176 CloseHandle (handles[0]);
178 CloseHandle (handles[1]);
179 g_clear_object (&win32);
180 g_weak_ref_clear (weak_ref);
186 g_memory_monitor_win32_initable_init (GInitable *initable,
187 GCancellable *cancellable,
190 GMemoryMonitorWin32 *win32 = G_MEMORY_MONITOR_WIN32 (initable);
191 GWeakRef *weak_ref = NULL;
193 win32->event = CreateEvent (NULL, FALSE, FALSE, NULL);
194 if (win32->event == NULL)
196 g_set_error_literal (error, G_IO_ERROR, g_io_error_from_errno (GetLastError ()),
197 "Failed to create event");
201 win32->mem = CreateMemoryResourceNotification (LowMemoryResourceNotification);
202 if (win32->mem == NULL)
204 g_set_error_literal (error, G_IO_ERROR, g_io_error_from_errno (GetLastError ()),
205 "Failed to create resource notification handle");
209 weak_ref = g_new0 (GWeakRef, 1);
210 g_weak_ref_init (weak_ref, win32);
211 /* Use CreateThread (not GThread) with a small stack to make it more lightweight. */
212 win32->thread = CreateThread (NULL, 1024, watch_thread_function, weak_ref, 0, NULL);
213 if (win32->thread == NULL)
215 g_set_error_literal (error, G_IO_ERROR, g_io_error_from_errno (GetLastError ()),
216 "Failed to create memory resource notification thread");
217 g_weak_ref_clear (weak_ref);
226 g_memory_monitor_win32_finalize (GObject *object)
228 GMemoryMonitorWin32 *win32 = G_MEMORY_MONITOR_WIN32 (object);
232 SetEvent (win32->event);
233 WaitForSingleObject (win32->thread, INFINITE);
234 CloseHandle (win32->thread);
238 CloseHandle (win32->event);
241 CloseHandle (win32->mem);
243 G_OBJECT_CLASS (g_memory_monitor_win32_parent_class)->finalize (object);
247 g_memory_monitor_win32_class_init (GMemoryMonitorWin32Class *nl_class)
249 GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
251 gobject_class->finalize = g_memory_monitor_win32_finalize;
255 g_memory_monitor_win32_iface_init (GMemoryMonitorInterface *monitor_iface)
260 g_memory_monitor_win32_initable_iface_init (GInitableIface *iface)
262 iface->init = g_memory_monitor_win32_initable_init;