1*3ac0a46fSAndroid Build Coastguard Worker /* 2*3ac0a46fSAndroid Build Coastguard Worker * The copyright in this software is being made available under the 2-clauses 3*3ac0a46fSAndroid Build Coastguard Worker * BSD License, included below. This software may be subject to other third 4*3ac0a46fSAndroid Build Coastguard Worker * party and contributor rights, including patent rights, and no such rights 5*3ac0a46fSAndroid Build Coastguard Worker * are granted under this license. 6*3ac0a46fSAndroid Build Coastguard Worker * 7*3ac0a46fSAndroid Build Coastguard Worker * Copyright (c) 2016, Even Rouault 8*3ac0a46fSAndroid Build Coastguard Worker * All rights reserved. 9*3ac0a46fSAndroid Build Coastguard Worker * 10*3ac0a46fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 11*3ac0a46fSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions 12*3ac0a46fSAndroid Build Coastguard Worker * are met: 13*3ac0a46fSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright 14*3ac0a46fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer. 15*3ac0a46fSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright 16*3ac0a46fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the 17*3ac0a46fSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution. 18*3ac0a46fSAndroid Build Coastguard Worker * 19*3ac0a46fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' 20*3ac0a46fSAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21*3ac0a46fSAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22*3ac0a46fSAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23*3ac0a46fSAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*3ac0a46fSAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*3ac0a46fSAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*3ac0a46fSAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*3ac0a46fSAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*3ac0a46fSAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*3ac0a46fSAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE. 30*3ac0a46fSAndroid Build Coastguard Worker */ 31*3ac0a46fSAndroid Build Coastguard Worker 32*3ac0a46fSAndroid Build Coastguard Worker #ifndef THREAD_H 33*3ac0a46fSAndroid Build Coastguard Worker #define THREAD_H 34*3ac0a46fSAndroid Build Coastguard Worker 35*3ac0a46fSAndroid Build Coastguard Worker #include "openjpeg.h" 36*3ac0a46fSAndroid Build Coastguard Worker 37*3ac0a46fSAndroid Build Coastguard Worker /** 38*3ac0a46fSAndroid Build Coastguard Worker @file thread.h 39*3ac0a46fSAndroid Build Coastguard Worker @brief Thread API 40*3ac0a46fSAndroid Build Coastguard Worker 41*3ac0a46fSAndroid Build Coastguard Worker The functions in thread.c have for goal to manage mutex, conditions, thread 42*3ac0a46fSAndroid Build Coastguard Worker creation and thread pools that accept jobs. 43*3ac0a46fSAndroid Build Coastguard Worker */ 44*3ac0a46fSAndroid Build Coastguard Worker 45*3ac0a46fSAndroid Build Coastguard Worker /** @defgroup THREAD THREAD - Mutex, conditions, threads and thread pools */ 46*3ac0a46fSAndroid Build Coastguard Worker /*@{*/ 47*3ac0a46fSAndroid Build Coastguard Worker 48*3ac0a46fSAndroid Build Coastguard Worker /** @name Mutex */ 49*3ac0a46fSAndroid Build Coastguard Worker /*@{*/ 50*3ac0a46fSAndroid Build Coastguard Worker 51*3ac0a46fSAndroid Build Coastguard Worker /** Opaque type for a mutex */ 52*3ac0a46fSAndroid Build Coastguard Worker typedef struct opj_mutex_t opj_mutex_t; 53*3ac0a46fSAndroid Build Coastguard Worker 54*3ac0a46fSAndroid Build Coastguard Worker /** Creates a mutex. 55*3ac0a46fSAndroid Build Coastguard Worker * @return the mutex or NULL in case of error (can for example happen if the library 56*3ac0a46fSAndroid Build Coastguard Worker * is built without thread support) 57*3ac0a46fSAndroid Build Coastguard Worker */ 58*3ac0a46fSAndroid Build Coastguard Worker opj_mutex_t* opj_mutex_create(void); 59*3ac0a46fSAndroid Build Coastguard Worker 60*3ac0a46fSAndroid Build Coastguard Worker /** Lock/acquire the mutex. 61*3ac0a46fSAndroid Build Coastguard Worker * @param mutex the mutex to acquire. 62*3ac0a46fSAndroid Build Coastguard Worker */ 63*3ac0a46fSAndroid Build Coastguard Worker void opj_mutex_lock(opj_mutex_t* mutex); 64*3ac0a46fSAndroid Build Coastguard Worker 65*3ac0a46fSAndroid Build Coastguard Worker /** Unlock/release the mutex. 66*3ac0a46fSAndroid Build Coastguard Worker * @param mutex the mutex to release. 67*3ac0a46fSAndroid Build Coastguard Worker */ 68*3ac0a46fSAndroid Build Coastguard Worker void opj_mutex_unlock(opj_mutex_t* mutex); 69*3ac0a46fSAndroid Build Coastguard Worker 70*3ac0a46fSAndroid Build Coastguard Worker /** Destroy a mutex 71*3ac0a46fSAndroid Build Coastguard Worker * @param mutex the mutex to destroy. 72*3ac0a46fSAndroid Build Coastguard Worker */ 73*3ac0a46fSAndroid Build Coastguard Worker void opj_mutex_destroy(opj_mutex_t* mutex); 74*3ac0a46fSAndroid Build Coastguard Worker 75*3ac0a46fSAndroid Build Coastguard Worker /*@}*/ 76*3ac0a46fSAndroid Build Coastguard Worker 77*3ac0a46fSAndroid Build Coastguard Worker /** @name Condition */ 78*3ac0a46fSAndroid Build Coastguard Worker /*@{*/ 79*3ac0a46fSAndroid Build Coastguard Worker 80*3ac0a46fSAndroid Build Coastguard Worker /** Opaque type for a condition */ 81*3ac0a46fSAndroid Build Coastguard Worker typedef struct opj_cond_t opj_cond_t; 82*3ac0a46fSAndroid Build Coastguard Worker 83*3ac0a46fSAndroid Build Coastguard Worker /** Creates a condition. 84*3ac0a46fSAndroid Build Coastguard Worker * @return the condition or NULL in case of error (can for example happen if the library 85*3ac0a46fSAndroid Build Coastguard Worker * is built without thread support) 86*3ac0a46fSAndroid Build Coastguard Worker */ 87*3ac0a46fSAndroid Build Coastguard Worker opj_cond_t* opj_cond_create(void); 88*3ac0a46fSAndroid Build Coastguard Worker 89*3ac0a46fSAndroid Build Coastguard Worker /** Wait for the condition to be signaled. 90*3ac0a46fSAndroid Build Coastguard Worker * The semantics is the same as the POSIX pthread_cond_wait. 91*3ac0a46fSAndroid Build Coastguard Worker * The provided mutex *must* be acquired before calling this function, and 92*3ac0a46fSAndroid Build Coastguard Worker * released afterwards. 93*3ac0a46fSAndroid Build Coastguard Worker * The mutex will be released by this function while it must wait for the condition 94*3ac0a46fSAndroid Build Coastguard Worker * and reacquired afterwards. 95*3ac0a46fSAndroid Build Coastguard Worker * In some particular situations, the function might return even if the condition is not signaled 96*3ac0a46fSAndroid Build Coastguard Worker * with opj_cond_signal(), hence the need to check with an application level 97*3ac0a46fSAndroid Build Coastguard Worker * mechanism. 98*3ac0a46fSAndroid Build Coastguard Worker * 99*3ac0a46fSAndroid Build Coastguard Worker * Waiting thread : 100*3ac0a46fSAndroid Build Coastguard Worker * \code 101*3ac0a46fSAndroid Build Coastguard Worker * opj_mutex_lock(mutex); 102*3ac0a46fSAndroid Build Coastguard Worker * while( !some_application_level_condition ) 103*3ac0a46fSAndroid Build Coastguard Worker * { 104*3ac0a46fSAndroid Build Coastguard Worker * opj_cond_wait(cond, mutex); 105*3ac0a46fSAndroid Build Coastguard Worker * } 106*3ac0a46fSAndroid Build Coastguard Worker * opj_mutex_unlock(mutex); 107*3ac0a46fSAndroid Build Coastguard Worker * \endcode 108*3ac0a46fSAndroid Build Coastguard Worker * 109*3ac0a46fSAndroid Build Coastguard Worker * Signaling thread : 110*3ac0a46fSAndroid Build Coastguard Worker * \code 111*3ac0a46fSAndroid Build Coastguard Worker * opj_mutex_lock(mutex); 112*3ac0a46fSAndroid Build Coastguard Worker * some_application_level_condition = TRUE; 113*3ac0a46fSAndroid Build Coastguard Worker * opj_cond_signal(cond); 114*3ac0a46fSAndroid Build Coastguard Worker * opj_mutex_unlock(mutex); 115*3ac0a46fSAndroid Build Coastguard Worker * \endcode 116*3ac0a46fSAndroid Build Coastguard Worker * 117*3ac0a46fSAndroid Build Coastguard Worker * @param cond the condition to wait. 118*3ac0a46fSAndroid Build Coastguard Worker * @param mutex the mutex (in acquired state before calling this function) 119*3ac0a46fSAndroid Build Coastguard Worker */ 120*3ac0a46fSAndroid Build Coastguard Worker void opj_cond_wait(opj_cond_t* cond, opj_mutex_t* mutex); 121*3ac0a46fSAndroid Build Coastguard Worker 122*3ac0a46fSAndroid Build Coastguard Worker /** Signal waiting threads on a condition. 123*3ac0a46fSAndroid Build Coastguard Worker * One of the thread waiting with opj_cond_wait() will be waken up. 124*3ac0a46fSAndroid Build Coastguard Worker * It is strongly advised that this call is done with the mutex that is used 125*3ac0a46fSAndroid Build Coastguard Worker * by opj_cond_wait(), in a acquired state. 126*3ac0a46fSAndroid Build Coastguard Worker * @param cond the condition to signal. 127*3ac0a46fSAndroid Build Coastguard Worker */ 128*3ac0a46fSAndroid Build Coastguard Worker void opj_cond_signal(opj_cond_t* cond); 129*3ac0a46fSAndroid Build Coastguard Worker 130*3ac0a46fSAndroid Build Coastguard Worker /** Destroy a condition 131*3ac0a46fSAndroid Build Coastguard Worker * @param cond the condition to destroy. 132*3ac0a46fSAndroid Build Coastguard Worker */ 133*3ac0a46fSAndroid Build Coastguard Worker void opj_cond_destroy(opj_cond_t* cond); 134*3ac0a46fSAndroid Build Coastguard Worker 135*3ac0a46fSAndroid Build Coastguard Worker /*@}*/ 136*3ac0a46fSAndroid Build Coastguard Worker 137*3ac0a46fSAndroid Build Coastguard Worker /** @name Thread */ 138*3ac0a46fSAndroid Build Coastguard Worker /*@{*/ 139*3ac0a46fSAndroid Build Coastguard Worker 140*3ac0a46fSAndroid Build Coastguard Worker /** Opaque type for a thread handle */ 141*3ac0a46fSAndroid Build Coastguard Worker typedef struct opj_thread_t opj_thread_t; 142*3ac0a46fSAndroid Build Coastguard Worker 143*3ac0a46fSAndroid Build Coastguard Worker /** User function to execute in a thread 144*3ac0a46fSAndroid Build Coastguard Worker * @param user_data user data provided with opj_thread_create() 145*3ac0a46fSAndroid Build Coastguard Worker */ 146*3ac0a46fSAndroid Build Coastguard Worker typedef void (*opj_thread_fn)(void* user_data); 147*3ac0a46fSAndroid Build Coastguard Worker 148*3ac0a46fSAndroid Build Coastguard Worker /** Creates a new thread. 149*3ac0a46fSAndroid Build Coastguard Worker * @param thread_fn Function to run in the new thread. 150*3ac0a46fSAndroid Build Coastguard Worker * @param user_data user data provided to the thread function. Might be NULL. 151*3ac0a46fSAndroid Build Coastguard Worker * @return a thread handle or NULL in case of failure (can for example happen if the library 152*3ac0a46fSAndroid Build Coastguard Worker * is built without thread support) 153*3ac0a46fSAndroid Build Coastguard Worker */ 154*3ac0a46fSAndroid Build Coastguard Worker opj_thread_t* opj_thread_create(opj_thread_fn thread_fn, void* user_data); 155*3ac0a46fSAndroid Build Coastguard Worker 156*3ac0a46fSAndroid Build Coastguard Worker /** Wait for a thread to be finished and release associated resources to the 157*3ac0a46fSAndroid Build Coastguard Worker * thread handle. 158*3ac0a46fSAndroid Build Coastguard Worker * @param thread the thread to wait for being finished. 159*3ac0a46fSAndroid Build Coastguard Worker */ 160*3ac0a46fSAndroid Build Coastguard Worker void opj_thread_join(opj_thread_t* thread); 161*3ac0a46fSAndroid Build Coastguard Worker 162*3ac0a46fSAndroid Build Coastguard Worker /*@}*/ 163*3ac0a46fSAndroid Build Coastguard Worker 164*3ac0a46fSAndroid Build Coastguard Worker /** @name Thread local storage */ 165*3ac0a46fSAndroid Build Coastguard Worker /*@{*/ 166*3ac0a46fSAndroid Build Coastguard Worker /** Opaque type for a thread local storage */ 167*3ac0a46fSAndroid Build Coastguard Worker typedef struct opj_tls_t opj_tls_t; 168*3ac0a46fSAndroid Build Coastguard Worker 169*3ac0a46fSAndroid Build Coastguard Worker /** Get a thread local value corresponding to the provided key. 170*3ac0a46fSAndroid Build Coastguard Worker * @param tls thread local storage handle 171*3ac0a46fSAndroid Build Coastguard Worker * @param key key whose value to retrieve. 172*3ac0a46fSAndroid Build Coastguard Worker * @return value associated with the key, or NULL is missing. 173*3ac0a46fSAndroid Build Coastguard Worker */ 174*3ac0a46fSAndroid Build Coastguard Worker void* opj_tls_get(opj_tls_t* tls, int key); 175*3ac0a46fSAndroid Build Coastguard Worker 176*3ac0a46fSAndroid Build Coastguard Worker /** Type of the function used to free a TLS value */ 177*3ac0a46fSAndroid Build Coastguard Worker typedef void (*opj_tls_free_func)(void* value); 178*3ac0a46fSAndroid Build Coastguard Worker 179*3ac0a46fSAndroid Build Coastguard Worker /** Set a thread local value corresponding to the provided key. 180*3ac0a46fSAndroid Build Coastguard Worker * @param tls thread local storage handle 181*3ac0a46fSAndroid Build Coastguard Worker * @param key key whose value to set. 182*3ac0a46fSAndroid Build Coastguard Worker * @param value value to set (may be NULL). 183*3ac0a46fSAndroid Build Coastguard Worker * @param free_func function to call currently installed value. 184*3ac0a46fSAndroid Build Coastguard Worker * @return OPJ_TRUE if successful. 185*3ac0a46fSAndroid Build Coastguard Worker */ 186*3ac0a46fSAndroid Build Coastguard Worker OPJ_BOOL opj_tls_set(opj_tls_t* tls, int key, void* value, 187*3ac0a46fSAndroid Build Coastguard Worker opj_tls_free_func free_func); 188*3ac0a46fSAndroid Build Coastguard Worker 189*3ac0a46fSAndroid Build Coastguard Worker /*@}*/ 190*3ac0a46fSAndroid Build Coastguard Worker 191*3ac0a46fSAndroid Build Coastguard Worker /** @name Thread pool */ 192*3ac0a46fSAndroid Build Coastguard Worker /*@{*/ 193*3ac0a46fSAndroid Build Coastguard Worker 194*3ac0a46fSAndroid Build Coastguard Worker /** Opaque type for a thread pool */ 195*3ac0a46fSAndroid Build Coastguard Worker typedef struct opj_thread_pool_t opj_thread_pool_t; 196*3ac0a46fSAndroid Build Coastguard Worker 197*3ac0a46fSAndroid Build Coastguard Worker /** Create a new thread pool. 198*3ac0a46fSAndroid Build Coastguard Worker * num_thread must nominally be >= 1 to create a real thread pool. If num_threads 199*3ac0a46fSAndroid Build Coastguard Worker * is negative or null, then a dummy thread pool will be created. All functions 200*3ac0a46fSAndroid Build Coastguard Worker * operating on the thread pool will work, but job submission will be run 201*3ac0a46fSAndroid Build Coastguard Worker * synchronously in the calling thread. 202*3ac0a46fSAndroid Build Coastguard Worker * 203*3ac0a46fSAndroid Build Coastguard Worker * @param num_threads the number of threads to allocate for this thread pool. 204*3ac0a46fSAndroid Build Coastguard Worker * @return a thread pool handle, or NULL in case of failure (can for example happen if the library 205*3ac0a46fSAndroid Build Coastguard Worker * is built without thread support) 206*3ac0a46fSAndroid Build Coastguard Worker */ 207*3ac0a46fSAndroid Build Coastguard Worker opj_thread_pool_t* opj_thread_pool_create(int num_threads); 208*3ac0a46fSAndroid Build Coastguard Worker 209*3ac0a46fSAndroid Build Coastguard Worker /** User function to execute in a thread 210*3ac0a46fSAndroid Build Coastguard Worker * @param user_data user data provided with opj_thread_create() 211*3ac0a46fSAndroid Build Coastguard Worker * @param tls handle to thread local storage 212*3ac0a46fSAndroid Build Coastguard Worker */ 213*3ac0a46fSAndroid Build Coastguard Worker typedef void (*opj_job_fn)(void* user_data, opj_tls_t* tls); 214*3ac0a46fSAndroid Build Coastguard Worker 215*3ac0a46fSAndroid Build Coastguard Worker 216*3ac0a46fSAndroid Build Coastguard Worker /** Submit a new job to be run by one of the thread in the thread pool. 217*3ac0a46fSAndroid Build Coastguard Worker * The job ( thread_fn, user_data ) will be added in the queue of jobs managed 218*3ac0a46fSAndroid Build Coastguard Worker * by the thread pool, and run by the first thread that is no longer busy. 219*3ac0a46fSAndroid Build Coastguard Worker * 220*3ac0a46fSAndroid Build Coastguard Worker * @param tp the thread pool handle. 221*3ac0a46fSAndroid Build Coastguard Worker * @param job_fn Function to run. Must not be NULL. 222*3ac0a46fSAndroid Build Coastguard Worker * @param user_data User data provided to thread_fn. 223*3ac0a46fSAndroid Build Coastguard Worker * @return OPJ_TRUE if the job was successfully submitted. 224*3ac0a46fSAndroid Build Coastguard Worker */ 225*3ac0a46fSAndroid Build Coastguard Worker OPJ_BOOL opj_thread_pool_submit_job(opj_thread_pool_t* tp, opj_job_fn job_fn, 226*3ac0a46fSAndroid Build Coastguard Worker void* user_data); 227*3ac0a46fSAndroid Build Coastguard Worker 228*3ac0a46fSAndroid Build Coastguard Worker /** Wait that no more than max_remaining_jobs jobs are remaining in the queue of 229*3ac0a46fSAndroid Build Coastguard Worker * the thread pool. The aim of this function is to avoid submitting too many 230*3ac0a46fSAndroid Build Coastguard Worker * jobs while the thread pool cannot cope fast enough with them, which would 231*3ac0a46fSAndroid Build Coastguard Worker * result potentially in out-of-memory situations with too many job descriptions 232*3ac0a46fSAndroid Build Coastguard Worker * being queued. 233*3ac0a46fSAndroid Build Coastguard Worker * 234*3ac0a46fSAndroid Build Coastguard Worker * @param tp the thread pool handle 235*3ac0a46fSAndroid Build Coastguard Worker * @param max_remaining_jobs maximum number of jobs allowed to be queued without waiting. 236*3ac0a46fSAndroid Build Coastguard Worker */ 237*3ac0a46fSAndroid Build Coastguard Worker void opj_thread_pool_wait_completion(opj_thread_pool_t* tp, 238*3ac0a46fSAndroid Build Coastguard Worker int max_remaining_jobs); 239*3ac0a46fSAndroid Build Coastguard Worker 240*3ac0a46fSAndroid Build Coastguard Worker /** Return the number of threads associated with the thread pool. 241*3ac0a46fSAndroid Build Coastguard Worker * 242*3ac0a46fSAndroid Build Coastguard Worker * @param tp the thread pool handle. 243*3ac0a46fSAndroid Build Coastguard Worker * @return number of threads associated with the thread pool. 244*3ac0a46fSAndroid Build Coastguard Worker */ 245*3ac0a46fSAndroid Build Coastguard Worker int opj_thread_pool_get_thread_count(opj_thread_pool_t* tp); 246*3ac0a46fSAndroid Build Coastguard Worker 247*3ac0a46fSAndroid Build Coastguard Worker /** Destroy a thread pool. 248*3ac0a46fSAndroid Build Coastguard Worker * @param tp the thread pool handle. 249*3ac0a46fSAndroid Build Coastguard Worker */ 250*3ac0a46fSAndroid Build Coastguard Worker void opj_thread_pool_destroy(opj_thread_pool_t* tp); 251*3ac0a46fSAndroid Build Coastguard Worker 252*3ac0a46fSAndroid Build Coastguard Worker /*@}*/ 253*3ac0a46fSAndroid Build Coastguard Worker 254*3ac0a46fSAndroid Build Coastguard Worker /*@}*/ 255*3ac0a46fSAndroid Build Coastguard Worker 256*3ac0a46fSAndroid Build Coastguard Worker #endif /* THREAD_H */ 257