[Bug 2127] New: WriteFile fails on threaded applications
Wine Bugs
wine-bugs at winehq.org
Thu Mar 25 10:51:00 CST 2004
http://bugs.winehq.org/show_bug.cgi?id=2127
Summary: WriteFile fails on threaded applications
Product: Wine
Version: 20040309
Platform: Other
OS/Version: other
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: wine-kernel
AssignedTo: wine-bugs at winehq.org
ReportedBy: tronb at bluesquirrel.com
Tested on: Fedora Core 1 and Fedora test 1.90
Version: Latest rpm and build from source 20040309
In threaded applications, which use CreateFile, WriteFile, CloseHandle heavily
(logging) in a threaded application, the WriteFile will fail because of a bad
fd even when the sequence (CreateFile, WriteFile, CloseHandle) is protected as
a critical section.
Test Code:
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int Test();
DWORD WINAPI AThread( LPVOID lpParameter );
int Log(const char * stuff);
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
Test();
}
return nRetCode;
}
int Test()
{
#define NUM_THREADS 10
int ThreadNo[NUM_THREADS];
HANDLE ThreadHandles[NUM_THREADS];
//Start 10 threads
for(int i=0;i<NUM_THREADS;i++)
{
ThreadNo[i] = i;
ThreadHandles[i] = CreateThread(NULL, 0, AThread, &(ThreadNo
[i]), 0, NULL);
if (ThreadHandles[i] == NULL)
printf("CreateThread failed to create thread %d with
GetLastError:%d\n", i, GetLastError());
}
printf("Created %d Threads\n", NUM_THREADS);
DWORD result = WaitForSingleObject(ThreadHandles[0], 2L*60L*1000L);
if (result == WAIT_TIMEOUT)
printf("Timed out waiting for thread 0\n");
return(i);
}
DWORD WINAPI AThread( LPVOID lpParameter )
{
char buffer[100];
while(1)
{
int *lpint = (int*)lpParameter;
sprintf(buffer, "This is some stuff from thread %d\n", *lpint);
Log(buffer);
}
}
int Log(const char * stuff)
{
HANDLE hMutex = CreateMutex(NULL, TRUE, NULL);
if (hMutex == 0)
printf("Mutex failed\n");
//Wait for 5 seconds
DWORD dwWaitResult = WaitForSingleObject( hMutex, 5000L );
if (dwWaitResult == WAIT_TIMEOUT)
printf("Wait timed out\n");
if (dwWaitResult == WAIT_ABANDONED)
printf("Wait abandoned\n");
HANDLE hFile = CreateFile("C:\\TESTLOG.LOG", FILE_WRITE_DATA,
FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("CreateFile Failed with GetLastError of %d\n",
GetLastError());
}
DWORD NewLoc = SetFilePointer (hFile, 0, NULL, FILE_END);
DWORD bytes_written;
BOOL write_file_result = WriteFile(hFile, stuff, (DWORD)strlen(stuff),
&bytes_written, NULL);
if (write_file_result == 0)
{
printf("WriteFile Failed with GetLastError of %d\n",
GetLastError());
}
if (CloseHandle(hFile) == 0)
printf("CloseHandle Failed with %d\n", GetLastError());
BOOL release_mutex_result = ReleaseMutex(hMutex);
if (release_mutex_result == 0)
printf("ReleaseMutex failed with %d\n", GetLastError());
return(0);
}
--
Configure bugmail: http://bugs.winehq.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
More information about the wine-bugs
mailing list