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., 59 Temple Place - Suite 330,
|
---|
19 | * Boston, MA 02111-1307, USA.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef KJS_INTERPRETER_LOCK_H
|
---|
24 | #define KJS_INTERPRETER_LOCK_H
|
---|
25 |
|
---|
26 | namespace KJS {
|
---|
27 |
|
---|
28 | // to make it safe to use JavaScript on multiple threads, it is
|
---|
29 | // important to lock before doing anything that allocates a
|
---|
30 | // garbage-collected object or which may affect other shared state
|
---|
31 | // such as the protect count hash table. The simplest way to do
|
---|
32 | // this is by having a local JSLock object for the scope
|
---|
33 | // where the lock must be held. The lock is recursive so nesting
|
---|
34 | // is ok.
|
---|
35 |
|
---|
36 | // Sometimes it is necessary to temporarily release the lock -
|
---|
37 | // since it is recursive you have to actually release all locks
|
---|
38 | // held by your thread. This is safe to do if you are executing
|
---|
39 | // code that doesn't require the lock, and reacquire the right
|
---|
40 | // number of locks at the end. You can do this by constructing a
|
---|
41 | // locally scoped JSLock::DropAllLocks object.
|
---|
42 |
|
---|
43 | class JSLock
|
---|
44 | {
|
---|
45 | public:
|
---|
46 | JSLock()
|
---|
47 | {
|
---|
48 | lock();
|
---|
49 | }
|
---|
50 | ~JSLock() {
|
---|
51 | unlock();
|
---|
52 | }
|
---|
53 |
|
---|
54 | static void lock();
|
---|
55 | static void unlock();
|
---|
56 | static int lockCount();
|
---|
57 |
|
---|
58 | class DropAllLocks {
|
---|
59 | public:
|
---|
60 | DropAllLocks();
|
---|
61 | ~DropAllLocks();
|
---|
62 | private:
|
---|
63 | int m_lockCount;
|
---|
64 |
|
---|
65 | DropAllLocks(const DropAllLocks&);
|
---|
66 | DropAllLocks& operator=(const DropAllLocks&);
|
---|
67 | };
|
---|
68 |
|
---|
69 | private:
|
---|
70 | JSLock(const JSLock&);
|
---|
71 | JSLock& operator=(const JSLock&);
|
---|
72 | };
|
---|
73 |
|
---|
74 | } // namespace
|
---|
75 |
|
---|
76 | #endif // KJS_INTERPRETER_LOCK_H
|
---|