Skip to content

Commit 8cd2295

Browse files
author
David Berry
committed
Fix thread locking issue in hdsLink
1 parent 5870dde commit 8cd2295

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

datErase.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ datErase(const HDSLoc *locator, const char *name_str, int *status) {
122122
CLEANUP:
123123
if (*status != SAI__OK) {
124124
emsRepf("datErase_2", "Error deleting component %s in group %s",
125-
status, cleanname, groupstr);
125+
status, name_str, groupstr);
126126
}
127127
return *status;
128128
}

hdsgroups.c

+23
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ static int hds2Link(HDSLoc *locator, const char *group_str, int *status) {
291291
int lock_status;
292292
int ok;
293293
int promoted;
294+
int locked;
294295

295296
if (*status != SAI__OK) return *status;
296297

@@ -311,6 +312,7 @@ static int hds2Link(HDSLoc *locator, const char *group_str, int *status) {
311312
the promotion fails (i.e. because another thread also has a read-lock). */
312313
ok = 0;
313314
promoted = 0;
315+
locked = 0;
314316
if( lock_status == 3 ){
315317
dat1HandleLock( locator->handle, 2, 0, 0, &lock_status, status );
316318
if( lock_status == 1 ) {
@@ -322,6 +324,15 @@ static int hds2Link(HDSLoc *locator, const char *group_str, int *status) {
322324
can continue withotu changing anything. */
323325
} else if( lock_status == 1 ){
324326
ok = 1;
327+
328+
/* If it is unlocked we temporarily lock it for reading and writing by
329+
the current thread. */
330+
} else if( lock_status == 0 ){
331+
dat1HandleLock( locator->handle, 2, 0, 0, &lock_status, status );
332+
if( lock_status == 1 ) {
333+
ok = 1;
334+
locked = 1;
335+
}
325336
}
326337

327338
/* If we cannot get a write-lock report an error and return. */
@@ -368,6 +379,18 @@ static int hds2Link(HDSLoc *locator, const char *group_str, int *status) {
368379
elt.locator = locator;
369380
utarray_push_back( entry->locators, &elt );
370381

382+
/* If the locator was originally unlocked, unlock it now. */
383+
if( locked ){
384+
dat1HandleLock( locator->handle, 3, 0, 1, &lock_status, status );
385+
if( lock_status != 1 && *status == SAI__OK ) {
386+
*status = DAT__THREAD;
387+
datMsg( "O", locator );
388+
emsRepf( " ", "hdsLink: The supplied HDS locator for '^O' cannot be used.",
389+
status );
390+
emsRep( " ", "The read-write lock cannot be unlocked (programming error).", status );
391+
}
392+
}
393+
371394
/* If the locator was originally promoted from a read lock to a
372395
read/write lock, demote it back to a read lock. */
373396
if( promoted ){

0 commit comments

Comments
 (0)