]> git.saurik.com Git - apple/javascriptcore.git/blame - kjs/JSLock.cpp
JavaScriptCore-461.tar.gz
[apple/javascriptcore.git] / kjs / JSLock.cpp
CommitLineData
b37bf2e1
A
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
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA
20 *
21 */
22
23#include "config.h"
24#include "JSLock.h"
25
26#include "collector.h"
27#if USE(MULTIPLE_THREADS)
28#include <pthread.h>
29#endif
30
31namespace KJS {
32
33#if USE(MULTIPLE_THREADS)
34
35// Acquire this mutex before accessing lock-related data.
36static pthread_mutex_t JSMutex = PTHREAD_MUTEX_INITIALIZER;
37
38// Thread-specific key that tells whether a thread holds the JSMutex.
39pthread_key_t didLockJSMutex;
40
41// Lock nesting count.
42static int JSLockCount;
43
44static void createDidLockJSMutex()
45{
46 pthread_key_create(&didLockJSMutex, 0);
47}
48pthread_once_t createDidLockJSMutexOnce = PTHREAD_ONCE_INIT;
49
50void JSLock::lock()
51{
52 pthread_once(&createDidLockJSMutexOnce, createDidLockJSMutex);
53
54 if (!pthread_getspecific(didLockJSMutex)) {
55 int result;
56 result = pthread_mutex_lock(&JSMutex);
57 ASSERT(!result);
58 pthread_setspecific(didLockJSMutex, &didLockJSMutex);
59 }
60 ++JSLockCount;
61}
62
63void JSLock::unlock()
64{
65 ASSERT(JSLockCount);
66 ASSERT(!!pthread_getspecific(didLockJSMutex));
67
68 --JSLockCount;
69 if (!JSLockCount) {
70 pthread_setspecific(didLockJSMutex, 0);
71 int result;
72 result = pthread_mutex_unlock(&JSMutex);
73 ASSERT(!result);
74 }
75}
76
77bool JSLock::currentThreadIsHoldingLock()
78{
79 pthread_once(&createDidLockJSMutexOnce, createDidLockJSMutex);
80 return !!pthread_getspecific(didLockJSMutex);
81}
82
83void JSLock::registerThread()
84{
85 Collector::registerThread();
86}
87
88JSLock::DropAllLocks::DropAllLocks()
89 : m_lockCount(0)
90{
91 pthread_once(&createDidLockJSMutexOnce, createDidLockJSMutex);
92
93 m_lockCount = !!pthread_getspecific(didLockJSMutex) ? JSLock::lockCount() : 0;
94 for (int i = 0; i < m_lockCount; i++)
95 JSLock::unlock();
96}
97
98JSLock::DropAllLocks::~DropAllLocks()
99{
100 for (int i = 0; i < m_lockCount; i++)
101 JSLock::lock();
102 m_lockCount = 0;
103}
104
105#else
106
107// If threading support is off, set the lock count to a constant value of 1 so assertions
108// that the lock is held don't fail
109const int JSLockCount = 1;
110
111bool JSLock::currentThreadIsHoldingLock()
112{
113 return true;
114}
115
116void JSLock::lock()
117{
118}
119
120void JSLock::unlock()
121{
122}
123
124void JSLock::registerThread()
125{
126}
127
128JSLock::DropAllLocks::DropAllLocks()
129{
130}
131
132JSLock::DropAllLocks::~DropAllLocks()
133{
134}
135
136#endif // USE(MULTIPLE_THREADS)
137
138int JSLock::lockCount()
139{
140 return JSLockCount;
141}
142
143}
144
145
146#include "JSLockC.h"
147
148#ifdef __cplusplus
149extern "C" {
150#endif
151
152int JSLockDropAllLocks(void)
153{
154 KJS::JSLock::lock();
155 int lockCount = KJS::JSLock::lockCount();
156 for (int i = 0; i < lockCount; i++)
157 KJS::JSLock::unlock();
158 return lockCount - 1;
159}
160
161void JSLockRecoverAllLocks(int lockCount)
162{
163 ASSERT(KJS::JSLock::lockCount() == 0);
164 for (int i = 0; i < lockCount; i++)
165 KJS::JSLock::lock();
166}
167
168static pthread_t javaScriptCollectionThread = 0;
169
170void JSSetJavaScriptCollectionThread (pthread_t thread)
171{
172 javaScriptCollectionThread = thread;
173}
174
175pthread_t JSJavaScriptCollectionThread (void)
176{
177 return javaScriptCollectionThread;
178}
179
180#ifdef __cplusplus
181}
182#endif
183