// cleans up OVERLAPPED instance after file operation completion static VOID CALLBACK _threadpoolCallback(_Inout_ PTP_CALLBACK_INSTANCE Instance, _Inout_opt_ PVOID Context, _Inout_opt_ PVOID Overlapped, _In_ ULONG IoResult, _In_ ULONG_PTR NumberOfBytesTransferred, _Inout_ PTP_IO Io) { free(Overlapped); CloseThreadpoolIo(Io); }
CF_PRIVATE Boolean _CFWriteBytesToFileAsync(CFURLRef url, const void *bytes, CFIndex length) { char path[CFMaxPathSize]; if (!CFURLGetFileSystemRepresentation(url, true, (uint8_t *)path, CFMaxPathSize)) { return false; } wchar_t wpath[CFMaxPathSize]; int convertedLength = MultiByteToWideChar(CP_UTF8, 0, path, CFMaxPathSize, wpath, CFMaxPathSize); if (0 == convertedLength) { unsigned error = GetLastError(); CFLog(kCFLogLevelWarning, CFSTR("_CFWriteBytesToFileAsync failed to convert the path (error %u)"), error); return false; } HANDLE fileHandle = NULL; CREATEFILE2_EXTENDED_PARAMETERS createExParams; createExParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS); createExParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; createExParams.dwFileFlags = FILE_FLAG_OVERLAPPED; createExParams.dwSecurityQosFlags = 0; createExParams.lpSecurityAttributes = NULL; createExParams.hTemplateFile = NULL; OVERLAPPED* overlapped = (OVERLAPPED*)calloc(1, sizeof(OVERLAPPED)); if ((fileHandle = CreateFile2(wpath, GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS, &createExParams)) == INVALID_HANDLE_VALUE) { unsigned error = GetLastError(); CFLog(kCFLogLevelWarning, CFSTR("_CFWriteBytesToFileAsync failed to open the file (error %u)"), error); free(overlapped); return false; } PTP_IO threadPoolIo = CreateThreadpoolIo(fileHandle, _threadpoolCallback, NULL, NULL); StartThreadpoolIo(threadPoolIo); if (!WriteFile(fileHandle, bytes, length, NULL, overlapped)) { unsigned error = GetLastError(); if (ERROR_IO_PENDING != error) { CFLog(kCFLogLevelWarning, CFSTR("_CFWriteBytesToFileAsync failed to write to the file (error %u)"), error); CloseHandle(fileHandle); CancelThreadpoolIo(threadPoolIo); CloseThreadpoolIo(threadPoolIo); free(overlapped); return false; } } CloseHandle(fileHandle); return true; }
void NamedPipe::Close() { base::AutoLock guard(lock_); CloseHandle(pipe_); pipe_ = INVALID_HANDLE_VALUE; if (io_ != nullptr) { auto local_io = io_; io_ = nullptr; base::AutoUnlock unlock(lock_); WaitForThreadpoolIoCallbacks(local_io, FALSE); CloseThreadpoolIo(local_io); } }
void Server::Destroy() { m_ShuttingDown = true; if (m_AcceptTPWORK != NULL) { WaitForThreadpoolWorkCallbacks(m_AcceptTPWORK, true); CloseThreadpoolWork(m_AcceptTPWORK); m_AcceptTPWORK = NULL; } if (m_listenSocket != INVALID_SOCKET) { Network::CloseSocket(m_listenSocket); CancelIoEx(reinterpret_cast<HANDLE>(m_listenSocket), NULL); m_listenSocket = INVALID_SOCKET; } if (m_pTPIO != NULL) { WaitForThreadpoolIoCallbacks(m_pTPIO, true); CloseThreadpoolIo(m_pTPIO); m_pTPIO = NULL; } if (m_ClientTPCLEAN != NULL) { CloseThreadpoolCleanupGroupMembers(m_ClientTPCLEAN, false, NULL); CloseThreadpoolCleanupGroup(m_ClientTPCLEAN); DestroyThreadpoolEnvironment(&m_ClientTPENV); m_ClientTPCLEAN = NULL; } EnterCriticalSection(&m_CSForClients); for (auto client : m_Clients) { delete client; } m_Clients.clear(); LeaveCriticalSection(&m_CSForClients); DeleteCriticalSection(&m_CSForClients); }
void NamedPipeChannel::Close() { base::AutoLock guard(lock_); if (handle_ != INVALID_HANDLE_VALUE) { CloseHandle(handle_); handle_ = INVALID_HANDLE_VALUE; end_point_ = EndPoint::kUnknown; } if (io_ != nullptr) { auto local_io = io_; io_ = nullptr; { base::AutoUnlock unlock(lock_); WaitForThreadpoolIoCallbacks(local_io, FALSE); } CloseThreadpoolIo(local_io); } }