Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: include/v8.h

Issue 6788023: Per-isolate v8::Locker and v8::Unlocker (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Code review feedback Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/api.h » ('j') | src/heap.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2007-2009 the V8 project authors. All rights reserved. 1 // Copyright 2007-2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3303 matching lines...) Expand 10 before | Expand all | Expand 10 after
3314 3314
3315 /** 3315 /**
3316 * Multiple threads in V8 are allowed, but only one thread at a time 3316 * Multiple threads in V8 are allowed, but only one thread at a time
3317 * is allowed to use any given V8 isolate. See Isolate class 3317 * is allowed to use any given V8 isolate. See Isolate class
3318 * comments. The definition of 'using V8 isolate' includes 3318 * comments. The definition of 'using V8 isolate' includes
3319 * accessing handles or holding onto object pointers obtained 3319 * accessing handles or holding onto object pointers obtained
3320 * from V8 handles while in the particular V8 isolate. It is up 3320 * from V8 handles while in the particular V8 isolate. It is up
3321 * to the user of V8 to ensure (perhaps with locking) that this 3321 * to the user of V8 to ensure (perhaps with locking) that this
3322 * constraint is not violated. 3322 * constraint is not violated.
3323 * 3323 *
3324 * More then one thread and multiple V8 isolates can be used 3324 * If the isolate is only accessed on a single thread, it can be
Vitaly Repeshko 2011/04/21 21:15:27 I think this paragraph adds little value and shoul
3325 * without any locking if each isolate is created and accessed 3325 * entered and exited on that thread at any time (subject to proper
3326 * by a single thread only. For example, one thread can use 3326 * nesting of Enter and Exit operations). More than one isolate may
3327 * multiple isolates or multiple threads can each create and run 3327 * be entered on any single thread.
3328 * their own isolate.
3329 * 3328 *
3330 * If you wish to start using V8 isolate in more then one thread 3329 * If the isolate is accessed from multiple threads of a process,
Vitaly Repeshko 2011/04/21 21:15:27 "the isolate" -> "an isolate" "of a process" -> ",
3331 * you can do this by constructing a v8::Locker object to guard 3330 * its use MUST be protected with appropriate v8::Locker use.
Vitaly Repeshko 2011/04/21 21:15:27 Replace the line with "v8::Locker must be used."
3331 * You can do this by constructing a v8::Locker object to guard
Vitaly Repeshko 2011/04/21 21:15:27 Replace the paragraph with "v8::Locker is a scoped
3332 * access to the isolate. After the code using V8 has completed 3332 * access to the isolate. After the code using V8 has completed
3333 * for the current thread you can call the destructor. This can 3333 * for the current thread you can call the destructor. This can
3334 * be combined with C++ scope-based construction as follows 3334 * be combined with C++ scope-based construction as follows
3335 * (assumes the default isolate that is used if not specified as 3335 * (assumes the default isolate that is used if not specified as
3336 * a parameter for the Locker): 3336 * a parameter for the Locker):
3337 * 3337 *
3338 * \code 3338 * \code
3339 * ... 3339 * ...
3340 * { 3340 * {
3341 * v8::Locker locker; 3341 * v8::Locker locker(isolate);
Vitaly Repeshko 2011/04/21 21:15:27 This snippet should show using Isolate::Scope.
3342 * ... 3342 * ...
3343 * // Code using V8 goes here. 3343 * // Code using V8 goes here.
3344 * ... 3344 * ...
3345 * } // Destructor called here 3345 * } // Destructor called here
3346 * \endcode 3346 * \endcode
3347 * 3347 *
3348 * If you wish to stop using V8 in a thread A you can do this by either 3348 * If you wish to stop using V8 in a thread A you can do this by either
3349 * by destroying the v8::Locker object as above or by constructing a 3349 * by destroying the v8::Locker object as above or by constructing a
3350 * v8::Unlocker object: 3350 * v8::Unlocker object:
3351 * 3351 *
3352 * \code 3352 * \code
3353 * { 3353 * {
Vitaly Repeshko 2011/04/21 21:15:27 There should be isolate->Exit() before '{' and iso
3354 * v8::Unlocker unlocker; 3354 * v8::Unlocker unlocker(isolate);
3355 * ... 3355 * ...
3356 * // Code not using V8 goes here while V8 can run in another thread. 3356 * // Code not using V8 goes here while V8 can run in another thread.
3357 * ... 3357 * ...
3358 * } // Destructor called here. 3358 * } // Destructor called here.
3359 * \endcode 3359 * \endcode
3360 * 3360 *
3361 * The Unlocker object is intended for use in a long-running callback 3361 * The Unlocker object is intended for use in a long-running callback
3362 * from V8, where you want to release the V8 lock for other threads to 3362 * from V8, where you want to release the V8 lock for other threads to
3363 * use. 3363 * use.
3364 * 3364 *
3365 * The v8::Locker is a recursive lock. That is, you can lock more than 3365 * The v8::Locker is a recursive lock. That is, you can lock more than
3366 * once in a given thread. This can be useful if you have code that can 3366 * once in a given thread. This can be useful if you have code that can
3367 * be called either from code that holds the lock or from code that does 3367 * be called either from code that holds the lock or from code that does
3368 * not. The Unlocker is not recursive so you can not have several 3368 * not. The Unlocker is not recursive so you can not have several
3369 * Unlockers on the stack at once, and you can not use an Unlocker in a 3369 * Unlockers on the stack at once, and you can not use an Unlocker in a
3370 * thread that is not inside a Locker's scope. 3370 * thread that is not inside a Locker's scope.
3371 * 3371 *
3372 * An unlocker will unlock several lockers if it has to and reinstate 3372 * An unlocker will unlock several lockers if it has to and reinstate
3373 * the correct depth of locking on its destruction. eg.: 3373 * the correct depth of locking on its destruction. eg.:
3374 * 3374 *
3375 * \code 3375 * \code
3376 * // V8 not locked. 3376 * // V8 not locked.
3377 * { 3377 * {
Vitaly Repeshko 2011/04/21 21:15:27 This snippet should use isolate as well.
3378 * v8::Locker locker; 3378 * v8::Locker locker;
3379 * // V8 locked. 3379 * // V8 locked.
3380 * { 3380 * {
3381 * v8::Locker another_locker; 3381 * v8::Locker another_locker;
3382 * // V8 still locked (2 levels). 3382 * // V8 still locked (2 levels).
3383 * { 3383 * {
3384 * v8::Unlocker unlocker; 3384 * v8::Unlocker unlocker;
3385 * // V8 not locked. 3385 * // V8 not locked.
3386 * } 3386 * }
3387 * // V8 locked again (2 levels). 3387 * // V8 locked again (2 levels).
3388 * } 3388 * }
3389 * // V8 still locked (1 level). 3389 * // V8 still locked (1 level).
3390 * } 3390 * }
3391 * // V8 Now no longer locked. 3391 * // V8 Now no longer locked.
3392 * \endcode 3392 * \endcode
3393 */ 3393 */
Vitaly Repeshko 2011/04/21 21:15:27 We need a special section describing the legacy us
3394 class V8EXPORT Unlocker { 3394 class V8EXPORT Unlocker {
3395 public: 3395 public:
3396 Unlocker(); 3396 /**
3397 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3398 */
3399 explicit Unlocker(Isolate* isolate = NULL);
3397 ~Unlocker(); 3400 ~Unlocker();
3401 private:
3402 internal::Isolate* isolate_;
3398 }; 3403 };
3399 3404
3400 3405
3401 class V8EXPORT Locker { 3406 class V8EXPORT Locker {
3402 public: 3407 public:
3403 Locker(); 3408 /**
3409 * Initialize Locker for a given Isolate. NULL means default isolate.
3410 */
3411 explicit Locker(Isolate* isolate = NULL);
3404 ~Locker(); 3412 ~Locker();
3405 3413
3406 /** 3414 /**
3407 * Start preemption. 3415 * Start preemption.
3408 * 3416 *
3409 * When preemption is started, a timer is fired every n milli seconds 3417 * When preemption is started, a timer is fired every n milli seconds
3410 * that will switch between multiple threads that are in contention 3418 * that will switch between multiple threads that are in contention
3411 * for the V8 lock. 3419 * for the V8 lock.
3412 */ 3420 */
3413 static void StartPreemption(int every_n_ms); 3421 static void StartPreemption(int every_n_ms);
3414 3422
3415 /** 3423 /**
3416 * Stop preemption. 3424 * Stop preemption.
3417 */ 3425 */
3418 static void StopPreemption(); 3426 static void StopPreemption();
3419 3427
3420 /** 3428 /**
3421 * Returns whether or not the locker is locked by the current thread. 3429 * Returns whether or not the locker for a given isolate, or default isolate i f NULL is given,
Vitaly Repeshko 2011/04/21 21:15:27 nit: Line too long.
3430 * is locked by the current thread.
3422 */ 3431 */
3423 static bool IsLocked(); 3432 static bool IsLocked(Isolate* isolate = NULL);
3424 3433
3425 /** 3434 /**
3426 * Returns whether v8::Locker is being used by this V8 instance. 3435 * Returns whether v8::Locker is being used by this V8 instance.
3427 */ 3436 */
3428 static bool IsActive() { return active_; } 3437 static bool IsActive() { return active_; }
3429 3438
3430 private: 3439 private:
3431 bool has_lock_; 3440 bool has_lock_;
3432 bool top_level_; 3441 bool top_level_;
3442 internal::Isolate* isolate_;
3433 3443
3434 static bool active_; 3444 static bool active_;
3435 3445
3436 // Disallow copying and assigning. 3446 // Disallow copying and assigning.
3437 Locker(const Locker&); 3447 Locker(const Locker&);
3438 void operator=(const Locker&); 3448 void operator=(const Locker&);
3439 }; 3449 };
3440 3450
3441 3451
3442 /** 3452 /**
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after
3991 4001
3992 4002
3993 } // namespace v8 4003 } // namespace v8
3994 4004
3995 4005
3996 #undef V8EXPORT 4006 #undef V8EXPORT
3997 #undef TYPE_CHECK 4007 #undef TYPE_CHECK
3998 4008
3999 4009
4000 #endif // V8_H_ 4010 #endif // V8_H_
OLDNEW
« no previous file with comments | « no previous file | src/api.h » ('j') | src/heap.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698