From 0be537a08d164975e9584a6c89240b28343a6ec2 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 22 Apr 2010 10:42:53 +0200 Subject: [PATCH] Introduce client_locking --- src/client.c | 19 ++++++++++++++++--- src/client.h | 2 ++ src/connection.c | 3 +++ src/session.c | 31 +++++++++++++++++++++++-------- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/client.c b/src/client.c index a9f2406..c238f0a 100644 --- a/src/client.c +++ b/src/client.c @@ -439,10 +439,13 @@ void client_search_response(struct client *cl) void client_got_records(struct client *cl) { - if (cl->session) + struct session *se = cl->session; + if (se) { - session_alert_watch(cl->session, SESSION_WATCH_SHOW); - session_alert_watch(cl->session, SESSION_WATCH_RECORD); + client_unlock(cl); + session_alert_watch(se, SESSION_WATCH_SHOW); + session_alert_watch(se, SESSION_WATCH_RECORD); + client_lock(cl); } } @@ -623,6 +626,16 @@ struct client *client_create(void) return r; } +void client_lock(struct client *c) +{ + yaz_mutex_enter(c->mutex); +} + +void client_unlock(struct client *c) +{ + yaz_mutex_leave(c->mutex); +} + void client_incref(struct client *c) { pazpar2_incref(&c->ref_count, c->mutex); diff --git a/src/client.h b/src/client.h index 767242e..4f39ebb 100644 --- a/src/client.h +++ b/src/client.h @@ -91,6 +91,8 @@ void client_set_startrecs(struct client *cl, int v); void client_remove_from_session(struct client *c); void client_incref(struct client *c); void client_got_records(struct client *c); +void client_lock(struct client *c); +void client_unlock(struct client *c); #endif /* diff --git a/src/connection.c b/src/connection.c index ac18130..1526938 100644 --- a/src/connection.c +++ b/src/connection.c @@ -275,11 +275,14 @@ static void connection_handler(IOCHAN iochan, int event) else { yaz_mutex_leave(host->mutex); + + client_lock(cl); non_block_events(co); ZOOM_connection_fire_event_socket(co->link, event); non_block_events(co); + client_unlock(cl); } } diff --git a/src/session.c b/src/session.c index 94ffdd2..77e4a3e 100644 --- a/src/session.c +++ b/src/session.c @@ -462,16 +462,24 @@ static void select_targets_callback(void *context, struct session_database *db) static void session_remove_clients(struct session *se) { - struct client_list *l = se->clients; + struct client_list *l; + + session_enter(se); + l = se->clients; + se->clients = 0; + session_leave(se); + while (l) { struct client_list *l_next = l->next; + client_lock(l->client); client_set_session(l->client, 0); + client_set_database(l->client, 0); + client_unlock(l->client); client_destroy(l->client); xfree(l); l = l_next; } - se->clients = 0; } // Associates a set of clients with a session; @@ -479,7 +487,6 @@ static void session_remove_clients(struct session *se) // setting overrides static int select_targets(struct session *se, const char *filter) { - session_remove_clients(se); return session_grep_databases(se, filter, select_targets_callback); } @@ -510,6 +517,8 @@ enum pazpar2_error_code search(struct session *se, yaz_log(YLOG_DEBUG, "Search"); *addinfo = 0; + + session_remove_clients(se); session_enter(se); reclist_destroy(se->reclist); @@ -529,14 +538,14 @@ enum pazpar2_error_code search(struct session *se, for (l = se->clients; l; l = l->next) { struct client *cl = l->client; + if (maxrecs) client_set_maxrecs(cl, atoi(maxrecs)); if (startrecs) client_set_startrecs(cl, atoi(startrecs)); if (prepare_session_database(se, client_get_database(cl)) < 0) - continue; - // Parse query for target - if (client_parse_query(cl, query) < 0) + ; + else if (client_parse_query(cl, query) < 0) no_failed++; else { @@ -1127,7 +1136,7 @@ int ingest_record(struct client *cl, const char *rec, int record_no, NMEM nmem) { struct session *se = client_get_session(cl); - int ret; + int ret = 0; struct session_database *sdb = client_get_database(cl); struct conf_service *service = se->service; xmlDoc *xdoc = normalize_record(sdb, service, rec, nmem); @@ -1154,9 +1163,15 @@ int ingest_record(struct client *cl, const char *rec, xmlFreeDoc(xdoc); return -1; } + client_unlock(cl); + pazpar2_sleep(0.01); session_enter(se); - ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm); + client_lock(cl); + if (client_get_session(cl) == se) + ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm); + client_unlock(cl); session_leave(se); + client_lock(cl); xmlFreeDoc(xdoc); return ret; -- 1.7.10.4