File indexing completed on 2024-05-12 15:43:24

0001 /*
0002  * This file is part of the KDE libraries
0003  * Copyright (C) 2005 Apple Computer, Inc.
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  *
0020  */
0021 
0022 #ifndef JSLOCK_H
0023 #define JSLOCK_H
0024 
0025 #include "global.h"
0026 
0027 namespace KJS
0028 {
0029 
0030 // to make it safe to use JavaScript on multiple threads, it is
0031 // important to lock before doing anything that allocates a
0032 // garbage-collected object or which may affect other shared state
0033 // such as the protect count hash table. The simplest way to do
0034 // this is by having a local JSLock object for the scope
0035 // where the lock must be held. The lock is recursive so nesting
0036 // is ok.
0037 
0038 // Sometimes it is necessary to temporarily release the lock -
0039 // since it is recursive you have to actually release all locks
0040 // held by your thread. This is safe to do if you are executing
0041 // code that doesn't require the lock, and reacquire the right
0042 // number of locks at the end. You can do this by constructing a
0043 // locally scoped JSLock::DropAllLocks object.
0044 
0045 class KJS_EXPORT JSLock
0046 {
0047 public:
0048     JSLock()
0049     {
0050         lock();
0051     }
0052     ~JSLock()
0053     {
0054         unlock();
0055     }
0056 
0057     static void lock();
0058     static void unlock();
0059     static int lockCount();
0060 
0061     class DropAllLocks
0062     {
0063     public:
0064         DropAllLocks();
0065         ~DropAllLocks();
0066     private:
0067         int m_lockCount;
0068 
0069         DropAllLocks(const DropAllLocks &);
0070         DropAllLocks &operator=(const DropAllLocks &);
0071     };
0072 
0073 private:
0074     JSLock(const JSLock &);
0075     JSLock &operator=(const JSLock &);
0076 };
0077 
0078 #if !USE(MULTIPLE_THREADS)
0079 inline void JSLock::lock()      {}
0080 inline void JSLock::unlock()    {}
0081 // Fix the lock count at 1 so assertions that the lock is held don't fail
0082 inline int JSLock::lockCount()
0083 {
0084     return 1;
0085 }
0086 #endif
0087 
0088 } // namespace
0089 
0090 #endif // JSLOCK_H