home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / threads / prdump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.3 KB  |  125 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. #include "primpl.h"
  20.  
  21. /* XXX use unbuffered nspr stdio */
  22.  
  23. PRFileDesc *_pr_dumpOut;
  24.  
  25. PRUint32 _PR_DumpPrintf(PRFileDesc *fd, const char *fmt, ...)
  26. {
  27.     char buf[100];
  28.     PRUint32 nb;
  29.     va_list ap;
  30.  
  31.     va_start(ap, fmt);
  32.     nb = PR_vsnprintf(buf, sizeof(buf), fmt, ap);
  33.     va_end(ap);
  34.     PR_Write(fd, buf, nb);
  35.  
  36.     return nb;
  37. }
  38.  
  39. void _PR_DumpThread(PRFileDesc *fd, PRThread *thread)
  40. {
  41.  
  42. #ifndef _PR_GLOBAL_THREADS_ONLY
  43.     _PR_DumpPrintf(fd, "%05d[%08p] pri=%2d flags=0x%02x",
  44.                    thread->id, thread, thread->priority, thread->flags);
  45.     switch (thread->state) {
  46.       case _PR_RUNNABLE:
  47.       case _PR_RUNNING:
  48.         break;
  49.       case _PR_LOCK_WAIT:
  50.         _PR_DumpPrintf(fd, " lock=%p", thread->wait.lock);
  51.         break;
  52.       case _PR_COND_WAIT:
  53.         _PR_DumpPrintf(fd, " condvar=%p sleep=%lldms",
  54.                        thread->wait.cvar, thread->sleep);
  55.         break;
  56.       case _PR_SUSPENDED:
  57.         _PR_DumpPrintf(fd, " suspended");
  58.         break;
  59.     }
  60.     PR_Write(fd, "\n", 1);
  61. #endif
  62.  
  63.     /* Now call dump routine */
  64.     if (thread->dump) {
  65.     thread->dump(fd, thread, thread->dumpArg);
  66.     }
  67. }
  68.  
  69. static void DumpThreadQueue(PRFileDesc *fd, PRCList *list)
  70. {
  71. #ifndef _PR_GLOBAL_THREADS_ONLY
  72.     PRCList *q;
  73.  
  74.     q = list->next;
  75.     while (q != list) {
  76.         PRThread *t = _PR_THREAD_PTR(q);
  77.         _PR_DumpThread(fd, t);
  78.         q = q->next;
  79.     }
  80. #endif
  81. }
  82.  
  83. void _PR_DumpThreads(PRFileDesc *fd)
  84. {
  85.     PRThread *t;
  86.     PRIntn i;
  87.  
  88.     _PR_DumpPrintf(fd, "Current Thread:\n");
  89.     t = _PR_MD_CURRENT_THREAD();
  90.     _PR_DumpThread(fd, t);
  91.  
  92.     _PR_DumpPrintf(fd, "Runnable Threads:\n");
  93.     for (i = 0; i < 32; i++) {
  94.         DumpThreadQueue(fd, &_PR_RUNQ(t->cpu)[i]);
  95.     }
  96.  
  97.     _PR_DumpPrintf(fd, "CondVar timed wait Threads:\n");
  98.     DumpThreadQueue(fd, &_PR_SLEEPQ(t->cpu));
  99.  
  100.     _PR_DumpPrintf(fd, "CondVar wait Threads:\n");
  101.     DumpThreadQueue(fd, &_PR_PAUSEQ(t->cpu));
  102.  
  103.     _PR_DumpPrintf(fd, "Suspended Threads:\n");
  104.     DumpThreadQueue(fd, &_PR_SUSPENDQ(t->cpu));
  105. }
  106.  
  107. void PR_ShowStatus(void)
  108. {
  109.     PRIntn is;
  110.  
  111.     if ( _PR_MD_CURRENT_THREAD()
  112.     && !_PR_IS_NATIVE_THREAD(_PR_MD_CURRENT_THREAD())) _PR_INTSOFF(is);
  113.     _pr_dumpOut = _pr_stderr;
  114.     _PR_DumpThreads(_pr_dumpOut);
  115.     if ( _PR_MD_CURRENT_THREAD()
  116.     && !_PR_IS_NATIVE_THREAD(_PR_MD_CURRENT_THREAD())) _PR_FAST_INTSON(is);
  117. }
  118.  
  119. PR_IMPLEMENT(void)
  120. PR_SetThreadDumpProc(PRThread* thread, PRThreadDumpProc dump, void *arg)
  121. {
  122.     thread->dump = dump;
  123.     thread->dumpArg = arg;
  124. }
  125.