4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
33 * Access control structure for a piece of nscd data. This structure
34 * is always tagged before the nscd data. nscd_alloc, which should
35 * be used to allocate memory that requires access control or usage
36 * count control, will initialize this access control structure at the
37 * start of the memory returned to the caller.
39 struct nscd_access_s
{
40 void *data
; /* addr of real data */
41 void (*free_func
)(nscd_acc_data_t
*data
); /* destructor */
42 mutex_t mutex
; /* protect this structure */
44 rwlock_t
*data_rwlock
;
46 int nUse
; /* usage count */
48 int delete; /* no longer available */
49 nscd_seq_num_t seq_num
; /* sequence number */
52 /* size should be in multiple of 8 */
53 static int sizeof_access
= roundup(sizeof (nscd_access_t
));
55 #define ABORT_DUE_TO_NO_VALID_NSCD_ACCESS_DATA 0
56 #define ASSERT_ACCESS_DATA \
57 if (access->data != data) \
58 assert(ABORT_DUE_TO_NO_VALID_NSCD_ACCESS_DATA)
60 #define SET_ACCESS_PTR \
61 access = (nscd_access_t *) \
62 ((void *)((char *)data - sizeof_access))
64 static void _nscd_free(nscd_acc_data_t
*data
);
67 * FUNCTION: _nscd_release
69 * Decrements the usage count maintained in the access data
70 * tagged before 'data'. Delete the nscd data item if the delete
71 * flag is set and the usage count reaches 0.
75 nscd_acc_data_t
*data
)
77 nscd_access_t
*access
;
78 char *me
= "_nscd_release";
85 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
86 (me
, "data = %p, access->data = %p, "
87 "seq = %lld, nUse = %d\n",
88 data
, access
->data
, access
->seq_num
, access
->nUse
);
91 (void) mutex_lock(&access
->mutex
);
93 if (access
->nUse
< 0) {
94 #define ACCESS_NUSE_LESS_THAN_ZERO 0
95 assert(ACCESS_NUSE_LESS_THAN_ZERO
);
97 if (access
->nUse
<= 0 &&
98 access
->delete == 1) {
100 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
101 (me
, "deleting data %p\n", access
->data
);
102 (access
->free_func
)(access
->data
);
105 * if we get here, no other thread could be
106 * holding the access->mutex lock, It is safe
107 * to free the memory containing the mutex
108 * structure. No mutex_unlock is necessary.
112 (void) mutex_unlock(&access
->mutex
);
117 * FUNCTION: _nscd_destroy
119 * Marks the nscd data item as to-be-deleted and then releases
120 * (If the usage count happens to be zero, then _nscd_release()
121 * will destroy the data.)
123 * Note that _nscd_destroy should only be called if the
124 * caller has created the nscd data with _nscd_alloc
125 * (with the exception of _nscd_set). That nscd data
126 * item should be private to the caller.
130 nscd_acc_data_t
*data
)
132 nscd_access_t
*access
;
133 char *me
= "_nscd_destroy";
140 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
141 (me
, "data = %p, access->data = %p\n", data
, access
->data
);
144 (void) mutex_lock(&access
->mutex
);
146 (void) mutex_unlock(&access
->mutex
);
152 * FUNCTION: _nscd_get
154 * Increment the usage count by one if 'data' can
155 * be found in the internal address database.
159 nscd_acc_data_t
*data
)
161 nscd_access_t
*access
;
163 rwlock_t
*addr_rwlock
;
164 char *me
= "_nscd_get";
171 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
172 (me
, "data = %p, access->data = %p, seq#= %lld, nUse = %d\n",
173 data
, access
->data
, access
->seq_num
, access
->nUse
);
177 * see if this addr is still valid,
178 * if so, _nscd_is_int_addr will
179 * do a read lock on the returned
180 * multiple readers/single writer lock
181 * to prevent the access data from being
182 * deleted while it is being accessed.
184 if ((addr_rwlock
= _nscd_is_int_addr(data
,
185 access
->seq_num
)) == NULL
) {
186 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
187 (me
, "internal address %p not found\n", data
);
188 assert(addr_rwlock
!= NULL
);
192 (void) mutex_lock(&access
->mutex
);
193 if (access
->delete == 1)
197 (void) mutex_unlock(&access
->mutex
);
200 * done with the multiple readers/single writer lock
202 (void) rw_unlock(addr_rwlock
);
208 * FUNCTION: _nscd_set
210 * _nscd_set sets the address of a nscd data item
211 * to 'new' and delete the old nscd data (old).
212 * The pointer 'new' is returned.
216 nscd_acc_data_t
*old
,
217 nscd_acc_data_t
*new)
219 nscd_acc_data_t
*old_data
, *new_data
;
220 char *me
= "_nscd_set";
225 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
226 (me
, "new = %p, old = %p\n", new, old
);
228 old_data
= _nscd_get(old
);
229 new_data
= _nscd_get(new);
231 if (old_data
!= new_data
) {
233 _nscd_destroy(old_data
);
234 _nscd_release(new_data
);
238 /* if old_data == new_data, both must be NULL */
243 * FUNCTION: _nscd_rdlock
245 * Lock (rw_rdlock) a nscd data item for reading. The caller
246 * needs to call _nscd_rw_unlock() to unlock the data item
247 * when done using the data.
251 nscd_acc_data_t
*data
)
253 nscd_access_t
*access
;
255 char *me
= "_nscd_rdlock";
257 ret
= _nscd_get(data
);
264 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
265 (me
, "data = %p, access->data = %p\n", data
, access
->data
);
268 assert(access
->data_rwlock
!= NULL
);
270 (void) rw_rdlock(access
->data_rwlock
);
276 * FUNCTION: _nscd_wrlock
278 * Lock (rw_wrlock) a nscd data item for writing. The caller
279 * needs to call _nscd_rw_unlock() to unlock the data item
280 * when done using the data.
284 nscd_acc_data_t
*data
)
286 nscd_access_t
*access
;
288 char *me
= "_nscd_wrlock";
290 ret
= _nscd_get(data
);
297 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
298 (me
, "data = %p, access->data = %p\n", data
, access
->data
);
301 assert(access
->data_rwlock
!= NULL
);
303 (void) rw_wrlock(access
->data_rwlock
);
309 * FUNCTION: _nscd_rw_unlock
311 * Unlock (rw_unlock) a locked nscd data item.
315 nscd_acc_data_t
*data
)
317 nscd_access_t
*access
;
318 char *me
= "_nscd_rw_unlock";
325 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
326 (me
, "data = %p, access->data = %p\n",
330 assert(access
->data_rwlock
!= NULL
);
332 (void) rw_unlock(access
->data_rwlock
);
337 * FUNCTION: _nscd_rw_unlock_no_release
339 * Unlock (rw_unlock) a locked nscd data item but without release
340 * it, i.e., without decrement the usage count to indicate that
341 * the data item is still being referenced.
344 _nscd_rw_unlock_no_release(
345 nscd_acc_data_t
*data
)
347 nscd_access_t
*access
;
355 assert(access
->data_rwlock
!= NULL
);
357 (void) rw_unlock(access
->data_rwlock
);
361 * FUNCTION: _nscd_mutex_lock
363 * Lock (mutex_lock) a nscd data item. The caller needs
364 * to call _nscd_mutex_unlock() to unlock the data item
365 * when done using the data.
369 nscd_acc_data_t
*data
)
371 nscd_access_t
*access
;
373 char *me
= "_nscd_mutex_lock";
375 ret
= _nscd_get(data
);
382 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
383 (me
, "data = %p, access->data = %p\n", data
, access
->data
);
386 assert(access
->data_mutex
!= NULL
);
388 (void) mutex_lock(access
->data_mutex
);
395 * FUNCTION: _nscd_mutex_unlock
397 * Unlock a locked nscd data item (that were locked by _nscd_mutex_lock)..
401 nscd_acc_data_t
*data
)
403 nscd_access_t
*access
;
404 char *me
= "_nscd_mutex_unlock";
411 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
412 (me
, "data = %p, access->data = %p\n", data
, access
->data
);
415 assert(access
->data_mutex
!= NULL
);
417 (void) mutex_unlock(access
->data_mutex
);
422 * FUNCTION: _nscd_cond_wait
424 * Perform a condition wait with the cond_t and mutex_t associated
429 nscd_acc_data_t
*data
, cond_t
*cond
)
431 nscd_access_t
*access
;
432 char *me
= "_nscd_cond_wait";
439 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
440 (me
, "data = %p, access->data = %p\n", data
, access
->data
);
443 assert(access
->data_cond
!= NULL
&& access
->data_mutex
!= NULL
);
446 (void) cond_wait(access
->data_cond
, access
->data_mutex
);
448 (void) cond_wait(cond
, access
->data_mutex
);
452 * FUNCTION: _nscd_cond_signal
454 * Perform a condition signal with the cond_t associated with 'data'.
458 nscd_acc_data_t
*data
)
460 nscd_access_t
*access
;
461 char *me
= "_nscd_cond_signal";
468 _NSCD_LOG(NSCD_LOG_ACCESS_INFO
, NSCD_LOG_LEVEL_DEBUG
)
469 (me
, "data = %p, access->data = %p\n", data
, access
->data
);
472 assert(access
->data_cond
!= NULL
);
474 (void) cond_signal(access
->data_cond
);
478 * FUNCTION: _nscd_alloc
480 * Allocate a piece of nscd memory. 'data_free'
481 * is the function to invoke to free the data
482 * stored in this memory, i.e., the desctrctor.
483 * 'option' indicate whether a mutex or a
484 * readers/writer (or both, or none) should also
491 void (*data_free
)(nscd_acc_data_t
*data
),
494 nscd_access_t
*access
;
495 nscd_acc_data_t
*ptr
;
496 nscd_seq_num_t seq_num
;
497 rwlock_t
*rwlock
= NULL
;
498 mutex_t
*mutex
= NULL
;
501 if ((ptr
= (nscd_acc_data_t
*)calloc(1,
502 size
+ sizeof_access
)) == NULL
)
504 if (option
& NSCD_ALLOC_MUTEX
) {
505 if ((mutex
= (mutex_t
*)calloc(1, sizeof (mutex_t
))) ==
510 (void) mutex_init(mutex
, USYNC_THREAD
, NULL
);
512 if (option
& NSCD_ALLOC_RWLOCK
) {
513 if ((rwlock
= (rwlock_t
*)calloc(1, sizeof (rwlock_t
))) ==
519 (void) rwlock_init(rwlock
, USYNC_THREAD
, NULL
);
521 if (option
& NSCD_ALLOC_COND
) {
522 if ((cond
= (cond_t
*)calloc(1, sizeof (cond_t
))) ==
529 (void) cond_init(cond
, USYNC_THREAD
, NULL
);
532 /* get current sequence number */
533 seq_num
= _nscd_get_seq_num();
535 access
= (nscd_access_t
*)ptr
;
536 access
->data
= (char *)ptr
+ sizeof_access
;
537 access
->data_mutex
= mutex
;
538 access
->data_rwlock
= rwlock
;
539 access
->data_cond
= cond
;
543 access
->free_func
= data_free
;
544 access
->seq_num
= seq_num
;
546 /* add the address to the internal address database */
547 if (_nscd_add_int_addr(access
->data
, type
,
548 seq_num
) != NSCD_SUCCESS
) {
553 return (access
->data
);
557 * FUNCTION: _nscd_free
559 * Free a piece of nscd memory.
563 nscd_acc_data_t
*data
)
565 nscd_access_t
*access
;
573 /* remove the address from the internal address database */
574 _nscd_del_int_addr(access
->data
, access
->seq_num
);
576 free(access
->data_mutex
);
577 free(access
->data_rwlock
);
578 free(access
->data_cond
);
580 (void) memset(access
, 0, sizeof (*access
));