Skip to content

Commit

Permalink
pthread and win32 need to be exclusive in multithread.c
Browse files Browse the repository at this point in the history
Signed-off-by: Ronnie Sahlberg <[email protected]>
  • Loading branch information
sahlberg committed Dec 16, 2024
1 parent 17591be commit 100e58a
Showing 1 changed file with 118 additions and 120 deletions.
238 changes: 118 additions & 120 deletions lib/multithreading.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,124 @@

#ifdef HAVE_MULTITHREADING

#ifdef HAVE_PTHREAD
#ifdef WIN32
nfs_tid_t nfs_mt_get_tid(void)
{
return GetCurrentThreadId();
}
static void* nfs_mt_service_thread(void* arg)
{
struct nfs_context* nfs = (struct nfs_context*)arg;
struct pollfd pfd;
int revents;
int ret;

nfs->rpc->multithreading_enabled = 1;

while (nfs->rpc->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;
nfs_mt_service_thread(nfs);
return 0;
}

int nfs_mt_service_thread_start(struct nfs_context* nfs)
{
nfs->nfsi->service_thread = CreateThread(NULL, 1024*1024, service_thread_init, nfs, 0, NULL);
if (nfs->nfsi->service_thread == NULL) {
nfs_set_error(nfs, "Failed to start service thread");
return -1;
}
while (nfs->rpc->multithreading_enabled == 0) {
Sleep(100);
}
return 0;
}

void nfs_mt_service_thread_stop(struct nfs_context* nfs)
{
nfs->rpc->multithreading_enabled = 0;
while (WaitForSingleObject(nfs->nfsi->service_thread, INFINITE) != WAIT_OBJECT_0);
}

int nfs_mt_mutex_init(libnfs_mutex_t* mutex)
{
*mutex = CreateSemaphoreA(NULL, 1, 1, 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)
{
ReleaseSemaphore(*mutex, 1, NULL);
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;
}

#elif HAVE_PTHREAD /* WIN32 */

#include <unistd.h>
#include <sys/syscall.h>

Expand Down Expand Up @@ -226,124 +343,5 @@ int nfs_mt_sem_wait(libnfs_sem_t *sem)

#endif /* HAVE_PTHREAD */

#ifdef WIN32
nfs_tid_t nfs_mt_get_tid(void)
{
return GetCurrentThreadId();
}
static void* nfs_mt_service_thread(void* arg)
{
struct nfs_context* nfs = (struct nfs_context*)arg;
struct pollfd pfd;
int revents;
int ret;

nfs->rpc->multithreading_enabled = 1;

while (nfs->rpc->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;
nfs_mt_service_thread(nfs);
return 0;
}

int nfs_mt_service_thread_start(struct nfs_context* nfs)
{
nfs->nfsi->service_thread = CreateThread(NULL, 1024*1024, service_thread_init, nfs, 0, NULL);
if (nfs->nfsi->service_thread == NULL) {
nfs_set_error(nfs, "Failed to start service thread");
return -1;
}
while (nfs->rpc->multithreading_enabled == 0) {
Sleep(100);
}
return 0;
}

void nfs_mt_service_thread_stop(struct nfs_context* nfs)
{
nfs->rpc->multithreading_enabled = 0;
while (WaitForSingleObject(nfs->nfsi->service_thread, INFINITE) != WAIT_OBJECT_0);
}

int nfs_mt_mutex_init(libnfs_mutex_t* mutex)
{
*mutex = CreateSemaphoreA(NULL, 1, 1, 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)
{
ReleaseSemaphore(*mutex, 1, NULL);
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 /* WIN32 */


#endif /* HAVE_MULTITHREADING */

0 comments on commit 100e58a

Please sign in to comment.