xref: /illumos-gate/usr/src/lib/libnisdb/nis_hashitem.h (revision 0fb96ba1f1ce26ff8b286f8f928769a6afcb00a6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_NIS_HASHITEM_H
28 #define	_NIS_HASHITEM_H
29 
30 #include <pthread.h>
31 #include <rpcsvc/nis.h>
32 
33 #include "nisdb_rw.h"
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 /* Private versions of the various NIS_HASH_ITEM functions */
40 typedef struct __nis_item_item {
41 	pthread_cond_t		lock;
42 	nis_name		name;
43 	int			keychain;
44 	uint32_t		readers;
45 	pthread_t		last_reader_id;
46 	uint32_t		writer;
47 	pthread_t		writer_id;
48 	struct __nis_item_item	*next;
49 	struct __nis_item_item	*prv_item;
50 	struct __nis_item_item	*nxt_item;
51 } __nis_hash_item_mt;
52 
53 typedef struct {
54 	pthread_mutex_t		lock;
55 	pthread_cond_t		cond;
56 	pthread_mutex_t		traverser_id_lock;
57 							/*
58 							 * Protects 'traversed'
59 							 * and 'traverser_id'.
60 							 */
61 	uint32_t		traversed;
62 	pthread_t		traverser_id;
63 	uint32_t		locked_items;
64 	__nis_hash_item_mt	*keys[64];
65 	__nis_hash_item_mt	*first;
66 	void			(*destroyItem)(void *);
67 } __nis_hash_table_mt;
68 
69 #define	NIS_HASH_TABLE_MT_INIT	{ \
70 					PTHREAD_MUTEX_INITIALIZER, \
71 					{0}, \
72 					PTHREAD_MUTEX_INITIALIZER \
73 					/* Zero is fine for the rest */ \
74 				}
75 
76 #define	LOCK_LIST(list, msg)	(void) __nis_lock_hash_table(list, 1, msg)
77 #define	ULOCK_LIST(list, msg)	(void) __nis_ulock_hash_table(list, 1, msg)
78 
79 
80 extern void	__nis_init_hash_table(__nis_hash_table_mt *, void (*)(void *));
81 extern int	__nis_lock_hash_table(__nis_hash_table_mt *, int, char *);
82 extern int	__nis_ulock_hash_table(__nis_hash_table_mt *, int, char *);
83 extern int	__nis_insert_item_mt(void *, __nis_hash_table_mt *, int);
84 extern void	__nis_insert_name_mt(nis_name, __nis_hash_table_mt *);
85 extern void	*__nis_find_item_mt(nis_name, __nis_hash_table_mt *, int,
86 					int *);
87 extern void	*__nis_pop_item_mt(__nis_hash_table_mt *);
88 extern void	*__nis_remove_item_mt(nis_name, __nis_hash_table_mt *);
89 extern int	__nis_release_item(void *, __nis_hash_table_mt *, int);
90 extern int	__nis_item_access(void *);
91 extern void	__nis_scan_table_mt(__nis_hash_table_mt *,
92 			bool_t (*)(__nis_hash_item_mt *, void *), void *);
93 
94 /* Define-magic */
95 #define	NIS_HASH_ITEM			__nis_hash_item_mt
96 #define	NIS_HASH_TABLE			__nis_hash_table_mt
97 #define	nis_insert_item(i, t)		__nis_insert_item_mt(i, t, -1)
98 #define	nis_insert_item_rw(i, t, rw)	__nis_insert_item_mt(i, t, rw)
99 #define	nis_insert_name(n, t)		__nis_insert_name_mt(n, t)
100 #define	nis_find_item(i, t)		__nis_find_item_mt(i, t, -1, 0)
101 #define	nis_find_item_rw(i, t, rw)	__nis_find_item_mt(i, t, rw, 0)
102 #define	nis_pop_item			__nis_pop_item_mt
103 #define	nis_remove_item			__nis_remove_item_mt
104 #define	nis_scan_table			__nis_scan_table_mt
105 
106 #define	MT_LOCK_TYPE(type)		(type < 0)?"W":(type > 0)?"R":"N"
107 
108 #ifdef	NIS_MT_DEBUG
109 #define	MT_LOG(condition, syslogarg)	if (condition) syslog ## syslogarg
110 #else
111 #define	MT_LOG(condition, syslogarg)
112 #endif	/* NIS_MT_DEBUG */
113 
114 #ifdef	__cplusplus
115 }
116 #endif	/* __cplusplus */
117 
118 #endif	/* _NIS_HASHITEM_H */
119