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
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.
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]
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 #pragma ident "%Z%%M% %I% %E% SMI"
31 * This file contains functions that enables the rpc library to synchronize
32 * between various threads while they compete for a particular file descriptor.
41 #include <sys/types.h>
46 * A block holds an array of maxBlockSize cell and associated recursive locks
49 #define CELLTBLSZ 1024
51 typedef struct rpcfd_block
{
52 struct rpcfd_block
*next
; /* Next Block */
53 struct rpcfd_block
*prev
; /* prev Block */
54 int end
; /* fd of last lock in the list */
55 mutex_t lock
[CELLTBLSZ
]; /* recursive locks */
58 mutex_t rpc_fd_list_lock
= DEFAULTMUTEX
; /* protects list manipulation */
60 /* Following functions create and manipulates the dgfd lock object */
62 static mutex_t
*search(const void *handle
, int fd
);
63 static rpcfd_block_t
*create_block(const void *handle
, int fd
);
69 * Create first chunk of CELLTBLSZ
70 * (No lock is required for initial creation.)
72 return (create_block(NULL
, 0));
76 * If rpc_fd_lock() fails to acquire a lock, it returns non-zero (ENOMEM).
77 * (The operation can only fail due to a malloc() failure.)
78 * The caller of rpc_fd_lock() must call rpc_fd_unlock() even if
79 * rpc_fd_lock() failed. This keeps _sigoff() and _sigon() balanced.
81 * If search() and create_block() fail for rpc_fd_lock(), then search()
82 * will fail for rpc_fd_unlock(), so mutex_lock() and mutex_unlock()
83 * calls will be balanced. In any case, since the mutex is marked
84 * LOCK_ERRORCHECK, an additional mutex_unlock() does nothing.
87 rpc_fd_lock(const void *handle
, int fd
)
93 (void) mutex_lock(&rpc_fd_list_lock
);
94 mp
= search(handle
, fd
);
96 p
= create_block(handle
, fd
);
98 mp
= &p
->lock
[fd
% CELLTBLSZ
];
100 (void) mutex_unlock(&rpc_fd_list_lock
);
103 (void) mutex_lock(mp
);
108 rpc_fd_unlock(const void *handle
, int fd
)
112 (void) mutex_lock(&rpc_fd_list_lock
);
113 mp
= search(handle
, fd
);
114 (void) mutex_unlock(&rpc_fd_list_lock
);
116 (void) mutex_unlock(mp
);
120 static rpcfd_block_t
*
121 create_block(const void *handle
, int fd
)
123 rpcfd_block_t
*l
, *lprev
;
127 p
= malloc(sizeof (rpcfd_block_t
));
131 for (i
= 0; i
< CELLTBLSZ
; i
++) {
132 (void) mutex_init(&p
->lock
[i
],
133 USYNC_THREAD
| LOCK_RECURSIVE
| LOCK_ERRORCHECK
, NULL
);
135 p
->end
= (((fd
+ CELLTBLSZ
) / CELLTBLSZ
) * CELLTBLSZ
) - 1;
137 for (l
= (rpcfd_block_t
*)handle
; l
; l
= l
->next
) {
154 * Called with rpc_fd_list_lock held.
157 search(const void *handle
, int fd
)
161 for (p
= (rpcfd_block_t
*)handle
; p
; p
= p
->next
) {
163 return (&p
->lock
[fd
% CELLTBLSZ
]);