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 | #ifndef KJS_JSLock_h
|
---|
24 | #define KJS_JSLock_h
|
---|
25 |
|
---|
26 | #include <wtf/Assertions.h>
|
---|
27 | #include <wtf/Noncopyable.h>
|
---|
28 |
|
---|
29 | namespace KJS {
|
---|
30 |
|
---|
31 | // To make it safe to use JavaScript on multiple threads, it is
|
---|
32 | // important to lock before doing anything that allocates a
|
---|
33 | // JavaScript data structure or that interacts with shared state
|
---|
34 | // such as the protect count hash table. The simplest way to lock
|
---|
35 | // is to create a local JSLock object in the scope where the lock
|
---|
36 | // must be held. The lock is recursive so nesting is ok. The JSLock
|
---|
37 | // object also acts as a convenience short-hand for running important
|
---|
38 | // initialization routines.
|
---|
39 |
|
---|
40 | // To avoid deadlock, sometimes it is necessary to temporarily
|
---|
41 | // release the lock. Since it is recursive you actually have to
|
---|
42 | // release all locks held by your thread. This is safe to do if
|
---|
43 | // you are executing code that doesn't require the lock, and you
|
---|
44 | // reacquire the right number of locks at the end. You can do this
|
---|
45 | // by constructing a locally scoped JSLock::DropAllLocks object. The
|
---|
46 | // DropAllLocks object takes care to release the JSLock only if your
|
---|
47 | // thread acquired it to begin with.
|
---|
48 |
|
---|
49 | class JSLock : Noncopyable {
|
---|
50 | public:
|
---|
51 | JSLock()
|
---|
52 | {
|
---|
53 | lock();
|
---|
54 | registerThread();
|
---|
55 | }
|
---|
56 |
|
---|
57 | ~JSLock()
|
---|
58 | {
|
---|
59 | unlock();
|
---|
60 | }
|
---|
61 |
|
---|
62 | static void lock();
|
---|
63 | static void unlock();
|
---|
64 | static int lockCount();
|
---|
65 | static bool currentThreadIsHoldingLock();
|
---|
66 |
|
---|
67 | static void registerThread();
|
---|
68 |
|
---|
69 | class DropAllLocks : Noncopyable {
|
---|
70 | public:
|
---|
71 | DropAllLocks();
|
---|
72 | ~DropAllLocks();
|
---|
73 |
|
---|
74 | private:
|
---|
75 | int m_lockCount;
|
---|
76 | };
|
---|
77 | };
|
---|
78 |
|
---|
79 | } // namespace
|
---|
80 |
|
---|
81 | #endif // KJS_JSLock_h
|
---|