diff options
author | Jiyoung Yun <jy910.yun@samsung.com> | 2015-04-07 15:21:41 +0900 |
---|---|---|
committer | Jiyoung Yun <jy910.yun@samsung.com> | 2015-04-07 19:37:59 +0900 |
commit | d32f876fdb862f41700a06581cdcc6f67a8cd98a (patch) | |
tree | a321d239dc84ae8ecd095fca0ddf670fb32cc0bd | |
parent | 36381a48ea7340f9489d717c8c8b128a88fb99d0 (diff) | |
download | deviced-d32f876fdb862f41700a06581cdcc6f67a8cd98a.tar.gz deviced-d32f876fdb862f41700a06581cdcc6f67a8cd98a.tar.bz2 deviced-d32f876fdb862f41700a06581cdcc6f67a8cd98a.zip |
deviced: Add device idler logic
It allows for callbacks to be called when the daemon is idle state.
func:
int add_idle_request(int (*func)(void *data), void *data);
Change-Id: I2d7879874da5bd072b6ed205ff4d33a81e919dc7
Signed-off-by: Jiyoung Yun <jy910.yun@samsung.com>
-rwxr-xr-x | CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/core/device-idler.c | 102 | ||||
-rw-r--r-- | src/core/device-idler.h | 29 |
3 files changed, 132 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 60528a8a..1f9b2b2b 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ SET(SRCS src/core/common.c src/core/config-parser.c src/control/control.c + src/core/device-idler.c src/core/device-notifier.c src/core/devices.c src/core/edbus-handler.c diff --git a/src/core/device-idler.c b/src/core/device-idler.c new file mode 100644 index 00000000..06e2317e --- /dev/null +++ b/src/core/device-idler.c @@ -0,0 +1,102 @@ +/* + * deviced + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include <Ecore.h> +#include <glib.h> +#include <errno.h> + +#include "log.h" + +struct device_request { + int (*func)(void *data); + void *data; +}; + +static GQueue req_queue = G_QUEUE_INIT; +static Ecore_Idler *idler; + +static int free_request(struct device_request *req) +{ + if (!req) + return -EINVAL; + + free(req); + return 0; +} + +static Eina_Bool idler_cb(void *data) +{ + struct device_request *req; + + req = g_queue_pop_head(&req_queue); + if (req) { + if (req->func) + req->func(req->data); + free_request(req); + } + + if (g_queue_is_empty(&req_queue)) { + idler = NULL; + return ECORE_CALLBACK_CANCEL; + } + + return ECORE_CALLBACK_RENEW; +} + +static void process_next_request_in_idle(void) +{ + if (g_queue_is_empty(&req_queue)) + return; + + if (idler) + return; + + idler = ecore_idler_add(idler_cb, NULL); + /** + * if idler is null, + * it means whole system might be an abnormal state. + * so it just prints out error log. + */ + if (!idler) + _E("fail to add request to idler"); +} + +int add_idle_request(int (*func)(void *data), void *data) +{ + struct device_request *req; + + if (!func) { + _E("invalid argumet : func(NULL)"); + return -EINVAL; + } + + req = calloc(1, sizeof(struct device_request)); + if (!req) { + _E("fail to allocate request : %s", strerror(errno)); + return -errno; + } + + req->func = func; + req->data = data; + + g_queue_push_tail(&req_queue, req); + process_next_request_in_idle(); + + return 0; +} diff --git a/src/core/device-idler.h b/src/core/device-idler.h new file mode 100644 index 00000000..add2ecf2 --- /dev/null +++ b/src/core/device-idler.h @@ -0,0 +1,29 @@ +/* + * deviced + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef __DEVICE_IDLER_H__ +#define __DEVICE_IDLER_H__ + +/* + * To allow for callbacks to be called when the daemon is idle state. + */ + +int add_idle_request(int (*func)(void *data), void *data); + +#endif /* __DEVICE_IDLER_H__ */ |