Add multithreading helpers

Add an abstraction for mutexts and threads
that handles both pthread api and native win32 api

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
This commit is contained in:
Ronnie Sahlberg
2025-03-07 08:23:32 +10:00
parent f8542ab5cc
commit cb44ad4e26
6 changed files with 471 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/*
Copyright (C) 2025 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _LIBISCSI_MULTITHREADING_H_
#define _LIBISCSI_MULTITHREADING_H_
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef HAVE_MULTITHREADING
#ifdef WIN32
typedef HANDLE libiscsi_thread_t;
typedef HANDLE libiscsi_mutex_t;
typedef HANDLE libiscsi_sem_t;
typedef DWORD iscsi_tid_t;
#elif defined(HAVE_PTHREAD)
#include <pthread.h>
typedef pthread_t libiscsi_thread_t;
typedef pthread_mutex_t libiscsi_mutex_t;
#if defined(__APPLE__) && defined(HAVE_DISPATCH_DISPATCH_H)
#include <dispatch/dispatch.h>
typedef dispatch_semaphore_t libiscsi_sem_t;
#else
#include <semaphore.h>
typedef sem_t libiscsi_sem_t;
#endif
#ifdef HAVE_PTHREAD_THREADID_NP
typedef uint64_t iscsi_tid_t;
#else
typedef pid_t iscsi_tid_t;
#endif
#endif /* HAVE_PTHREAD */
iscsi_tid_t iscsi_mt_get_tid(void);
int iscsi_mt_mutex_init(libiscsi_mutex_t *mutex);
int iscsi_mt_mutex_destroy(libiscsi_mutex_t *mutex);
int iscsi_mt_mutex_lock(libiscsi_mutex_t *mutex);
int iscsi_mt_mutex_unlock(libiscsi_mutex_t *mutex);
int iscsi_mt_sem_init(libiscsi_sem_t *sem, int value);
int iscsi_mt_sem_destroy(libiscsi_sem_t *sem);
int iscsi_mt_sem_post(libiscsi_sem_t *sem);
int iscsi_mt_sem_wait(libiscsi_sem_t *sem);
#endif /* HAVE_MULTITHREADING */
#ifdef __cplusplus
}
#endif
#endif /* !_LIBISCSI_MULTITHREADING_H_ */

View File

@@ -28,6 +28,39 @@
#include "iscsi.h"
#ifdef HAVE_MULTITHREADING
#ifdef HAVE_STDATOMIC_H
#include <stdatomic.h>
#define ATOMIC_INC(rpc, x) \
atomic_fetch_add_explicit(&x, 1, memory_order_relaxed)
#define ATOMIC_DEC(rpc, x) \
atomic_fetch_sub_explicit(&x, 1, memory_order_relaxed)
#else /* HAVE_STDATOMIC_H */
#define ATOMIC_INC(rpc, x) \
if (rpc->multithreading_enabled) { \
nfs_mt_mutex_lock(&rpc->atomic_int_mutex); \
} \
x++; \
if (rpc->multithreading_enabled) { \
nfs_mt_mutex_unlock(&rpc->atomic_int_mutex); \
}
#define ATOMIC_DEC(rpc, x) \
if (rpc->multithreading_enabled) { \
nfs_mt_mutex_lock(&rpc->atomic_int_mutex); \
} \
x--; \
if (rpc->multithreading_enabled) { \
nfs_mt_mutex_unlock(&rpc->atomic_int_mutex); \
}
#endif /* HAVE_STDATOMIC_H */
#else /* HAVE_MULTITHREADING */
/* no multithreading support, no need to protect the increment */
#define ATOMIC_INC(rpc, x) x++
#define ATOMIC_DEC(rpc, x) x--
#endif /* HAVE_MULTITHREADING */
#include "iscsi-multithreading.h"
#ifdef __cplusplus
extern "C" {
#endif
@@ -184,6 +217,19 @@ struct iscsi_context {
int no_ua_on_reconnect;
void (*fd_dup_cb)(struct iscsi_context *iscsi, void *opaque);
void *fd_dup_opaque;
#ifdef HAVE_MULTITHREADING
int multithreading_enabled;
libiscsi_mutex_t iscsi_mutex;
libiscsi_thread_t service_thread;
int poll_timeout;
//libnfs_mutex_t nfs4_open_counter_mutex;
//libnfs_mutex_t nfs4_open_call_mutex;
//struct nfs_thread_context *thread_ctx;
#ifndef HAVE_STDATOMIC_H
libiscsi_mutex_t atomic_int_mutex;
#endif /* HAVE_STDATOMIC_H */
#endif /* HAVE_MULTITHREADING */
};
#define ISCSI_PDU_IMMEDIATE 0x40