1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of gsignond
*
* Copyright (C) 2013 Intel Corporation.
*
* Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "gsignond-disposable.h"
#include "gsignond/gsignond-log.h"
struct _GSignondDisposablePrivate
{
guint timeout; /* timeout in seconds */
volatile gint keep_obj_counter; /* keep object request counter */
guint timer_id; /* timer source id */
};
enum {
PROP_0,
PROP_TIMEOUT,
PROP_AUTO_DISPOSE,
PROP_MAX,
};
enum {
SIG_DISPOSING,
SIG_MAX
};
static GParamSpec *properties[PROP_MAX];
static guint signals[SIG_MAX];
#define GSIGNOND_DISPOSABLE_PRIV(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), GSIGNOND_TYPE_DISPOSABLE, GSignondDisposablePrivate)
G_DEFINE_ABSTRACT_TYPE (GSignondDisposable, gsignond_disposable, G_TYPE_OBJECT);
static void
_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
g_return_if_fail (object && GSIGNOND_IS_DISPOSABLE (object));
GSignondDisposable *self = GSIGNOND_DISPOSABLE (object);
switch (property_id) {
case PROP_TIMEOUT:
gsignond_disposable_set_timeout (self, g_value_get_uint (value));
break;
case PROP_AUTO_DISPOSE:
gsignond_disposable_set_auto_dispose (self, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
g_return_if_fail (object && GSIGNOND_IS_DISPOSABLE (object));
GSignondDisposable *self = GSIGNOND_DISPOSABLE (object);
switch (property_id) {
case PROP_TIMEOUT:
g_value_set_int (value, self->priv->timeout);
break;
case PROP_AUTO_DISPOSE:
g_value_set_boolean (value, gsignond_disposable_get_auto_dispose(self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
_dispose (GObject *object)
{
g_return_if_fail (object && GSIGNOND_IS_DISPOSABLE (object));
GSignondDisposable *self = GSIGNOND_DISPOSABLE (object);
DBG ("%s DISPOSE", G_OBJECT_TYPE_NAME (self));
if (self->priv->timer_id) {
DBG (" - TIMER CLEAR");
g_source_remove (self->priv->timer_id);
self->priv->timer_id = 0;
}
G_OBJECT_CLASS (gsignond_disposable_parent_class)->dispose (object);
}
static void
_finalize (GObject *object)
{
g_return_if_fail (object && GSIGNOND_IS_DISPOSABLE (object));
DBG ("FINALIZE");
G_OBJECT_CLASS (gsignond_disposable_parent_class)->finalize (object);
}
static void
gsignond_disposable_class_init (GSignondDisposableClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (object_class, sizeof (GSignondDisposablePrivate));
object_class->set_property = _set_property;
object_class->get_property = _get_property;
object_class->dispose = _dispose;
object_class->finalize = _finalize;
properties[PROP_TIMEOUT] =
g_param_spec_uint ("timeout",
"Object timeout",
"object timeout",
0,
G_MAXUINT,
0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
properties[PROP_AUTO_DISPOSE] =
g_param_spec_boolean ("auto-dispose",
"Auto dispose",
"auto dispose",
TRUE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, PROP_MAX, properties);
signals[SIG_DISPOSING] = g_signal_new ("disposing",
GSIGNOND_TYPE_DISPOSABLE,
G_SIGNAL_RUN_FIRST| G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
0,
NULL, NULL,
NULL,
G_TYPE_NONE,
0,
G_TYPE_NONE);
}
static void
gsignond_disposable_init (GSignondDisposable *self)
{
self->priv = GSIGNOND_DISPOSABLE_PRIV (self);
self->priv->timer_id = 0;
self->priv->timeout = 0;
g_atomic_int_set(&self->priv->keep_obj_counter, 0);
DBG ("INIT");
}
static gboolean
_auto_dispose (gpointer user_data)
{
g_return_val_if_fail (user_data && GSIGNOND_IS_DISPOSABLE (user_data), FALSE);
GSignondDisposable *self = GSIGNOND_DISPOSABLE (user_data);
g_signal_emit (self, signals[SIG_DISPOSING], 0);
/* destroy object */
DBG ("%s AUTO DISPOSE", G_OBJECT_TYPE_NAME (self));
g_object_unref (G_OBJECT (self));
return FALSE;
}
static gboolean
_timer_dispose (gpointer user_data)
{
g_return_val_if_fail (user_data && GSIGNOND_IS_DISPOSABLE (user_data), FALSE);
GSignondDisposable *self = GSIGNOND_DISPOSABLE (user_data);
DBG ("%s TIMER DISPOSE", G_OBJECT_TYPE_NAME (self));
/* clear out timer since we are already inside timer cb */
self->priv->timer_id = 0;
return _auto_dispose (user_data);
}
static void
_update_timer (GSignondDisposable *self)
{
DBG("%s (%p): keep_obj_counter : %d, timeout : %d",
G_OBJECT_TYPE_NAME(self),
self,
self->priv->keep_obj_counter,
self->priv->timeout);
if (g_atomic_int_get(&self->priv->keep_obj_counter) == 0) {
if (self->priv->timeout) {
DBG("Setting object timeout to %d", self->priv->timeout);
self->priv->timer_id = g_timeout_add_seconds (self->priv->timeout,
_timer_dispose,
self);
}
}
else if (self->priv->timer_id) {
g_source_remove (self->priv->timer_id);
self->priv->timer_id = 0;
}
}
void
gsignond_disposable_set_auto_dispose (GSignondDisposable *self,
gboolean dispose)
{
g_return_if_fail (self && GSIGNOND_IS_DISPOSABLE (self));
if (g_atomic_int_get(&self->priv->keep_obj_counter) == 0 && dispose) return;
g_atomic_int_add (&self->priv->keep_obj_counter, !dispose ? +1 : -1);
_update_timer (self);
}
void
gsignond_disposable_set_timeout (GSignondDisposable *self,
guint timeout)
{
g_return_if_fail (self && GSIGNOND_IS_DISPOSABLE (self));
if (self->priv->timeout == timeout) return;
self->priv->timeout = timeout;
_update_timer (self);
}
void
gsignond_disposable_delete_later (GSignondDisposable *self)
{
if (self->priv->timer_id)
g_source_remove (self->priv->timer_id);
INFO ("Object '%s' (%p) about to dispose...",
G_OBJECT_TYPE_NAME (self), self);
self->priv->timer_id = g_idle_add (_auto_dispose, self);
}
gboolean
gsignond_disposable_get_auto_dispose (GSignondDisposable *self)
{
g_return_val_if_fail (self && GSIGNOND_IS_DISPOSABLE(self), FALSE);
return g_atomic_int_get(&self->priv->keep_obj_counter) == 0 ;
}
|