1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/*
Copyright (C) 2010 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/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef AROS
#include "aros_compat.h"
#endif
#ifdef PS2_EE
#include "ps2_compat.h"
#endif
#ifdef PS3_PPU
#include "ps3_compat.h"
#endif
#ifdef WIN32
#include <win32/win32_compat.h>
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include "libnfs.h"
#include "libnfs-raw.h"
#include "libnfs-private.h"
#ifdef HAVE_MULTITHREADING
#ifdef HAVE_PTHREAD
#include <unistd.h>
#include <sys/syscall.h>
nfs_tid_t nfs_mt_get_tid(void)
{
#ifdef SYS_gettid
pid_t tid = syscall(SYS_gettid);
return tid;
#else
#error "SYS_gettid unavailable on this system"
#endif
}
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;
}
int nfs_mt_service_thread_start(struct nfs_context *nfs)
{
if (pthread_create(&nfs->nfsi->service_thread, NULL,
&nfs_mt_service_thread, nfs)) {
nfs_set_error(nfs, "Failed to start service thread");
return -1;
}
while (nfs->rpc->multithreading_enabled == 0) {
struct timespec ts = {0, 1000000};
nanosleep(&ts, NULL);
}
return 0;
}
void nfs_mt_service_thread_stop(struct nfs_context *nfs)
{
nfs->rpc->multithreading_enabled = 0;
pthread_join(nfs->nfsi->service_thread, NULL);
}
int nfs_mt_mutex_init(libnfs_mutex_t *mutex)
{
pthread_mutex_init(mutex, NULL);
return 0;
}
int nfs_mt_mutex_destroy(libnfs_mutex_t *mutex)
{
pthread_mutex_destroy(mutex);
return 0;
}
int nfs_mt_mutex_lock(libnfs_mutex_t *mutex)
{
return pthread_mutex_lock(mutex);
}
int nfs_mt_mutex_unlock(libnfs_mutex_t *mutex)
{
return pthread_mutex_unlock(mutex);
}
int nfs_mt_sem_init(libnfs_sem_t *sem, int value)
{
return sem_init(sem, 0, value);
}
int nfs_mt_sem_destroy(libnfs_sem_t *sem)
{
return sem_destroy(sem);
}
int nfs_mt_sem_post(libnfs_sem_t *sem)
{
return sem_post(sem);
}
int nfs_mt_sem_wait(libnfs_sem_t *sem)
{
return sem_wait(sem);
}
#elif 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
#endif /* HAVE_MULTITHREADING */
|