00001 /* SVN FILE INFO 00002 * $Revision: 231 $ : Last Committed Revision 00003 * $Date: 2008-10-24 15:58:25 -0700 (Fri, 24 Oct 2008) $ : Last Committed Date */ 00004 /*[ 00005 * Copyright (c) 2007 Integration Engineering Laboratory 00006 University of California, Davis 00007 * 00008 * Permission to use, copy, and distribute this software and its 00009 * documentation for any purpose with or without fee is hereby granted, 00010 * provided that the above copyright notice appear in all copies and 00011 * that both that copyright notice and this permission notice appear 00012 * in supporting documentation. 00013 * 00014 * Permission to modify the software is granted, but not the right to 00015 * distribute the complete modified source code. Modifications are to 00016 * be distributed as patches to the released version. Permission to 00017 * distribute binaries produced by compiling modified sources is granted, 00018 * provided you 00019 * 1. distribute the corresponding source modifications from the 00020 * released version in the form of a patch file along with the binaries, 00021 * 2. add special version identification to distinguish your version 00022 * in addition to the base release version number, 00023 * 3. provide your name and address as the primary contact for the 00024 * support of your modified version, and 00025 * 4. retain our contact information in regard to use of the base 00026 * software. 00027 * Permission to distribute the released version of the source code along 00028 * with corresponding source modifications in the form of a patch file is 00029 * granted with same provisions 2 through 4 for binary distributions. 00030 * 00031 * This software is provided "as is" without express or implied warranty 00032 * to the extent permitted by applicable law. 00033 ]*/ 00034 00035 /* Macro Synopses 00036 * 00037 * Error Checking 00038 * 00039 * CHECK_NULL(var, action) : Checks value of 'var' for NULL. If it is 00040 * null, an error message is printed to stderr and 'action' is 00041 * performed. 00042 * 00043 * Thread Creation/Cancellation 00044 * 00045 * GET_THREAD_MODE(started_threads, thread): Returns 1 if 'thread' is set 00046 * run in 'started_threads'. 00047 * SET_THREAD_ON(a, b) : Sets thread 'b' to 'on' in status 'a'. 00048 * SET_THREAD_OFF(a, b) : See SET_THREAD_ON 00049 * 00050 * Thread Syncronization 00051 * 00052 * MUTEX_T : Platform independant mutex data type. 00053 * COND_T : Platform independant condition variable data type. 00054 * MUTEX_INIT( mutex ) : Platform ind. mutex initialization. 00055 * COND_INIT(cond) : Platform ind. condition variable initialization. 00056 * MUTEX_DESTROY(mutex) : Platform ind. mutex variable destruction. 00057 * COND_DESTROY(cond) : Platform ind. cond variable destruction. 00058 * MUTEX_LOCK(mutex) : Locks mutex 'mutex'. 00059 * MUTEX_UNLOCK(mutex) : Unlocks mutex 'mutex'. 00060 * SIGNAL(cond, mutex, action) : Sends a signal to condition variable 00061 * 'cond' protected by mutex 'mutex'. Executes action 'action' after 00062 * locking mutex 'mutex' but before signalling the condition 00063 * variable. 00064 * WAKE_QUEUE(queue) : Signals and activates a thread sleeping on one 00065 * of the system queues. 00066 * COND_SLEEP(cond, mutex) : Causes a thread to sleep on a condition 00067 * variable 'cond', protected by 'mutex'. 00068 * COND_RESET(cond, mutex) : Resets condition variables after being 00069 * woken from COND_SLEEP. 00070 * 00071 * Ch Datatype Conversions 00072 * 00073 * CH_DATATYPE_SIZE(type, size) : Takes 'ChType_t type' as input, 00074 * fills 'size' with the byte size of 'type'. 00075 * CH_DATATYPE_STRING(type, string) : Takes 'ChType_t type' as input, 00076 * fills 'char* string' with a string of the type. 00077 * CH_STRING_DATATYPE(string, type) : Takes 'char* string' as input 00078 * and fills 'ChType_t type' 00079 * Ch_DATATYPE_STR_TO_VAL(type, string, val) : Takes 'ChType_t type' and 00080 * 'char* string' as input and fills 'void* val' with the value 00081 * contained within 'string'. 00082 */ 00083 #ifndef _MACROS_H_ 00084 #define _MACROS_H_ 00085 00086 #ifndef _WIN32 00087 #include <pthread.h> 00088 #include <semaphore.h> 00089 #include "config.h" 00090 #else 00091 #include <windows.h> 00092 #endif 00093 00094 /* * * * * * * * * * * * * * * 00095 * T H R E A D S T A T U S * 00096 * * * * * * * * * * * * * * */ 00097 00098 /************************************* 00099 * AI => Agent Initializing Thread * 00100 * AM => Agent Managing Thread * 00101 * CL => Connection Listening Thread * 00102 * DF => Directory Facilitator Thr. * 00103 * MR => Message Receiving Thread * 00104 * MS => Message Sending Thread * 00105 * CP => Command Prompt Thread * 00106 *************************************/ 00107 00108 /* GET_THREAD_MODE Macro: 00109 * Usage: GET_THREAD_MODE(runthreads, MC_THREAD_AM) 00110 * Returns 1 if thread is set to run, 0 otherwise. */ 00111 #define GET_THREAD_MODE(a, b) ( (a & (1<<b)) / (1<<b) ) 00112 00113 /* SET_THREAD_XXX(runthreads, XX_THREAD); */ 00114 #define SET_THREAD_ON(a, b) a = (a | (1<<b)) 00115 #define SET_THREAD_OFF(a, b) a = (a & (~(1<<b))) 00116 00117 00118 /* Struct Macro */ 00119 #define STRUCT( name, members ) \ 00120 typedef struct name##_s { \ 00121 members \ 00122 } name##_t; \ 00123 typedef name##_t* name##_p; 00124 00125 00126 /* * * * * * * * * * * * * 00127 * U N I X M A C R O S * 00128 * * * * * * * * * * * * */ /* Windows macros follow */ 00129 #ifndef _WIN32 00130 00131 /* ****** * 00132 * THREAD * 00133 * ****** */ 00134 #define PTHREAD_STACK_SIZE 131072 /* This is PTHREAD_STACK_MIN * 8 for me */ 00135 #define THREAD_T pthread_t 00136 #define THREAD_CREATE( thread_handle, function, arg ) \ 00137 pthread_create( \ 00138 thread_handle, \ 00139 &attr, \ 00140 function, \ 00141 (void*) arg \ 00142 ) 00143 00144 #define THREAD_CANCEL( thread_handle ) \ 00145 pthread_cancel( thread_handle ) 00146 00147 #define THREAD_JOIN(thread_handle ) \ 00148 pthread_join( thread_handle, NULL ) 00149 00150 /* ***** */ 00151 /* MUTEX */ 00152 /* ***** */ 00153 00154 /* Typedef */ 00155 #define MUTEX_T pthread_mutex_t 00156 /* Init */ 00157 #define MUTEX_INIT(mutex) \ 00158 pthread_mutex_init(mutex, NULL) 00159 /* Destroy */ 00160 #define MUTEX_DESTROY(mutex) \ 00161 pthread_mutex_destroy(mutex) 00162 /* functions */ 00163 #define MUTEX_LOCK(mutex) \ 00164 if (pthread_mutex_lock( mutex )) \ 00165 fprintf(stderr, "pthread lock error: %s:%d\n", __FILE__, __LINE__) 00166 #define MUTEX_UNLOCK(mutex) \ 00167 pthread_mutex_unlock( mutex ) 00168 #define MUTEX_NEW(mutex) \ 00169 mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); \ 00170 if (mutex == NULL) \ 00171 fprintf(stderr, "Memory Error. %s:%d\n", __FILE__,__LINE__); \ 00172 00173 /* **** * 00174 * COND * 00175 * **** */ 00176 /* Typedef */ 00177 #define COND_T pthread_cond_t 00178 /* Init */ 00179 #define COND_INIT(cond) \ 00180 pthread_cond_init(cond, NULL) 00181 /* Destroy */ 00182 #define COND_DESTROY(cond) \ 00183 pthread_cond_destroy(cond) 00184 /* functions */ 00185 #define COND_WAIT( cond , mutex ) \ 00186 pthread_cond_wait(cond, mutex ) 00187 /* Wait until 'test' is true */ 00188 O$w=a6Yl!ϐSG֪XGN%WVG=|9Fh;%#.DgEO:RrЉ#4u~C OCCgPFm7 sA:Z:A`Xu7lf,O\k7vnX.*uS~ѧ?'Aaz 9]#MEN<j~a2xnw