Skip to content

Commit

Permalink
Replaced 12 second locking system with a slighly more complex one inv…
Browse files Browse the repository at this point in the history
…olving random numbers
  • Loading branch information
crcrewso authored and crcrewso committed Jun 18, 2018
1 parent 98126e4 commit 2475674
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions HEN_HOUSE/cutils/egs_c_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@


#include "egs_c_utils.h"
#include <stdlib.h>

#ifdef WIN32
#include <io.h>
Expand Down Expand Up @@ -226,6 +227,31 @@ void egsLockControlFile(int *status) {
#endif
if( __is_locked == 1 ) { *status = 0; return; }
if( __my_fd < 0 ) { *status = -1; return; }

/**
* Random lock control introduced to ensure a large number of threads won't lock up and cause a failure.
*/



int elapsedTime = 0;
int cycleTime = 0;

while (elapsedTime < 1200){
/* for the first tries < 120 seconds a randomizer is used to protect against multiple threads continuing to collide.
* Following the first 2 minutes of this, 30 second intervals are used, this should only triggered if the storage system
* is incredibly slow
*/
if (elapsedTime < 120) { cycleTime = 2 + (rand() % 20); }
else {cycleTime = 30;}
*status = fcntl(__my_fd,F_SETLK,&fl_write);
if( *status == 0 ) { __is_locked = 1; return; }
elapsedTime += cycleTime;
sleep(cycleTime);
}
printf("egsLockControlFile: failed to lock file for 1200 seconds...\n");

/*
for(i1=0; i1<5; i1++) {
for(i2=0; i2<12; i2++) {
*status = fcntl(__my_fd,F_SETLK,&fl_write);
Expand All @@ -234,6 +260,7 @@ void egsLockControlFile(int *status) {
}
printf("egsLockControlFile: failed to lock file for 12 seconds...\n");
}
*/
/*
*status = fcntl(__my_fd,F_SETLKW,&fl_write);
if( *status == 0 ) __is_locked = 1;
Expand Down

0 comments on commit 2475674

Please sign in to comment.