Helixis 1.0
Task Programming API
sources/parallel/parallel_events.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2009-2011, helixis.org
00003 ** All rights reserved.
00004 **
00005 ** Redistribution and use in source and binary forms, with or without modification, are permitted provided
00006 ** that the following conditions are met:
00007 **
00008 ** Redistributions of source code must retain the above copyright notice, this list of conditions and the
00009 ** following disclaimer.
00010 **
00011 ** Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00012 ** the following disclaimer in the documentation and/or other materials provided with the distribution.
00013 **
00014 ** Neither the name of the helixis.org nor the names of its contributors may be used to endorse or promote
00015 ** products derived from this software without specific prior written permission.
00016 **
00017 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00018 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00019 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00020 ** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00021 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00022 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00023 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00024 ** POSSIBILITY OF SUCH DAMAGE.
00025 */
00026 
00027 /*
00028 ** INCLUDES
00029 */
00030 
00031 #include "hlx/core.h"
00032 #include "helixis_internal.h"
00033 
00034 #if defined(HLX_BUILD_WITH_PARALLEL_THREADING)
00035 
00036 #ifdef __cplusplus
00037 extern "C"      {
00038 #endif
00039 
00040 /*
00041 ** EXTERNALS
00042 */
00043 
00044 extern hlx_api gl_api;
00045 extern hlx_scheduler    gl_scheduler;
00046 extern hlx_event_manager*       gl_event_manager;
00047 
00048 /*
00049 ** FUNCTIONS
00050 */
00051 
00052 hlx_event_manager*              hlx_event_manager_create()
00053 {
00054         _hlx_event_manager*     manager;
00055 
00056         if ((manager = gl_api.malloc_entry(sizeof(*manager))) == 0)
00057                 return 0;
00058         slist_new(&manager->handlers, 0, 0);
00059         return ((hlx_event_manager*)manager);
00060 }
00061 
00062 void                                    hlx_event_manager_destroy(hlx_event_manager* manager)
00063 {
00064         struct slist_element*   it;
00065         _hlx_event_handler*     handler;
00066 
00067         it = 0;
00068         while (slist_get_head_and_then_next(((_hlx_event_manager*)manager)->handlers, &it))
00069         {
00070                 slist_get_user_data_from_element(it, (void **)&handler);
00071                 _hlx_event_handler_destroy(handler);
00072                 gl_api.free_entry(handler);
00073         }
00074         slist_delete(((_hlx_event_manager*)manager)->handlers);
00075         _hlx_event_default_manager_destroy();
00076 }
00077 
00078 hlx_event_errors                _hlx_event_handler_create_by_hash(_hlx_event_manager* manager, unsigned long id, unsigned int size)
00079 {
00080         _hlx_event_handler*     handler;
00081 
00082         if (_hlx_event_return_handler_by_hash(manager, id))
00083                 return (EV_ERROR_ALREADY_EXISTS);
00084         if ((handler = gl_api.malloc_entry(sizeof(*handler))) == 0)
00085                 return (EV_ERROR_MEMORY_EXHAUSTED);
00086         handler->hash_code = id;
00087         if (slist_new(&handler->callbacks, _hlx_event_delete_callback, 0) == 0 || slist_new_head(manager->handlers, handler) == 0)
00088                 return (EV_ERROR_MEMORY_EXHAUSTED);
00089         handler->param_size = size;
00090         return (EV_ERROR_SUCCESS);
00091 }
00092 
00093 void    _hlx_event_handler_destroy(_hlx_event_handler* handler)
00094 {
00095         slist_delete_all_elements(handler->callbacks);
00096 }
00097 
00098 hlx_event_errors                        _hlx_event_register_callback_with_group(_hlx_event_manager* manager, _hlx_event_handler* handler, unsigned long id, _hlx_event_callback* callback, hlx_group_id group_id)
00099 {
00100         HLX_UNUSED(id);
00101         HLX_UNUSED(manager);
00102         callback->group_id = group_id;
00103         if (slist_new_head(handler->callbacks, callback) == 0)
00104         {
00105                 gl_api.free_entry(callback);
00106                 return (EV_ERROR_MEMORY_EXHAUSTED);
00107         }
00108         return (EV_ERROR_SUCCESS);
00109 }
00110 
00111 _hlx_event_handler*                     _hlx_event_return_handler_by_hash(_hlx_event_manager* manager, unsigned long id)
00112 {
00113         _hlx_event_handler*             handler;
00114         struct slist_element*   it;
00115 
00116         it = 0;
00117         while (slist_get_head_and_then_next(manager->handlers, &it))
00118                 if (slist_get_user_data_from_element(it, (void **)&handler) && handler->hash_code == id)
00119                         return (handler);
00120         return (0);
00121 }
00122 
00123 hlx_event_errors                        hlx_event_notify(hlx_event_manager* manager, unsigned long id, void* event_data)
00124 {
00125         _hlx_event_handler*             handler;
00126         _hlx_event_callback*    callback;
00127         hlx_event_args*                 task_args;
00128         hlx_event_args                  func_args;
00129         struct slist_element*   it;
00130 
00131         if ((handler = _hlx_event_return_handler((_hlx_event_manager*)manager, id)) == 0)
00132                 return (EV_ERROR_UNDEFINED);
00133         if (handler->param_size && event_data == 0)
00134                 return (EV_ERROR_NO_EVENT_DATA);
00135         func_args.event_data = event_data;
00136         func_args.callback = 0;
00137         it = 0;
00138         while (slist_get_head_and_then_next(handler->callbacks, &it))
00139         {
00140                 slist_get_user_data_from_element(it, (void **)&callback);
00141                 if (callback->func)
00142                 {
00143                         func_args.user_data = callback->params.user_data;
00144                         callback->func(&func_args);
00145                 }
00146                 if (callback->task)
00147                 {
00148                         if ((task_args = gl_api.malloc_entry(sizeof(hlx_event_args))) == 0)
00149                                 return (EV_ERROR_MEMORY_EXHAUSTED);
00150                         task_args->user_data = callback->params.user_data;
00151                         task_args->callback = callback->params.callback;
00152                         if (handler->param_size)
00153                         {
00154                                 if ((task_args->event_data = gl_api.malloc_entry(handler->param_size)) == 0)
00155                                         return (EV_ERROR_MEMORY_EXHAUSTED);
00156                                 gl_api.memcpy_entry(task_args->event_data, event_data, handler->param_size);
00157                         }
00158                         else
00159                                 task_args->event_data = 0;
00160                         if (callback->group_id > 0)
00161                                 hlx_core_add_task_in_group_callback(callback->task, task_args, callback->group_id, 0, _hlx_event_task_callback);
00162                         else
00163                                 hlx_core_add_task_callback(callback->task, task_args, 0, _hlx_event_task_callback);
00164                 }
00165         }
00166         return (EV_ERROR_SUCCESS);
00167 }
00168 
00169 #endif /* HLX_BUILD_WITH_PARALLEL_THREADING */
00170 
00171 #ifdef __cplusplus
00172 }
00173 #endif
00174 
00175 /*
00176 ** END OF FILE
00177 */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines