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 |
|
---|
31 | namespace KJS {
|
---|
32 |
|
---|
33 | #if USE(MULTIPLE_THREADS)
|
---|
34 |
|
---|
35 | // Acquire this mutex before accessing lock-related data.
|
---|
36 | static pthread_mutex_t JSMutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
37 |
|
---|
38 | // Thread-specific key that tells whether a thread holds the JSMutex.
|
---|
39 | pthread_key_t didLockJSMutex;
|
---|
40 |
|
---|
41 | // Lock nesting count.
|
---|
42 | static int JSLockCount;
|
---|
43 |
|
---|
44 | static void createDidLockJSMutex()
|
---|
45 | {
|
---|
46 | pthread_key_create(&didLockJSMutex, 0);
|
---|
47 | }
|
---|
48 | pthread_once_t createDidLockJSMutexOnce = PTHREAD_ONCE_INIT;
|
---|
49 |
|
---|
50 | void 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 |
|
---|
63 | void 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 |
|
---|
77 | bool JSLock::currentThreadIsHoldingLock()
|
---|
78 | {
|
---|
79 | pthread_once(&createDidLockJSMutexOnce, createDidLockJSMutex);
|
---|
80 | return !!pthread_getspecific(didLockJSMutex);
|
---|
81 | }
|
---|
82 |
|
---|
83 | void JSLock::registerThread()
|
---|
84 | {
|
---|
85 | Collector::registerThread();
|
---|
86 | }
|
---|
87 |
|
---|
88 | JSLock::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 |
|
---|
98 | JSLock::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
|
---|
109 | const int JSLockCount = 1;
|
---|
110 |
|
---|
111 | bool JSLock::currentThreadIsHoldingLock()
|
---|
112 | {
|
---|
113 | return true;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void JSLock::lock()
|
---|
117 | {
|
---|
118 | }
|
---|
119 |
|
---|
120 | void JSLock::unlock()
|
---|
121 | {
|
---|
122 | }
|
---|
123 |
|
---|
124 | void JSLock::registerThread()
|
---|
125 | {
|
---|
126 | }
|
---|
127 |
|
---|
128 | JSLock::DropAllLocks::DropAllLocks()
|
---|
129 | {
|
---|
130 | }
|
---|
131 |
|
---|
132 | JSLock::DropAllLocks::~DropAllLocks()
|
---|
133 | {
|
---|
134 | }
|
---|
135 |
|
---|
136 | #endif // USE(MULTIPLE_THREADS)
|
---|
137 |
|
---|
138 | int JSLock::lockCount()
|
---|
139 | {
|
---|
140 | return JSLockCount;
|
---|
141 | }
|
---|
142 |
|
---|
143 | }
|
---|