mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 1 | // -*- mode: c++; c-basic-offset: 4 -*- |
| 2 | /* |
| 3 | * This file is part of the KDE libraries |
| 4 | * Copyright (C) 2005 Apple Computer, Inc. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Library General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Library General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Library General Public License |
| 17 | * along with this library; see the file COPYING.LIB. If not, write to |
staikos | a09b185 | 2006-01-23 06:10:54 +0000 | [diff] [blame] | 18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 | * Boston, MA 02110-1301, USA |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | #include "JSLock.h" |
| 25 | |
| 26 | #include "collector.h" |
| 27 | |
| 28 | namespace KJS { |
| 29 | |
mjs | 3bfb61b | 2006-03-02 09:12:06 +0000 | [diff] [blame] | 30 | #if USE(MULTIPLE_THREADS) |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 31 | |
| 32 | static pthread_once_t interpreterLockOnce = PTHREAD_ONCE_INIT; |
| 33 | static pthread_mutex_t interpreterLock; |
| 34 | static int interpreterLockCount = 0; |
| 35 | |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 36 | static void initializeJSLock() |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 37 | { |
| 38 | pthread_mutexattr_t attr; |
| 39 | |
| 40 | pthread_mutexattr_init(&attr); |
| 41 | pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); |
| 42 | |
| 43 | pthread_mutex_init(&interpreterLock, &attr); |
| 44 | } |
| 45 | |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 46 | void JSLock::lock() |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 47 | { |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 48 | pthread_once(&interpreterLockOnce, initializeJSLock); |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 49 | pthread_mutex_lock(&interpreterLock); |
| 50 | interpreterLockCount++; |
| 51 | Collector::registerThread(); |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 52 | } |
| 53 | |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 54 | void JSLock::unlock() |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 55 | { |
| 56 | interpreterLockCount--; |
| 57 | pthread_mutex_unlock(&interpreterLock); |
| 58 | } |
| 59 | |
| 60 | #else |
| 61 | |
| 62 | // If threading support is off, set the lock count to a constant value of 1 so assertions |
| 63 | // that the lock is held don't fail |
| 64 | const int interpreterLockCount = 1; |
| 65 | |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 66 | void JSLock::lock() |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 70 | void JSLock::unlock() |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 71 | { |
| 72 | } |
| 73 | |
| 74 | #endif |
| 75 | |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 76 | int JSLock::lockCount() |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 77 | { |
| 78 | return interpreterLockCount; |
| 79 | } |
| 80 | |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 81 | JSLock::DropAllLocks::DropAllLocks() |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 82 | { |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 83 | int lockCount = JSLock::lockCount(); |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 84 | for (int i = 0; i < lockCount; i++) { |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 85 | JSLock::unlock(); |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 86 | } |
| 87 | m_lockCount = lockCount; |
| 88 | } |
| 89 | |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 90 | JSLock::DropAllLocks::~DropAllLocks() |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 91 | { |
| 92 | int lockCount = m_lockCount; |
| 93 | for (int i = 0; i < lockCount; i++) { |
mjs | 038246e | 2005-11-27 07:54:53 +0000 | [diff] [blame] | 94 | JSLock::lock(); |
mjs | 649c517 | 2005-11-23 05:41:23 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | } |