13
13
* Library routine
14
14
15
15
* Invocation:
16
- * Handle *dat1Handle( const HDSLoc *parent, const char *name, int *status );
16
+ * Handle *dat1Handle( const HDSLoc *parent, const char *name,
17
+ * int rdonly, int *status );
17
18
18
19
* Arguments:
19
20
* parent = const HDSLoc * (Given)
26
27
* component does in fact exist within the parent object, although
27
28
* this is not checked. If "parent" is NULL, the path to the
28
29
* contained file should be supplied.
30
+ * rdonly = int (Given)
31
+ * If a new Handle is created as a result of calling this function,
32
+ * it is locked for use by the current thread. If "parent" is
33
+ * non-NULL, the type of lock (read-only or read-write) is copied
34
+ * from the parent. If "parent" is NULL, the type of lock is
35
+ * specified by the "rdonly" argument. The supplied "rdonly" value
36
+ * is ignored if "parent" is non-NULL or if a pointer to an existing
37
+ * handle is returned.
29
38
* status = int* (Given and Returned)
30
39
* Pointer to global status.
31
40
103
112
#include "dat1.h"
104
113
#include "dat_err.h"
105
114
106
- Handle * dat1Handle ( const HDSLoc * parent_loc , const char * name , int * status ){
115
+ Handle * dat1Handle ( const HDSLoc * parent_loc , const char * name , int rdonly ,
116
+ int * status ){
107
117
108
118
109
119
/* Local Variables; */
@@ -112,6 +122,7 @@ Handle *dat1Handle( const HDSLoc *parent_loc, const char *name, int * status ){
112
122
Handle * parent ;
113
123
Handle * result = NULL ;
114
124
int ichild ;
125
+ int lock_status ;
115
126
116
127
/* Return immediately if an error has already occurred. */
117
128
if ( * status != SAI__OK ) return result ;
@@ -121,7 +132,7 @@ Handle *dat1Handle( const HDSLoc *parent_loc, const char *name, int * status ){
121
132
lname = MEM_CALLOC ( strlen ( name ) + 1 , sizeof (char ) );
122
133
if ( !lname ) {
123
134
* status = DAT__NOMEM ;
124
- emsRep ("dat1Handle" , "Could not reallocate memory for the "
135
+ emsRep ("dat1Handle" , "Could not allocate memory for the "
125
136
"component name in an HDS Handle" , status );
126
137
} else {
127
138
strcpy ( lname , name );
@@ -175,9 +186,6 @@ Handle *dat1Handle( const HDSLoc *parent_loc, const char *name, int * status ){
175
186
parent -> children [ parent -> nchild - 1 ] = result ;
176
187
}
177
188
178
- /* Copy other information from the parent Handle. */
179
- result -> locked = parent -> locked ;
180
- result -> locker = parent -> locker ;
181
189
}
182
190
183
191
/* Store the component name. Nullify "lname" to indicate the memory is
@@ -188,14 +196,40 @@ Handle *dat1Handle( const HDSLoc *parent_loc, const char *name, int * status ){
188
196
/* Initialise a mutex that is used to serialise access to the values
189
197
stored in the handle. */
190
198
if ( * status == SAI__OK &&
191
- pthread_mutex_init ( & (result -> mutex2 ), NULL ) != 0 ) {
192
- emsRep ( " " , "Failed to initialise POSIX mutex2 for a new Handle." ,
199
+ pthread_mutex_init ( & (result -> mutex ), NULL ) != 0 ) {
200
+ * status = DAT__MUTEX ;
201
+ emsRep ( " " , "Failed to initialise POSIX mutex for a new Handle." ,
193
202
status );
194
203
}
195
204
196
- /* Indicate the Handle is locked for use by the current thread. */
197
- result -> locked = 1 ;
198
- result -> locker = pthread_self ();
205
+ /* Initialise the Handle to indicate it is currently unlocked. */
206
+ result -> nwrite_lock = 0 ;
207
+ result -> nread_lock = 0 ;
208
+ result -> read_lockers = NULL ;
209
+ result -> maxreaders = 0 ;
210
+
211
+ /* If a parent was supplied, see if the current thread has a read or
212
+ write lock on the parent object. We give the same sort of lock to the
213
+ new Handle below (ignoring the supplied value for "rdonly"). */
214
+ if ( parent ) {
215
+ lock_status = dat1HandleLock ( parent , 1 , 0 , 0 , status );
216
+ if ( lock_status == 1 ) {
217
+ rdonly = 0 ;
218
+ } else if ( lock_status == 3 ) {
219
+ rdonly = 1 ;
220
+ } else if ( * status == SAI__OK ) {
221
+ * status = DAT__FATAL ;
222
+ emsRepf ( " " , "dat1Handle: Unexpected lock value (%d) for "
223
+ "object '%s' - parent of '%s' (internal HDS "
224
+ "programming error)." , status , lock_status ,
225
+ parent -> name , name );
226
+ }
227
+ }
228
+
229
+ /* Lock the new Handle for use by the current thread. The type of lock
230
+ (read-only or read-write) is inherited from the parent (if there is a
231
+ parent) or supplied by the caller. */
232
+ dat1HandleLock ( result , 2 , 0 , rdonly , status );
199
233
}
200
234
}
201
235
0 commit comments