Beispiel #1
0
    void ThreadPool::executeOneTaskIfPossible_CRITICAL_SECTION()
    {
      if ( m_tasks.empty() && m_idleTasks.empty() && (!m_isMainThread.get() || m_mainThreadTasks.empty()) )
        m_stateCond.wait( m_stateMutex );
      else
      {
        std::vector<Task *> *taskQueue;
        if ( m_isMainThread.get() && !m_mainThreadTasks.empty() )
          taskQueue = &m_mainThreadTasks;
        else if ( !m_tasks.empty() )
          taskQueue = &m_tasks;
        else taskQueue = &m_idleTasks;
        
        Task *task = taskQueue->back();
        
        size_t index;
        bool keep;
        task->preExecute_CRITICAL_SECTION( index, keep );
        if ( !keep )
          taskQueue->pop_back();

        m_stateMutex.release();
        try
        {
          task->execute( index );
        }
        catch ( Exception e )
        {
          Util::SimpleString prefixedException = "Exception: " + e.getDesc();
          RC::Handle<LogCollector> logCollector = task->getLogCollector();
          if ( logCollector )
            logCollector->add( prefixedException.data(), prefixedException.length() );
        }
        catch ( ... )
        {
          static Util::SimpleString const genericException = "Exception (unknown)";
          RC::Handle<LogCollector> logCollector = task->getLogCollector();
          if ( logCollector )
            logCollector->add( genericException.data(), genericException.length() );
        }
        m_stateMutex.acquire();
        
        task->postExecute_CRITICAL_SECTION();
        if ( task->completed_CRITICAL_SECTION() )
        {
          void (*finishedCallback)( void * ) = task->getFinishedCallback();
          if ( finishedCallback )
            finishedCallback( task->getFinishedUserdata() );

          task->dispose();
          
          // [pzion 20101108] Must wake waiter because they might be
          // waiting on the task completion
          m_stateCond.broadcast();
        }
      }
    }
Beispiel #2
0
 v8::Handle<v8::Value> ClientWrap::JSONExec( v8::Arguments const &args )
 {
   ClientWrap *wrap = node::ObjectWrap::Unwrap<ClientWrap>( args.This() );
   if ( !wrap->m_client )
     return v8::ThrowException( v8::String::New( "client has already been closed" ) );
   
   if ( args.Length() != 1 || !args[0]->IsString() )
     return v8::ThrowException( v8::String::New( "jsonExec: takes one string parameter (jsonEncodedCommands)" ) );
   
   v8::HandleScope v8HandleScope;
   v8::Handle<v8::String> v8JSONEncodedCommands = v8::Handle<v8::String>::Cast( args[0] );
   v8::String::Utf8Value v8JSONEncodedCommandsUtf8Value( v8JSONEncodedCommands );
   Util::SimpleString jsonEncodedResults;
   {
     v8::Handle<v8::Object> v8This = args.This();
     JSON::Encoder resultJSON( &jsonEncodedResults );
     wrap->m_client->jsonExec(
       const_cast<char const *>(*v8JSONEncodedCommandsUtf8Value),
       size_t(v8JSONEncodedCommandsUtf8Value.length()),
       resultJSON
       );
   }
   v8::Handle<v8::String> v8JSONEncodedResults = v8::String::New( jsonEncodedResults.data(), jsonEncodedResults.length() );
   return v8HandleScope.Close( v8JSONEncodedResults );
 }
Beispiel #3
0
 void Client::notify( Util::SimpleString const &jsonEncodedNotifications ) const
 {
   if ( m_clientWrap )
     m_clientWrap->notify( jsonEncodedNotifications.data(), jsonEncodedNotifications.length() );
 }