Back to index

lightning-sunbird  0.9+nobinonly
TestIPCLocks.cpp
Go to the documentation of this file.
00001 /* vim:set ts=2 sw=2 sts=2 et cindent: */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla IPC.
00016  *
00017  * The Initial Developer of the Original Code is IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@meer.net>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 // This test program spawns N copies of itself, and each copy spawns M threads.
00039 // Each thread acquires and releases a named, interprocess lock.
00040 // Randomized delays are injected at various points to exercise the system, and
00041 // help expose any race conditions that may exist.
00042 //
00043 // Usage: TestIPCLocks [-N]
00044 
00045 #include <stdlib.h>
00046 #include <stdio.h>
00047 #include "ipcILockService.h"
00048 #include "ipcLockCID.h"
00049 #include "nsServiceManagerUtils.h"
00050 #include "nsIEventQueueService.h"
00051 #include "nsCOMPtr.h"
00052 #include "nsXPCOM.h"
00053 #include "prproces.h"
00054 #include "prprf.h"
00055 
00056 #if defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
00057 #include <unistd.h>
00058 static unsigned GetPID()
00059 {
00060   return (unsigned) getpid();
00061 }
00062 #elif defined(XP_WIN)
00063 #include <windows.h>
00064 static unsigned GetPID()
00065 {
00066   return (unsigned) GetCurrentProcessId();  
00067 }
00068 #else
00069 static unsigned int GetPID()
00070 {
00071   return 0; // implement me!
00072 }
00073 #endif
00074 
00075 static void LOG(const char *fmt, ... )
00076 {
00077   va_list ap;
00078   va_start(ap, fmt);
00079   PRUint32 nb = 0;
00080   char buf[512];
00081 
00082   nb = PR_snprintf(buf, sizeof(buf), "[%u:%p] ", GetPID(), PR_GetCurrentThread());
00083 
00084   PR_vsnprintf(buf + nb, sizeof(buf) - nb, fmt, ap);
00085   buf[sizeof(buf) - 1] = '\0';
00086 
00087   fwrite(buf, strlen(buf), 1, stdout);
00088   fflush(stdout);
00089 
00090   va_end(ap);
00091 }
00092 
00093 static void RandomSleep(PRUint32 fromMS, PRUint32 toMS)
00094 {
00095   PRUint32 ms = fromMS + (PRUint32) ((toMS - fromMS) * ((double) rand() / RAND_MAX));
00096   //LOG("putting thread to sleep for %u ms\n", ms);
00097   PR_Sleep(PR_MillisecondsToInterval(ms));
00098 }
00099 
00100 static ipcILockService *gLockService;
00101 
00102 PR_STATIC_CALLBACK(void) TestThread(void *arg)
00103 {
00104   const char *lockName = (const char *) arg;
00105 
00106   LOG("entering TestThread [lock=%s]\n", lockName);
00107 
00108   nsresult rv;
00109 
00110   RandomSleep(1000, 1100);
00111 
00112   //LOG("done sleeping\n");
00113 
00114   rv = gLockService->AcquireLock(lockName, PR_TRUE);
00115   if (NS_SUCCEEDED(rv))
00116   {
00117     //LOG("acquired lock \"%s\"\n", lockName);
00118     RandomSleep(500, 1000);
00119     //LOG("releasing lock \"%s\"\n", lockName);
00120     rv = gLockService->ReleaseLock(lockName);
00121     if (NS_FAILED(rv))
00122     {
00123       LOG("failed to release lock [rv=%x]\n", rv);
00124       NS_ERROR("failed to release lock");
00125     }
00126   }
00127   else
00128   {
00129     LOG("failed to acquire lock [rv=%x]\n", rv);
00130     NS_NOTREACHED("failed to acquire lock");
00131   }
00132 
00133   LOG("exiting TestThread [lock=%s rv=%x]\n", lockName, rv);
00134 }
00135 
00136 static const char *kLockNames[] = {
00137   "foopy",
00138   "test",
00139   "1",
00140   "xyz",
00141   "moz4ever",
00142   nsnull
00143 };
00144 
00145 static nsresult DoTest()
00146 {
00147   nsresult rv;
00148 
00149   nsCOMPtr<nsIEventQueueService> eqs =
00150       do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
00151   if (NS_FAILED(rv))
00152     return rv;
00153 
00154   rv = eqs->CreateMonitoredThreadEventQueue();
00155   if (NS_FAILED(rv))
00156     return rv;
00157 
00158   nsCOMPtr<nsIEventQueue> eq;
00159   rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eq));
00160   if (NS_FAILED(rv))
00161     return rv;
00162 
00163   nsCOMPtr<ipcILockService> lockService =
00164       do_GetService(IPC_LOCKSERVICE_CONTRACTID);
00165 
00166   gLockService = lockService;
00167 
00168   PRThread *threads[10] = {0};
00169   int i = 0;
00170 
00171   for (const char **lockName = kLockNames; *lockName; ++lockName, ++i)
00172   {
00173     threads[i] = PR_CreateThread(PR_USER_THREAD,
00174                                  TestThread,
00175                                  (void *) *lockName,
00176                                  PR_PRIORITY_NORMAL,
00177                                  PR_GLOBAL_THREAD,
00178                                  PR_JOINABLE_THREAD,
00179                                  0);
00180   }
00181 
00182   for (i=0; threads[i]; ++i)
00183   {
00184     PR_JoinThread(threads[i]);
00185     threads[i] = nsnull;
00186   }
00187 
00188   gLockService = nsnull;
00189 
00190   LOG("joined with all threads; exiting DoTest\n");
00191   return NS_OK;
00192 }
00193 
00194 int main(int argc, char **argv)
00195 {
00196   LOG("entering main\n");
00197 
00198   int numProcs = 10;
00199 
00200   // if this is a child process, then just run the test
00201   if (argc > 1)
00202   {
00203     if (strcmp(argv[1], "-child") == 0)
00204     {
00205       RandomSleep(1000, 1000);
00206       LOG("running child test\n");
00207       NS_InitXPCOM2(nsnull, nsnull, nsnull);
00208       DoTest();
00209       NS_ShutdownXPCOM(nsnull);
00210       return 0;
00211     }
00212     else if (argv[1][0] == '-')
00213     {
00214       // argument is a number
00215       numProcs = atoi(argv[1] + 1);
00216       if (numProcs == 0)
00217       {
00218         printf("### usage: TestIPCLocks [-N]\n"
00219                "where, N is the number of test processes to spawn.\n");
00220         return -1;
00221       }
00222     }
00223   }
00224 
00225   LOG("sleeping for 1 second\n");
00226   PR_Sleep(PR_SecondsToInterval(1));
00227 
00228   PRProcess **procs = (PRProcess **) malloc(sizeof(PRProcess*) * numProcs);
00229   int i;
00230 
00231   // else, spawn the child processes
00232   for (i=0; i<numProcs; ++i)
00233   {
00234     char *const argv[] = {"./TestIPCLocks", "-child", nsnull};
00235     LOG("spawning child test\n");
00236     procs[i] = PR_CreateProcess("./TestIPCLocks", argv, nsnull, nsnull);
00237   }
00238 
00239   PRInt32 exitCode;
00240   for (i=0; i<numProcs; ++i)
00241     PR_WaitProcess(procs[i], &exitCode);
00242   
00243   return 0;
00244 }