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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
// Copyright (C) 2004-2021 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF 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 Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#include "mupdf/helpers/mu-threads.h"
#ifdef DISABLE_MUTHREADS
#include <stdlib.h>
/* Null implementation. Just error out. */
int mu_create_semaphore(mu_semaphore *sem)
{
return 1; /* Just Error */
}
void mu_destroy_semaphore(mu_semaphore *sem)
{
}
int mu_trigger_semaphore(mu_semaphore *sem)
{
abort();
return 1;
}
int mu_wait_semaphore(mu_semaphore *sem)
{
abort();
return 1;
}
int mu_create_thread(mu_thread *th, mu_thread_fn *fn, void *arg)
{
return 1;
}
void mu_destroy_thread(mu_thread *th)
{
}
int mu_create_mutex(mu_mutex *mutex)
{
return 1;
}
void mu_destroy_mutex(mu_mutex *mutex)
{
}
void mu_lock_mutex(mu_mutex *mutex)
{
abort();
}
void mu_unlock_mutex(mu_mutex *mutex)
{
abort();
}
#elif MU_THREAD_IMPL_TYPE == 1
/* Windows threads */
int mu_create_semaphore(mu_semaphore *sem)
{
sem->handle = CreateSemaphore(NULL, 0, 1, NULL);
return (sem->handle == NULL);
}
void mu_destroy_semaphore(mu_semaphore *sem)
{
if (sem->handle == NULL)
return;
/* We can't sensibly handle this failing */
(void)CloseHandle(sem->handle);
}
int mu_trigger_semaphore(mu_semaphore *sem)
{
if (sem->handle == NULL)
return 0;
/* We can't sensibly handle this failing */
return !ReleaseSemaphore(sem->handle, 1, NULL);
}
int mu_wait_semaphore(mu_semaphore *sem)
{
if (sem->handle == NULL)
return 0;
/* We can't sensibly handle this failing */
return !WaitForSingleObject(sem->handle, INFINITE);
}
static DWORD WINAPI thread_starter(LPVOID arg)
{
mu_thread *th = (mu_thread *)arg;
th->fn(th->arg);
return 0;
}
int mu_create_thread(mu_thread *th, mu_thread_fn *fn, void *arg)
{
th->fn = fn;
th->arg = arg;
th->handle = CreateThread(NULL, 0, thread_starter, th, 0, NULL);
return (th->handle == NULL);
}
void mu_destroy_thread(mu_thread *th)
{
if (th->handle == NULL)
return;
/* We can't sensibly handle this failing */
(void)WaitForSingleObject(th->handle, INFINITE);
(void)CloseHandle(th->handle);
th->handle = NULL;
}
int mu_create_mutex(mu_mutex *mutex)
{
InitializeCriticalSection(&mutex->mutex);
return 0; /* Magic function, never fails */
}
void mu_destroy_mutex(mu_mutex *mutex)
{
const static CRITICAL_SECTION empty = { 0 };
if (memcmp(&mutex->mutex, &empty, sizeof(empty)) == 0)
return;
DeleteCriticalSection(&mutex->mutex);
mutex->mutex = empty;
}
void mu_lock_mutex(mu_mutex *mutex)
{
EnterCriticalSection(&mutex->mutex);
}
void mu_unlock_mutex(mu_mutex *mutex)
{
LeaveCriticalSection(&mutex->mutex);
}
#elif MU_THREAD_IMPL_TYPE == 2
/*
PThreads - without working unnamed semaphores.
Neither ios nor OSX supports unnamed semaphores.
Named semaphores are a pain to use, so we implement
our own semaphores using condition variables and
mutexes.
*/
#include <string.h>
int
mu_create_semaphore(mu_semaphore *sem)
{
int scode;
sem->count = 0;
scode = pthread_mutex_init(&sem->mutex, NULL);
if (scode == 0)
{
scode = pthread_cond_init(&sem->cond, NULL);
if (scode)
pthread_mutex_destroy(&sem->mutex);
}
if (scode)
memset(sem, 0, sizeof(*sem));
return scode;
}
void
mu_destroy_semaphore(mu_semaphore *sem)
{
const static mu_semaphore empty = { 0 };
if (memcmp(sem, &empty, sizeof(empty)) == 0)
return;
(void)pthread_cond_destroy(&sem->cond);
(void)pthread_mutex_destroy(&sem->mutex);
*sem = empty;
}
int
mu_wait_semaphore(mu_semaphore *sem)
{
int scode, scode2;
scode = pthread_mutex_lock(&sem->mutex);
if (scode)
return scode;
while (sem->count == 0) {
scode = pthread_cond_wait(&sem->cond, &sem->mutex);
if (scode)
break;
}
if (scode == 0)
--sem->count;
scode2 = pthread_mutex_unlock(&sem->mutex);
if (scode == 0)
scode = scode2;
return scode;
}
int
mu_trigger_semaphore(mu_semaphore * sem)
{
int scode, scode2;
scode = pthread_mutex_lock(&sem->mutex);
if (scode)
return scode;
if (sem->count++ == 0)
scode = pthread_cond_signal(&sem->cond);
scode2 = pthread_mutex_unlock(&sem->mutex);
if (scode == 0)
scode = scode2;
return scode;
}
static void *thread_starter(void *arg)
{
mu_thread *th = (mu_thread *)arg;
th->fn(th->arg);
return NULL;
}
int mu_create_thread(mu_thread *th, mu_thread_fn *fn, void *arg)
{
th->fn = fn;
th->arg = arg;
return pthread_create(&th->thread, NULL, thread_starter, th);
}
void mu_destroy_thread(mu_thread *th)
{
const static mu_thread empty; /* static objects are always initialized to zero */
if (memcmp(th, &empty, sizeof(empty)) == 0)
return;
(void)pthread_join(th->thread, NULL);
*th = empty;
}
int mu_create_mutex(mu_mutex *mutex)
{
return pthread_mutex_init(&mutex->mutex, NULL);
}
void mu_destroy_mutex(mu_mutex *mutex)
{
const static mu_mutex empty; /* static objects are always initialized to zero */
if (memcmp(mutex, &empty, sizeof(empty)) == 0)
return;
(void)pthread_mutex_destroy(&mutex->mutex);
*mutex = empty;
}
void mu_lock_mutex(mu_mutex *mutex)
{
(void)pthread_mutex_lock(&mutex->mutex);
}
void mu_unlock_mutex(mu_mutex *mutex)
{
(void)pthread_mutex_unlock(&mutex->mutex);
}
#else
#error Unknown MU_THREAD_IMPL_TYPE setting
#endif
|