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 3375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3386 | 3386 |
3387 /** | 3387 /** |
3388 * Multiple threads in V8 are allowed, but only one thread at a time | 3388 * Multiple threads in V8 are allowed, but only one thread at a time |
3389 * is allowed to use any given V8 isolate. See Isolate class | 3389 * is allowed to use any given V8 isolate. See Isolate class |
3390 * comments. The definition of 'using V8 isolate' includes | 3390 * comments. The definition of 'using V8 isolate' includes |
3391 * accessing handles or holding onto object pointers obtained | 3391 * accessing handles or holding onto object pointers obtained |
3392 * from V8 handles while in the particular V8 isolate. It is up | 3392 * from V8 handles while in the particular V8 isolate. It is up |
3393 * to the user of V8 to ensure (perhaps with locking) that this | 3393 * to the user of V8 to ensure (perhaps with locking) that this |
3394 * constraint is not violated. | 3394 * constraint is not violated. |
3395 * | 3395 * |
3396 * More then one thread and multiple V8 isolates can be used | 3396 * v8::Locker is a scoped lock object. While it's |
3397 * without any locking if each isolate is created and accessed | 3397 * active (i.e. between its construction and destruction) the current thread is |
3398 * by a single thread only. For example, one thread can use | 3398 * allowed to use the locked isolate. V8 guarantees that an isolate can be locke
d |
3399 * multiple isolates or multiple threads can each create and run | 3399 * by at most one thread at any time. In other words, the scope of a v8::Locker
is |
3400 * their own isolate. | 3400 * a critical section. |
3401 * | 3401 * |
3402 * If you wish to start using V8 isolate in more then one thread | 3402 * Sample usage: |
3403 * you can do this by constructing a v8::Locker object to guard | 3403 * \code |
3404 * access to the isolate. After the code using V8 has completed | |
3405 * for the current thread you can call the destructor. This can | |
3406 * be combined with C++ scope-based construction as follows | |
3407 * (assumes the default isolate that is used if not specified as | |
3408 * a parameter for the Locker): | |
3409 * | |
3410 * \code | |
3411 * ... | 3404 * ... |
3412 * { | 3405 * { |
3413 * v8::Locker locker; | 3406 * v8::Locker locker(isolate); |
| 3407 * v8::Isolate::Scope isolate_scope(isolate); |
3414 * ... | 3408 * ... |
3415 * // Code using V8 goes here. | 3409 * // Code using V8 and isolate goes here. |
3416 * ... | 3410 * ... |
3417 * } // Destructor called here | 3411 * } // Destructor called here |
3418 * \endcode | 3412 * \endcode |
3419 * | 3413 * |
3420 * If you wish to stop using V8 in a thread A you can do this by either | 3414 * If you wish to stop using V8 in a thread A you can do this by either |
3421 * by destroying the v8::Locker object as above or by constructing a | 3415 * by destroying the v8::Locker object as above or by constructing a |
3422 * v8::Unlocker object: | 3416 * v8::Unlocker object: |
3423 * | 3417 * |
3424 * \code | 3418 * \code |
3425 * { | 3419 * { |
3426 * v8::Unlocker unlocker; | 3420 * isolate->Exit(); |
| 3421 * v8::Unlocker unlocker(isolate); |
3427 * ... | 3422 * ... |
3428 * // Code not using V8 goes here while V8 can run in another thread. | 3423 * // Code not using V8 goes here while V8 can run in another thread. |
3429 * ... | 3424 * ... |
3430 * } // Destructor called here. | 3425 * } // Destructor called here. |
| 3426 * isolate->Enter(); |
3431 * \endcode | 3427 * \endcode |
3432 * | 3428 * |
3433 * The Unlocker object is intended for use in a long-running callback | 3429 * The Unlocker object is intended for use in a long-running callback |
3434 * from V8, where you want to release the V8 lock for other threads to | 3430 * from V8, where you want to release the V8 lock for other threads to |
3435 * use. | 3431 * use. |
3436 * | 3432 * |
3437 * The v8::Locker is a recursive lock. That is, you can lock more than | 3433 * The v8::Locker is a recursive lock. That is, you can lock more than |
3438 * once in a given thread. This can be useful if you have code that can | 3434 * once in a given thread. This can be useful if you have code that can |
3439 * be called either from code that holds the lock or from code that does | 3435 * be called either from code that holds the lock or from code that does |
3440 * not. The Unlocker is not recursive so you can not have several | 3436 * not. The Unlocker is not recursive so you can not have several |
3441 * Unlockers on the stack at once, and you can not use an Unlocker in a | 3437 * Unlockers on the stack at once, and you can not use an Unlocker in a |
3442 * thread that is not inside a Locker's scope. | 3438 * thread that is not inside a Locker's scope. |
3443 * | 3439 * |
3444 * An unlocker will unlock several lockers if it has to and reinstate | 3440 * An unlocker will unlock several lockers if it has to and reinstate |
3445 * the correct depth of locking on its destruction. eg.: | 3441 * the correct depth of locking on its destruction. eg.: |
3446 * | 3442 * |
3447 * \code | 3443 * \code |
3448 * // V8 not locked. | 3444 * // V8 not locked. |
3449 * { | 3445 * { |
3450 * v8::Locker locker; | 3446 * v8::Locker locker(isolate); |
| 3447 * Isolate::Scope isolate_scope(isolate); |
3451 * // V8 locked. | 3448 * // V8 locked. |
3452 * { | 3449 * { |
3453 * v8::Locker another_locker; | 3450 * v8::Locker another_locker(isolate); |
3454 * // V8 still locked (2 levels). | 3451 * // V8 still locked (2 levels). |
3455 * { | 3452 * { |
3456 * v8::Unlocker unlocker; | 3453 * isolate->Exit(); |
| 3454 * v8::Unlocker unlocker(isolate); |
3457 * // V8 not locked. | 3455 * // V8 not locked. |
3458 * } | 3456 * } |
| 3457 * isolate->Enter(); |
3459 * // V8 locked again (2 levels). | 3458 * // V8 locked again (2 levels). |
3460 * } | 3459 * } |
3461 * // V8 still locked (1 level). | 3460 * // V8 still locked (1 level). |
3462 * } | 3461 * } |
3463 * // V8 Now no longer locked. | 3462 * // V8 Now no longer locked. |
3464 * \endcode | 3463 * \endcode |
| 3464 * |
| 3465 * |
3465 */ | 3466 */ |
3466 class V8EXPORT Unlocker { | 3467 class V8EXPORT Unlocker { |
3467 public: | 3468 public: |
3468 Unlocker(); | 3469 /** |
| 3470 * Initialize Unlocker for a given Isolate. NULL means default isolate. |
| 3471 */ |
| 3472 explicit Unlocker(Isolate* isolate = NULL); |
3469 ~Unlocker(); | 3473 ~Unlocker(); |
| 3474 private: |
| 3475 internal::Isolate* isolate_; |
3470 }; | 3476 }; |
3471 | 3477 |
3472 | 3478 |
3473 class V8EXPORT Locker { | 3479 class V8EXPORT Locker { |
3474 public: | 3480 public: |
3475 Locker(); | 3481 /** |
| 3482 * Initialize Locker for a given Isolate. NULL means default isolate. |
| 3483 */ |
| 3484 explicit Locker(Isolate* isolate = NULL); |
3476 ~Locker(); | 3485 ~Locker(); |
3477 | 3486 |
3478 /** | 3487 /** |
3479 * Start preemption. | 3488 * Start preemption. |
3480 * | 3489 * |
3481 * When preemption is started, a timer is fired every n milli seconds | 3490 * When preemption is started, a timer is fired every n milli seconds |
3482 * that will switch between multiple threads that are in contention | 3491 * that will switch between multiple threads that are in contention |
3483 * for the V8 lock. | 3492 * for the V8 lock. |
3484 */ | 3493 */ |
3485 static void StartPreemption(int every_n_ms); | 3494 static void StartPreemption(int every_n_ms); |
3486 | 3495 |
3487 /** | 3496 /** |
3488 * Stop preemption. | 3497 * Stop preemption. |
3489 */ | 3498 */ |
3490 static void StopPreemption(); | 3499 static void StopPreemption(); |
3491 | 3500 |
3492 /** | 3501 /** |
3493 * Returns whether or not the locker is locked by the current thread. | 3502 * Returns whether or not the locker for a given isolate, or default isolate i
f NULL is given, |
| 3503 * is locked by the current thread. |
3494 */ | 3504 */ |
3495 static bool IsLocked(); | 3505 static bool IsLocked(Isolate* isolate = NULL); |
3496 | 3506 |
3497 /** | 3507 /** |
3498 * Returns whether v8::Locker is being used by this V8 instance. | 3508 * Returns whether v8::Locker is being used by this V8 instance. |
3499 */ | 3509 */ |
3500 static bool IsActive() { return active_; } | 3510 static bool IsActive() { return active_; } |
3501 | 3511 |
3502 private: | 3512 private: |
3503 bool has_lock_; | 3513 bool has_lock_; |
3504 bool top_level_; | 3514 bool top_level_; |
| 3515 internal::Isolate* isolate_; |
3505 | 3516 |
3506 static bool active_; | 3517 static bool active_; |
3507 | 3518 |
3508 // Disallow copying and assigning. | 3519 // Disallow copying and assigning. |
3509 Locker(const Locker&); | 3520 Locker(const Locker&); |
3510 void operator=(const Locker&); | 3521 void operator=(const Locker&); |
3511 }; | 3522 }; |
3512 | 3523 |
3513 | 3524 |
3514 /** | 3525 /** |
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4063 | 4074 |
4064 | 4075 |
4065 } // namespace v8 | 4076 } // namespace v8 |
4066 | 4077 |
4067 | 4078 |
4068 #undef V8EXPORT | 4079 #undef V8EXPORT |
4069 #undef TYPE_CHECK | 4080 #undef TYPE_CHECK |
4070 | 4081 |
4071 | 4082 |
4072 #endif // V8_H_ | 4083 #endif // V8_H_ |
OLD | NEW |