Helixis 1.0
Task Programming API
sources/parallel/parallel_task_groups.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 
00046 /*
00047 ** FUNCTIONS
00048 */
00049 
00050 void hlx_group_construct(hlx_group* group)
00051 {
00052         group->id = (hlx_group_id)group;
00053         hlx_list_construct(&group->tasks);
00054         queue_new(&group->buf_tasks, MAX_TASK_PER_GROUP);
00055         gl_api.thread_api.thread_construct(&(group->thread));
00056         group->size = 0;
00057         group->active_count = 0;
00058 }
00059 
00060 void hlx_group_destruct(hlx_group* group)
00061 {
00062         hlx_list_node* it = group->tasks.first;
00063         hlx_task* task;
00064 
00065         while (it != 0)
00066         {
00067                 task = it->data;
00068                 if (task->stack != 0)
00069                         gl_api.free_entry(task->stack);
00070                 gl_api.free_entry(it->data);
00071                 it = it->next;
00072         }
00073         hlx_list_clear(&group->tasks);
00074         while (queue_dequeue(group->buf_tasks, (void**)&task))
00075         {
00076                 if (task->stack != 0)
00077                         gl_api.free_entry(task->stack);
00078                 gl_api.free_entry(task);
00079         }
00080         queue_delete(group->buf_tasks, 0, 0);
00081         gl_api.thread_api.thread_destruct(&(group->thread));
00082 }
00083 
00084 void hlx_group_flush_task_buffer(hlx_group* group)
00085 {
00086         hlx_task* task;
00087 
00088   task = 0;
00089         while (queue_dequeue(group->buf_tasks, (void**)&task))
00090                 hlx_list_push_back(&group->tasks, task);
00091 }
00092 
00093 void hlx_group_bufferize_task(hlx_group* group, hlx_task* task)
00094 {
00095         if (!queue_enqueue(group->buf_tasks, task))
00096                 queue_guaranteed_enqueue(group->buf_tasks, task);
00097         if (task->status == HLX_TASK_STATUS_RUN)
00098                 gl_api.atomic_incr_entry(&group->active_count);
00099         gl_api.thread_api.thread_resume(&(group->thread));
00100 }
00101 
00102 void hlx_group_add_task(hlx_group* group, hlx_task* task)
00103 {
00104         hlx_list_push_back(&group->tasks, task);
00105         if (task->status == HLX_TASK_STATUS_RUN)
00106                 gl_api.atomic_incr_entry(&group->active_count);
00107         gl_api.thread_api.thread_resume(&(group->thread));
00108 
00109 }
00110 
00111 void hlx_group_del_task(hlx_group* group, hlx_task* task)
00112 {
00113         hlx_list_node* it;
00114 
00115         if ((it = hlx_list_find(&group->tasks, task)) == 0)
00116                 return;
00117         if (task->status == HLX_TASK_STATUS_RUN)
00118                 gl_api.atomic_decr_entry(&group->active_count);
00119         gl_api.free_entry(it->data);
00120         hlx_list_erase(&group->tasks, it);
00121 }
00122 
00123 atom_t hlx_group_get_size(hlx_group* group)
00124 {
00125         atom_t queue_size;
00126 
00127         queue_query(group->buf_tasks, QUEUE_QUERY_ELEMENT_COUNT, 0, &queue_size);
00128         return (group->tasks.size + queue_size);
00129 }
00130 
00131 #endif /* !HLX_BUILD_WITH_PARALLEL_THREADING */
00132 
00133 #ifdef __cplusplus
00134 }
00135 #endif
00136 
00137 /*
00138 ** END OF FILE
00139 */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines