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 "JSLock.h" |
24 | #include <config-kjs.h> |
25 | |
26 | #include "collector.h" |
27 | |
28 | namespace KJS { |
29 | |
30 | #if USE(MULTIPLE_THREADS) |
31 | |
32 | static pthread_once_t interpreterLockOnce = PTHREAD_ONCE_INIT; |
33 | static pthread_mutex_t interpreterLock; |
34 | static int interpreterLockCount = 0; |
35 | |
36 | static void initializeJSLock() |
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 | |
46 | void JSLock::lock() |
47 | { |
48 | pthread_once(&interpreterLockOnce, initializeJSLock); |
49 | pthread_mutex_lock(&interpreterLock); |
50 | interpreterLockCount++; |
51 | Collector::registerThread(); |
52 | } |
53 | |
54 | void JSLock::unlock() |
55 | { |
56 | interpreterLockCount--; |
57 | pthread_mutex_unlock(&interpreterLock); |
58 | } |
59 | |
60 | int JSLock::lockCount() |
61 | { |
62 | return interpreterLockCount; |
63 | } |
64 | |
65 | #endif |
66 | |
67 | JSLock::DropAllLocks::DropAllLocks() |
68 | { |
69 | int lockCount = JSLock::lockCount(); |
70 | for (int i = 0; i < lockCount; i++) { |
71 | JSLock::unlock(); |
72 | } |
73 | m_lockCount = lockCount; |
74 | } |
75 | |
76 | JSLock::DropAllLocks::~DropAllLocks() |
77 | { |
78 | int lockCount = m_lockCount; |
79 | for (int i = 0; i < lockCount; i++) { |
80 | JSLock::lock(); |
81 | } |
82 | } |
83 | |
84 | } |
85 | |