Back to index

lightning-sunbird  0.9+nobinonly
Functions | Variables
TestIPCLocks.cpp File Reference
#include <stdlib.h>
#include <stdio.h>
#include "ipcILockService.h"
#include "ipcLockCID.h"
#include "nsServiceManagerUtils.h"
#include "nsIEventQueueService.h"
#include "nsCOMPtr.h"
#include "nsXPCOM.h"
#include "prproces.h"
#include "prprf.h"

Go to the source code of this file.

Functions

static unsigned int GetPID ()
static void LOG (const char *fmt,...)
static void RandomSleep (PRUint32 fromMS, PRUint32 toMS)
 PR_STATIC_CALLBACK (void) TestThread(void *arg)
static nsresult DoTest ()
int main (int argc, char **argv)
 The Xalan testcases app.

Variables

static ipcILockServicegLockService
static const char * kLockNames []

Function Documentation

static nsresult DoTest ( ) [static]

Definition at line 145 of file TestIPCLocks.cpp.

{
  nsresult rv;

  nsCOMPtr<nsIEventQueueService> eqs =
      do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
  if (NS_FAILED(rv))
    return rv;

  rv = eqs->CreateMonitoredThreadEventQueue();
  if (NS_FAILED(rv))
    return rv;

  nsCOMPtr<nsIEventQueue> eq;
  rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eq));
  if (NS_FAILED(rv))
    return rv;

  nsCOMPtr<ipcILockService> lockService =
      do_GetService(IPC_LOCKSERVICE_CONTRACTID);

  gLockService = lockService;

  PRThread *threads[10] = {0};
  int i = 0;

  for (const char **lockName = kLockNames; *lockName; ++lockName, ++i)
  {
    threads[i] = PR_CreateThread(PR_USER_THREAD,
                                 TestThread,
                                 (void *) *lockName,
                                 PR_PRIORITY_NORMAL,
                                 PR_GLOBAL_THREAD,
                                 PR_JOINABLE_THREAD,
                                 0);
  }

  for (i=0; threads[i]; ++i)
  {
    PR_JoinThread(threads[i]);
    threads[i] = nsnull;
  }

  gLockService = nsnull;

  LOG("joined with all threads; exiting DoTest\n");
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static unsigned int GetPID ( ) [static]

Definition at line 69 of file TestIPCLocks.cpp.

{
  return 0; // implement me!
}

Here is the caller graph for this function:

static void LOG ( const char *  fmt,
  ... 
) [static]

Definition at line 75 of file TestIPCLocks.cpp.

{
  va_list ap;
  va_start(ap, fmt);
  PRUint32 nb = 0;
  char buf[512];

  nb = PR_snprintf(buf, sizeof(buf), "[%u:%p] ", GetPID(), PR_GetCurrentThread());

  PR_vsnprintf(buf + nb, sizeof(buf) - nb, fmt, ap);
  buf[sizeof(buf) - 1] = '\0';

  fwrite(buf, strlen(buf), 1, stdout);
  fflush(stdout);

  va_end(ap);
}

Here is the call graph for this function:

int main ( int  argc,
char **  argv 
)

The Xalan testcases app.

Definition at line 194 of file TestIPCLocks.cpp.

{
  LOG("entering main\n");

  int numProcs = 10;

  // if this is a child process, then just run the test
  if (argc > 1)
  {
    if (strcmp(argv[1], "-child") == 0)
    {
      RandomSleep(1000, 1000);
      LOG("running child test\n");
      NS_InitXPCOM2(nsnull, nsnull, nsnull);
      DoTest();
      NS_ShutdownXPCOM(nsnull);
      return 0;
    }
    else if (argv[1][0] == '-')
    {
      // argument is a number
      numProcs = atoi(argv[1] + 1);
      if (numProcs == 0)
      {
        printf("### usage: TestIPCLocks [-N]\n"
               "where, N is the number of test processes to spawn.\n");
        return -1;
      }
    }
  }

  LOG("sleeping for 1 second\n");
  PR_Sleep(PR_SecondsToInterval(1));

  PRProcess **procs = (PRProcess **) malloc(sizeof(PRProcess*) * numProcs);
  int i;

  // else, spawn the child processes
  for (i=0; i<numProcs; ++i)
  {
    char *const argv[] = {"./TestIPCLocks", "-child", nsnull};
    LOG("spawning child test\n");
    procs[i] = PR_CreateProcess("./TestIPCLocks", argv, nsnull, nsnull);
  }

  PRInt32 exitCode;
  for (i=0; i<numProcs; ++i)
    PR_WaitProcess(procs[i], &exitCode);
  
  return 0;
}

Here is the call graph for this function:

Definition at line 102 of file TestIPCLocks.cpp.

{
  const char *lockName = (const char *) arg;

  LOG("entering TestThread [lock=%s]\n", lockName);

  nsresult rv;

  RandomSleep(1000, 1100);

  //LOG("done sleeping\n");

  rv = gLockService->AcquireLock(lockName, PR_TRUE);
  if (NS_SUCCEEDED(rv))
  {
    //LOG("acquired lock \"%s\"\n", lockName);
    RandomSleep(500, 1000);
    //LOG("releasing lock \"%s\"\n", lockName);
    rv = gLockService->ReleaseLock(lockName);
    if (NS_FAILED(rv))
    {
      LOG("failed to release lock [rv=%x]\n", rv);
      NS_ERROR("failed to release lock");
    }
  }
  else
  {
    LOG("failed to acquire lock [rv=%x]\n", rv);
    NS_NOTREACHED("failed to acquire lock");
  }

  LOG("exiting TestThread [lock=%s rv=%x]\n", lockName, rv);
}
static void RandomSleep ( PRUint32  fromMS,
PRUint32  toMS 
) [static]

Definition at line 93 of file TestIPCLocks.cpp.

{
  PRUint32 ms = fromMS + (PRUint32) ((toMS - fromMS) * ((double) rand() / RAND_MAX));
  //LOG("putting thread to sleep for %u ms\n", ms);
  PR_Sleep(PR_MillisecondsToInterval(ms));
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 100 of file TestIPCLocks.cpp.

const char* kLockNames[] [static]
Initial value:
 {
  "foopy",
  "test",
  "1",
  "xyz",
  "moz4ever",
  nsnull
}

Definition at line 136 of file TestIPCLocks.cpp.