Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/loop.c | 15 ++++++++++++
lib/loop_int.h | 3 ++
lib/loop_job.c | 6 ++++-
lib/loop_poll.c | 62 +++++++++++++++++++++++++++++++++++++++++---------
lib/loop_timerlist.c | 27 +++++++++++++++++----
tests/check_loop.c | 19 ++++++++++++---
6 files changed, 111 insertions(+), 21 deletions(-)
diff --git a/lib/loop.c b/lib/loop.c
index f996f1f..a529bb3 100644
--- a/lib/loop.c
+++ b/lib/loop.c
@@ -26,6 +26,8 @@
#include "loop_int.h"
#include "util_int.h"
+static struct qb_loop *default_intance = NULL;
+
static void
qb_loop_run_level(struct qb_loop_level *level)
{
@@ -69,6 +71,12 @@ qb_loop_level_item_del(struct qb_loop_level *level, struct qb_loop_item
*job)
}
struct qb_loop *
+qb_loop_default_get(void)
+{
+ return default_intance;
+}
+
+struct qb_loop *
qb_loop_create(void)
{
struct qb_loop *l = malloc(sizeof(struct qb_loop));
@@ -93,6 +101,9 @@ qb_loop_create(void)
l->fd_source = qb_loop_poll_create(l);
l->signal_source = qb_loop_signals_create(l);
+ if (default_intance == NULL) {
+ default_intance = l;
+ }
return l;
}
@@ -103,6 +114,10 @@ qb_loop_destroy(struct qb_loop *l)
qb_loop_jobs_destroy(l);
qb_loop_poll_destroy(l);
qb_loop_signals_destroy(l);
+
+ if (default_intance == l) {
+ default_intance = NULL;
+ }
free(l);
}
diff --git a/lib/loop_int.h b/lib/loop_int.h
index 84c18cd..672be21 100644
--- a/lib/loop_int.h
+++ b/lib/loop_int.h
@@ -66,6 +66,9 @@ struct qb_loop {
struct qb_loop_source * signal_source;
};
+struct qb_loop *
+qb_loop_default_get(void);
+
struct qb_loop_source *
qb_loop_jobs_create(struct qb_loop *l);
diff --git a/lib/loop_job.c b/lib/loop_job.c
index 32aa7b9..3f261f2 100644
--- a/lib/loop_job.c
+++ b/lib/loop_job.c
@@ -87,12 +87,16 @@ qb_loop_jobs_destroy(struct qb_loop *l)
}
int32_t
-qb_loop_job_add(struct qb_loop *l,
+qb_loop_job_add(struct qb_loop *lp,
enum qb_loop_priority p,
void *data, qb_loop_job_dispatch_fn dispatch_fn)
{
struct qb_loop_job *job;
+ struct qb_loop *l = lp;
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
if (l == NULL || dispatch_fn == NULL) {
return -EINVAL;
}
diff --git a/lib/loop_poll.c b/lib/loop_poll.c
index ae4286c..c798633 100644
--- a/lib/loop_poll.c
+++ b/lib/loop_poll.c
@@ -550,7 +550,7 @@ _qb_poll_add_to_jobs_(struct qb_loop *l, struct qb_poll_entry *pe)
}
int32_t
-qb_loop_poll_add(struct qb_loop * l,
+qb_loop_poll_add(struct qb_loop * lp,
enum qb_loop_priority p,
int32_t fd,
int32_t events,
@@ -560,6 +560,11 @@ qb_loop_poll_add(struct qb_loop * l,
int32_t size;
int32_t new_size;
int32_t res;
+ struct qb_loop *l = lp;
+
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
size = ((struct qb_poll_source *)l->fd_source)->poll_entry_count;
res = _poll_add_(l, p, fd, events, data, &pe);
@@ -578,7 +583,7 @@ qb_loop_poll_add(struct qb_loop * l,
}
int32_t
-qb_loop_poll_mod(struct qb_loop * l,
+qb_loop_poll_mod(struct qb_loop * lp,
enum qb_loop_priority p,
int32_t fd,
int32_t events,
@@ -587,10 +592,16 @@ qb_loop_poll_mod(struct qb_loop * l,
uint32_t i;
int32_t res = 0;
struct qb_poll_entry *pe;
- struct qb_poll_source *s = (struct qb_poll_source *)l->fd_source;
+ struct qb_poll_source *s;
#ifdef HAVE_EPOLL
struct epoll_event ev;
#endif /* HAVE_EPOLL */
+ struct qb_loop *l = lp;
+
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
+ s = (struct qb_poll_source *)l->fd_source;
/*
* Find file descriptor to modify events and dispatch function
@@ -626,13 +637,18 @@ qb_loop_poll_mod(struct qb_loop * l,
}
int32_t
-qb_loop_poll_del(struct qb_loop * l, int32_t fd)
+qb_loop_poll_del(struct qb_loop * lp, int32_t fd)
{
int32_t i;
int32_t res = 0;
struct qb_poll_entry *pe;
- struct qb_poll_source *s = (struct qb_poll_source *)l->fd_source;
+ struct qb_poll_source *s;
+ struct qb_loop *l = lp;
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
+ s = (struct qb_poll_source *)l->fd_source;
for (i = 0; i < s->poll_entry_count; i++) {
assert(qb_array_index(s->poll_entries, i, (void **)&pe) == 0);
if (pe->ufd.fd != fd || pe->item.type != QB_LOOP_FD) {
@@ -706,7 +722,7 @@ qb_loop_timer_destroy(struct qb_loop *l)
}
int32_t
-qb_loop_timer_add(struct qb_loop *l,
+qb_loop_timer_add(struct qb_loop *lp,
enum qb_loop_priority p,
uint64_t nsec_duration,
void *data,
@@ -718,7 +734,11 @@ qb_loop_timer_add(struct qb_loop *l,
int32_t res;
int32_t size, new_size;
struct itimerspec its;
+ struct qb_loop *l = lp;
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
if (l == NULL || timer_fn == NULL) {
qb_util_log(LOG_ERR,
"can't add a timer with either (l == NULL || timer_fn == NULL)");
@@ -769,12 +789,16 @@ close_and_return:
}
int32_t
-qb_loop_timer_del(struct qb_loop * l, qb_loop_timer_handle th)
+qb_loop_timer_del(struct qb_loop * lp, qb_loop_timer_handle th)
{
struct qb_poll_entry *pe;
struct qb_poll_source *s;
int32_t res;
+ struct qb_loop *l = lp;
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
if (l == NULL || th == 0) {
return -EINVAL;
}
@@ -823,13 +847,17 @@ qb_loop_timer_del(struct qb_loop * l, qb_loop_timer_handle th)
}
uint64_t
-qb_loop_timer_expire_time_get(struct qb_loop * l, qb_loop_timer_handle th)
+qb_loop_timer_expire_time_get(struct qb_loop * lp, qb_loop_timer_handle th)
{
struct qb_poll_entry *pe;
struct qb_poll_source *s;
int32_t res = 0;
struct itimerspec its;
+ struct qb_loop *l = lp;
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
if (l == NULL || th == 0) {
return 0;
}
@@ -1044,7 +1072,7 @@ _adjust_sigactions_(struct qb_signal_source *s)
}
int32_t
-qb_loop_signal_add(qb_loop_t * l,
+qb_loop_signal_add(qb_loop_t * lp,
enum qb_loop_priority p,
int32_t the_sig,
void *data,
@@ -1053,7 +1081,11 @@ qb_loop_signal_add(qb_loop_t * l,
{
struct qb_loop_sig *sig;
struct qb_signal_source *s;
+ struct qb_loop *l = lp;
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
if (l == NULL || dispatch_fn == NULL) {
return -EINVAL;
}
@@ -1086,7 +1118,7 @@ qb_loop_signal_add(qb_loop_t * l,
}
int32_t
-qb_loop_signal_mod(qb_loop_t * l,
+qb_loop_signal_mod(qb_loop_t * lp,
enum qb_loop_priority p,
int32_t the_sig,
void *data,
@@ -1095,7 +1127,11 @@ qb_loop_signal_mod(qb_loop_t * l,
{
struct qb_signal_source *s;
struct qb_loop_sig *sig = (struct qb_loop_sig *)handle;
+ struct qb_loop *l = lp;
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
if (l == NULL || dispatch_fn == NULL || handle == NULL) {
return -EINVAL;
}
@@ -1117,10 +1153,14 @@ qb_loop_signal_mod(qb_loop_t * l,
}
int32_t
-qb_loop_signal_del(qb_loop_t * l, qb_loop_signal_handle handle)
+qb_loop_signal_del(qb_loop_t * lp, qb_loop_signal_handle handle)
{
struct qb_loop_sig *sig = (struct qb_loop_sig *)handle;
+ struct qb_loop *l = lp;
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
qb_list_del(&sig->item.list);
free(sig);
return 0;
diff --git a/lib/loop_timerlist.c b/lib/loop_timerlist.c
index 7e7cb19..dc28a55 100644
--- a/lib/loop_timerlist.c
+++ b/lib/loop_timerlist.c
@@ -171,7 +171,7 @@ _get_empty_array_position_(struct qb_timer_source *s)
}
int32_t
-qb_loop_timer_add(struct qb_loop * l,
+qb_loop_timer_add(struct qb_loop * lp,
enum qb_loop_priority p,
uint64_t nsec_duration,
void *data,
@@ -181,6 +181,11 @@ qb_loop_timer_add(struct qb_loop * l,
struct qb_loop_timer *t;
struct qb_timer_source *my_src;
int32_t i;
+ struct qb_loop *l = lp;
+
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
if (l == NULL || timer_fn == NULL) {
return -EINVAL;
@@ -214,11 +219,17 @@ qb_loop_timer_add(struct qb_loop * l,
}
int32_t
-qb_loop_timer_del(struct qb_loop * l, qb_loop_timer_handle th)
+qb_loop_timer_del(struct qb_loop * lp, qb_loop_timer_handle th)
{
- struct qb_timer_source *s = (struct qb_timer_source *)l->timer_source;
+ struct qb_timer_source *s;
struct qb_loop_timer *t;
int32_t res;
+ struct qb_loop *l = lp;
+
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
+ s = (struct qb_timer_source *)l->timer_source;
res = _timer_from_handle_(s, th, &t);
if (res != 0) {
@@ -243,11 +254,17 @@ qb_loop_timer_del(struct qb_loop * l, qb_loop_timer_handle th)
}
uint64_t
-qb_loop_timer_expire_time_get(struct qb_loop * l, qb_loop_timer_handle th)
+qb_loop_timer_expire_time_get(struct qb_loop * lp, qb_loop_timer_handle th)
{
- struct qb_timer_source *s = (struct qb_timer_source *)l->timer_source;
+ struct qb_timer_source *s;
struct qb_loop_timer *t;
int32_t res;
+ struct qb_loop *l = lp;
+
+ if (l == NULL) {
+ l = qb_loop_default_get();
+ }
+ s = (struct qb_timer_source *)l->timer_source;
res = _timer_from_handle_(s, th, &t);
if (res != 0) {
diff --git a/tests/check_loop.c b/tests/check_loop.c
index f135cb1..3090faf 100644
--- a/tests/check_loop.c
+++ b/tests/check_loop.c
@@ -107,11 +107,16 @@ static void job_1_add_nuts(void *data)
START_TEST(test_loop_job_input)
{
int32_t res;
- qb_loop_t *l = qb_loop_create();
- fail_if(l == NULL);
+ qb_loop_t *l;
res = qb_loop_job_add(NULL, QB_LOOP_LOW, NULL, job_2);
ck_assert_int_eq(res, -EINVAL);
+
+ l = qb_loop_create();
+ fail_if(l == NULL);
+
+ res = qb_loop_job_add(NULL, QB_LOOP_LOW, NULL, job_2);
+ ck_assert_int_eq(res, 0);
res = qb_loop_job_add(l, 89, NULL, job_2);
ck_assert_int_eq(res, -EINVAL);
res = qb_loop_job_add(l, QB_LOOP_LOW, NULL, NULL);
@@ -255,11 +260,17 @@ static qb_loop_timer_handle test_th;
START_TEST(test_loop_timer_input)
{
int32_t res;
- qb_loop_t *l = qb_loop_create();
- fail_if(l == NULL);
+ qb_loop_t *l;
res = qb_loop_timer_add(NULL, QB_LOOP_LOW, 5*QB_TIME_NS_IN_MSEC, NULL, job_2,
&test_th);
ck_assert_int_eq(res, -EINVAL);
+
+ l = qb_loop_create();
+ fail_if(l == NULL);
+
+ res = qb_loop_timer_add(NULL, QB_LOOP_LOW, 5*QB_TIME_NS_IN_MSEC, NULL, job_2,
&test_th);
+ ck_assert_int_eq(res, 0);
+
res = qb_loop_timer_add(l, QB_LOOP_LOW, 5*QB_TIME_NS_IN_MSEC, l, NULL, &test_th);
ck_assert_int_eq(res, -EINVAL);
qb_loop_destroy(l);
--
1.7.7.6