DEVTHREADPARMS* CreateDeviceThread(unsigned short wDevNum) { DEVTHREADPARMS* pThreadParms; // ptr to returned device_thread parameters DWORD dwThreadID; // (work) pThreadParms = malloc(sizeof(DEVTHREADPARMS)); // (allocate structure) if (!pThreadParms) { WRMSG ( HHC04111, "E", 0, wDevNum, "malloc(DEVTHREADPARMS)", errno, strerror(errno) ); return NULL; // (error) } pThreadParms->hShutdownEvent = MyCreateEvent(NULL,TRUE,FALSE,NULL); if (!pThreadParms->hShutdownEvent) { WRMSG ( HHC04111, "E", 0, wDevNum, "CreateEvent(hShutdownEvent)", errno, strerror(errno) ); free(pThreadParms); return NULL; // (error) } pThreadParms->hRequestQueuedEvent = MyCreateEvent(NULL,TRUE,FALSE,NULL); if (!pThreadParms->hRequestQueuedEvent) { WRMSG ( HHC04111, "E", 0, wDevNum, "CreateEvent(hRequestQueuedEvent)", errno, strerror(errno) ); MyCloseHandle(pThreadParms->hShutdownEvent); free(pThreadParms); return NULL; // (error) } MyInitializeCriticalSection(&pThreadParms->IORequestListLock); InitializeListLink(&pThreadParms->ThreadListLinkingListEntry); InitializeListHead(&pThreadParms->IORequestListHeadListEntry); pThreadParms->bThreadIsDead = FALSE; pThreadParms->dwThreadID = 0; if (fthread_create(&dwThreadID,NULL,DeviceThread,pThreadParms,"DeviceThread") != 0) { WRMSG ( HHC04111, "E", 0, wDevNum, "fthread_create(DeviceThread)", errno, strerror(errno) ); MyCloseHandle(pThreadParms->hShutdownEvent); MyCloseHandle(pThreadParms->hRequestQueuedEvent); MyDeleteCriticalSection(&pThreadParms->IORequestListLock); free(pThreadParms); return NULL; // (error) } // Add the newly created device_thread to the end of our list of managed threads. InsertListTail(&ThreadListHeadListEntry,&pThreadParms->ThreadListLinkingListEntry); if (++ios_devtnbr > ios_devthwm) ios_devthwm = ios_devtnbr; LockThreadParms(pThreadParms); // (lock thread parms before using) return pThreadParms; // (success) }
void RemoveThisThreadFromOurList(DEVTHREADPARMS* pThreadParms) { RemoveListEntry(&pThreadParms->ThreadListLinkingListEntry); MyCloseHandle(pThreadParms->hShutdownEvent); MyCloseHandle(pThreadParms->hRequestQueuedEvent); MyDeleteCriticalSection(&pThreadParms->IORequestListLock); free(pThreadParms); ios_devtnbr--; // (track number of active device_thread) }
/** * main * * executable entry point */ int __cdecl main( int argc, char **argv ) { /* local variables */ int err; WORD wVersionRequested; WSADATA wsaData; DWORD dwError; HANDLE hEvent; struct sockaddr_in mySockaddr; SOCKET sock = INVALID_SOCKET; /* PAL initialization */ if ( (PAL_Initialize(argc, argv)) != 0 ) { return( FAIL ); } /* initialize winsock version 2.2 */ wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { Fail( "Unexpected WSAStartup call failed with error code %d\n", err ); } /* Confirm that the WinSock DLL supports the specified version. */ if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { /* Tell the user that we could not find a usable winsock DLL. */ CleanupWinsock(); Fail( "Requested winsock version unsupported, " "returned version %d.%d\n", LOBYTE( wsaData.wVersion ), HIBYTE( wsaData.wVersion ) ); } /* create an event for socket notifications */ hEvent = CreateEvent( NULL, FALSE, FALSE, "foo" ); if( hEvent == NULL ) { dwError = GetLastError(); Trace( "CreateEvent() call failed with error code %lu\n", dwError ); CleanupWinsock(); Fail( "test failed\n" ); } /* create a stream socket in Internet domain */ sock = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ); if ( sock == INVALID_SOCKET ) { /* check the socket-specific error code */ dwError = GetLastError(); Trace( "socket() call failed with error code %lu\n", dwError ); MyCloseHandle( hEvent ); CleanupWinsock(); Fail( "test failed\n" ); } /* call WSAEventSelect -- this should set the socket to non-blocking */ err = WSAEventSelect( sock, hEvent, FD_CONNECT ); /* prepare the sockaddr structure */ mySockaddr.sin_family = AF_INET; mySockaddr.sin_port = getRotorTestPort(); mySockaddr.sin_addr.S_un.S_addr = inet_addr( "127.0.0.1" ); memset( &(mySockaddr.sin_zero), 0, 8 ); /* try to connect, there should be no listening socket */ err = connect( sock, (struct sockaddr *)&mySockaddr, sizeof(struct sockaddr) ); if( (err == 0) || (err == SOCKET_ERROR && (((dwError = GetLastError()) != WSAEWOULDBLOCK) && (dwError!=WSAECONNREFUSED) ) ) ) { if( err == SOCKET_ERROR ) { Trace( "connect called failed with error code %lu, " " expected WSAEWOULDBLOCK or WSAECONNREFUSED\n", dwError ); } else { Trace( "connect call succeeded, expected WSAEWOULDBLOCK\n" ); } CloseSocket( sock ); MyCloseHandle( hEvent ); CleanupWinsock(); Fail( "test failed\n" ); } /* close the socket */ err = closesocket( sock ); if ( err != 0 ) { Trace( "closesocket() call failed for accept socket\n" ); MyCloseHandle( hEvent ); CleanupWinsock(); Fail( "test failed\n" ); } /* close the handle */ if( ! CloseHandle( hEvent ) ) { dwError = GetLastError(); Trace( "CloseHandle() call failed with error code %lu\n", dwError ); CleanupWinsock(); Fail( "test failed\n" ); } /* cleanup the winsock library */ err = WSACleanup(); if ( err != 0 ) { /* check the socket-specific error code */ dwError = GetLastError(); Fail( "WSACleanup call failed with error code %d\n", dwError ); } /* PAL termination */ PAL_Terminate(); return PASS; }
HRes Semaphore_Close(CSemaphore *p) { return MyCloseHandle(&p->handle); }
HRes Event_Close(CEvent *p) { return MyCloseHandle(&p->handle); }
HRes Thread_Close(CThread *thread) { return MyCloseHandle(&thread->handle); }