From e583a127856d4f363ff2ba7b4321bcc8c048edba Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 29 Sep 1997 09:08:36 +0000 Subject: [PATCH] Revised locking system to be thread safe for the server. --- index/index.h | 8 +++--- index/lockidx.c | 37 ++++++++++++++------------ index/locksrv.c | 77 +++++++++++++++++++++++++++++++++++------------------- index/lockutil.c | 21 +++++++-------- index/zserver.c | 38 +++++++++------------------ index/zserver.h | 34 +++++++++++++++++++++--- 6 files changed, 128 insertions(+), 87 deletions(-) diff --git a/index/index.h b/index/index.h index 31fe3b3..2b5e1de 100644 --- a/index/index.h +++ b/index/index.h @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: index.h,v $ - * Revision 1.53 1997-09-25 14:54:43 adam + * Revision 1.54 1997-09-29 09:08:36 adam + * Revised locking system to be thread safe for the server. + * + * Revision 1.53 1997/09/25 14:54:43 adam * WIN32 files lock support. * * Revision 1.52 1997/09/22 12:39:06 adam @@ -293,8 +296,6 @@ int fileExtract (SYSNO *sysno, const char *fname, void rec_prstat (void); -void zebraLockPrefix (Res res, char *pathPrefix); - void zebraIndexLockMsg (const char *str); void zebraIndexUnlock (void); void zebraIndexLock (BFiles bfs, int commitNow, const char *rval); @@ -312,6 +313,7 @@ int zebra_lock (ZebraLockHandle h); int zebra_lock_nb (ZebraLockHandle h); int zebra_unlock (ZebraLockHandle h); int zebra_lock_fd (ZebraLockHandle h); +void zebra_lock_prefix (Res res, char *dst); void init_charmap(Res res); const char **map_chrs_input(void *vp, const char **from, int len); diff --git a/index/lockidx.c b/index/lockidx.c index 9cc80c2..30bc6d0 100644 --- a/index/lockidx.c +++ b/index/lockidx.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: lockidx.c,v $ - * Revision 1.12 1997-09-25 14:54:43 adam + * Revision 1.13 1997-09-29 09:08:36 adam + * Revised locking system to be thread safe for the server. + * + * Revision 1.12 1997/09/25 14:54:43 adam * WIN32 files lock support. * * Revision 1.11 1997/09/17 12:19:15 adam @@ -69,17 +72,16 @@ static ZebraLockHandle server_lock_org = NULL; int zebraIndexWait (int commitPhase) { - char pathPrefix[1024]; - char path[1024]; ZebraLockHandle h; - - zebraLockPrefix (common_resource, pathPrefix); if (server_lock_cmt) zebra_unlock (server_lock_cmt); else { - sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK); + char path[1024]; + + zebra_lock_prefix (common_resource, path); + strcat (path, FNAME_COMMIT_LOCK); server_lock_cmt = zebra_lock_create (path, 1); if (!server_lock_cmt) { @@ -91,7 +93,10 @@ int zebraIndexWait (int commitPhase) zebra_unlock (server_lock_org); else { - sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK); + char path[1024]; + + zebra_lock_prefix (common_resource, path); + strcat (path, FNAME_ORG_LOCK); server_lock_org = zebra_lock_create (path, 1); if (!server_lock_org) { @@ -130,7 +135,6 @@ int zebraIndexWait (int commitPhase) void zebraIndexLockMsg (const char *str) { char path[1024]; - char pathPrefix[1024]; int l, r, fd; assert (server_lock_main); @@ -143,8 +147,8 @@ void zebraIndexLockMsg (const char *str) logf (LOG_FATAL|LOG_ERRNO, "write lock file"); exit (1); } - zebraLockPrefix (common_resource, pathPrefix); - sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME); + zebra_lock_prefix (common_resource, path); + strcat (path, FNAME_TOUCH_TIME); fd = creat (path, 0666); close (fd); } @@ -152,24 +156,23 @@ void zebraIndexLockMsg (const char *str) void zebraIndexUnlock (void) { char path[1024]; - char pathPrefix[1024]; - - zebraLockPrefix (common_resource, pathPrefix); - sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK); + + zebra_lock_prefix (common_resource, path); + strcat (path, FNAME_MAIN_LOCK); unlink (path); } void zebraIndexLock (BFiles bfs, int commitNow, const char *rval) { char path[1024]; - char pathPrefix[1024]; char buf[256]; int r; if (server_lock_main) return ; - zebraLockPrefix (common_resource, pathPrefix); - sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK); + + zebra_lock_prefix (common_resource, path); + strcat (path, FNAME_MAIN_LOCK); while (1) { server_lock_main = zebra_lock_create (path, 2); diff --git a/index/locksrv.c b/index/locksrv.c index 304dc88..2b17ba0 100644 --- a/index/locksrv.c +++ b/index/locksrv.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: locksrv.c,v $ - * Revision 1.9 1997-09-25 14:54:43 adam + * Revision 1.10 1997-09-29 09:08:36 adam + * Revised locking system to be thread safe for the server. + * + * Revision 1.9 1997/09/25 14:54:43 adam * WIN32 files lock support. * * Revision 1.8 1997/09/17 12:19:15 adam @@ -51,28 +54,49 @@ #include "zserver.h" -static ZebraLockHandle server_lock_cmt = NULL; -static ZebraLockHandle server_lock_org = NULL; +int zebra_server_lock_init (ZServerInfo *zi) +{ + char path_prefix[1024]; + + assert (zi->res); + zi->server_lock_cmt = NULL; + zi->server_lock_org = NULL; + + zebra_lock_prefix (zi->res, path_prefix); + zi->server_path_prefix = xmalloc (strlen(path_prefix)+1); + strcpy (zi->server_path_prefix, path_prefix); -int zebraServerLock (Res res, int commitPhase) + logf (LOG_DEBUG, "Locking system initialized"); + return 0; +} + +int zebra_server_lock_destroy (ZServerInfo *zi) { - char pathPrefix[1024]; - char path[1024]; - - zebraLockPrefix (res, pathPrefix); + xfree (zi->server_path_prefix); + zebra_lock_destroy (zi->server_lock_cmt); + zebra_lock_destroy (zi->server_lock_org); + logf (LOG_DEBUG, "Locking system destroyed"); + return 0; +} - if (!server_lock_cmt) +int zebra_server_lock (ZServerInfo *zi, int commitPhase) +{ + if (!zi->server_lock_cmt) { - sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK); - if (!(server_lock_cmt = zebra_lock_create (path, 0))) + char path[1024]; + + strcpy (path, zi->server_path_prefix); + strcat (path, FNAME_COMMIT_LOCK); + if (!(zi->server_lock_cmt = zebra_lock_create (path, 0))) { logf (LOG_FATAL|LOG_ERRNO, "create %s", path); return -1; } - assert (server_lock_org == NULL); + assert (zi->server_lock_org == NULL); - sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK); - if (!(server_lock_org = zebra_lock_create (path, 0))) + strcpy (path, zi->server_path_prefix); + strcat (path, FNAME_ORG_LOCK); + if (!(zi->server_lock_org = zebra_lock_create (path, 0))) { logf (LOG_FATAL|LOG_ERRNO, "create %s", path); return -1; @@ -81,49 +105,48 @@ int zebraServerLock (Res res, int commitPhase) if (commitPhase) { logf (LOG_DEBUG, "Server locks org"); - zebra_lock (server_lock_org); + zebra_lock (zi->server_lock_org); } else { logf (LOG_DEBUG, "Server locks cmt"); - zebra_lock (server_lock_cmt); + zebra_lock (zi->server_lock_cmt); } return 0; } -void zebraServerUnlock (int commitPhase) +void zebra_server_unlock (ZServerInfo *zi, int commitPhase) { - if (server_lock_org == NULL) + if (zi->server_lock_org == NULL) return; if (commitPhase) { logf (LOG_DEBUG, "Server unlocks org"); - zebra_unlock (server_lock_org); + zebra_unlock (zi->server_lock_org); } else { logf (LOG_DEBUG, "Server unlocks cmt"); - zebra_unlock (server_lock_cmt); + zebra_unlock (zi->server_lock_cmt); } } -int zebraServerLockGetState (Res res, time_t *timep) +int zebra_server_lock_get_state (ZServerInfo *zi, time_t *timep) { - char pathPrefix[1024]; char path[1024]; char buf[256]; int fd; struct stat xstat; - zebraLockPrefix (res, pathPrefix); - - sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME); + strcpy (path, zi->server_path_prefix); + strcat (path, FNAME_TOUCH_TIME); if (stat (path, &xstat) == -1) *timep = 1; else *timep = xstat.st_ctime; - - sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK); + + strcpy (path, zi->server_path_prefix); + strcat (path, FNAME_MAIN_LOCK); fd = open (path, O_BINARY|O_RDONLY); if (fd == -1) { diff --git a/index/lockutil.c b/index/lockutil.c index 366f936..bebe318 100644 --- a/index/lockutil.c +++ b/index/lockutil.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: lockutil.c,v $ - * Revision 1.9 1997-09-25 14:54:43 adam + * Revision 1.10 1997-09-29 09:08:36 adam + * Revised locking system to be thread safe for the server. + * + * Revision 1.9 1997/09/25 14:54:43 adam * WIN32 files lock support. * * Revision 1.8 1997/09/17 12:19:15 adam @@ -52,8 +55,6 @@ #include "index.h" -static char *lockDir = NULL; - struct zebra_lock_info { int fd; int excl_flag; @@ -93,15 +94,13 @@ void zebra_lock_destroy (ZebraLockHandle h) xfree (h); } -void zebraLockPrefix (Res res, char *pathPrefix) +void zebra_lock_prefix (Res res, char *path) { - if (!lockDir) - lockDir = res_get_def (res, "lockDir", ""); - assert (lockDir); - - strcpy (pathPrefix, lockDir); - if (*pathPrefix && pathPrefix[strlen(pathPrefix)-1] != '/') - strcat (pathPrefix, "/"); + char *lock_dir = res_get_def (res, "lockDir", ""); + + strcpy (path, lock_dir); + if (*path && path[strlen(path)-1] != '/') + strcat (path, "/"); } #ifdef WINDOWS diff --git a/index/zserver.c b/index/zserver.c index 9c2fbff..12314b5 100644 --- a/index/zserver.c +++ b/index/zserver.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: zserver.c,v $ - * Revision 1.49 1997-09-25 14:57:23 adam + * Revision 1.50 1997-09-29 09:08:36 adam + * Revised locking system to be thread safe for the server. + * + * Revision 1.49 1997/09/25 14:57:23 adam * Windows NT port. * * Revision 1.48 1997/09/17 12:19:19 adam @@ -193,29 +196,12 @@ #include #include -#ifdef __linux__ -#define USE_TIMES 1 -#endif - -#ifndef USE_TIMES -#define USE_TIMES 0 -#endif - -#if USE_TIMES -#include -#endif #include "zserver.h" - -#if USE_TIMES -static struct tms tms1; -static struct tms tms2; -#endif - static int register_lock (ZServerInfo *zi) { time_t lastChange; - int state = zebraServerLockGetState(zi->res, &lastChange); + int state = zebra_server_lock_get_state(zi, &lastChange); switch (state) { @@ -225,9 +211,9 @@ static int register_lock (ZServerInfo *zi) default: state = 0; } - zebraServerLock (zi->res, state); + zebra_server_lock (zi, state); #if USE_TIMES - times (&tms1); + times (&zi->tms1); #endif if (zi->registerState == state) { @@ -282,10 +268,10 @@ static void register_unlock (ZServerInfo *zi) static int waitSec = -1; #if USE_TIMES - times (&tms2); + times (&zi->tms2); logf (LOG_LOG, "user/system: %ld/%ld", - (long) (tms2.tms_utime - tms1.tms_utime), - (long) (tms2.tms_stime - tms1.tms_stime)); + (long) (zi->tms2.tms_utime - zi->tms1.tms_utime), + (long) (zi->tms2.tms_stime - zi->tms1.tms_stime)); #endif if (waitSec == -1) { @@ -301,7 +287,7 @@ static void register_unlock (ZServerInfo *zi) sleep (waitSec); #endif if (zi->registerState != -1) - zebraServerUnlock (zi->registerState); + zebra_server_unlock (zi, zi->registerState); } bend_initresult *bend_init (bend_initrequest *q) @@ -323,6 +309,7 @@ bend_initresult *bend_init (bend_initrequest *q) logf (LOG_FATAL, "Cannot open resource `%s'", sob->configname); exit (1); } + zebra_server_lock_init (zi); zi->dh = data1_create (); zi->bfs = bfs_create (res_get (zi->res, "register")); bf_lockDir (zi->bfs, res_get (zi->res, "lockDir")); @@ -586,6 +573,7 @@ void bend_close (void *handle) } bfs_destroy (zi->bfs); data1_destroy (zi->dh); + zebra_server_lock_destroy (zi); return; } diff --git a/index/zserver.h b/index/zserver.h index f2c014d..600c732 100644 --- a/index/zserver.h +++ b/index/zserver.h @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: zserver.h,v $ - * Revision 1.24 1997-09-17 12:19:19 adam + * Revision 1.25 1997-09-29 09:08:36 adam + * Revised locking system to be thread safe for the server. + * + * Revision 1.24 1997/09/17 12:19:19 adam * Zebra version corresponds to YAZ version 1.4. * Changed Zebra server so that it doesn't depend on global common_resource. * @@ -84,6 +87,19 @@ * */ + +#ifndef USE_TIMES +#ifdef __linux__ +#define USE_TIMES 1 +#else +#define USE_TIMES 0 +#endif +#endif + +#if USE_TIMES +#include +#endif + #include #include @@ -118,6 +134,14 @@ typedef struct { data1_attset *registered_sets; BFiles bfs; Res res; + + ZebraLockHandle server_lock_cmt; + ZebraLockHandle server_lock_org; + char *server_path_prefix; +#ifdef USE_TIMES + struct tms tms1; + struct tms tms2; +#endif } ZServerInfo; int rpn_search (ZServerInfo *zi, @@ -142,9 +166,11 @@ ZServerSetSysno *resultSetSysnoGet (ZServerInfo *zi, const char *name, void resultSetSysnoDel (ZServerInfo *zi, ZServerSetSysno *records, int num); void zlog_rpn (Z_RPNQuery *rpn); -int zebraServerLock (Res res, int lockCommit); -void zebraServerUnlock (int commitPhase); -int zebraServerLockGetState (Res res, time_t *timep); +int zebra_server_lock_init (ZServerInfo *zi); +int zebra_server_lock_destroy (ZServerInfo *zi); +int zebra_server_lock (ZServerInfo *zi, int lockCommit); +void zebra_server_unlock (ZServerInfo *zi, int commitPhase); +int zebra_server_lock_get_state (ZServerInfo *zi, time_t *timep); typedef struct attent { -- 1.7.10.4