@@ -291,6 +291,7 @@ static int hds2Link(HDSLoc *locator, const char *group_str, int *status) {
291
291
int lock_status ;
292
292
int ok ;
293
293
int promoted ;
294
+ int locked ;
294
295
295
296
if (* status != SAI__OK ) return * status ;
296
297
@@ -311,6 +312,7 @@ static int hds2Link(HDSLoc *locator, const char *group_str, int *status) {
311
312
the promotion fails (i.e. because another thread also has a read-lock). */
312
313
ok = 0 ;
313
314
promoted = 0 ;
315
+ locked = 0 ;
314
316
if ( lock_status == 3 ){
315
317
dat1HandleLock ( locator -> handle , 2 , 0 , 0 , & lock_status , status );
316
318
if ( lock_status == 1 ) {
@@ -322,6 +324,15 @@ static int hds2Link(HDSLoc *locator, const char *group_str, int *status) {
322
324
can continue withotu changing anything. */
323
325
} else if ( lock_status == 1 ){
324
326
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
+ }
325
336
}
326
337
327
338
/* 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) {
368
379
elt .locator = locator ;
369
380
utarray_push_back ( entry -> locators , & elt );
370
381
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
+
371
394
/* If the locator was originally promoted from a read lock to a
372
395
read/write lock, demote it back to a read lock. */
373
396
if ( promoted ){
0 commit comments