1 // -*- mode: c++; c-basic-offset: 4 -*-
3 * This file is part of the KDE libraries
4 * Copyright (C) 2005 Apple Computer, Inc.
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.
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.
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
26 #include "collector.h"
27 #if USE(MULTIPLE_THREADS)
33 #if USE(MULTIPLE_THREADS)
35 // Acquire this mutex before accessing lock-related data.
36 static pthread_mutex_t JSMutex
= PTHREAD_MUTEX_INITIALIZER
;
38 // Thread-specific key that tells whether a thread holds the JSMutex.
39 pthread_key_t didLockJSMutex
;
41 // Lock nesting count.
42 static int JSLockCount
;
44 static void createDidLockJSMutex()
46 pthread_key_create(&didLockJSMutex
, 0);
48 pthread_once_t createDidLockJSMutexOnce
= PTHREAD_ONCE_INIT
;
52 pthread_once(&createDidLockJSMutexOnce
, createDidLockJSMutex
);
54 if (!pthread_getspecific(didLockJSMutex
)) {
56 result
= pthread_mutex_lock(&JSMutex
);
58 pthread_setspecific(didLockJSMutex
, &didLockJSMutex
);
66 ASSERT(!!pthread_getspecific(didLockJSMutex
));
70 pthread_setspecific(didLockJSMutex
, 0);
72 result
= pthread_mutex_unlock(&JSMutex
);
77 bool JSLock::currentThreadIsHoldingLock()
79 pthread_once(&createDidLockJSMutexOnce
, createDidLockJSMutex
);
80 return !!pthread_getspecific(didLockJSMutex
);
83 void JSLock::registerThread()
85 Collector::registerThread();
88 JSLock::DropAllLocks::DropAllLocks()
91 pthread_once(&createDidLockJSMutexOnce
, createDidLockJSMutex
);
93 m_lockCount
= !!pthread_getspecific(didLockJSMutex
) ? JSLock::lockCount() : 0;
94 for (int i
= 0; i
< m_lockCount
; i
++)
98 JSLock::DropAllLocks::~DropAllLocks()
100 for (int i
= 0; i
< m_lockCount
; i
++)
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
109 const int JSLockCount
= 1;
111 bool JSLock::currentThreadIsHoldingLock()
120 void JSLock::unlock()
124 void JSLock::registerThread()
128 JSLock::DropAllLocks::DropAllLocks()
132 JSLock::DropAllLocks::~DropAllLocks()
136 #endif // USE(MULTIPLE_THREADS)
138 int JSLock::lockCount()
152 int JSLockDropAllLocks(void)
155 int lockCount
= KJS::JSLock::lockCount();
156 for (int i
= 0; i
< lockCount
; i
++)
157 KJS::JSLock::unlock();
158 return lockCount
- 1;
161 void JSLockRecoverAllLocks(int lockCount
)
163 ASSERT(KJS::JSLock::lockCount() == 0);
164 for (int i
= 0; i
< lockCount
; i
++)
168 static pthread_t javaScriptCollectionThread
= 0;
170 void JSSetJavaScriptCollectionThread (pthread_t thread
)
172 javaScriptCollectionThread
= thread
;
175 pthread_t
JSJavaScriptCollectionThread (void)
177 return javaScriptCollectionThread
;