Changeset 140040 in webkit
- Timestamp:
- Jan 17, 2013 2:17:12 PM (11 years ago)
- Location:
- trunk/Source/WTF
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r139982 r140040 1 2013-01-17 Adam Barth <abarth@webkit.org> 2 3 Teach Functional.h about WeakPtr 4 https://bugs.webkit.org/show_bug.cgi?id=107105 5 6 Reviewed by Anders Carlsson. 7 8 A common pattern in cross-thread communication is to call member 9 functions of an object on a remote thread. If the caller's reference to 10 the object on the remote thread is a WeakPtr, the caller usually wants 11 to validate that the object still exists when the call actually takes 12 place. 13 14 It's possible to do this manually for every cross-thread call, but that 15 is tiresome and error prone. Instead, we can teach bind to validate 16 WeakPtr arguments when passed as the "this" parameter to a member 17 function. 18 19 * wtf/Functional.h: 20 (WTF::ParamStorageTraits::validate): 21 1 22 2013-01-17 David Kilzer <ddkilzer@apple.com> 2 23 -
trunk/Source/WTF/wtf/Functional.h
r135882 r140040 31 31 #include <wtf/RefPtr.h> 32 32 #include <wtf/ThreadSafeRefCounted.h> 33 #include <wtf/WeakPtr.h> 33 34 34 35 #if PLATFORM(MAC) && COMPILER_SUPPORTS(BLOCKS) … … 83 84 typedef R ResultType; 84 85 static const bool shouldRefFirstParameter = false; 86 static const bool shouldValidateFirstParameter = false; 85 87 86 88 explicit FunctionWrapper(R (*function)()) … … 103 105 typedef R ResultType; 104 106 static const bool shouldRefFirstParameter = false; 107 static const bool shouldValidateFirstParameter = false; 105 108 106 109 explicit FunctionWrapper(R (*function)(P1)) … … 123 126 typedef R ResultType; 124 127 static const bool shouldRefFirstParameter = false; 128 static const bool shouldValidateFirstParameter = false; 125 129 126 130 explicit FunctionWrapper(R (*function)(P1, P2)) … … 143 147 typedef R ResultType; 144 148 static const bool shouldRefFirstParameter = false; 149 static const bool shouldValidateFirstParameter = false; 145 150 146 151 explicit FunctionWrapper(R (*function)(P1, P2, P3)) … … 163 168 typedef R ResultType; 164 169 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 170 static const bool shouldValidateFirstParameter = true; 165 171 166 172 explicit FunctionWrapper(R (C::*function)()) … … 183 189 typedef R ResultType; 184 190 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 191 static const bool shouldValidateFirstParameter = true; 185 192 186 193 explicit FunctionWrapper(R (C::*function)(P1)) … … 203 210 typedef R ResultType; 204 211 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 212 static const bool shouldValidateFirstParameter = true; 205 213 206 214 explicit FunctionWrapper(R (C::*function)(P1, P2)) … … 223 231 typedef R ResultType; 224 232 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 233 static const bool shouldValidateFirstParameter = true; 225 234 226 235 explicit FunctionWrapper(R (C::*function)(P1, P2, P3)) … … 243 252 typedef R ResultType; 244 253 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 254 static const bool shouldValidateFirstParameter = true; 245 255 246 256 explicit FunctionWrapper(R (C::*function)(P1, P2, P3, P4)) … … 263 273 typedef R ResultType; 264 274 static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value; 275 static const bool shouldValidateFirstParameter = true; 265 276 266 277 explicit FunctionWrapper(R (C::*function)(P1, P2, P3, P4, P5)) … … 284 295 typedef R ResultType; 285 296 static const bool shouldRefFirstParameter = false; 297 static const bool shouldValidateFirstParameter = false; 286 298 287 299 explicit FunctionWrapper(R (^block)()) … … 324 336 325 337 static StorageType wrap(const T& value) { return value; } 338 static bool validate(const StorageType&) { return true; } 326 339 static const T& unwrap(const StorageType& value) { return value; } 327 340 }; … … 331 344 332 345 static StorageType wrap(PassRefPtr<T> value) { return value; } 346 static bool validate(const StorageType&) { return true; } 333 347 static T* unwrap(const StorageType& value) { return value.get(); } 334 348 }; … … 338 352 339 353 static StorageType wrap(RefPtr<T> value) { return value.release(); } 354 static bool validate(const StorageType&) { return true; } 355 static T* unwrap(const StorageType& value) { return value.get(); } 356 }; 357 358 template<typename T> struct ParamStorageTraits<WeakPtr<T> > { 359 typedef WeakPtr<T> StorageType; 360 361 static StorageType wrap(WeakPtr<T> value) { return value; } 362 static bool validate(const StorageType& value) { return value.get(); } 340 363 static T* unwrap(const StorageType& value) { return value.get(); } 341 364 }; … … 348 371 349 372 static StorageType wrap(const RetainPtr<T>& value) { return value; } 373 static bool validate(const StorageType&) { return true; } 350 374 static typename RetainPtr<T>::PtrType unwrap(const StorageType& value) { return value.get(); } 351 375 }; … … 376 400 } 377 401 378 virtual Roperator()()402 virtual typename FunctionWrapper::ResultType operator()() 379 403 { 380 404 return m_functionWrapper(); … … 387 411 template<typename FunctionWrapper, typename R, typename P1> 388 412 class BoundFunctionImpl<FunctionWrapper, R (P1)> : public FunctionImpl<typename FunctionWrapper::ResultType ()> { 389 390 413 public: 391 414 BoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1) … … 401 424 } 402 425 403 virtual R operator()() 404 { 426 virtual typename FunctionWrapper::ResultType operator()() 427 { 428 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1)) 429 return typename FunctionWrapper::ResultType(); 405 430 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1)); 406 431 } … … 429 454 virtual typename FunctionWrapper::ResultType operator()() 430 455 { 456 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1)) 457 return typename FunctionWrapper::ResultType(); 431 458 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2)); 432 459 } … … 457 484 virtual typename FunctionWrapper::ResultType operator()() 458 485 { 486 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1)) 487 return typename FunctionWrapper::ResultType(); 459 488 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3)); 460 489 } … … 487 516 virtual typename FunctionWrapper::ResultType operator()() 488 517 { 518 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1)) 519 return typename FunctionWrapper::ResultType(); 489 520 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4)); 490 521 } … … 519 550 virtual typename FunctionWrapper::ResultType operator()() 520 551 { 552 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1)) 553 return typename FunctionWrapper::ResultType(); 521 554 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4), ParamStorageTraits<P5>::unwrap(m_p5)); 522 555 } … … 553 586 virtual typename FunctionWrapper::ResultType operator()() 554 587 { 588 if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1)) 589 return typename FunctionWrapper::ResultType(); 555 590 return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4), ParamStorageTraits<P5>::unwrap(m_p5), ParamStorageTraits<P6>::unwrap(m_p6)); 556 591 }
Note: See TracChangeset
for help on using the changeset viewer.