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 |
|
---|
28 | namespace KJS {
|
---|
29 |
|
---|
30 | #if KJS_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 | // FIXME: Hack-o-rama. To prevent construction of a global object with a null prototype (4342216),
|
---|
54 | // we need to intialize our constants before the first object is constructed. InterpreterImp::lock()
|
---|
55 | // is a good place to do this because you have to call it before doing any allocations. Once we change our
|
---|
56 | // implementation to use immediate values, we should remove this code.
|
---|
57 | ConstantValues::initIfNeeded();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void JSLock::unlock()
|
---|
61 | {
|
---|
62 | interpreterLockCount--;
|
---|
63 | pthread_mutex_unlock(&interpreterLock);
|
---|
64 | }
|
---|
65 |
|
---|
66 | #else
|
---|
67 |
|
---|
68 | // If threading support is off, set the lock count to a constant value of 1 so assertions
|
---|
69 | // that the lock is held don't fail
|
---|
70 | const int interpreterLockCount = 1;
|
---|
71 |
|
---|
72 | void JSLock::lock()
|
---|
73 | {
|
---|
74 | // FIXME: Hack-o-rama. To prevent construction of a global object with a null prototype (4342216),
|
---|
75 | // we need to intialize our constants before the first object is constructed. InterpreterImp::lock()
|
---|
76 | // is a good place to do this because you have to call it before doing any allocations. Once we change our
|
---|
77 | // implementation to use immediate values, we should remove this code.
|
---|
78 | ConstantValues::initIfNeeded();
|
---|
79 | }
|
---|
80 |
|
---|
81 | void JSLock::unlock()
|
---|
82 | {
|
---|
83 | }
|
---|
84 |
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | int JSLock::lockCount()
|
---|
88 | {
|
---|
89 | return interpreterLockCount;
|
---|
90 | }
|
---|
91 |
|
---|
92 | JSLock::DropAllLocks::DropAllLocks()
|
---|
93 | {
|
---|
94 | int lockCount = JSLock::lockCount();
|
---|
95 | for (int i = 0; i < lockCount; i++) {
|
---|
96 | JSLock::unlock();
|
---|
97 | }
|
---|
98 | m_lockCount = lockCount;
|
---|
99 | }
|
---|
100 |
|
---|
101 | JSLock::DropAllLocks::~DropAllLocks()
|
---|
102 | {
|
---|
103 | int lockCount = m_lockCount;
|
---|
104 | for (int i = 0; i < lockCount; i++) {
|
---|
105 | JSLock::lock();
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | }
|
---|