| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 3337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3348 | 3348 |
| 3349 /** | 3349 /** |
| 3350 * Multiple threads in V8 are allowed, but only one thread at a time | 3350 * Multiple threads in V8 are allowed, but only one thread at a time |
| 3351 * is allowed to use any given V8 isolate. See Isolate class | 3351 * is allowed to use any given V8 isolate. See Isolate class |
| 3352 * comments. The definition of 'using V8 isolate' includes | 3352 * comments. The definition of 'using V8 isolate' includes |
| 3353 * accessing handles or holding onto object pointers obtained | 3353 * accessing handles or holding onto object pointers obtained |
| 3354 * from V8 handles while in the particular V8 isolate. It is up | 3354 * from V8 handles while in the particular V8 isolate. It is up |
| 3355 * to the user of V8 to ensure (perhaps with locking) that this | 3355 * to the user of V8 to ensure (perhaps with locking) that this |
| 3356 * constraint is not violated. | 3356 * constraint is not violated. |
| 3357 * | 3357 * |
| 3358 * v8::Locker is a scoped lock object. While it's | 3358 * More then one thread and multiple V8 isolates can be used |
| 3359 * active (i.e. between its construction and destruction) the current thread is | 3359 * without any locking if each isolate is created and accessed |
| 3360 * allowed to use the locked isolate. V8 guarantees that an isolate can be locke
d | 3360 * by a single thread only. For example, one thread can use |
| 3361 * by at most one thread at any time. In other words, the scope of a v8::Locker
is | 3361 * multiple isolates or multiple threads can each create and run |
| 3362 * a critical section. | 3362 * their own isolate. |
| 3363 * | 3363 * |
| 3364 * Sample usage: | 3364 * If you wish to start using V8 isolate in more then one thread |
| 3365 * \code | 3365 * you can do this by constructing a v8::Locker object to guard |
| 3366 * access to the isolate. After the code using V8 has completed |
| 3367 * for the current thread you can call the destructor. This can |
| 3368 * be combined with C++ scope-based construction as follows |
| 3369 * (assumes the default isolate that is used if not specified as |
| 3370 * a parameter for the Locker): |
| 3371 * |
| 3372 * \code |
| 3366 * ... | 3373 * ... |
| 3367 * { | 3374 * { |
| 3368 * v8::Locker locker(isolate); | 3375 * v8::Locker locker; |
| 3369 * v8::Isolate::Scope isolate_scope(isolate); | |
| 3370 * ... | 3376 * ... |
| 3371 * // Code using V8 and isolate goes here. | 3377 * // Code using V8 goes here. |
| 3372 * ... | 3378 * ... |
| 3373 * } // Destructor called here | 3379 * } // Destructor called here |
| 3374 * \endcode | 3380 * \endcode |
| 3375 * | 3381 * |
| 3376 * If you wish to stop using V8 in a thread A you can do this by either | 3382 * If you wish to stop using V8 in a thread A you can do this by either |
| 3377 * by destroying the v8::Locker object as above or by constructing a | 3383 * by destroying the v8::Locker object as above or by constructing a |
| 3378 * v8::Unlocker object: | 3384 * v8::Unlocker object: |
| 3379 * | 3385 * |
| 3380 * \code | 3386 * \code |
| 3381 * { | 3387 * { |
| 3382 * isolate->Exit(); | 3388 * v8::Unlocker unlocker; |
| 3383 * v8::Unlocker unlocker(isolate); | |
| 3384 * ... | 3389 * ... |
| 3385 * // Code not using V8 goes here while V8 can run in another thread. | 3390 * // Code not using V8 goes here while V8 can run in another thread. |
| 3386 * ... | 3391 * ... |
| 3387 * } // Destructor called here. | 3392 * } // Destructor called here. |
| 3388 * isolate->Enter(); | |
| 3389 * \endcode | 3393 * \endcode |
| 3390 * | 3394 * |
| 3391 * The Unlocker object is intended for use in a long-running callback | 3395 * The Unlocker object is intended for use in a long-running callback |
| 3392 * from V8, where you want to release the V8 lock for other threads to | 3396 * from V8, where you want to release the V8 lock for other threads to |
| 3393 * use. | 3397 * use. |
| 3394 * | 3398 * |
| 3395 * The v8::Locker is a recursive lock. That is, you can lock more than | 3399 * The v8::Locker is a recursive lock. That is, you can lock more than |
| 3396 * once in a given thread. This can be useful if you have code that can | 3400 * once in a given thread. This can be useful if you have code that can |
| 3397 * be called either from code that holds the lock or from code that does | 3401 * be called either from code that holds the lock or from code that does |
| 3398 * not. The Unlocker is not recursive so you can not have several | 3402 * not. The Unlocker is not recursive so you can not have several |
| 3399 * Unlockers on the stack at once, and you can not use an Unlocker in a | 3403 * Unlockers on the stack at once, and you can not use an Unlocker in a |
| 3400 * thread that is not inside a Locker's scope. | 3404 * thread that is not inside a Locker's scope. |
| 3401 * | 3405 * |
| 3402 * An unlocker will unlock several lockers if it has to and reinstate | 3406 * An unlocker will unlock several lockers if it has to and reinstate |
| 3403 * the correct depth of locking on its destruction. eg.: | 3407 * the correct depth of locking on its destruction. eg.: |
| 3404 * | 3408 * |
| 3405 * \code | 3409 * \code |
| 3406 * // V8 not locked. | 3410 * // V8 not locked. |
| 3407 * { | 3411 * { |
| 3408 * v8::Locker locker(isolate); | 3412 * v8::Locker locker; |
| 3409 * Isolate::Scope isolate_scope(isolate); | |
| 3410 * // V8 locked. | 3413 * // V8 locked. |
| 3411 * { | 3414 * { |
| 3412 * v8::Locker another_locker(isolate); | 3415 * v8::Locker another_locker; |
| 3413 * // V8 still locked (2 levels). | 3416 * // V8 still locked (2 levels). |
| 3414 * { | 3417 * { |
| 3415 * isolate->Exit(); | 3418 * v8::Unlocker unlocker; |
| 3416 * v8::Unlocker unlocker(isolate); | |
| 3417 * // V8 not locked. | 3419 * // V8 not locked. |
| 3418 * } | 3420 * } |
| 3419 * isolate->Enter(); | |
| 3420 * // V8 locked again (2 levels). | 3421 * // V8 locked again (2 levels). |
| 3421 * } | 3422 * } |
| 3422 * // V8 still locked (1 level). | 3423 * // V8 still locked (1 level). |
| 3423 * } | 3424 * } |
| 3424 * // V8 Now no longer locked. | 3425 * // V8 Now no longer locked. |
| 3425 * \endcode | 3426 * \endcode |
| 3426 * | |
| 3427 * | |
| 3428 */ | 3427 */ |
| 3429 class V8EXPORT Unlocker { | 3428 class V8EXPORT Unlocker { |
| 3430 public: | 3429 public: |
| 3431 /** | 3430 Unlocker(); |
| 3432 * Initialize Unlocker for a given Isolate. NULL means default isolate. | |
| 3433 */ | |
| 3434 explicit Unlocker(Isolate* isolate = NULL); | |
| 3435 ~Unlocker(); | 3431 ~Unlocker(); |
| 3436 private: | |
| 3437 internal::Isolate* isolate_; | |
| 3438 }; | 3432 }; |
| 3439 | 3433 |
| 3440 | 3434 |
| 3441 class V8EXPORT Locker { | 3435 class V8EXPORT Locker { |
| 3442 public: | 3436 public: |
| 3443 /** | 3437 Locker(); |
| 3444 * Initialize Locker for a given Isolate. NULL means default isolate. | |
| 3445 */ | |
| 3446 explicit Locker(Isolate* isolate = NULL); | |
| 3447 ~Locker(); | 3438 ~Locker(); |
| 3448 | 3439 |
| 3449 /** | 3440 /** |
| 3450 * Start preemption. | 3441 * Start preemption. |
| 3451 * | 3442 * |
| 3452 * When preemption is started, a timer is fired every n milli seconds | 3443 * When preemption is started, a timer is fired every n milli seconds |
| 3453 * that will switch between multiple threads that are in contention | 3444 * that will switch between multiple threads that are in contention |
| 3454 * for the V8 lock. | 3445 * for the V8 lock. |
| 3455 */ | 3446 */ |
| 3456 static void StartPreemption(int every_n_ms); | 3447 static void StartPreemption(int every_n_ms); |
| 3457 | 3448 |
| 3458 /** | 3449 /** |
| 3459 * Stop preemption. | 3450 * Stop preemption. |
| 3460 */ | 3451 */ |
| 3461 static void StopPreemption(); | 3452 static void StopPreemption(); |
| 3462 | 3453 |
| 3463 /** | 3454 /** |
| 3464 * Returns whether or not the locker for a given isolate, or default isolate i
f NULL is given, | 3455 * Returns whether or not the locker is locked by the current thread. |
| 3465 * is locked by the current thread. | |
| 3466 */ | 3456 */ |
| 3467 static bool IsLocked(Isolate* isolate = NULL); | 3457 static bool IsLocked(); |
| 3468 | 3458 |
| 3469 /** | 3459 /** |
| 3470 * Returns whether v8::Locker is being used by this V8 instance. | 3460 * Returns whether v8::Locker is being used by this V8 instance. |
| 3471 */ | 3461 */ |
| 3472 static bool IsActive() { return active_; } | 3462 static bool IsActive() { return active_; } |
| 3473 | 3463 |
| 3474 private: | 3464 private: |
| 3475 bool has_lock_; | 3465 bool has_lock_; |
| 3476 bool top_level_; | 3466 bool top_level_; |
| 3477 internal::Isolate* isolate_; | |
| 3478 | 3467 |
| 3479 static bool active_; | 3468 static bool active_; |
| 3480 | 3469 |
| 3481 // Disallow copying and assigning. | 3470 // Disallow copying and assigning. |
| 3482 Locker(const Locker&); | 3471 Locker(const Locker&); |
| 3483 void operator=(const Locker&); | 3472 void operator=(const Locker&); |
| 3484 }; | 3473 }; |
| 3485 | 3474 |
| 3486 | 3475 |
| 3487 /** | 3476 /** |
| (...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4036 | 4025 |
| 4037 | 4026 |
| 4038 } // namespace v8 | 4027 } // namespace v8 |
| 4039 | 4028 |
| 4040 | 4029 |
| 4041 #undef V8EXPORT | 4030 #undef V8EXPORT |
| 4042 #undef TYPE_CHECK | 4031 #undef TYPE_CHECK |
| 4043 | 4032 |
| 4044 | 4033 |
| 4045 #endif // V8_H_ | 4034 #endif // V8_H_ |
| OLD | NEW |