1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 * Tests multi-threaded functionality of Mutex
45 #include "testutil_nss.h"
47 static PKIX_PL_Mutex
*mutex
;
48 void *plContext
= NULL
;
50 static void t1(/* ARGSUSED */ void* arg
) {
51 PKIX_Error
*errorResult
;
53 (void) printf("t1 acquiring lock...\n");
54 errorResult
= PKIX_PL_Mutex_Lock(mutex
, plContext
);
55 if (errorResult
) testError("PKIX_PL_Mutex_Lock failed");
57 (void) printf("t1 sleeplng for 3 seconds\n");
58 PR_Sleep(PR_SecondsToInterval(3));
59 (void) printf("t1 releasing lock...\n");
61 errorResult
= PKIX_PL_Mutex_Unlock(mutex
, plContext
);
62 if (errorResult
) testError("PKIX_PL_Mutex_Unlock failed");
64 (void) printf("t1 exiting...\n");
67 static void t2(/* ARGSUSED */ void* arg
) {
68 PKIX_Error
*errorResult
;
70 (void) printf("t2 acquiring lock...\n");
71 errorResult
= PKIX_PL_Mutex_Lock(mutex
, plContext
);
72 if (errorResult
) testError("PKIX_PL_Mutex_Lock failed");
74 (void) printf("t2 releasing lock...\n");
75 errorResult
= PKIX_PL_Mutex_Unlock(mutex
, plContext
);
76 if (errorResult
) testError("PKIX_PL_Mutex_Unlock failed");
78 (void) printf("t2 exiting...\n");
81 int main(int argc
, char *argv
[]) {
82 PRThread
*thread
, *thread2
;
83 PKIX_UInt32 actualMinorVersion
;
85 PKIX_Boolean useArenas
= PKIX_FALSE
;
89 startTests("Mutex and Threads");
91 useArenas
= PKIX_TEST_ARENAS_ARG(argv
[1]);
93 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Initialize
94 (PKIX_TRUE
, /* nssInitNeeded */
102 subTest("Mutex Creation");
103 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Mutex_Create(&mutex
, plContext
));
105 subTest("Starting thread");
106 thread
= PR_CreateThread(PR_USER_THREAD
,
114 thread2
= PR_CreateThread(PR_USER_THREAD
,
122 PR_JoinThread(thread2
);
123 PR_JoinThread(thread
);
127 PKIX_TEST_DECREF_AC(mutex
);
129 PKIX_Shutdown(plContext
);
133 endTests("Mutex and Threads");