File: idle.c

package info (click to toggle)
ofono 2.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,064 kB
  • sloc: ansic: 224,979; sh: 5,012; python: 4,040; makefile: 956
file content (153 lines) | stat: -rw-r--r-- 2,943 bytes parent folder | download | duplicates (11)
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
/*
 * Embedded Linux library
 * Copyright (C) 2011-2014  Intel Corporation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>

#include "useful.h"
#include "idle.h"
#include "main-private.h"
#include "private.h"

/**
 * SECTION:idle
 * @short_description: Idle processing support
 *
 * Idle processing support
 */

/**
 * l_idle:
 *
 * Opaque object representing the idle time event.
 */
struct l_idle {
	union {
		l_idle_notify_cb_t callback;
		l_idle_oneshot_cb_t oneshot;
	};

	l_idle_destroy_cb_t destroy;
	void *user_data;
	int id;
};

static void idle_destroy(void *user_data)
{
	struct l_idle *idle = user_data;

	if (idle->destroy)
		idle->destroy(idle->user_data);

	l_free(idle);
}

static void idle_callback(void *user_data)
{
	struct l_idle *idle = user_data;

	if (idle->callback)
		idle->callback(idle, idle->user_data);
}

static void oneshot_callback(void *user_data)
{
	struct l_idle *idle = user_data;

	if (idle->oneshot)
		idle->oneshot(idle->user_data);

	idle_remove(idle->id);
}

/**
 * l_idle_create:
 * @callback: idle callback function
 * @user_data: user data provided to idle callback function
 * @destroy: destroy function for user data
 *
 * Create a new idle event processing object.
 *
 * The idle callback will be called until canceled using l_idle_remove().
 *
 * Returns: a newly allocated #l_idle object
 **/
LIB_EXPORT struct l_idle *l_idle_create(l_idle_notify_cb_t callback,
			void *user_data, l_idle_destroy_cb_t destroy)
{
	struct l_idle *idle;

	if (unlikely(!callback))
		return NULL;

	idle = l_new(struct l_idle, 1);

	idle->callback = callback;
	idle->destroy = destroy;
	idle->user_data = user_data;

	idle->id = idle_add(idle_callback, idle, 0, idle_destroy);
	if (idle->id < 0) {
		l_free(idle);
		return NULL;
	}

	return idle;
}

/**
 * l_idle_oneshot:
 * @callback: idle callback function
 * @user_data: user data provided to idle callback function
 * @destroy: destroy function for user data
 *
 * Create a new idle event processing object.  The callback will be called
 * only once at which point the object will be destroyed.
 *
 * Returns: true if the oneshot idle object could be created successfully.
 **/
LIB_EXPORT bool l_idle_oneshot(l_idle_oneshot_cb_t callback, void *user_data,
				l_idle_destroy_cb_t destroy)
{
	struct l_idle *idle;

	if (unlikely(!callback))
		return NULL;

	idle = l_new(struct l_idle, 1);

	idle->oneshot = callback;
	idle->destroy = destroy;
	idle->user_data = user_data;

	idle->id = idle_add(oneshot_callback, idle,
				IDLE_FLAG_NO_WARN_DANGLING, idle_destroy);
	if (idle->id < 0) {
		l_free(idle);
		return false;
	}

	return true;
}
/**
 * l_idle_remove:
 * @idle: idle object
 *
 * Remove idle event processing object.
 **/
LIB_EXPORT void l_idle_remove(struct l_idle *idle)
{
	if (unlikely(!idle))
		return;

	idle_remove(idle->id);
}