home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jslock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.8 KB  |  121 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  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 <stdio.h>
  20. #include <stdlib.h>
  21. #include "jslock.h"
  22.  
  23. #ifdef JS_THREADSAFE
  24. #include "prlog.h"
  25. #include "prlock.h"
  26. #include "prthread.h"
  27. #include "prlong.h"
  28.  
  29. PRLock* js_owner_lock = 0;
  30. PRThread  *js_owner_thread = 0;
  31. unsigned  js_owner_count = 0;
  32.  
  33. void
  34. js_lock_task(JSTaskState *task)
  35. {
  36.     PRThread *me = PR_GetCurrentThread();
  37.  
  38.     if (! js_owner_lock) {
  39.       js_owner_lock = PR_NewLock();
  40.     }
  41.  
  42.     if ( js_owner_thread == me) {
  43.         PR_ASSERT(js_owner_count > 0);
  44.         js_owner_count++;
  45.     }
  46.     else {
  47.         PR_Lock(js_owner_lock);
  48.         PR_ASSERT(js_owner_count == 0);
  49.         js_owner_count = 1;
  50.         js_owner_thread = me;
  51.     }
  52.  
  53. }
  54.  
  55. void
  56. js_unlock_task(JSTaskState *task)
  57. {
  58.     if (js_owner_thread != PR_GetCurrentThread())
  59.         return;
  60.  
  61.     if ( --js_owner_count == 0) {
  62.         js_owner_thread = 0;
  63.         PR_Unlock(js_owner_lock);
  64.     }
  65.     PR_ASSERT(js_owner_count >= 0);
  66. }
  67.  
  68.  
  69. /*
  70. PRMonitor *js_owner_monitor;
  71. PRThread  *js_owner_thread;
  72. unsigned  js_owner_count;
  73.  
  74. void
  75. js_lock_task(JSTaskState *task)
  76. {
  77.     PRThread *t = PR_CurrentThread();
  78.  
  79.     if (!js_owner_monitor)
  80.         js_owner_monitor = PR_NewNamedMonitor("js-owner-monitor");
  81.     PR_EnterMonitor(js_owner_monitor);
  82.  
  83.     if (js_owner_thread == t) {
  84.         js_owner_count++;
  85.     } else {
  86.         do {
  87.             if (!js_owner_thread) {
  88.                 js_owner_thread = t;
  89.                 break;
  90.             }
  91.             PR_Wait(js_owner_monitor, PR_INTERVAL_MAX);
  92.         } while (js_owner_thread != t);
  93.     }
  94.     PR_ExitMonitor(js_owner_monitor);
  95. }
  96.  
  97. void
  98. js_unlock_task(JSTaskState *task)
  99. {
  100.     PR_ASSERT(PR_CurrentThread() == js_owner_thread);
  101.     PR_EnterMonitor(js_owner_monitor);
  102.     if (js_owner_count) {
  103.         js_owner_count--;
  104.     } else {
  105.         js_owner_thread = NULL;
  106.         PR_Notify(js_owner_monitor);
  107.     }
  108.     PR_ExitMonitor(js_owner_monitor);
  109. }
  110. */
  111.  
  112. #ifdef DEBUG
  113. int
  114. js_is_task_locked(JSTaskState *task)
  115. {
  116.     return (PR_CurrentThread() == js_owner_thread);
  117. }
  118. #endif
  119.  
  120. #endif /* JS_THREADSAFE */
  121.