blob: 7e854a438f96587bd776c8f3ccb922e75ab9cb58 [file] [log] [blame]
mjs649c5172005-11-23 05:41:23 +00001// -*- 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
staikosa09b1852006-01-23 06:10:54 +000018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA
mjs649c5172005-11-23 05:41:23 +000020 *
21 */
22
23#include "config.h"
24#include "JSLock.h"
25
26#include "collector.h"
27
28namespace KJS {
29
mjs3bfb61b2006-03-02 09:12:06 +000030#if USE(MULTIPLE_THREADS)
mjs649c5172005-11-23 05:41:23 +000031
32static pthread_once_t interpreterLockOnce = PTHREAD_ONCE_INIT;
33static pthread_mutex_t interpreterLock;
34static int interpreterLockCount = 0;
35
mjs038246e2005-11-27 07:54:53 +000036static void initializeJSLock()
mjs649c5172005-11-23 05:41:23 +000037{
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
mjs038246e2005-11-27 07:54:53 +000046void JSLock::lock()
mjs649c5172005-11-23 05:41:23 +000047{
mjs038246e2005-11-27 07:54:53 +000048 pthread_once(&interpreterLockOnce, initializeJSLock);
mjs649c5172005-11-23 05:41:23 +000049 pthread_mutex_lock(&interpreterLock);
50 interpreterLockCount++;
51 Collector::registerThread();
mjs649c5172005-11-23 05:41:23 +000052}
53
mjs038246e2005-11-27 07:54:53 +000054void JSLock::unlock()
mjs649c5172005-11-23 05:41:23 +000055{
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
64const int interpreterLockCount = 1;
65
mjs038246e2005-11-27 07:54:53 +000066void JSLock::lock()
mjs649c5172005-11-23 05:41:23 +000067{
68}
69
mjs038246e2005-11-27 07:54:53 +000070void JSLock::unlock()
mjs649c5172005-11-23 05:41:23 +000071{
72}
73
74#endif
75
mjs038246e2005-11-27 07:54:53 +000076int JSLock::lockCount()
mjs649c5172005-11-23 05:41:23 +000077{
78 return interpreterLockCount;
79}
80
mjs038246e2005-11-27 07:54:53 +000081JSLock::DropAllLocks::DropAllLocks()
mjs649c5172005-11-23 05:41:23 +000082{
mjs038246e2005-11-27 07:54:53 +000083 int lockCount = JSLock::lockCount();
mjs649c5172005-11-23 05:41:23 +000084 for (int i = 0; i < lockCount; i++) {
mjs038246e2005-11-27 07:54:53 +000085 JSLock::unlock();
mjs649c5172005-11-23 05:41:23 +000086 }
87 m_lockCount = lockCount;
88}
89
mjs038246e2005-11-27 07:54:53 +000090JSLock::DropAllLocks::~DropAllLocks()
mjs649c5172005-11-23 05:41:23 +000091{
92 int lockCount = m_lockCount;
93 for (int i = 0; i < lockCount; i++) {
mjs038246e2005-11-27 07:54:53 +000094 JSLock::lock();
mjs649c5172005-11-23 05:41:23 +000095 }
96}
97
98}