Helixis 1.0
Task Programming API
sources/internal_includes/parallel/parallel_channels.h
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 #ifndef __HLX_PARALLEL_CHANNELS_H__
00028 # define __HLX_PARALLEL_CHANNELS_H__
00029 
00030 #if defined(HLX_BUILD_WITH_PARALLEL_THREADING)
00031 
00032 #ifdef __cplusplus
00033 extern "C"      {
00034 #endif
00035 
00036 /*
00037 ** MACROS
00038 */
00039 
00040 # define HLX_CHAN_AVAILABLE             (0)
00041 # define HLX_CHAN_READING               (1)
00042 # define HLX_CHAN_WRITING               (2)
00043 # define HLX_CHAN_FILLED                (3)
00044 
00045 # define HLX_CHAN_WRITE_GENERATOR(name, type, union_var)        \
00046 int             __hlx_chan_write_##name(unsigned long id, type value)   \
00047 {       \
00048         _hlx_channel            *chan;  \
00049         \
00050         if ((chan = _hlx_get_channel_by_hash(id)) == 0) \
00051            return (0);  \
00052         if (gl_api.atomic_cas_entry(&chan->status, HLX_CHAN_WRITING, HLX_CHAN_AVAILABLE))       \
00053            return (0);  \
00054         chan->data.union_var = value;   \
00055         gl_api.atomic_cas_entry(&chan->status, HLX_CHAN_FILLED, HLX_CHAN_WRITING); \
00056         return (1);     \
00057 }
00058 
00059 # define HLX_CHAN_READ_GENERATOR(name, type, union_var) \
00060 int             __hlx_chan_read_##name(unsigned long id, type* value)   \
00061 {       \
00062         _hlx_channel            *chan;  \
00063         \
00064         if ((chan = _hlx_get_channel_by_hash(id)) == 0) \
00065            return (0);  \
00066         if (!gl_api.atomic_cas_entry(&chan->status, HLX_CHAN_READING, HLX_CHAN_FILLED)) \
00067            return (0);  \
00068         *value = chan->data.union_var;  \
00069         gl_api.atomic_cas_entry(&chan->status, HLX_CHAN_AVAILABLE, HLX_CHAN_READING);   \
00070         return (1);     \
00071 }
00072 
00073 
00074 /*
00075 ** TYPES
00076 */
00077 
00078 typedef union
00079 {
00080         char    c;
00081         short   s;
00082         int             i;
00083         float   f;
00084         void*   p;
00085 }       _hlx_channel_data;
00086 
00087 typedef struct
00088 {
00089         _hlx_channel_data       data;
00090         atom_t                          status;
00091         unsigned long           hash_code;
00092 }                                               _hlx_channel;
00093 
00094 typedef struct
00095 {
00096         struct slist_state      *channels;
00097 }                                               _hlx_channel_manager;
00098 
00099 /*
00100 ** FUNCTIONS
00101 */
00102 
00103 void    _init_channel_manager(void);
00104 void    _destroy_channel_manager(void);
00105 
00106 #ifdef __cplusplus
00107 }
00108 #endif
00109 
00110 #endif /* !HLX_BUILD_WITH_PARALLEL_THREADING */
00111 
00112 #endif /* !__HLX_PARALLEL_CHANNELS_H__ */
00113 
00114 /*
00115 ** END OF FILE
00116 */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines