📄 lock.c
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the Netscape Portable Runtime (NSPR). * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. *//*** File: lock.c** Purpose: test basic locking functions**** Modification History:** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.** The debug mode will print all of the printfs associated with this test.** The regress mode will be the default mode. Since the regress tool limits** the output to a one line status:PASS or FAIL,all of the printf statements** have been handled with an if (debug_mode) statement.** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to** recognize the return code from tha main program.**** 11-Aug-97 LarryH. Win16 port of NSPR.** - Added "PASS", "FAIL" messages on completion.** - Change stack variables to static scope variables** because of shadow-stack use by Win16** - Added PR_CALLBACK attribute to functions called by NSPR** - Added command line arguments:** - l <num> to control the number of loops** - c <num> to control the number of CPUs.** (was positional argv).** *************************************************************************//************************************************************************* Includes***********************************************************************//* Used to get the command line option */#include "plgetopt.h"#include "prio.h"#include "prcmon.h"#include "prinit.h"#include "prinrval.h"#include "prprf.h"#include "prlock.h"#include "prlog.h"#include "prmon.h"#include "prmem.h"#include "prthread.h"#include "prtypes.h"#include "plstr.h"#include <stdlib.h>#if defined(XP_UNIX)#include <string.h>#endif#ifdef XP_MAC#include "prlog.h"#define printf PR_LogPrintextern void SetupMacPrintfLog(char *logFile);#endifstatic PRIntn failed_already=0;static PRFileDesc *std_err = NULL;static PRBool verbosity = PR_FALSE;static PRBool debug_mode = PR_FALSE;const static PRIntervalTime contention_interval = 50;typedef struct LockContentious_s { PRLock *ml; PRInt32 loops; PRUint32 contender; PRUint32 contentious; PRIntervalTime overhead; PRIntervalTime interval;} LockContentious_t;typedef struct MonitorContentious_s { PRMonitor *ml; PRInt32 loops; PRUint32 contender; PRUint32 contentious; PRIntervalTime overhead; PRIntervalTime interval;} MonitorContentious_t;static PRIntervalTime Sleeper(PRUint32 loops){ PRIntervalTime predicted = 0; while (loops-- > 0) { predicted += contention_interval; (void)PR_Sleep(contention_interval); } return predicted;} /* Sleeper *//*** BASIC LOCKS*/static PRIntervalTime MakeLock(PRUint32 loops){ PRLock *ml = NULL; while (loops-- > 0) { ml = PR_NewLock(); PR_DestroyLock(ml); ml = NULL; } return 0;} /* MakeLock */static PRIntervalTime NonContentiousLock(PRUint32 loops){ PRLock *ml = NULL; ml = PR_NewLock(); while (loops-- > 0) { PR_Lock(ml); PR_Unlock(ml); } PR_DestroyLock(ml); return 0;} /* NonContentiousLock */static void PR_CALLBACK LockContender(void *arg){ LockContentious_t *contention = (LockContentious_t*)arg; while (contention->loops-- > 0) { PR_Lock(contention->ml); contention->contender+= 1; contention->overhead += contention->interval; PR_Sleep(contention->interval); PR_Unlock(contention->ml); }} /* LockContender */static PRIntervalTime ContentiousLock(PRUint32 loops){ PRStatus status; PRThread *thread = NULL; LockContentious_t * contention; PRIntervalTime rv, overhead, timein = PR_IntervalNow(); contention = PR_NEWZAP(LockContentious_t); contention->loops = loops; contention->overhead = 0; contention->ml = PR_NewLock(); contention->interval = contention_interval; thread = PR_CreateThread( PR_USER_THREAD, LockContender, contention, PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); PR_ASSERT(thread != NULL); overhead = PR_IntervalNow() - timein; while (contention->loops-- > 0) { PR_Lock(contention->ml); contention->contentious+= 1; contention->overhead += contention->interval; PR_Sleep(contention->interval); PR_Unlock(contention->ml); } timein = PR_IntervalNow(); status = PR_JoinThread(thread); PR_DestroyLock(contention->ml); overhead += (PR_IntervalNow() - timein); rv = overhead + contention->overhead; if (verbosity) PR_fprintf( std_err, "Access ratio: %u to %u\n", contention->contentious, contention->contender); PR_Free(contention); return rv;} /* ContentiousLock *//*** MONITORS*/static PRIntervalTime MakeMonitor(PRUint32 loops){ PRMonitor *ml = NULL; while (loops-- > 0) { ml = PR_NewMonitor(); PR_DestroyMonitor(ml); ml = NULL; } return 0;} /* MakeMonitor */static PRIntervalTime NonContentiousMonitor(PRUint32 loops){ PRMonitor *ml = NULL; ml = PR_NewMonitor(); while (loops-- > 0) { PR_EnterMonitor(ml); PR_ExitMonitor(ml); } PR_DestroyMonitor(ml); return 0;} /* NonContentiousMonitor */static void PR_CALLBACK TryEntry(void *arg){ PRMonitor *ml = (PRMonitor*)arg; if (debug_mode) PR_fprintf(std_err, "Reentrant thread created\n"); PR_EnterMonitor(ml); if (debug_mode) PR_fprintf(std_err, "Reentrant thread acquired monitor\n"); PR_ExitMonitor(ml); if (debug_mode) PR_fprintf(std_err, "Reentrant thread released monitor\n");} /* TryEntry */static PRIntervalTime ReentrantMonitor(PRUint32 loops){ PRStatus status; PRThread *thread; PRMonitor *ml = PR_NewMonitor(); if (debug_mode) PR_fprintf(std_err, "\nMonitor created for reentrant test\n"); PR_EnterMonitor(ml); PR_EnterMonitor(ml); if (debug_mode) PR_fprintf(std_err, "Monitor acquired twice\n"); thread = PR_CreateThread( PR_USER_THREAD, TryEntry, ml, PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); PR_ASSERT(thread != NULL); PR_Sleep(PR_SecondsToInterval(1)); PR_ExitMonitor(ml); if (debug_mode) PR_fprintf(std_err, "Monitor released first time\n"); PR_ExitMonitor(ml); if (debug_mode) PR_fprintf(std_err, "Monitor released second time\n"); status = PR_JoinThread(thread);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -