changeset 8:1e2d1d978e4a

This is the whole folder of 2nd test
author Sara
date Sat, 15 Feb 2014 11:27:27 -0800
parents 524090d5a407
children 2650da94fa6f
files V8_tests/Passing_Context_and_compiledScript_to_threads/OneContextAfterAnotherTest.cpp V8_tests/Passing_Context_and_compiledScript_to_threads/ReadMe V8_tests/Passing_Context_and_compiledScript_to_threads/fourth_strategy.cpp V8_tests/Passing_Context_and_compiledScript_to_threads/second_strategy.cpp V8_tests/Passing_Context_and_compiledScript_to_threads/third_strategy.cpp
diffstat 5 files changed, 398 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/V8_tests/Passing_Context_and_compiledScript_to_threads/OneContextAfterAnotherTest.cpp	Sat Feb 15 11:27:27 2014 -0800
     1.3 @@ -0,0 +1,48 @@
     1.4 +#include <v8.h>
     1.5 +#include <pthread.h>
     1.6 +using namespace v8;
     1.7 +Isolate* isolate;
     1.8 +int main(int argc, char* argv[]) {
     1.9 +   isolate =  Isolate::New();
    1.10 +   {
    1.11 +      if(isolate == NULL){
    1.12 +        printf("Error");
    1.13 +        return 0;
    1.14 +    }
    1.15 +  Locker locker(isolate);
    1.16 +  Isolate::Scope isolate_scope(isolate);
    1.17 +  // Create a stack-allocated handle scope.
    1.18 +  HandleScope handle_scope(isolate);
    1.19 +  // Create a new context.
    1.20 +  Handle<Context> context1 = Context::New(isolate);
    1.21 +  // Enter the context for compiling and running the hello world script.
    1.22 +  Context::Scope context_scope1(context1);
    1.23 +  {
    1.24 +  // Create a string containing the JavaScript source code
    1.25 +  Handle<String> source1 = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
    1.26 +  // Compile the source code.
    1.27 +  Handle<Script> script1 = Script::Compile(source1);
    1.28 +  // Run the script to get the result.
    1.29 +  Handle<Value> result1 = script1->Run();
    1.30 +  // Convert the result to an UTF8 string and print it.
    1.31 +  String::Utf8Value utf81(result1);
    1.32 +  printf("%s\n", *utf81);
    1.33 +}
    1.34 +//create the 2nd context
    1.35 + Handle<Context> context2 = Context::New(isolate);
    1.36 +  // Enter the context for compiling and running the hello world script.
    1.37 +  Context::Scope context_scope2(context2);
    1.38 +  {
    1.39 +  // Create a string containing the JavaScript source code.
    1.40 +  Handle<String> source2 = String::NewFromUtf8(isolate, "'Hi' + ', Dr Sean'");
    1.41 +  // Compile the source code.
    1.42 +  Handle<Script> script2 = Script::Compile(source2);
    1.43 +  // Run the script to get the result.
    1.44 +  Handle<Value> result2 = script2->Run();
    1.45 +  // Convert the result to an UTF8 string and print it.
    1.46 +  String::Utf8Value utf82(result2);
    1.47 +  printf("%s\n", *utf82);
    1.48 +}
    1.49 +  return 0;
    1.50 +  }
    1.51 + }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/V8_tests/Passing_Context_and_compiledScript_to_threads/ReadMe	Sat Feb 15 11:27:27 2014 -0800
     2.3 @@ -0,0 +1,7 @@
     2.4 +This folder represents the 2nd test; which is creating two context in the main thread, and compiling the scripts in the main thread one after the other, then create the two thread and pass the compiled script to one and the other script to the other thread, and also pass the 2 context one to one thread and the other to the 2nd thread. 
     2.5 +
     2.6 +so, I tried different startegy by just replacing and playing with the code and the order of the code's line. 
     2.7 +here you will find four strategy, I explained every line in the code, 
     2.8 +
     2.9 +also there is a file called "OneContextAfterAnotherTest.cpp" this file only for testing and making sure that the two context are working successfully without using multi-threading. 
    2.10 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/V8_tests/Passing_Context_and_compiledScript_to_threads/fourth_strategy.cpp	Sat Feb 15 11:27:27 2014 -0800
     3.3 @@ -0,0 +1,108 @@
     3.4 +#include <v8.h>
     3.5 +#include <pthread.h>
     3.6 +using namespace v8;
     3.7 +Isolate* isolate;
     3.8 +//make the two compiled scripts global to be accessible by the two threads and the main thread
     3.9 +Handle<Script> script2;
    3.10 +Handle<Script> script1;
    3.11 +//the structure that holds both the context and the compiled script
    3.12 +struct threadParam{
    3.13 +   Handle<Context> context;
    3.14 +   Handle<Script> compiledScript;
    3.15 +};
    3.16 +
    3.17 +void* threadFunction(void* param){
    3.18 +//lock the global isolate"v8-engine" to be used only by the current thread,  
    3.19 +// but any code that not contain any API of v8-engine can be run in parallel 
    3.20 +//this as far as I read
    3.21 +    Locker locker1(isolate);
    3.22 +    //isolate->Enter();
    3.23 +    Isolate::Scope isolate_scope1(isolate);
    3.24 +    HandleScope handle_scope1(isolate);
    3.25 +    struct threadParam* p = (struct threadParam*) param;
    3.26 +//Enter the passed context of the current thread
    3.27 +    Context::Scope context_scope1(p->context);
    3.28 +//run the passed compiled script of the current thread
    3.29 +    Handle<Value> result = p->compiledScript->Run();
    3.30 +    String::Utf8Value utf8(result);
    3.31 +    printf("%s\n", *utf8);
    3.32 +    //isolate->Exit();
    3.33 +//unlock the global isolate "v8-engine" to be used by another thread
    3.34 +    Unlocker unlocker1(isolate);
    3.35 +return NULL;
    3.36 +
    3.37 +}
    3.38 +
    3.39 +int main(int argc, char* argv[]) {
    3.40 +
    3.41 +  // Get the default Isolate created at startup.
    3.42 +   isolate =  Isolate::New();
    3.43 +   {
    3.44 +        if(isolate == NULL){
    3.45 +           printf("Error");
    3.46 +           return 0;
    3.47 +         }
    3.48 +        Locker locker(isolate);
    3.49 +        Isolate::Scope isolate_scope(isolate);
    3.50 +        // Create a stack-allocated handle scope.
    3.51 +        HandleScope handle_scope(isolate);
    3.52 +        // Create a the first context.
    3.53 +        Handle<Context> context1 = Context::New(isolate);
    3.54 +        // Enter the context for compiling and running the hello world script.
    3.55 +        Context::Scope context_scope1(context1);
    3.56 +        // Create a string containing the JavaScript source code.
    3.57 +       Handle<String>source1 = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
    3.58 +            // Compile the first script
    3.59 +            script1 = Script::Compile(source1);
    3.60 +            // Run the script to get the result.
    3.61 +            Handle<Value> result1 = script1->Run();
    3.62 +           // Convert the result to an UTF8 string and print it.
    3.63 +            String::Utf8Value utf81(result1);
    3.64 +            printf("%s\n", *utf81);
    3.65 +   
    3.66 +
    3.67 +        //create the 2nd context
    3.68 +          Handle<Context> context2 = Context::New(isolate);
    3.69 +        // Enter the context for compiling and running the hello world script.
    3.70 +
    3.71 +        //Note--- until now the first context is still the current context----
    3.72 +
    3.73 +        //Enter the 2nd context
    3.74 +         Context::Scope context_scope2(context2);
    3.75 +//Note---here the 2nd context is the current context----using the following test
    3.76 +           Local<Context> temp = isolate->GetCurrentContext();
    3.77 +           if(temp == context1)
    3.78 +              printf("Yes the first context");
    3.79 +           else if(temp == context2)
    3.80 +              printf("No this is context 2");
    3.81 +              //-------End of the test---//
    3.82 +
    3.83 +             // the 2nd string that holds the src script
    3.84 +             Handle<String> source2 = String::NewFromUtf8(isolate, "'Hi' + ',  Dr Sean'");
    3.85 +             // Compile the 2nd source code.
    3.86 +             script2 = Script::Compile(source2);
    3.87 +             // Run the 2nd script to get the result.
    3.88 +             Handle<Value> result2 = script2->Run();
    3.89 +             // Convert the result to an UTF8 string and print it.
    3.90 +             String::Utf8Value utf82(result2);
    3.91 +             printf("%s\n", *utf82);
    3.92 +             
    3.93 +            //Here we will work with threads
    3.94 +
    3.95 +            pthread_t thread1_id, thread2_id;
    3.96 +            struct threadParam testStruct1, testStruct2;
    3.97 +            testStruct1.compiledScript = script1;
    3.98 +            testStruct1.context = context1;
    3.99 +            testStruct2.compiledScript = script2;
   3.100 +            testStruct2.context = context2;
   3.101 +            pthread_create(&thread1_id, NULL, &threadFunction, &testStruct1 );
   3.102 +            pthread_create(&thread2_id, NULL, &threadFunction, &testStruct2 );
   3.103 +            pthread_join(thread1_id, NULL);
   3.104 +            pthread_join(thread2_id, NULL);
   3.105 +            int i =100;
   3.106 +            while(i!=0)
   3.107 +              i = i-1;             
   3.108 +
   3.109 +             return 0;
   3.110 +           }
   3.111 + }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/V8_tests/Passing_Context_and_compiledScript_to_threads/second_strategy.cpp	Sat Feb 15 11:27:27 2014 -0800
     4.3 @@ -0,0 +1,117 @@
     4.4 +#include <v8.h>
     4.5 +#include <pthread.h>
     4.6 +using namespace v8;
     4.7 +Isolate* isolate;
     4.8 +struct threadParam{
     4.9 +   Handle<Context> context;
    4.10 +   Handle<Script> compiledScript;
    4.11 +
    4.12 +};
    4.13 +/*declare the two structure and make them global to be accessible by the two context */
    4.14 +struct threadParam testStruct1, testStruct2;
    4.15 +
    4.16 +void* threadFunction(void* param){
    4.17 +    struct threadParam* p = (struct threadParam*) param;
    4.18 +    p->context->Enter();
    4.19 +/*Locks the v8-engine to be used only by the current thread but any code that doesn't contain v8-API can still be run */
    4.20 +    Locker locker1(isolate);
    4.21 +    //isolate->Enter();
    4.22 +    Isolate::Scope isolate_scope1(isolate);
    4.23 +    HandleScope handle_scope1(isolate);
    4.24 +//Enter the passed context
    4.25 +    Context::Scope context_scope1(p->context);
    4.26 +//runs the compiled script
    4.27 +    Handle<Value> result = p->compiledScript->Run();
    4.28 +    String::Utf8Value utf8(result);
    4.29 +    printf("%s\n", *utf8);
    4.30 +    //isolate->Exit();
    4.31 +/*unlocks the global isolate so that anthor thread can use it now */
    4.32 +    Unlocker unlocker1(isolate);
    4.33 +    return NULL;
    4.34 +
    4.35 +}
    4.36 +
    4.37 +int main(int argc, char* argv[]) {
    4.38 +
    4.39 +   isolate =  Isolate::New();
    4.40 +   {
    4.41 +        if(isolate == NULL){
    4.42 +           printf("Error");
    4.43 +           return 0;
    4.44 +         }
    4.45 +        Locker locker(isolate);
    4.46 +        Isolate::Scope isolate_scope(isolate);
    4.47 +        // Create a stack-allocated handle scope.
    4.48 +        HandleScope handle_scope(isolate);
    4.49 +        // Create the first context.
    4.50 +        Handle<Context> context1 = Context::New(isolate);
    4.51 +       /*Enter the first context for compiling and running the hello world script.*/
    4.52 +        
    4.53 +        Context::Scope context_scope1(context1);
    4.54 +        // Create the first string containing the JavaScript source code.
    4.55 +       Handle<String>source1 = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
    4.56 +            // Compile the first source code.
    4.57 +       Handle<Script> script1 = Script::Compile(source1);
    4.58 +            // Run the script to get the result.
    4.59 +       Handle<Value> result1 = script1->Run();
    4.60 +           // Convert the result to an UTF8 string and print it.
    4.61 +       
    4.62 +       String::Utf8Value utf81(result1);
    4.63 +       printf("%s\n", *utf81);
    4.64 +/*Declare the two threads inside the first context*/
    4.65 +       pthread_t thread1_id, thread2_id;
    4.66 +       
    4.67 +
    4.68 +        //create the 2nd context
    4.69 +          Handle<Context> context2 = Context::New(isolate);
    4.70 +        /* Enter the 2nd context for compiling and running the hello world script.*/
    4.71 +          context2->Enter();//------------>try without it
    4.72 +        //Note--- Here the first context is still the current context----
    4.73 +         Context::Scope context_scope2(context2);
    4.74 +          /*Note---here the 2nd context is the current context----using the following test*/
    4.75 +           Local<Context> temp = isolate->GetCurrentContext();
    4.76 +           if(temp == context1)
    4.77 +              printf("Yes the first context");
    4.78 +           else if(temp == context2)
    4.79 +              printf("No this is context 2");
    4.80 +              //-------End of the test---//
    4.81 +
    4.82 +             // Create the 2nd string containing the JavaScript source code.
    4.83 +             Handle<String> source2 = String::NewFromUtf8(isolate, "'Hi' + ',  Dr Sean'");
    4.84 +             // Compile the 2nd source code.
    4.85 +             Handle<Script> script2 = Script::Compile(source2);
    4.86 +             // Run the script to get the result.
    4.87 +             Handle<Value> result2 = script2->Run();
    4.88 +             // Convert the result to an UTF8 string and print it.
    4.89 +             String::Utf8Value utf82(result2);
    4.90 +             printf("%s\n", *utf82);
    4.91 +             
    4.92 +/*Here we will work with threads so exit from the 2nd context, it doesn't see the two threads */
    4.93 +            context2->Exit();
    4.94 +            //context1->Exit();
    4.95 +            
    4.96 +            /*Enter the first context to pass the 1st thread's data to first structure*/
    4.97 +            context1->Enter();
    4.98 +            /*Pass the data to the first thread's structure*/
    4.99 +            testStruct1.compiledScript = script1;
   4.100 +            testStruct1.context = context1;
   4.101 +            /* Exit from the first context to pass the 2nd thread's data to the 2nd structure*/
   4.102 +            context1->Exit();
   4.103 +           /*Enter the 2nd context*/
   4.104 +            context2->Enter();
   4.105 +            testStruct2.compiledScript = script2;
   4.106 +            testStruct2.context = context2;
   4.107 +           /*Exit from the 2nd context and enter the first context to see the two threads as they are declared inside the first context and aren't accessible by the 2nd context*/
   4.108 +            context2->Exit();
   4.109 +            context1->Enter();
   4.110 +            pthread_create(&thread1_id, NULL, &threadFunction, &testStruct1 );
   4.111 +            pthread_create(&thread2_id, NULL, &threadFunction, &testStruct2 );
   4.112 +            pthread_join(thread1_id, NULL);
   4.113 +            pthread_join(thread2_id, NULL);
   4.114 +            int i =100;
   4.115 +            while(i!=0)
   4.116 +              i = i-1;             
   4.117 +
   4.118 +             return 0;
   4.119 +           }
   4.120 + }
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/V8_tests/Passing_Context_and_compiledScript_to_threads/third_strategy.cpp	Sat Feb 15 11:27:27 2014 -0800
     5.3 @@ -0,0 +1,118 @@
     5.4 +#include <v8.h>
     5.5 +#include <pthread.h>
     5.6 +using namespace v8;
     5.7 +Isolate* isolate;
     5.8 +struct threadParam{
     5.9 +   Handle<Context> context;
    5.10 +   Handle<Script> compiledScript;
    5.11 +
    5.12 +};
    5.13 +
    5.14 +void* threadFunction(void* param){
    5.15 +    struct threadParam* p = (struct threadParam*) param;
    5.16 +    p->context->Enter();
    5.17 +/*locks the v8-engine to be used by the current thread only, but any code 
    5.18 + that doesn't include any v8-API still running in parallel*/
    5.19 +    Locker locker1(isolate);
    5.20 +    //isolate->Enter();
    5.21 +    Isolate::Scope isolate_scope1(isolate);
    5.22 +    HandleScope handle_scope1(isolate);
    5.23 +    Context::Scope context_scope1(p->context);
    5.24 +    Handle<Value> result = p->compiledScript->Run();
    5.25 +    String::Utf8Value utf8(result);
    5.26 +    printf("%s\n", *utf8);
    5.27 +    //isolate->Exit();
    5.28 +    Unlocker unlocker1(isolate);
    5.29 +    return NULL;
    5.30 +
    5.31 +}
    5.32 +
    5.33 +int main(int argc, char* argv[]) {
    5.34 +
    5.35 +  // Get the default Isolate created at startup.
    5.36 +   isolate =  Isolate::New();
    5.37 +   {
    5.38 +        if(isolate == NULL){
    5.39 +           printf("Error");
    5.40 +           return 0;
    5.41 +         }
    5.42 +        Locker locker(isolate);
    5.43 +        Isolate::Scope isolate_scope(isolate);
    5.44 +        // Create a stack-allocated handle scope.
    5.45 +        HandleScope handle_scope(isolate);
    5.46 +        // Create a new context.
    5.47 +        Handle<Context> context1 = Context::New(isolate);
    5.48 +        // Enter the context for compiling and running the hello world script.
    5.49 +        context1->Enter();
    5.50 +        Context::Scope context_scope1(context1);
    5.51 +        // Create a string containing the first JavaScript source code.
    5.52 +       Handle<String>source1 = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
    5.53 +            // Compile the first source code.
    5.54 +       Handle<Script> script1 = Script::Compile(source1);
    5.55 +            // Run the first compiled script to get the result.
    5.56 +       Handle<Value> result1 = script1->Run();
    5.57 +           // Convert the result to an UTF8 string and print it.
    5.58 +       
    5.59 +       String::Utf8Value utf81(result1);
    5.60 +       printf("%s\n", *utf81);
    5.61 +//exit from the first context
    5.62 +       context1->Exit();
    5.63 +
    5.64 +        //create the 2nd context
    5.65 +          Handle<Context> context2 = Context::New(isolate);
    5.66 +     // Enter the 2nd context for compiling and running the hello world script.
    5.67 +          context2->Enter();//--------------------->try without this
    5.68 +        //Note--- Here the first context is still the current context----
    5.69 +         Context::Scope context_scope2(context2);
    5.70 +//Note---here the 2nd context is the current context----using the following test
    5.71 +           Local<Context> temp = isolate->GetCurrentContext();
    5.72 +           if(temp == context1)
    5.73 +              printf("Yes the first context");
    5.74 +           else if(temp == context2)
    5.75 +              printf("No this is context 2");
    5.76 +              //-------End of the test---//
    5.77 +
    5.78 +             // Create the 2nd string containing the JavaScript source code.
    5.79 +             Handle<String> source2 = String::NewFromUtf8(isolate, "'Hi' + ',  Dr Sean'");
    5.80 +             // Compile the 2nd source code.
    5.81 +             Handle<Script> script2 = Script::Compile(source2);
    5.82 +             // Run the 2nd script to get the result.
    5.83 +             Handle<Value> result2 = script2->Run();
    5.84 +             // Convert the result to an UTF8 string and print it.
    5.85 +             String::Utf8Value utf82(result2);
    5.86 +             printf("%s\n", *utf82);
    5.87 +             
    5.88 +            //Here we will work with threads
    5.89 +            // exit from the 2nd context
    5.90 +            context2->Exit();
    5.91 +            //context1->Exit();
    5.92 +/*  declares the two threads outside the 2 context-----but try to create them inside main context----------------------->>>*/
    5.93 +            pthread_t thread1_id, thread2_id;
    5.94 +            struct threadParam testStruct1, testStruct2;
    5.95 +          
    5.96 +      /*Enter the first context to pass the first structure that holds 
    5.97 +      thread's paramater*/
    5.98 +            context1->Enter();
    5.99 +            testStruct1.compiledScript = script1;
   5.100 +            testStruct1.context = context1;
   5.101 +            context1->Exit();
   5.102 +  
   5.103 +      /*Enter the second context to pass the first structure that holds 
   5.104 +      thread's paramater*/
   5.105 +            context2->Enter();
   5.106 +            testStruct2.compiledScript = script2;
   5.107 +            testStruct2.context = context2;
   5.108 +            context2->Exit();
   5.109 +           // context1->Exit();
   5.110 +//create the two threads
   5.111 +            pthread_create(&thread1_id, NULL, &threadFunction, &testStruct1 );
   5.112 +            pthread_create(&thread2_id, NULL, &threadFunction, &testStruct2 );
   5.113 +            pthread_join(thread1_id, NULL);
   5.114 +            pthread_join(thread2_id, NULL);
   5.115 +            int i =100;
   5.116 +            while(i!=0)
   5.117 +              i = i-1;             
   5.118 +
   5.119 +             return 0;
   5.120 +           }
   5.121 + }