Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / pkix_pl / system / test_mutex3.c
blobcaf78ea6d8b03573469bc981d5113fa3f8a7bcfa
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
12 * License.
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.
21 * Contributor(s):
22 * Sun Microsystems
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 ***** */
38 * test_mutex3.c
40 * Tests multi-threaded functionality of Mutex
44 #include "testutil.h"
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;
84 PKIX_UInt32 j = 0;
85 PKIX_Boolean useArenas = PKIX_FALSE;
87 PKIX_TEST_STD_VARS();
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 */
95 useArenas,
96 PKIX_MAJOR_VERSION,
97 PKIX_MINOR_VERSION,
98 PKIX_MINOR_VERSION,
99 &actualMinorVersion,
100 &plContext));
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,
108 NULL,
109 PR_PRIORITY_NORMAL,
110 PR_LOCAL_THREAD,
111 PR_JOINABLE_THREAD,
114 thread2 = PR_CreateThread(PR_USER_THREAD,
116 NULL,
117 PR_PRIORITY_NORMAL,
118 PR_LOCAL_THREAD,
119 PR_JOINABLE_THREAD,
122 PR_JoinThread(thread2);
123 PR_JoinThread(thread);
125 cleanup:
127 PKIX_TEST_DECREF_AC(mutex);
129 PKIX_Shutdown(plContext);
131 PKIX_TEST_RETURN();
133 endTests("Mutex and Threads");
135 return (0);