2 * Copyright (c) 2005 MontaVista Software, Inc.
6 * Author: Steven Dake (sdake@mvista.com)
8 * This software licensed under BSD license, the text of which follows:
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * - Neither the name of the MontaVista Software, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/select.h>
50 SaNameT resource_name_async
;
51 SaLckResourceHandleT resource_handle_async
;
53 void testLckResourceOpenCallback (
54 SaInvocationT invocation
,
55 SaLckResourceHandleT lockResourceHandle
,
58 printf ("testLckResourceOpenCallback invocation %llu error %d\n",
59 (unsigned long long)invocation
, error
);
60 resource_handle_async
= lockResourceHandle
;
63 void testLckLockGrantCallback (
64 SaInvocationT invocation
,
65 SaLckLockStatusT lockStatus
,
68 printf ("testLckLockGrantCallback invocation %llu status %d error %d\n",
69 (unsigned long long)invocation
, lockStatus
, error
);
72 SaLckLockIdT pr_lock_id
;
74 void testLckLockWaiterCallback (
75 SaLckWaiterSignalT waiterSignal
,
77 SaLckLockModeT modeHeld
,
78 SaLckLockModeT modeRequested
)
81 printf ("waiter callback mode held %d mode requested %d lock id %llu\n",
84 (unsigned long long)lockId
);
85 printf ("pr lock id %llu\n", (unsigned long long)pr_lock_id
);
86 result
= saLckResourceUnlockAsync (
89 printf ("saLckResourceUnlockAsync result %d (should be 1)\n", result
);
92 void testLckResourceUnlockCallback (
93 SaInvocationT invocation
,
96 printf ("testLckResourceUnlockCallback async invocation %llu error %d\n",
97 (unsigned long long)invocation
, error
);
100 SaLckCallbacksT callbacks
= {
101 .saLckResourceOpenCallback
= testLckResourceOpenCallback
,
102 .saLckLockGrantCallback
= testLckLockGrantCallback
,
103 .saLckLockWaiterCallback
= testLckLockWaiterCallback
,
104 .saLckResourceUnlockCallback
= testLckResourceUnlockCallback
107 SaVersionT version
= { 'B', 1, 1 };
109 void setSaNameT (SaNameT
*name
, char *str
) {
110 name
->length
= strlen (str
);
111 memcpy (name
->value
, str
, name
->length
);
114 void sigintr_handler (int signum
) {
122 void *th_dispatch (void *arg
)
124 struct th_data
*th_data
= (struct th_data
*)arg
;
126 saLckDispatch (th_data
->handle
, SA_DISPATCH_BLOCKING
);
132 SaLckResourceHandleT resource_handle
;
134 SaLckLockIdT ex_lock_id
;
135 SaLckLockStatusT status
;
136 SaNameT resource_name
;
137 pthread_t dispatch_thread
;
139 struct th_data th_data
;
141 signal (SIGINT
, sigintr_handler
);
143 result
= saLckInitialize (&handle
, &callbacks
, &version
);
144 if (result
!= SA_AIS_OK
) {
145 printf ("Could not initialize Lock Service API instance error %d\n", result
);
148 printf ("saLckInitialize result is %d (should be 1)\n", result
);
150 th_data
.handle
= handle
;
151 pthread_create (&dispatch_thread
, NULL
, th_dispatch
, &th_data
);
153 setSaNameT (&resource_name
, "test_resource");
154 setSaNameT (&resource_name_async
, "test_resource_async");
156 result
= saLckResourceOpen (
159 SA_LCK_RESOURCE_CREATE
,
163 printf ("saLckResourceOpen %d (should be 12)\n", result
);
165 result
= saLckResourceClose (resource_handle
);
166 printf ("saLckResourceClose %d (should be 9)\n", result
);
168 result
= saLckResourceOpen (
171 SA_LCK_RESOURCE_CREATE
,
174 printf ("saLckResourceOpen %d (should be 1)\n", result
);
176 result
= saLckResourceOpenAsync (
179 &resource_name_async
,
180 SA_LCK_RESOURCE_CREATE
);
181 printf ("saLckResourceOpenAsync %d (should be 1)\n", result
);
183 result
= saLckResourceLock (
191 printf ("saLckResourceLock PR %d (should be 1)\n", result
);
192 printf ("status %d\n", status
);
194 result
= saLckResourceLock (
202 printf ("saLckResourceLock EX %d (should be 1)\n", result
);
203 printf ("status %d\n", status
);
205 result
= saLckResourceLockAsync (
212 printf ("saLckResourceLockAsync PR %d (should be 1)\n", result
);
213 printf ("status %d\n", status
);
214 printf ("press the enter key to exit\n");
217 FD_SET (STDIN_FILENO
, &read_fds
);
218 result
= select (STDIN_FILENO
+ 1, &read_fds
, 0, 0, 0);
219 if (FD_ISSET (STDIN_FILENO
, &read_fds
)) {
224 result
= saLckResourceUnlock (
227 printf ("saLckResourceUnlock result %d (should be 1)\n",
230 result
= saLckResourceClose (resource_handle
);
231 printf ("saLckResourceClose result %d (should be 1)\n", result
);
233 result
= saLckFinalize (handle
);
234 printf ("saLckFinalize %d (should be 1)\n", result
);