From patchwork Fri Jul 9 00:35:35 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 110970 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o690Zn1S028230 for ; Fri, 9 Jul 2010 00:35:50 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757184Ab0GIAfr (ORCPT ); Thu, 8 Jul 2010 20:35:47 -0400 Received: from mx1.redhat.com ([209.132.183.28]:18890 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756984Ab0GIAfr (ORCPT ); Thu, 8 Jul 2010 20:35:47 -0400 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o690ZjNF003769 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 8 Jul 2010 20:35:46 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o690ZhnB017189; Thu, 8 Jul 2010 20:35:44 -0400 Received: from localhost.localdomain (dhcp-1-188.tlv.redhat.com [10.35.1.188]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o690Zdrc002117; Thu, 8 Jul 2010 20:35:40 -0400 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [KVM-AUTOTEST PATCH 1/2] KVM test: rss.cpp: use critical section instead of mutex for text buffer access Date: Fri, 9 Jul 2010 03:35:35 +0300 Message-Id: <1278635736-14852-1-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Fri, 09 Jul 2010 00:35:50 +0000 (UTC) A critical section should be faster. The difference for this application may or may not be noticeable (with a large number of files). Signed-off-by: Michael Goldish --- client/tests/kvm/deps/rss.cpp | 15 ++++++++------- 1 files changed, 8 insertions(+), 7 deletions(-) diff --git a/client/tests/kvm/deps/rss.cpp b/client/tests/kvm/deps/rss.cpp index 8df70e4..5b30b48 100644 --- a/client/tests/kvm/deps/rss.cpp +++ b/client/tests/kvm/deps/rss.cpp @@ -101,11 +101,12 @@ int file_transfer_port = 10023; HWND hMainWindow = NULL; HWND hTextBox = NULL; -HANDLE hTextBufferMutex = NULL; char text_buffer[8192] = {0}; int text_size = 0; +CRITICAL_SECTION critical_section; + struct client_info { SOCKET socket; char addr_str[256]; @@ -179,12 +180,12 @@ void AppendMessage(const char *message, ...) strcat(str, "\r\n"); int len = strlen(str); - WaitForSingleObject(hTextBufferMutex, INFINITE); + EnterCriticalSection(&critical_section); if (text_size + len + 1 > sizeof(text_buffer)) FlushTextBuffer(); strcpy(text_buffer + text_size, str); text_size += len; - ReleaseMutex(hTextBufferMutex); + LeaveCriticalSection(&critical_section); } // Flush the text buffer every 250 ms @@ -192,9 +193,9 @@ DWORD WINAPI UpdateTextBox(LPVOID client_info_ptr) { while (1) { Sleep(250); - WaitForSingleObject(hTextBufferMutex, INFINITE); + EnterCriticalSection(&critical_section); FlushTextBuffer(); - ReleaseMutex(hTextBufferMutex); + LeaveCriticalSection(&critical_section); } return 0; } @@ -904,8 +905,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) MAKELPARAM(FALSE, 0)); // Set size limit SendMessage(hTextBox, EM_LIMITTEXT, TEXTBOX_LIMIT, 0); - // Create mutex for text buffer access - hTextBufferMutex = CreateMutex(NULL, FALSE, NULL); + // Initialize critical section object for text buffer access + InitializeCriticalSection(&critical_section); // Create text box update thread if (!CreateThread(NULL, 0, UpdateTextBox, NULL, 0, NULL)) ExitOnError("Could not create text box update thread");