File indexing completed on 2024-05-12 15:43:24

0001 /*
0002  * This file is part of the KDE libraries
0003  * Copyright (C) 2005 Apple Computer, Inc.
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA
0019  *
0020  */
0021 
0022 #include "JSLock.h"
0023 
0024 #include "collector.h"
0025 
0026 namespace KJS
0027 {
0028 
0029 #if USE(MULTIPLE_THREADS)
0030 
0031 static pthread_once_t interpreterLockOnce = PTHREAD_ONCE_INIT;
0032 static pthread_mutex_t interpreterLock;
0033 static int interpreterLockCount = 0;
0034 
0035 static void initializeJSLock()
0036 {
0037     pthread_mutexattr_t attr;
0038 
0039     pthread_mutexattr_init(&attr);
0040     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
0041 
0042     pthread_mutex_init(&interpreterLock, &attr);
0043 }
0044 
0045 void JSLock::lock()
0046 {
0047     pthread_once(&interpreterLockOnce, initializeJSLock);
0048     pthread_mutex_lock(&interpreterLock);
0049     interpreterLockCount++;
0050     Collector::registerThread();
0051 }
0052 
0053 void JSLock::unlock()
0054 {
0055     interpreterLockCount--;
0056     pthread_mutex_unlock(&interpreterLock);
0057 }
0058 
0059 int JSLock::lockCount()
0060 {
0061     return interpreterLockCount;
0062 }
0063 
0064 #endif
0065 
0066 JSLock::DropAllLocks::DropAllLocks()
0067 {
0068     int lockCount = JSLock::lockCount();
0069     for (int i = 0; i < lockCount; i++) {
0070         JSLock::unlock();
0071     }
0072     m_lockCount = lockCount;
0073 }
0074 
0075 JSLock::DropAllLocks::~DropAllLocks()
0076 {
0077     int lockCount = m_lockCount;
0078     for (int i = 0; i < lockCount; i++) {
0079         JSLock::lock();
0080     }
0081 }
0082 
0083 }