test_mutex3.c (2630B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 /* 5 * test_mutex3.c 6 * 7 * Tests multi-threaded functionality of Mutex 8 * 9 */ 10 11 #include "testutil.h" 12 #include "testutil_nss.h" 13 14 static PKIX_PL_Mutex *mutex; 15 static void *plContext = NULL; 16 17 static void 18 t1(/* ARGSUSED */ void *arg) 19 { 20 PKIX_Error *errorResult; 21 22 (void)printf("t1 acquiring lock...\n"); 23 errorResult = PKIX_PL_Mutex_Lock(mutex, plContext); 24 if (errorResult) 25 testError("PKIX_PL_Mutex_Lock failed"); 26 27 (void)printf("t1 sleeplng for 3 seconds\n"); 28 PR_Sleep(PR_SecondsToInterval(3)); 29 (void)printf("t1 releasing lock...\n"); 30 31 errorResult = PKIX_PL_Mutex_Unlock(mutex, plContext); 32 if (errorResult) 33 testError("PKIX_PL_Mutex_Unlock failed"); 34 35 (void)printf("t1 exiting...\n"); 36 } 37 38 static void 39 t2(/* ARGSUSED */ void *arg) 40 { 41 PKIX_Error *errorResult; 42 43 (void)printf("t2 acquiring lock...\n"); 44 errorResult = PKIX_PL_Mutex_Lock(mutex, plContext); 45 if (errorResult) 46 testError("PKIX_PL_Mutex_Lock failed"); 47 48 (void)printf("t2 releasing lock...\n"); 49 errorResult = PKIX_PL_Mutex_Unlock(mutex, plContext); 50 if (errorResult) 51 testError("PKIX_PL_Mutex_Unlock failed"); 52 53 (void)printf("t2 exiting...\n"); 54 } 55 56 int 57 test_mutex3(int argc, char *argv[]) 58 { 59 PRThread *thread, *thread2; 60 PKIX_UInt32 actualMinorVersion; 61 PKIX_UInt32 j = 0; 62 63 PKIX_TEST_STD_VARS(); 64 65 startTests("Mutex and Threads"); 66 67 PKIX_TEST_EXPECT_NO_ERROR( 68 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext)); 69 70 subTest("Mutex Creation"); 71 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Mutex_Create(&mutex, plContext)); 72 73 subTest("Starting thread"); 74 thread = PR_CreateThread(PR_USER_THREAD, 75 t1, 76 NULL, 77 PR_PRIORITY_NORMAL, 78 PR_LOCAL_THREAD, 79 PR_JOINABLE_THREAD, 80 0); 81 82 thread2 = PR_CreateThread(PR_USER_THREAD, 83 t2, 84 NULL, 85 PR_PRIORITY_NORMAL, 86 PR_LOCAL_THREAD, 87 PR_JOINABLE_THREAD, 88 0); 89 90 PR_JoinThread(thread2); 91 PR_JoinThread(thread); 92 93 cleanup: 94 95 PKIX_TEST_DECREF_AC(mutex); 96 97 PKIX_Shutdown(plContext); 98 99 PKIX_TEST_RETURN(); 100 101 endTests("Mutex and Threads"); 102 103 return (0); 104 }