From: Vlad K. <hv...@us...> - 2008-03-13 13:48:31
|
Update of /cvsroot/firebird/firebird2/src/remote In directory sc8-pr-cvs17.sourceforge.net:/tmp/cvs-serv11498/remote Modified Files: inet.cpp interface.cpp server.cpp Log Message: Use EnsureUnlock where appropriate. Remove not used thread accounting in CNTL. Fix few mistakes\typos in my previous commit. Index: inet.cpp =================================================================== RCS file: /cvsroot/firebird/firebird2/src/remote/inet.cpp,v retrieving revision 1.209 retrieving revision 1.210 diff -b -U3 -r1.209 -r1.210 --- inet.cpp 13 Mar 2008 12:05:49 -0000 1.209 +++ inet.cpp 13 Mar 2008 13:47:55 -0000 1.210 @@ -3478,7 +3478,9 @@ while (more) { more = false; - for (rem_port* port = main_port; port; port = port->port_next) { + for (rem_port* port = main_port; port; port = port->port_next) + { + Firebird::RefMutexEnsureUnlock portGuard(*port->port_sync); if (port->port_sync->tryEnter()) { if (port->port_state == state_disconnected) { more = true; Index: interface.cpp =================================================================== RCS file: /cvsroot/firebird/firebird2/src/remote/interface.cpp,v retrieving revision 1.168 retrieving revision 1.169 diff -b -U3 -r1.168 -r1.169 --- interface.cpp 12 Mar 2008 16:39:44 -0000 1.168 +++ interface.cpp 13 Mar 2008 13:47:55 -0000 1.169 @@ -325,7 +325,7 @@ return user_status[1]; } - Firebird::RefMutexGuard portGuard(*port->port_sync); // hvlad ??? + Firebird::RefMutexGuard portGuard(*port->port_sync); rdb = port->port_context; rdb->rdb_status_vector = user_status; @@ -855,7 +855,7 @@ return user_status[1]; } - Firebird::RefMutexGuard portGuard(*port->port_sync); // hvlad ??? + Firebird::RefMutexGuard portGuard(*port->port_sync); rdb = port->port_context; rdb->rdb_status_vector = user_status; @@ -3871,7 +3871,7 @@ return user_status[1]; } - Firebird::RefMutexGuard portGuard(*port->port_sync); // hvlad ??? + Firebird::RefMutexGuard portGuard(*port->port_sync); rdb = port->port_context; rdb->rdb_status_vector = user_status; @@ -5383,7 +5383,6 @@ rem_port* port = (rem_port*)arg; PACKET packet; - Firebird::RefMutexGuard portGuard(*port->port_sync); for (;;) { /* zero packet */ @@ -5391,7 +5390,11 @@ /* read what should be an event message */ - rem_port* stuff = port->receive(&packet); + rem_port* stuff = NULL; + { + Firebird::RefMutexGuard portGuard(*port->port_sync); + stuff = port->receive(&packet); + } const P_OP operation = packet.p_operation; @@ -5409,7 +5412,11 @@ if (operation == op_event) { P_EVENT* pevent = &packet.p_event; - RVNT event = find_event(port, pevent->p_event_rid); + RVNT event = NULL; + { + Firebird::RefMutexGuard portGuard(*port->port_sync); + event = find_event(port, pevent->p_event_rid); + } if (event) { /* Call the asynchronous event routine associated Index: server.cpp =================================================================== RCS file: /cvsroot/firebird/firebird2/src/remote/server.cpp,v retrieving revision 1.178 retrieving revision 1.179 diff -b -U3 -r1.178 -r1.179 --- server.cpp 13 Mar 2008 12:05:49 -0000 1.178 +++ server.cpp 13 Mar 2008 13:47:55 -0000 1.179 @@ -212,14 +212,16 @@ } - class Worker { public: + static const int MAX_THREADS = 128; + static const int IDLE_TIMEOUT = 60; + Worker(); ~Worker(); - bool Wait(int timeout); // true is success, false if timeout + bool Wait(int timeout = IDLE_TIMEOUT); // true is success, false if timeout static bool WakeUp(); static void WakeUpAll(); @@ -303,7 +305,7 @@ * **************************************/ - //ISC_enter(); /* Setup floating point exception handler once and for all. */ + ISC_enter(); /* Setup floating point exception handler once and for all. */ // Setup context pool for main thread Firebird::ContextPoolHolder mainThreadContext(getDefaultMemoryPool()); @@ -361,7 +363,8 @@ * if empty - allocate the new one. * **************************************/ - Firebird::MutexLockGuard queGuard(request_que_mutex); + Firebird::MutexEnsureUnlock queGuard(request_que_mutex); + queGuard.enter(); SERVER_REQ request = free_requests; #if defined(DEV_BUILD) && defined(DEBUG) @@ -388,9 +391,9 @@ request and hope another thread will free memory or request blocks that we can then use. */ - request_que_mutex->leave(); + queGuard.leave(); THREAD_SLEEP(1 * 1000); - request_que_mutex->enter(); + queGuard.enter(); } zap_packet(&request->req_send, true); zap_packet(&request->req_receive, true); @@ -529,6 +532,7 @@ memcpy(port->port_queue->add().getBuffer(dataSize), buffer, dataSize); } + Firebird::RefMutexEnsureUnlock portGuard(*port->port_sync); const bool portLocked = port->port_sync->tryEnter(); if (portLocked || !dataSize) { @@ -567,8 +571,7 @@ // gds__log("op=%d ds=%d", request->req_receive.p_operation, dataSize); request->req_port = port; - if (portLocked) - { + if (portLocked) { port->port_sync->leave(); } @@ -585,7 +588,6 @@ port->port_requests_queued); fflush(stdout); #endif - if (!Worker::WakeUp()) { gds__thread_start(loopThread, (void*)(IPTR) flags, THREAD_medium, THREAD_ast, 0); @@ -593,7 +595,6 @@ } request = 0; } - port = 0; } } @@ -5231,7 +5232,7 @@ status_vector[2] = isc_arg_end; } -static THREAD_ENTRY_DECLARE loopThread(THREAD_ENTRY_PARAM arg) +static THREAD_ENTRY_DECLARE loopThread(THREAD_ENTRY_PARAM) { /************************************** * @@ -5244,22 +5245,15 @@ * **************************************/ - //ISC_enter(); /* Setup floating point exception handler once and for all. */ + ISC_enter(); /* Setup floating point exception handler once and for all. */ - const SLONG flags = (SLONG)(IPTR) arg; Worker worker; - -#ifdef WIN_NT - void* thread = NULL; // silence non initialized warning - if (!(flags & SRVR_non_service)) - thread = CNTL_insert_thread(); -#endif - rem_port* port; while (!shutting_down) { - request_que_mutex->enter(); + Firebird::MutexEnsureUnlock reqQueGuard(request_que_mutex); + reqQueGuard.enter(); SERVER_REQ request = request_que; if (request) { @@ -5267,7 +5261,7 @@ REMOTE_TRACE(("Dequeue request %p", request_que)); request_que = request->req_next; - request_que_mutex->leave(); + reqQueGuard.leave(); while (request) { @@ -5293,6 +5287,7 @@ /* Validate port. If it looks ok, process request */ + Firebird::RefMutexEnsureUnlock portQueGuard(*request->req_port->port_sync); { // port_sync scope Firebird::RefMutexGuard portGuard(*request->req_port->port_sync); @@ -5415,32 +5410,36 @@ SERVER_REQ next = request->req_chain; free_request(request); //request = next; - if (next) { + + // Try to be fair - put new request at the end of waiting + // requests queue and take request to work on from the + // head of the queue + if (next) + { append_request_next(next, &request_que); + request = request_que; + request_que = request->req_next; + } + else { + request = NULL; } - request = 0; } } // request_que_mutex scope } // while (request) } else { - request_que_mutex->leave(); + reqQueGuard.leave(); worker.setState(false); if (shutting_down) break; - if (!worker.Wait(10)) + if (!worker.Wait()) break; } } -#ifdef WIN_NT - if (!(flags & SRVR_non_service)) - CNTL_remove_thread(thread); -#endif - return 0; } @@ -5604,6 +5603,7 @@ --m_cntAll; } + bool Worker::Wait(int timeout) { return m_sem.tryEnter(timeout); @@ -5627,7 +5627,7 @@ m_idleWorkers->m_sem.release(); return true; } - return (m_cntAll >= 128); + return (m_cntAll >= MAX_THREADS); } void Worker::WakeUpAll() |