Debian Codegraph @

src/dex-work-queue.c

raw symbols SCIP
1 /*
2 * dex-work-queue.c
3 *
4 * Copyright 2022 Christian Hergert <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include "dex-object-private.h"
25 #include "dex-semaphore-private.h"
26 #include "dex-work-queue-private.h"
27
28 struct _DexWorkQueue
29 {
30 DexObject parent_instance;
31 DexSemaphore *semaphore;
32 GMutex mutex;
33 GQueue queue;
34 };
35
36 typedef struct _DexWorkQueueClass
37 {
38 DexObjectClass parent_class;
39 } DexWorkQueueClass;
40
41 DEX_DEFINE_FINAL_TYPE (DexWorkQueue, dex_work_queue, DEX_TYPE_OBJECT)
42
43 typedef struct _DexWorkQueueItem
44 {
45 GList link;
46 DexWorkItem work_item;
47 } DexWorkQueueItem;
48
49 static void
50 dex_work_queue_finalize (DexObject *object)
51 {
52 DexWorkQueue *work_queue = DEX_WORK_QUEUE (object);
53
54 if (work_queue->queue.length > 0)
55 g_critical ("Work queue %p freed with %u items still in it!",
56 work_queue, work_queue->queue.length);
57
58 g_mutex_clear (&work_queue->mutex);
59 dex_clear (&work_queue->semaphore);
60
61 DEX_OBJECT_CLASS (dex_work_queue_parent_class)->finalize (object);
62 }
63
64 static void
65 dex_work_queue_class_init (DexWorkQueueClass *work_queue_class)
66 {
67 DexObjectClass *object_class = DEX_OBJECT_CLASS (work_queue_class);
68
69 object_class->finalize = dex_work_queue_finalize;
70 }
71
72 static void
73 dex_work_queue_init (DexWorkQueue *work_queue)
74 {
75 work_queue->semaphore = dex_semaphore_new ();
76 g_mutex_init (&work_queue->mutex);
77 }
78
79 DexWorkQueue *
80 dex_work_queue_new (void)
81 {
82 return (DexWorkQueue *)dex_object_create_instance (DEX_TYPE_WORK_QUEUE);
83 }
84
85 void
86 dex_work_queue_push (DexWorkQueue *work_queue,
87 DexWorkItem work_item)
88 {
89 DexWorkQueueItem *work_queue_item;
90
91 g_return_if_fail (DEX_IS_WORK_QUEUE (work_queue));
92 g_return_if_fail (work_item.func != NULL);
93
94 work_queue_item = g_new0 (DexWorkQueueItem, 1);
95 work_queue_item->link.data = work_queue_item;
96 work_queue_item->work_item = work_item;
97
98 g_mutex_lock (&work_queue->mutex);
99 g_queue_push_tail_link (&work_queue->queue, &work_queue_item->link);
100 g_mutex_unlock (&work_queue->mutex);
101
102 dex_semaphore_post (work_queue->semaphore);
103 }
104
105 gboolean
106 dex_work_queue_try_pop (DexWorkQueue *work_queue,
107 DexWorkItem *out_work_item)
108 {
109 GList *link;
110
111 g_return_val_if_fail (DEX_IS_WORK_QUEUE (work_queue), FALSE);
112 g_return_val_if_fail (out_work_item != NULL, FALSE);
113
114 g_mutex_lock (&work_queue->mutex);
115 link = g_queue_pop_head_link (&work_queue->queue);
116 g_mutex_unlock (&work_queue->mutex);
117
118 if (link != NULL)
119 {
120 DexWorkQueueItem *item = link->data;
121 *out_work_item = item->work_item;
122 g_free (item);
123 }
124
125 return link != NULL;
126 }
127
128 static DexFuture *
129 dex_work_queue_run_cb (DexFuture *future,
130 gpointer user_data)
131 {
132 DexWorkQueue *work_queue = user_data;
133 DexWorkItem work_item;
134
135 g_assert (DEX_IS_WORK_QUEUE (work_queue));
136
137 if (dex_work_queue_try_pop (work_queue, &work_item))
138 dex_work_item_invoke (&work_item);
139
140 return dex_semaphore_wait (work_queue->semaphore);
141 }
142
143 DexFuture *
144 dex_work_queue_run (DexWorkQueue *work_queue)
145 {
146 DexFuture *future;
147
148 g_return_val_if_fail (work_queue != NULL, NULL);
149
150 future = dex_semaphore_wait (work_queue->semaphore);
151 future = dex_future_finally_loop (future,
152 dex_work_queue_run_cb,
153 dex_ref (work_queue),
154 dex_unref);
155
156 return future;
157 }