Exemple #1
0
int WorkQueue::Initialize() {
    // init thread index for main thread
    tls = new ThreadLocal();
    ThreadLocal::SetThreadLocal(0);
    // start threads
    int cores = 4;
    for (int i = 0; i < cores; i++) {
        WorkerThread * Thread = new WorkerThread(this, i + 1);
        Workers.PushBack(Thread);
        Thread->Start();
    }
    return 0;
}
Exemple #2
0
int BasicTCPServer::WorkerThread::ThreadBody()
{
	InternetAddress addr;
	TCPSocket sock;
	ActionStatus st = m_pServer->m_Listener.Accept(&sock, &addr);
	if (st.Successful() && sock.Valid())
	{
		m_pServer->m_ActiveThreadCount++;
		WorkerThread *pThread = new WorkerThread(m_pServer);
		if (!pThread || !pThread->Start())
			delete pThread;
		m_pServer->ConnectionHandler(sock, addr);
	}
	if (!--m_pServer->m_ActiveThreadCount)
		m_pServer->m_LastThreadDone.Set();
	return 0;
}
int main()
{
	WorkerThread* wt = new WorkerThread();

	wt->Start( iThread::Priority_Normal );

	wt->AddTask( new TestTask() );
	wt->AddTask( new TestTask() );
	wt->AddTask( new TestTask() );

	// Wait for task completion
	while ( wt->GetQueueSize() > 0 ) {}

	fflush( stdout );

	return 0;
}
Exemple #4
0
ActionStatus BasicTCPServer::Start(unsigned PortNumber)
{
	if (m_ActiveThreadCount)
		return MAKE_STATUS(InvalidState);
	ActionStatus st = m_Listener.Listen(PortNumber);
	if (!st.Successful())
		return st;
	m_LastThreadDone.Reset();
	m_ActiveThreadCount++;

	WorkerThread *pThread = new WorkerThread(this);
	if (!pThread)
		return MAKE_STATUS(NoMemory);

	if (!pThread->Start())
	{
		delete pThread;
		m_ActiveThreadCount--;
		return MAKE_STATUS(UnknownError);
	}
	return MAKE_STATUS(Success);
}
Exemple #5
0
USING_NS_IOCPSERVD

NS_IOCPSERVD_BEGIN

int IocpServd_main(int argc, char *argv[])
{
    using namespace jimi;

    jmLog.log_begin();

    bool isService = true;
    jimi::CommandLine cmdLine;
    int cnt;
    if ((cnt = cmdLine.parse(argc, argv)) >= 0) {
        std::string strCmdLine = cmdLine.getCmdLine();
        jmLog.info(strCmdLine.c_str());
    }

    IocpServdService *service = new IocpServdService();
    if (service) {
        IocpServdService::SetInstance(service, true);

        service->SetServiceName(g_ServiceName);
        service->SetServiceDisplayName(g_ServiceDisplayName);
        service->SetServiceDescription(g_ServiceDescription);

        jmLog.info("cmdLine.parse(argc, argv): argc_cnt = %d", cnt);

        if (cmdLine.hasArgument("-run") || cmdLine.hasArgument("/run")
            || cmdLine.hasArgument("-r") || cmdLine.hasArgument("/r")) {
            service->RunService();
            //service->RunServiceEx(g_ServiceTable);
        }
        else if (cmdLine.hasArgument("-install") || cmdLine.hasArgument("/install")
            || cmdLine.hasArgument("-i") || cmdLine.hasArgument("/i")) {
            service->InstallService();
        }
        else if (cmdLine.hasArgument("-uninstall") || cmdLine.hasArgument("/uninstall")
            || cmdLine.hasArgument("-u") || cmdLine.hasArgument("/u")
            || cmdLine.hasArgument("-uninst") || cmdLine.hasArgument("/uninst")) {
            service->UninstallService();
        }
        else {
            // is not a windows service
            isService = false;
        }

        if (service) {
            //service->Destroy();
            delete service;
        }
    }

    printf("\n");

#if 1
    SampleThread *thread = new SampleThread();
    thread->Start();
    thread->Join();
    thread->Abort(1000);
    if (thread) {
        delete thread;
    }

    WorkerThread *workerThread = new WorkerThread();
    workerThread->Start();
    workerThread->Join();
    if (workerThread) {
        delete workerThread;
    }

    printf("\n");
#endif

     do {
        jimi::Object *object = new jimi::Object();
        jimi::Object newObject = object->Clone();
        printf("newObject.Equals(object) = %d\n\n", newObject.Equals(object));
        object->Close();
        newObject.Close();
        if (object)
            delete object;
    } while (0);

    printf("\n");

    do {
        csharp::ManualResetEvent *event = new csharp::ManualResetEvent(false);
        //ManualResetEvent event2(false);
        csharp::EventWaitHandle *event3 = new csharp::EventWaitHandle();
        csharp::EventWaitHandle *event_base = (csharp::EventWaitHandle *)event;
        event->Set();
        event->Reset();
        if (event_base) {
            delete event_base;
            event_base = NULL;
        }
        if (event) {
            //delete event;
            event = NULL;
        }
        bool bSignal = event3->WaitOne();
        if (event3)
            delete event3;
    } while (0);

    printf("\n");

    ///*
    //mt::mutex read_mutex;
    //mt::scoped_lock<mt::mutex> lock(read_mutex);
    mt::mutex read_mutex;
    mt::scoped_lock<mt::mutex> lock(read_mutex);
    lock.acquire(read_mutex);
    lock.try_acquire(read_mutex, 4000);
    lock.release();
    //*/

    if (!isService)
        ::system("pause");

    jmLog.log_end();
    return 0;
}
int CSocketServer::Run()
{
	try
	{
		vector<WorkerThread *> workers;
		
		workers.reserve( m_numThreads );
		
		for ( size_t i = 0; i < m_numThreads; ++i )
		{
			/*
			 * Call to unqualified virtual function
			 */
			WorkerThread *pThread = CreateWorkerThread( m_iocp ); 
			
			workers.push_back( pThread );
			
			pThread->Start();
		}
		
		HANDLE handlesToWaitFor[2];
		
		handlesToWaitFor[0] = m_shutdownEvent.GetEvent();
		handlesToWaitFor[1] = m_acceptConnectionsEvent.GetEvent();
		
		while ( !m_shutdownEvent.Wait( 0 ) )
		{
			DWORD waitResult = ::WaitForMultipleObjects( 2, handlesToWaitFor, false, INFINITE );
			
			if ( waitResult == WAIT_OBJECT_0 )
			{
				/*
				 * Time to shutdown
				 */
				break;
			}
			else if ( waitResult == WAIT_OBJECT_0 + 1 )
			{
				/*
				 * accept connections
				 */
				
				while ( !m_shutdownEvent.Wait( 0 ) && m_acceptConnectionsEvent.Wait( 0 ) )
				{
					CIOBuffer *pAddress = Allocate();
					
					int addressSize = (int)pAddress->GetSize();
					
					SOCKET acceptedSocket = ::WSAAccept(
									m_listeningSocket, 
									reinterpret_cast<sockaddr *>(const_cast<BYTE *>( pAddress->GetBuffer() ) ), 
									&addressSize, 
									0, 
									0);
					
					pAddress->Use( addressSize );
					
					if ( acceptedSocket != INVALID_SOCKET )
					{
						Socket *pSocket = AllocateSocket( acceptedSocket );
						
						/*
						 * Call to unqualified virtual function
						 */
						OnConnectionEstablished( pSocket, pAddress );
					}
					else if ( m_acceptConnectionsEvent.Wait( 0 ) )
					{
						/*
						 * Call to unqualified virtual function
						 */
						OnError( _T("CSocketServer::Run() - WSAAccept:") + GetLastErrorMessage( ::WSAGetLastError() ) );
					}
					
					pAddress->Release();
				}
			}
			else
			{
				/*
				 * Call to unqualified virtual function
				 */
				OnError( _T("CSocketServer::Run() - WaitForMultipleObjects: ") + GetLastErrorMessage( ::GetLastError() ) );
			}
		}
		
		for ( i = 0; i < m_numThreads; ++i )
		{
			workers[i]->InitiateShutdown();
		}  
		
		for ( i = 0; i < m_numThreads; ++i )
		{
			workers[i]->WaitForShutdownToComplete();
			
			delete workers[i];
			
			workers[i] = 0;
		}  
	}
	catch( const CException &e )
	{
		/*
		 * Call to unqualified virtual function
		 */
		OnError( _T("CSocketServer::Run() - Exception: ") + e.GetWhere() + _T(" - ") + e.GetMessage() );
	}
	catch(...)
	{
		/*
		 * Call to unqualified virtual function
		 */
		OnError( _T("CSocketServer::Run() - Unexpected exception") );
	}
	
	/*
	 * Call to unqualified virtual function
	 */
	OnShutdownComplete();
	
	return 0;
}