source: webkit/trunk/JavaScriptCore/wtf/ThreadingGtk.cpp@ 37556

Last change on this file since 37556 was 37556, checked in by [email protected], 17 years ago

2008-10-13 Marco Barisione <[email protected]>

Reviewed by Darin Adler. Landed by Jan Alonzo.

WebKit GTK Port needs a smartpointer to handle g_free (GFreePtr?)
http://bugs.webkit.org/show_bug.cgi?id=20483

Start the conversion to use GOwnPtr and fix a memory leak.

  • platform/graphics/gtk/MediaPlayerPrivateGStreamer.cpp: (WebCore::mediaPlayerPrivateErrorCallback):

2008-10-13 Marco Barisione <[email protected]>

Reviewed by Darin Adler. Landed by Jan Alonzo.

WebKit GTK Port needs a smartpointer to handle g_free (GFreePtr?)
http://bugs.webkit.org/show_bug.cgi?id=20483

Add a GOwnPtr smart pointer (similar to OwnPtr) to handle memory
allocated by GLib and start the conversion to use it.

  • GNUmakefile.am:
  • wtf/GOwnPtr.cpp: Added. (WTF::GError): (WTF::GList): (WTF::GCond): (WTF::GMutex): (WTF::GPatternSpec): (WTF::GDir):
  • wtf/GOwnPtr.h: Added. (WTF::freeOwnedPtr): (WTF::GOwnPtr::GOwnPtr): (WTF::GOwnPtr::~GOwnPtr): (WTF::GOwnPtr::get): (WTF::GOwnPtr::release): (WTF::GOwnPtr::rawPtr): (WTF::GOwnPtr::set): (WTF::GOwnPtr::clear): (WTF::GOwnPtr::operator*): (WTF::GOwnPtr::operator->): (WTF::GOwnPtr::operator!): (WTF::GOwnPtr::operator UnspecifiedBoolType): (WTF::GOwnPtr::swap): (WTF::swap): (WTF::operator==): (WTF::operator!=): (WTF::getPtr):
  • wtf/Threading.h:
  • wtf/ThreadingGtk.cpp: (WTF::Mutex::~Mutex): (WTF::Mutex::lock): (WTF::Mutex::tryLock): (WTF::Mutex::unlock): (WTF::ThreadCondition::~ThreadCondition): (WTF::ThreadCondition::wait): (WTF::ThreadCondition::timedWait): (WTF::ThreadCondition::signal): (WTF::ThreadCondition::broadcast):
  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1/*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Justin Haygood ([email protected])
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "Threading.h"
32
33#include "HashMap.h"
34#include "MainThread.h"
35#include "MathExtras.h"
36
37#include <glib.h>
38
39namespace WTF {
40
41Mutex* atomicallyInitializedStaticMutex;
42
43static ThreadIdentifier mainThreadIdentifier;
44
45static Mutex& threadMapMutex()
46{
47 static Mutex mutex;
48 return mutex;
49}
50
51void initializeThreading()
52{
53 if (!g_thread_supported())
54 g_thread_init(NULL);
55 ASSERT(g_thread_supported());
56
57 if (!atomicallyInitializedStaticMutex) {
58 atomicallyInitializedStaticMutex = new Mutex;
59 threadMapMutex();
60 wtf_random_init();
61 mainThreadIdentifier = currentThread();
62 initializeMainThread();
63 }
64}
65
66static HashMap<ThreadIdentifier, GThread*>& threadMap()
67{
68 static HashMap<ThreadIdentifier, GThread*> map;
69 return map;
70}
71
72static ThreadIdentifier establishIdentifierForThread(GThread*& thread)
73{
74 MutexLocker locker(threadMapMutex());
75
76 static ThreadIdentifier identifierCount = 1;
77
78 threadMap().add(identifierCount, thread);
79
80 return identifierCount++;
81}
82
83static ThreadIdentifier identifierByGthreadHandle(GThread*& thread)
84{
85 MutexLocker locker(threadMapMutex());
86
87 HashMap<ThreadIdentifier, GThread*>::iterator i = threadMap().begin();
88 for (; i != threadMap().end(); ++i) {
89 if (i->second == thread)
90 return i->first;
91 }
92
93 return 0;
94}
95
96static GThread* threadForIdentifier(ThreadIdentifier id)
97{
98 MutexLocker locker(threadMapMutex());
99
100 return threadMap().get(id);
101}
102
103static void clearThreadForIdentifier(ThreadIdentifier id)
104{
105 MutexLocker locker(threadMapMutex());
106
107 ASSERT(threadMap().contains(id));
108
109 threadMap().remove(id);
110}
111
112ThreadIdentifier createThread(ThreadFunction entryPoint, void* data, const char*)
113{
114 GThread* thread;
115 if (!(thread = g_thread_create(entryPoint, data, TRUE, 0))) {
116 LOG_ERROR("Failed to create thread at entry point %p with data %p", entryPoint, data);
117 return 0;
118 }
119
120 ThreadIdentifier threadID = establishIdentifierForThread(thread);
121 return threadID;
122}
123
124int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
125{
126 ASSERT(threadID);
127
128 GThread* thread = threadForIdentifier(threadID);
129
130 *result = g_thread_join(thread);
131
132 clearThreadForIdentifier(threadID);
133 return 0;
134}
135
136void detachThread(ThreadIdentifier)
137{
138}
139
140ThreadIdentifier currentThread()
141{
142 GThread* currentThread = g_thread_self();
143 if (ThreadIdentifier id = identifierByGthreadHandle(currentThread))
144 return id;
145 return establishIdentifierForThread(currentThread);
146}
147
148bool isMainThread()
149{
150 return currentThread() == mainThreadIdentifier;
151}
152
153Mutex::Mutex()
154 : m_mutex(g_mutex_new())
155{
156}
157
158Mutex::~Mutex()
159{
160}
161
162void Mutex::lock()
163{
164 g_mutex_lock(m_mutex.get());
165}
166
167bool Mutex::tryLock()
168{
169 return g_mutex_trylock(m_mutex.get());
170}
171
172void Mutex::unlock()
173{
174 g_mutex_unlock(m_mutex.get());
175}
176
177ThreadCondition::ThreadCondition()
178 : m_condition(g_cond_new())
179{
180}
181
182ThreadCondition::~ThreadCondition()
183{
184}
185
186void ThreadCondition::wait(Mutex& mutex)
187{
188 g_cond_wait(m_condition.get(), mutex.impl().get());
189}
190
191bool ThreadCondition::timedWait(Mutex& mutex, double interval)
192{
193 if (interval < 0.0) {
194 wait(mutex);
195 return true;
196 }
197
198 int intervalSeconds = static_cast<int>(interval);
199 int intervalMicroseconds = static_cast<int>((interval - intervalSeconds) * 1000000.0);
200
201 GTimeVal targetTime;
202 g_get_current_time(&targetTime);
203
204 targetTime.tv_sec += intervalSeconds;
205 targetTime.tv_usec += intervalMicroseconds;
206 if (targetTime.tv_usec > 1000000) {
207 targetTime.tv_usec -= 1000000;
208 targetTime.tv_sec++;
209 }
210
211 return g_cond_timed_wait(m_condition.get(), mutex.impl().get(), &targetTime);
212}
213
214void ThreadCondition::signal()
215{
216 g_cond_signal(m_condition.get());
217}
218
219void ThreadCondition::broadcast()
220{
221 g_cond_broadcast(m_condition.get());
222}
223
224
225}
Note: See TracBrowser for help on using the repository browser.