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