00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "rcs_defs.hh"
00026
00027 #ifdef EXTERN_C_STD_HEADERS
00028 extern "C"
00029 {
00030 #endif
00031
00032 #ifndef UNDER_CE
00033 #include <stddef.h>
00034 #endif
00035
00036 #ifdef EXTERN_C_STD_HEADERS
00037 };
00038 #endif
00039
00040 #include "timer.hh"
00041 #include "memsem.hh"
00042 #include "rcs_prnt.hh"
00043
00044 #define TIMEOUT_MIN ((double) 1.0E-6)
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #if 0
00069 int
00070 mem_get_access (void *data, long connection_number,
00071 long total_connections, double timeout, double semdelay,
00072 int read_only, int split_buffer, char &toggle_bit)
00073 #endif
00074 int mem_get_access (struct mem_access_object *mo)
00075 {
00076 register char *mylock;
00077 register char current_lock;
00078 register char *plock;
00079 char *lastlock;
00080 int semaphores_clear;
00081 double start_time, time;
00082 int split_buffer = 0;
00083 int read_only;
00084 int total_connections;
00085 int connection_number;
00086 double timeout;
00087 #ifdef DEBUG
00088 rcs_print ("mem_get_access: - Time = %lf\n", etime ());
00089 #endif
00090 #ifdef DEBUG
00091 static int count = 0;
00092 count++;
00093 #endif
00094
00095
00096 if ((total_connections = mo->total_connections) <=
00097 (connection_number = mo->connection_number) || connection_number < 0)
00098 {
00099 return -1;
00100 }
00101 if (NULL == mo->data)
00102 {
00103 return -1;
00104 }
00105
00106
00107 int wait_requested = 1;
00108 lastlock = ((char *) mo->data) + total_connections;
00109 mylock = ((char *) mo->data) + connection_number;
00110 time = start_time = etime ();
00111 while (wait_requested
00112 && (time - start_time < mo->timeout / 2 || mo->timeout < 0))
00113 {
00114 wait_requested = 0;
00115 for (plock = (char *) mo->data; plock < lastlock; plock++)
00116 {
00117 if (5 == (current_lock = *plock) && plock != mylock)
00118 {
00119 wait_requested = 1;
00120 }
00121 }
00122 if (wait_requested)
00123 {
00124 esleep (mo->sem_delay);
00125 }
00126 }
00127
00128
00129
00130 *mylock = 4;
00131 mo->toggle_bit = ((char *) mo->data)[total_connections];
00132 read_only = mo->read_only;
00133 #ifdef DEBUG
00134 if (read_only)
00135 {
00136 rcs_print ("added sleep: %d - Time = %lf\n", __LINE__, etime ());
00137 esleep (0.02);
00138 }
00139 #endif
00140 if (read_only)
00141 {
00142 split_buffer = mo->split_buffer;
00143 if (split_buffer)
00144 {
00145 *mylock = 2 + mo->toggle_bit;
00146 return 0;
00147 }
00148 *mylock = 2;
00149 }
00150 else
00151 {
00152 *mylock = 1;
00153 }
00154
00155 #ifdef debug
00156 if (read_only)
00157 {
00158 rcs_print ("added sleep: %d - time = %lf\n", __line__, etime ());
00159 esleep (0.01);
00160 }
00161 #endif
00162 semaphores_clear = 1;
00163 lastlock = ((char *) mo->data) + total_connections;
00164 mo->toggle_bit = ((char *) mo->data)[total_connections];
00165 for (plock = (char *) mo->data; plock < lastlock; plock++)
00166 {
00167 if (0 != (current_lock = *plock))
00168 {
00169 if (plock != mylock)
00170 {
00171 if (!(read_only && current_lock > 1) &&
00172 !(split_buffer && current_lock == 2 + mo->toggle_bit)
00173 && (current_lock != 5))
00174 {
00175 semaphores_clear = 0;
00176 }
00177 }
00178 }
00179 }
00180 #ifdef debug
00181 if (0)
00182 {
00183 rcs_print ("added sleep: %d - time = %lf\n", __line__, etime ());
00184 esleep (0.01);
00185 }
00186 #endif
00187 if (semaphores_clear)
00188 {
00189 return 0;
00190 }
00191 timeout = mo->timeout;
00192 if (timeout < TIMEOUT_MIN && timeout > 0)
00193 {
00194 *mylock = 0;
00195 return (-2);
00196 }
00197
00198 *mylock = 5;
00199
00200 if (NULL != mo->sem)
00201 {
00202 if (-1 == mo->sem->wait ())
00203 {
00204 *mylock = 0;
00205 return -1;
00206 }
00207 }
00208 else
00209 {
00210 esleep (mo->sem_delay);
00211 }
00212 if (read_only)
00213 {
00214 *mylock = 2;
00215 }
00216 else
00217 {
00218 *mylock = 1;
00219 }
00220
00221 #ifdef debug
00222 if (0)
00223 {
00224 rcs_print ("added sleep: %d - time = %lf\n", __line__, etime ());
00225 esleep (0.01);
00226 }
00227 #endif
00228 while ((timeout >= 0) ? ((time - start_time) < timeout) : 1)
00229 {
00230 if (split_buffer)
00231 {
00232 mo->toggle_bit = ((char *) mo->data)[total_connections];
00233 }
00234 semaphores_clear = 1;
00235 plock = (char *) mo->data;
00236 mo->toggle_bit = ((char *) mo->data)[total_connections];
00237 for (; plock < lastlock; plock++)
00238 {
00239 current_lock = *plock;
00240 if (0 != current_lock)
00241 {
00242 if (plock != mylock &&
00243 !(read_only && current_lock > 1) &&
00244 !(split_buffer && current_lock == 2 + mo->toggle_bit)
00245 && (current_lock != 5))
00246 {
00247 semaphores_clear = 0;
00248 }
00249 }
00250 }
00251 if (semaphores_clear)
00252 {
00253 return 0;
00254 }
00255 if (NULL != mo->sem)
00256 {
00257 *mylock = 5;
00258 mo->sem->wait ();
00259 }
00260 else
00261 {
00262 *mylock = 5;
00263 esleep (mo->sem_delay);
00264 }
00265 if (read_only)
00266 {
00267 *mylock = 2;
00268 }
00269 else
00270 {
00271 *mylock = 1;
00272 }
00273 #ifdef DEBUG
00274 if (0)
00275 {
00276 rcs_print ("added sleep: %d - Time = %lf\n", __LINE__, etime ());
00277 esleep (0.01);
00278 }
00279 #endif
00280 time = etime ();
00281 }
00282 *mylock = 0;
00283 return (-2);
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 int
00299 mem_release_access (struct mem_access_object *mo)
00300 {
00301 int i;
00302 int process_waiting = 0;
00303 #ifdef DEBUG
00304 rcs_print ("mem_release_access: - Time = %lf\n", etime ());
00305 #endif
00306 #ifdef DEBUG
00307 static int count = 0;
00308 count++;
00309 #endif
00310 if (NULL == mo)
00311 {
00312 rcs_print_error ("mem_release_access: Invalid memory object.\n");
00313 }
00314 if (NULL == mo->data || mo->connection_number < 0)
00315 {
00316 rcs_print_error ("mem_release_access: Invalid memory object.\n");
00317 return -1;
00318 }
00319
00320 if (mo->sem != NULL)
00321 {
00322 process_waiting = 0;
00323 for (i = 0; i < mo->total_connections; i++)
00324 {
00325 if (((char *) mo->data)[i] == 5)
00326 {
00327 process_waiting = 1;
00328 break;
00329 }
00330 }
00331 }
00332 #ifdef DEBUG
00333 if (0)
00334 {
00335 rcs_print ("added sleep: %d - Time = %lf\n", __LINE__, etime ());
00336 esleep (0.01);
00337 }
00338 #endif
00339
00340
00341
00342 if (mo->split_buffer && ((char *) mo->data)[mo->connection_number] == 1)
00343 {
00344 ((char *) mo->data)[mo->total_connections] = !(mo->toggle_bit);
00345 }
00346 #ifdef DEBUG
00347 if (0)
00348 {
00349 rcs_print ("added sleep: %d - Time = %lf\n", __LINE__, etime ());
00350 esleep (0.01);
00351 }
00352 #endif
00353
00354 ((char *) mo->data)[mo->connection_number] = 0;
00355 #ifdef DEBUG
00356 if (0)
00357 {
00358 rcs_print ("added sleep: %d - Time = %lf\n", __LINE__, etime ());
00359 esleep (0.01);
00360 }
00361 #endif
00362
00363 if (mo->sem != NULL)
00364 {
00365 if (process_waiting)
00366 {
00367 mo->sem->post ();
00368 }
00369 }
00370 return (0);
00371 }