WIN32: add multithreading support for windows

master
Ronnie Sahlberg 2022-01-01 12:28:19 +11:00
parent 1648cd8393
commit 630a80c056
13 changed files with 194 additions and 20 deletions

View File

@ -37,6 +37,12 @@ typedef pthread_mutex_t libnfs_mutex_t;
typedef sem_t libnfs_sem_t;
#endif /* HAVE_PTHREAD */
#ifdef WIN32
typedef HANDLE libnfs_thread_t;
typedef HANDLE libnfs_mutex_t;
typedef HANDLE libnfs_sem_t;
#endif
int nfs_mt_mutex_init(libnfs_mutex_t *mutex);
int nfs_mt_mutex_destroy(libnfs_mutex_t *mutex);
int nfs_mt_mutex_lock(libnfs_mutex_t *mutex);

View File

@ -34,6 +34,7 @@
#define CADDR_T_DEFINED
typedef char *caddr_t;
#endif
#include <winsock2.h>
#endif
#include <stdio.h>

View File

@ -85,6 +85,10 @@ struct rpc_context *rpc_init_context(void)
rpc->magic = RPC_CONTEXT_MAGIC;
#ifdef HAVE_MULTITHREADING
nfs_mt_mutex_init(&rpc->rpc_mutex);
#endif /* HAVE_MULTITHREADING */
rpc->auth = authunix_create_default();
if (rpc->auth == NULL) {
free(rpc);
@ -116,9 +120,6 @@ struct rpc_context *rpc_init_context(void)
/* Default is no timeout */
rpc->timeout = -1;
#ifdef HAVE_MULTITHREADING
nfs_mt_mutex_init(&rpc->rpc_mutex);
#endif /* HAVE_MULTITHREADING */
return rpc;
}

View File

@ -17,6 +17,8 @@ nfs_close_async
nfs_closedir
nfs_creat
nfs_creat_async
nfs_create
nfs_create_async
nfs_destroy_context
nfs_fchmod
nfs_fchmod_async
@ -26,6 +28,8 @@ nfs_fcntl
nfs_fcntl_async
nfs_fstat
nfs_fstat_async
nfs_fstat64
nfs_fstat64_async
nfs_fsync
nfs_fsync_async
nfs_ftruncate

View File

@ -68,7 +68,6 @@ static void *nfs_mt_service_thread(void *arg)
pfd.events = nfs_which_events(nfs);
pfd.revents = 0;
//qqq ~12737 iterations with busy loop
ret = poll(&pfd, 1, 0);
if (ret < 0) {
nfs_set_error(nfs, "Poll failed");
@ -146,7 +145,121 @@ int nfs_mt_sem_wait(libnfs_sem_t *sem)
return sem_wait(sem);
}
#endif /* HAVE_PTHREAD */
#elif WIN32
static void* nfs_mt_service_thread(void* arg)
{
struct nfs_context* nfs = (struct nfs_context*)arg;
struct pollfd pfd;
int revents;
int ret;
nfs->multithreading_enabled = 1;
while (nfs->multithreading_enabled) {
pfd.fd = nfs_get_fd(nfs);
pfd.events = nfs_which_events(nfs);
pfd.revents = 0;
ret = poll(&pfd, 1, 0);
if (ret < 0) {
nfs_set_error(nfs, "Poll failed");
revents = -1;
}
else {
revents = pfd.revents;
}
if (nfs_service(nfs, revents) < 0) {
if (revents != -1)
nfs_set_error(nfs, "nfs_service failed");
}
}
return NULL;
}
static DWORD WINAPI service_thread_init(LPVOID lpParam)
{
HANDLE hStdout;
struct nfs_context* nfs;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdout == INVALID_HANDLE_VALUE) {
return 1;
}
nfs = (struct nfs_context *)lpParam;
printf("SERVICE THREAD\n");
nfs_mt_service_thread(nfs);
return 0;
}
int nfs_mt_service_thread_start(struct nfs_context* nfs)
{
nfs->service_thread = CreateThread(NULL, 1024*1024, service_thread_init, nfs, 0, NULL);
if (nfs->service_thread == NULL) {
nfs_set_error(nfs, "Failed to start service thread");
return -1;
}
while (nfs->multithreading_enabled == 0) {
Sleep(100);
}
return 0;
}
void nfs_mt_service_thread_stop(struct nfs_context* nfs)
{
nfs->multithreading_enabled = 0;
while (WaitForSingleObject(nfs->service_thread, INFINITE) != WAIT_OBJECT_0);
}
int nfs_mt_mutex_init(libnfs_mutex_t* mutex)
{
*mutex = CreateMutex(NULL, 0, NULL);
return 0;
}
int nfs_mt_mutex_destroy(libnfs_mutex_t* mutex)
{
CloseHandle(*mutex);
return 0;
}
int nfs_mt_mutex_lock(libnfs_mutex_t* mutex)
{
while (WaitForSingleObject(*mutex, INFINITE) != WAIT_OBJECT_0);
return 0;
}
int nfs_mt_mutex_unlock(libnfs_mutex_t* mutex)
{
ReleaseMutex(*mutex);
return 0;
}
int nfs_mt_sem_init(libnfs_sem_t* sem, int value)
{
*sem = CreateSemaphoreA(NULL, 0, 16, NULL);
return 0;
}
int nfs_mt_sem_destroy(libnfs_sem_t* sem)
{
CloseHandle(*sem);
return 0;
}
int nfs_mt_sem_post(libnfs_sem_t* sem)
{
ReleaseSemaphore(*sem, 1, NULL);
return 0;
}
int nfs_mt_sem_wait(libnfs_sem_t* sem)
{
while (WaitForSingleObject(*sem, INFINITE) != WAIT_OBJECT_0);
return 0;
}
#endif
#endif /* HAVE_MULTITHREADING */

View File

@ -165,6 +165,7 @@ int main(int argc, char *argv[])
struct client client;
struct statvfs stvfs;
struct nfs_url *url = NULL;
int mt_started = 0;
#ifdef WIN32
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
@ -232,7 +233,6 @@ int main(int argc, char *argv[])
}
nfs_destroy_url(url);
}
url = nfs_parse_url_dir(nfs, argv[argc - 1]);
if (url == NULL) {
fprintf(stderr, "%s\n", nfs_get_error(nfs));
@ -247,7 +247,16 @@ int main(int argc, char *argv[])
fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs));
goto finished;
}
/*
* Before we can use multithreading we must initialize and
* start the service thread.
*/
if (nfs_mt_service_thread_start(nfs)) {
printf("failed to start service thread\n");
exit(10);
}
mt_started = 1;
process_dir(nfs, "", 16);
if (summary) {
@ -261,6 +270,11 @@ int main(int argc, char *argv[])
ret = 0;
finished:
if (mt_started) {
printf("closing service thread\n");
nfs_mt_service_thread_stop(nfs);
}
if (ret > 0) {
print_usage();
}

View File

@ -28,23 +28,27 @@
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
@ -111,7 +115,7 @@
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;_U_=;__STDC_CONSTANT_MACROS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>HAVE_MULTITHREADING;WIN32;NDEBUG;_WINDOWS;_USRDLL;_U_=;__STDC_CONSTANT_MACROS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\..\include\nfsc;..\..\include;..\..\.;..\..\win32;..\..\mount;..\..\nfs;..\..\nfs4;..\..\portmap;..\..\lib</AdditionalIncludeDirectories>
<CompileAs>Default</CompileAs>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
@ -185,6 +189,7 @@
<ClCompile Include="..\..\lib\libnfs-sync.c" />
<ClCompile Include="..\..\lib\libnfs-zdr.c" />
<ClCompile Include="..\..\lib\libnfs.c" />
<ClCompile Include="..\..\lib\multithreading.c" />
<ClCompile Include="..\..\lib\nfs_v3.c" />
<ClCompile Include="..\..\lib\nfs_v4.c" />
<ClCompile Include="..\..\lib\pdu.c" />
@ -207,7 +212,8 @@
<ClCompile Include="..\win32_compat.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\nfsc\libnfs-zdr.h" />
<ClInclude Include="..\..\include\libnfs-multithreading.h" />
<ClInclude Include="..\..\include\nfsc\libnfs-zdr.h" />
<ClInclude Include="..\..\mount\libnfs-raw-mount.h" />
<ClInclude Include="..\..\nfs\libnfs-raw-nfs.h" />
<ClInclude Include="..\..\nfs4\libnfs-raw-nfs4.h" />
@ -227,4 +233,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -83,6 +83,9 @@
<ClCompile Include="..\..\nsm\nsm.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\lib\multithreading.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\nfs\libnfs-raw-nfs.h">
@ -115,11 +118,14 @@
<ClInclude Include="..\..\nlm\libnfs-raw-nlm.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\libnfs-multithreading.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\lib\libnfs-win32.def" />
</ItemGroup>
<ItemGroup>
<ItemGroup>
<ResourceCompile Include="version.rc" />
</ItemGroup>
</Project>
</Project>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
@ -28,23 +28,27 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
@ -68,7 +72,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<LibraryPath>..\libnfs\Debug;..\..\bin;$(LibraryPath)</LibraryPath>
<LibraryPath>..\libnfs\x64\Debug;..\libnfs\Debug;..\..\bin;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
@ -104,7 +108,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_WIN32_WINNT=0x0501;_U_=;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>;..\..\include;..\..\.;..\..\win32;..\..\mount;..\..\nfs;..\..\portmap</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\include\nfsc;..\..\include;..\..\.;..\..\win32;..\..\mount;..\..\nfs;..\..\portmap</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -160,4 +164,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
@ -28,23 +28,27 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
@ -68,7 +72,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<LibraryPath>..\libnfs\Debug;..\..\bin;$(LibraryPath)</LibraryPath>
<LibraryPath>..\libnfs\x64\Debug;..\libnfs\Debug;..\..\bin;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
@ -104,7 +108,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_WIN32_WINNT=0x0501;_U_=;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\..\include;..\..\.;..\..\win32;..\..\mount;..\..\nfs;..\..\portmap</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\include\nfsc;..\..\include;..\..\.;..\..\win32;..\..\mount;..\..\nfs;..\..\portmap</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -160,4 +164,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>nfs://10.10.10.11/data/SNAP-4</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>