Helixis 1.0
Task Programming API
sources/native/native_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         hlx_list_construct(&manager->handlers);
00059         return ((hlx_event_manager*)manager);
00060 }
00061 
00062 void                                    hlx_event_manager_destroy(hlx_event_manager* manager)
00063 {
00064         hlx_list_node*                  it;
00065 
00066         for (it = ((_hlx_event_manager*)manager)->handlers.first; it; it = it->next)
00067         {
00068                 _hlx_event_handler_destroy((_hlx_event_handler*)it->data);
00069                 gl_api.free_entry((_hlx_event_handler*)it->data);
00070         }
00071         hlx_list_destruct(&(((_hlx_event_manager*)manager)->handlers));
00072         _hlx_event_default_manager_destroy();
00073 }
00074 
00075 hlx_event_errors                _hlx_event_handler_create_by_hash(_hlx_event_manager* manager, unsigned long id, unsigned int size)
00076 {
00077         _hlx_event_handler*     handler;
00078 
00079         if (_hlx_event_return_handler_by_hash(manager, id))
00080                 return (EV_ERROR_ALREADY_EXISTS);
00081         if ((handler = gl_api.malloc_entry(sizeof(*handler))) == 0)
00082                 return (EV_ERROR_MEMORY_EXHAUSTED);
00083         handler->hash_code = id;
00084         hlx_list_construct(&handler->callbacks);
00085         if (hlx_list_push_front(&manager->handlers, handler) == 0)
00086                 return (EV_ERROR_MEMORY_EXHAUSTED);
00087         handler->param_size = size;
00088         return (EV_ERROR_SUCCESS);
00089 }
00090 
00091 void    _hlx_event_handler_destroy(_hlx_event_handler* handler)
00092 {
00093         hlx_list_node*                  it;
00094 
00095         for (it = handler->callbacks.first; it; it = it->next)
00096                 _hlx_event_delete_callback(it->data, 0);
00097         hlx_list_destruct(&handler->callbacks);
00098 }
00099 
00100 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)
00101 {
00102         HLX_UNUSED(id);
00103         HLX_UNUSED(manager);
00104         callback->group_id = group_id;
00105         if (hlx_list_push_front(&handler->callbacks, callback) == 0)
00106         {
00107                 gl_api.free_entry(callback);
00108                 return (EV_ERROR_MEMORY_EXHAUSTED);
00109         }
00110         return (EV_ERROR_SUCCESS);
00111 }
00112 
00113 _hlx_event_handler*                     _hlx_event_return_handler_by_hash(_hlx_event_manager* manager, unsigned long id)
00114 {
00115         hlx_list_node*                  it;
00116 
00117         for (it = manager->handlers.first; it; it = it->next)
00118                 if (((_hlx_event_handler*)it->data)->hash_code == id)
00119                         return ((_hlx_event_handler*)it->data);
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         hlx_list_node*                          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         for (it = handler->callbacks.first; it; it = it->next)
00138         {
00139                 callback = (_hlx_event_callback*)it->data;
00140                 if (callback->func)
00141                 {
00142                         func_args.user_data = callback->params.user_data;
00143                         callback->func(&func_args);
00144                 }
00145                 if (callback->task)
00146                 {
00147                         if ((task_args = gl_api.malloc_entry(sizeof(hlx_event_args))) == 0)
00148                                 return (EV_ERROR_MEMORY_EXHAUSTED);
00149                         task_args->user_data = callback->params.user_data;
00150                         task_args->callback = callback->params.callback;
00151                         if (handler->param_size)
00152                         {
00153                                 if ((task_args->event_data = gl_api.malloc_entry(handler->param_size)) == 0)
00154                                         return (EV_ERROR_MEMORY_EXHAUSTED);
00155                                 gl_api.memcpy_entry(task_args->event_data, event_data, handler->param_size);
00156                         }
00157                         else
00158                                 task_args->event_data = 0;
00159                         if (callback->group_id > 0)
00160                                 hlx_core_add_task_in_group_callback(callback->task, task_args, callback->group_id, 0, _hlx_event_task_callback);
00161                         else
00162                                 hlx_core_add_task_callback(callback->task, task_args, 0,_hlx_event_task_callback);
00163                 }
00164         }
00165         return (EV_ERROR_SUCCESS);
00166 }
00167 
00168 #endif /* HLX_BUILD_WITH_PARALLEL_THREADING */
00169 
00170 #ifdef __cplusplus
00171 }
00172 #endif
00173 
00174 /*
00175 ** END OF FILE
00176 */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines