home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / intrupt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.7 KB  |  284 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * File:        intrupt.c
  21.  * Purpose:     testing thread interrupts
  22.  */
  23.  
  24. #include "plgetopt.h"
  25. #include "prcvar.h"
  26. #include "prerror.h"
  27. #include "prinit.h"
  28. #include "prinrval.h"
  29. #include "prio.h"
  30. #include "prlock.h"
  31. #include "prlog.h"
  32. #include "prthread.h"
  33. #include "prtypes.h"
  34. #include "prnetdb.h"
  35.  
  36. #include <stdio.h>
  37. #include <string.h>
  38.  
  39. #ifdef XP_MAC
  40. #include "prlog.h"
  41. #define printf PR_LogPrint
  42. extern void SetupMacPrintfLog(char *logFile);
  43. #endif
  44.  
  45. static PRLock *ml = NULL;
  46. static PRCondVar *cv = NULL;
  47.  
  48. static PRBool passed = PR_TRUE;
  49. static PRBool debug_mode = PR_FALSE;
  50. static PRThreadScope thread_scope = PR_LOCAL_THREAD;
  51.  
  52. static void PR_CALLBACK AbortCV(void *arg)
  53. {
  54.     PRStatus rv;
  55.     PRThread *me = PR_CurrentThread();
  56.  
  57.     /* some other thread (main) is doing the interrupt */
  58.     PR_Lock(ml);
  59.     rv = PR_WaitCondVar(cv, PR_INTERVAL_NO_TIMEOUT);
  60.     if (debug_mode) printf( "Expected interrupt on wait CV and ");
  61.     if (PR_FAILURE == rv)
  62.     {
  63.         if (PR_PENDING_INTERRUPT_ERROR == PR_GetError())
  64.         {
  65.             if (debug_mode) printf("got it\n");
  66.         }
  67.         else
  68.         {
  69.             if (debug_mode) printf("got random error\n");
  70.             passed = PR_FALSE;
  71.         }
  72.     }
  73.     else
  74.     {
  75.         if (debug_mode) printf("got a successful completion\n");
  76.         passed = PR_FALSE;
  77.     }
  78.  
  79.     rv = PR_WaitCondVar(cv, 10);
  80.     if (debug_mode)
  81.     {
  82.         printf(
  83.             "Expected success on wait CV and %s\n",
  84.             (PR_SUCCESS == rv) ? "got it" : "failed");
  85.     }
  86.     passed = ((PR_TRUE == passed) && (PR_SUCCESS == rv)) ? PR_TRUE : PR_FALSE;
  87.  
  88.     /* interrupt myself, then clear */
  89.     PR_Interrupt(me);
  90.     PR_ClearInterrupt();
  91.     rv = PR_WaitCondVar(cv, 10);
  92.     if (debug_mode)
  93.     {
  94.         printf("Expected success on wait CV and ");
  95.         if (PR_FAILURE == rv)
  96.         {
  97.             printf(
  98.                 "%s\n", (PR_PENDING_INTERRUPT_ERROR == PR_GetError()) ?
  99.                 "got interrupted" : "a random failure");
  100.         }
  101.         printf("got it\n");
  102.     }
  103.     passed = ((PR_TRUE == passed) && (PR_SUCCESS == rv)) ? PR_TRUE : PR_FALSE;
  104.  
  105.     /* set, then wait - interrupt - then wait again */
  106.     PR_Interrupt(me);
  107.     rv = PR_WaitCondVar(cv, 10);
  108.     if (debug_mode) printf( "Expected interrupt on wait CV and ");
  109.     if (PR_FAILURE == rv)
  110.     {
  111.         if (PR_PENDING_INTERRUPT_ERROR == PR_GetError())
  112.         {
  113.             if (debug_mode) printf("got it\n");
  114.         }
  115.         else
  116.         {
  117.             if (debug_mode) printf("failed\n");
  118.             passed = PR_FALSE;
  119.         }
  120.     }
  121.     else
  122.     {
  123.         if (debug_mode) printf("got a successful completion\n");
  124.         passed = PR_FALSE;
  125.     }
  126.  
  127.     rv = PR_WaitCondVar(cv, 10);
  128.     if (debug_mode)
  129.     {
  130.         printf(
  131.             "Expected success on wait CV and %s\n",
  132.             (PR_SUCCESS == rv) ? "got it" : "failed");
  133.     }
  134.     passed = ((PR_TRUE == passed) && (PR_SUCCESS == rv)) ? PR_TRUE : PR_FALSE;
  135.  
  136.     PR_Unlock(ml);
  137.  
  138. }  /* AbortCV */
  139.  
  140. static void PR_CALLBACK AbortIO(void *arg)
  141. {
  142.     PRStatus rv;
  143.     PR_Sleep(PR_SecondsToInterval(2));
  144.     rv = PR_Interrupt((PRThread*)arg);
  145.     PR_ASSERT(PR_SUCCESS == rv);
  146. }  /* AbortIO */
  147.  
  148. static void PR_CALLBACK AbortJoin(void *arg)
  149. {
  150. }  /* AbortJoin */
  151.  
  152. void PR_CALLBACK Intrupt(void *arg)
  153. {
  154.     PRStatus rv;
  155.     PRNetAddr netaddr;
  156.     PRFileDesc *listner;
  157.     PRInt16 port = 12848;
  158.     PRThread *abortCV, *abortIO, *abortJoin;
  159.  
  160.     ml = PR_NewLock();
  161.     cv = PR_NewCondVar(ml);
  162.  
  163. #ifdef XP_MAC
  164.     SetupMacPrintfLog("intrupt.log");
  165.     debug_mode = PR_TRUE;
  166. #endif
  167.  
  168.     /* Part I */
  169.     if (debug_mode) printf("Part I\n");
  170.     abortCV = PR_CreateThread(
  171.         PR_USER_THREAD, AbortCV, 0, PR_PRIORITY_NORMAL,
  172.         thread_scope, PR_JOINABLE_THREAD, 0);
  173.  
  174.     PR_Sleep(PR_SecondsToInterval(2));
  175.     rv = PR_Interrupt(abortCV);
  176.     PR_ASSERT(PR_SUCCESS == rv);
  177.     rv = PR_JoinThread(abortCV);
  178.     PR_ASSERT(PR_SUCCESS == rv);
  179.  
  180.     /* Part II */
  181.     if (debug_mode) printf("Part II\n");
  182.     abortJoin = PR_CreateThread(
  183.         PR_USER_THREAD, AbortJoin, 0, PR_PRIORITY_NORMAL,
  184.         thread_scope, PR_JOINABLE_THREAD, 0);
  185.     PR_Sleep(PR_SecondsToInterval(2));
  186.     if (debug_mode) printf("Expecting to interrupt an exited thread ");
  187.     rv = PR_Interrupt(abortJoin);
  188.     PR_ASSERT(PR_SUCCESS == rv);
  189.     rv = PR_JoinThread(abortJoin);
  190.     PR_ASSERT(PR_SUCCESS == rv);
  191.     if (debug_mode) printf("and succeeded\n");
  192.  
  193.     /* Part III */
  194.     if (debug_mode) printf("Part III\n");
  195.     listner = PR_NewTCPSocket();
  196.     memset(&netaddr, 0, sizeof(netaddr));
  197.     netaddr.inet.ip = PR_htonl(INADDR_ANY);
  198.     netaddr.inet.family = AF_INET;
  199.     do
  200.     {
  201.         netaddr.inet.port = PR_htons(port);
  202.         rv = PR_Bind(listner, &netaddr);
  203.         port += 1;
  204.         PR_ASSERT(port < (12848 + 10));
  205.     } while (PR_FAILURE == rv);
  206.  
  207.     rv = PR_Listen(listner, 5);
  208.  
  209.     if (PR_GetSockName(listner, &netaddr) < 0) {
  210.         if (debug_mode) printf("intrupt: ERROR - PR_GetSockName failed\n");
  211.         passed = PR_FALSE;
  212.         return;
  213.     }
  214.  
  215.     abortIO = PR_CreateThread(
  216.         PR_USER_THREAD, AbortIO, PR_CurrentThread(), PR_PRIORITY_NORMAL,
  217.         thread_scope, PR_JOINABLE_THREAD, 0);
  218.  
  219.     if (PR_Accept(listner, &netaddr, PR_INTERVAL_NO_TIMEOUT) == NULL)
  220.     {
  221.         PRInt32 error = PR_GetError();
  222.         if (debug_mode) printf("Expected interrupt on PR_Accept() and ");
  223.         if (PR_PENDING_INTERRUPT_ERROR == error)
  224.         {
  225.             if (debug_mode) printf("got it\n");
  226.         }
  227.         else
  228.         {
  229.             if (debug_mode) printf("failed\n");
  230.             passed = PR_FALSE;
  231.         }
  232.     }
  233.     else
  234.     {
  235.         if (debug_mode) printf("Failed to interrupt PR_Accept()\n");
  236.         passed = PR_FALSE;
  237.     }
  238.  
  239.     (void)PR_Close(listner); listner = NULL;
  240.  
  241.     rv = PR_JoinThread(abortIO);
  242.     PR_ASSERT(PR_SUCCESS == rv);
  243.  
  244.     PR_DestroyCondVar(cv);
  245.     PR_DestroyLock(ml);    
  246. }  /* Intrupt */
  247.  
  248. PRIntn main(PRIntn argc, char **argv)
  249. {
  250.     PRThread *intrupt;
  251.     PLOptStatus os;
  252.       PLOptState *opt = PL_CreateOptState(argc, argv, "dG");
  253.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  254.     {
  255.         if (PL_OPT_BAD == os) continue;
  256.         switch (opt->option)
  257.         {
  258.         case 'd':  /* debug mode */
  259.             debug_mode = PR_TRUE;
  260.             break;
  261.         case 'G':  /* use global threads */
  262.             thread_scope = PR_GLOBAL_THREAD;
  263.             break;
  264.         }
  265.     }
  266.     PL_DestroyOptState(opt);
  267.     PR_STDIO_INIT();
  268.     intrupt = PR_CreateThread(
  269.         PR_USER_THREAD, Intrupt, NULL, PR_PRIORITY_NORMAL,
  270.         thread_scope, PR_JOINABLE_THREAD, 0);
  271.     if (intrupt == NULL) {
  272.         fprintf(stderr, "cannot create thread\n");
  273.         passed = PR_FALSE;
  274.     } else {
  275.         PRStatus rv;
  276.         rv = PR_JoinThread(intrupt);
  277.         PR_ASSERT(rv == PR_SUCCESS);
  278.     }
  279.     printf("%s\n", ((passed) ? "PASSED" : "FAILED"));
  280.     return ((passed) ? 0 : 1);
  281. }  /* main */
  282.  
  283. /* intrupt.c */
  284.