00001 00002 // 00003 // Copyright (c) 2000-2003 Intel Corporation 00004 // All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // * Redistributions of source code must retain the above copyright notice, 00010 // this list of conditions and the following disclaimer. 00011 // * Redistributions in binary form must reproduce the above copyright notice, 00012 // this list of conditions and the following disclaimer in the documentation 00013 // and/or other materials provided with the distribution. 00014 // * Neither name of Intel Corporation nor the names of its contributors 00015 // may be used to endorse or promote products derived from this software 00016 // without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 00022 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00026 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00031 00032 #ifndef ITHREADH 00033 #define ITHREADH 00034 #ifdef __cplusplus 00035 extern "C" { 00036 #endif 00037 00038 00039 #ifdef DEBUG 00040 #define DEBUG_ONLY(x) x 00041 #else 00042 #define DEBUG_ONLY(x) 00043 #endif 00044 00045 #include <pthread.h> 00046 #ifndef WIN32 00047 #include <unistd.h> 00048 #endif 00049 00050 #if defined(__FreeBSD__) || defined(__NetBSD__) 00051 #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE 00052 #endif 00053 00054 #define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_FAST_NP 00055 #define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE_NP 00056 #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK_NP 00057 #define ITHREAD_CANCELED PTHREAD_CANCELED 00058 00059 00060 /*************************************************************************** 00061 * Name: ithread_t 00062 * 00063 * Description: 00064 * Thread handle. 00065 * typedef to pthread_t. 00066 * Internal Use Only. 00067 ***************************************************************************/ 00068 typedef pthread_t ithread_t; 00069 00070 /**************************************************************************** 00071 * Name: ithread_attr_t 00072 * 00073 * Description: 00074 * Thread attribute. 00075 * typedef to pthread_attr_t 00076 * Internal Use Only 00077 ***************************************************************************/ 00078 typedef pthread_attr_t ithread_attr_t; 00079 00080 00081 /**************************************************************************** 00082 * Name: start_routine 00083 * 00084 * Description: 00085 * Thread start routine 00086 * Internal Use Only. 00087 ***************************************************************************/ 00088 typedef void * (*start_routine) (void *arg); 00089 00090 00091 /**************************************************************************** 00092 * Name: ithread_cond_t 00093 * 00094 * Description: 00095 * condition variable. 00096 * typedef to pthread_cond_t 00097 * Internal Use Only. 00098 ***************************************************************************/ 00099 typedef pthread_cond_t ithread_cond_t; 00100 00101 00102 /**************************************************************************** 00103 * Name: ithread_mutexattr_t 00104 * 00105 * Description: 00106 * Mutex attribute. 00107 * typedef to pthread_mutexattr_t 00108 * Internal Use Only 00109 ***************************************************************************/ 00110 typedef pthread_mutexattr_t ithread_mutexattr_t; 00111 00112 00113 /**************************************************************************** 00114 * Name: ithread_mutex_t 00115 * 00116 * Description: 00117 * Mutex. 00118 * typedef to pthread_mutex_t 00119 * Internal Use Only. 00120 ***************************************************************************/ 00121 typedef pthread_mutex_t ithread_mutex_t; 00122 00123 00124 /**************************************************************************** 00125 * Name: ithread_condattr_t 00126 * 00127 * Description: 00128 * Condition attribute. 00129 * typedef to pthread_condattr_t 00130 * NOT USED 00131 * Internal Use Only 00132 ***************************************************************************/ 00133 typedef pthread_condattr_t ithread_condattr_t; 00134 00135 /**************************************************************************** 00136 * Function: ithread_mutexattr_init 00137 * 00138 * Description: 00139 * Initializes a mutex attribute variable. 00140 * Used to set the type of the mutex. 00141 * Parameters: 00142 * ithread_mutexattr_init * attr (must be valid non NULL pointer to 00143 * pthread_mutexattr_t) 00144 * Returns: 00145 * 0 on success, Nonzero on failure. 00146 * Always returns 0. 00147 * See man page for pthread_mutexattr_init 00148 ***************************************************************************/ 00149 00150 #define ithread_mutexattr_init pthread_mutexattr_init 00151 00152 /**************************************************************************** 00153 * Function: ithread_mutexattr_destroy 00154 * 00155 * Description: 00156 * Releases any resources held by the mutex attribute. 00157 * Currently there are no resources associated with the attribute 00158 * Parameters: 00159 * ithread_mutexattr_t * attr (must be valid non NULL pointer to 00160 * pthread_mutexattr_t) 00161 * Returns: 00162 * 0 on success, Nonzero on failure. 00163 * Always returns 0. 00164 * See man page for pthread_mutexattr_destroy 00165 ***************************************************************************/ 00166 #define ithread_mutexattr_destroy pthread_mutexattr_destroy 00167 00168 00169 /**************************************************************************** 00170 * Function: ithread_mutexattr_setkind_np 00171 * 00172 * Description: 00173 * Sets the mutex type in the attribute. 00174 * Valid types are: ITHREAD_MUTEX_FAST_NP 00175 * ITHREAD_MUTEX_RECURSIVE_NP 00176 * ITHREAD_MUTEX_ERRORCHECK_NP 00177 * 00178 * Parameters: 00179 * ithread_mutexattr_t * mutex (must be valid non NULL pointer to 00180 * ithread_mutexattr_t) 00181 * int kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP 00182 * or ITHREAD_MUTEX_ERRORCHECK_NP) 00183 * Returns: 00184 * 0 on success. Nonzero on failure. 00185 * Returns EINVAL if the kind is not supported. 00186 * See man page for pthread_mutexattr_setkind_np 00187 *****************************************************************************/ 00188 #define ithread_mutexattr_setkind_np pthread_mutexattr_setkind_np 00189 00190 00191 /**************************************************************************** 00192 * Function: ithread_mutexattr_getkind_np 00193 * 00194 * Description: 00195 * Gets the mutex type in the attribute. 00196 * Valid types are: ITHREAD_MUTEX_FAST_NP 00197 * ITHREAD_MUTEX_RECURSIVE_NP 00198 * ITHREAD_MUTEX_ERRORCHECK_NP 00199 * 00200 * Parameters: 00201 * ithread_mutexattr_t * mutex (must be valid non NULL pointer to 00202 * pthread_mutexattr_t) 00203 * int *kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP 00204 * or ITHREAD_MUTEX_ERRORCHECK_NP) 00205 * Returns: 00206 * 0 on success. Nonzero on failure. 00207 * Always returns 0. 00208 * See man page for pthread_mutexattr_getkind_np 00209 *****************************************************************************/ 00210 #define ithread_mutexattr_getkind_np pthread_mutexattr_getkind_np 00211 00212 00213 /**************************************************************************** 00214 * Function: ithread_mutex_init 00215 * 00216 * Description: 00217 * Initializes mutex. 00218 * Must be called before use. 00219 * 00220 * Parameters: 00221 * ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t) 00222 * const ithread_mutexattr_t * mutex_attr 00223 * Returns: 00224 * 0 on success, Nonzero on failure. 00225 * Always returns 0. 00226 * See man page for pthread_mutex_init 00227 *****************************************************************************/ 00228 #define ithread_mutex_init pthread_mutex_init 00229 00230 /**************************************************************************** 00231 * Function: ithread_mutex_lock 00232 * 00233 * Description: 00234 * Locks mutex. 00235 * Parameters: 00236 * ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t) 00237 * mutex must be initialized. 00238 * 00239 * Returns: 00240 * 0 on success, Nonzero on failure. 00241 * Always returns 0. 00242 * See man page for pthread_mutex_lock 00243 *****************************************************************************/ 00244 #define ithread_mutex_lock pthread_mutex_lock 00245 00246 00247 /**************************************************************************** 00248 * Function: ithread_mutex_unlock 00249 * 00250 * Description: 00251 * Unlocks mutex. 00252 * 00253 * Parameters: 00254 * ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t) 00255 * mutex must be initialized. 00256 * 00257 * Returns: 00258 * 0 on success, Nonzero on failure. 00259 * Always returns 0. 00260 * See man page for pthread_mutex_unlock 00261 *****************************************************************************/ 00262 #define ithread_mutex_unlock pthread_mutex_unlock 00263 00264 00265 /**************************************************************************** 00266 * Function: ithread_mutex_destroy 00267 * 00268 * Description: 00269 * Releases any resources held by the mutex. 00270 * Mutex can no longer be used after this call. 00271 * Mutex is only destroyed when there are no longer any threads waiting on it. 00272 * Mutex cannot be destroyed if it is locked. 00273 * Parameters: 00274 * ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t) 00275 * mutex must be initialized. 00276 * Returns: 00277 * 0 on success. Nonzero on failure. 00278 * Always returns 0. 00279 * See man page for pthread_mutex_destroy 00280 *****************************************************************************/ 00281 #define ithread_mutex_destroy pthread_mutex_destroy 00282 00283 00284 /**************************************************************************** 00285 * Function: ithread_cond_init 00286 * 00287 * Description: 00288 * Initializes condition variable. 00289 * Must be called before use. 00290 * Parameters: 00291 * ithread_cond_t * cond (must be valid non NULL pointer to pthread_cond_t) 00292 * const ithread_condattr_t * cond_attr (ignored) 00293 * Returns: 00294 * 0 on success, Nonzero on failure. 00295 * See man page for pthread_cond_init 00296 *****************************************************************************/ 00297 #define ithread_cond_init pthread_cond_init 00298 00299 00300 00301 /**************************************************************************** 00302 * Function: ithread_cond_signal 00303 * 00304 * Description: 00305 * Wakes up exactly one thread waiting on condition. 00306 * Associated mutex MUST be locked by thread before entering this call. 00307 * Parameters: 00308 * ithread_cond_t * cond (must be valid non NULL pointer to 00309 * ithread_cond_t) 00310 * cond must be initialized 00311 * Returns: 00312 * 0 on success, Nonzero on failure. 00313 * See man page for pthread_cond_signal 00314 *****************************************************************************/ 00315 #define ithread_cond_signal pthread_cond_signal 00316 00317 00318 /**************************************************************************** 00319 * Function: ithread_cond_broadcast 00320 * 00321 * Description: 00322 * Wakes up all threads waiting on condition. 00323 * Associated mutex MUST be locked by thread before entering this call. 00324 * Parameters: 00325 * ithread_cond_t * cond (must be valid non NULL pointer to 00326 * ithread_cond_t) 00327 * cond must be initialized 00328 * Returns: 00329 * 0 on success, Nonzero on failure. 00330 * See man page for pthread_cond_broadcast 00331 *****************************************************************************/ 00332 #define ithread_cond_broadcast pthread_cond_broadcast 00333 00334 00335 /**************************************************************************** 00336 * Function: ithread_cond_wait 00337 * 00338 * Description: 00339 * Atomically releases mutex and waits on condition. 00340 * Associated mutex MUST be locked by thread before entering this call. 00341 * Mutex is reacquired when call returns. 00342 * Parameters: 00343 * ithread_cond_t * cond (must be valid non NULL pointer to 00344 * ithread_cond_t) 00345 * cond must be initialized 00346 * ithread_mutex_t *mutex (must be valid non NULL pointer to 00347 * ithread_mutex_t) 00348 * Mutex must be locked. 00349 * Returns: 00350 * 0 on success, Nonzero on failure. 00351 * See man page for pthread_cond_wait 00352 *****************************************************************************/ 00353 #define ithread_cond_wait pthread_cond_wait 00354 00355 00356 /**************************************************************************** 00357 * Function: pthread_cond_timedwait 00358 * 00359 * Description: 00360 * Atomically releases the associated mutex and waits on the condition. 00361 * If the condition is not signaled in the specified time 00362 * than the 00363 * call times out and returns. 00364 * Associated mutex MUST be locked by thread before entering 00365 * this call. 00366 * Mutex is reacquired when call returns. 00367 * Parameters: 00368 * ithread_cond_t * cond (must be valid non NULL pointer to 00369 * ithread_cond_t) 00370 * cond must be initialized 00371 * ithread_mutex_t *mutex (must be valid non NULL pointer to 00372 * ithread_mutex_t) 00373 * Mutex must be locked. 00374 * const struct timespec *abstime (absolute time, measured 00375 * from Jan 1, 1970) 00376 * Returns: 00377 * 0 on success. ETIMEDOUT on timeout. Nonzero on failure. 00378 * See man page for pthread_cond_timedwait 00379 ***************************************************************************/ 00380 00381 #define ithread_cond_timedwait pthread_cond_timedwait 00382 00383 00384 /**************************************************************************** 00385 * Function: ithread_cond_destroy 00386 * 00387 * Description: 00388 * Releases any resources held by the condition variable. 00389 * Condition variable can no longer be used after this call. 00390 * Parameters: 00391 * ithread_cond_t * cond (must be valid non NULL pointer to 00392 * ithread_cond_t) 00393 * cond must be initialized. 00394 * Returns: 00395 * 0 on success. Nonzero on failure. 00396 * See man page for pthread_cond_destroy 00397 ***************************************************************************/ 00398 #define ithread_cond_destroy pthread_cond_destroy 00399 00400 00401 /**************************************************************************** 00402 * Function: ithread_create 00403 * 00404 * Description: 00405 * Creates a thread with the given start routine 00406 * and argument. 00407 * Parameters: 00408 * ithread_t * thread (must be valid non NULL pointer to pthread_t) 00409 * ithread_attr_t *attr, IGNORED 00410 * void * (start_routine) (void *arg) (start routine) 00411 * void * arg - argument. 00412 * Returns: 00413 * 0 on success. Nonzero on failure. 00414 * Returns EAGAIN if a new thread can not be created. 00415 * Returns EINVAL if there is a problem with the arguments. 00416 * See man page fore pthread_create 00417 ***************************************************************************/ 00418 #define ithread_create pthread_create 00419 00420 00421 /**************************************************************************** 00422 * Function: ithread_cancel 00423 * 00424 * Description: 00425 * Cancels a thread. 00426 * Parameters: 00427 * ithread_t * thread (must be valid non NULL pointer to ithread_t) 00428 * Returns: 00429 * 0 on success. Nonzero on failure. 00430 * See man page for pthread_cancel 00431 ***************************************************************************/ 00432 #define ithread_cancel pthread_cancel 00433 00434 00435 /**************************************************************************** 00436 * Function: ithread_exit 00437 * 00438 * Description: 00439 * Returns a return code from a thread. 00440 * Implicitly called when the start routine returns. 00441 * Parameters: 00442 * void * return_code return code to return 00443 * See man page for pthread_exit 00444 ***************************************************************************/ 00445 #define ithread_exit pthread_exit 00446 00447 /**************************************************************************** 00448 * Function: ithread_get_current_thread_id 00449 * 00450 * Description: 00451 * Returns the handle of the currently running thread. 00452 * Returns: 00453 * The handle of the currently running thread. 00454 * See man page for pthread_self 00455 ***************************************************************************/ 00456 #define ithread_get_current_thread_id pthread_self 00457 00458 00459 /**************************************************************************** 00460 * Function: ithread_self 00461 * 00462 * Description: 00463 * Returns the handle of the currently running thread. 00464 * Returns: 00465 * The handle of the currently running thread. 00466 * See man page for pthread_self 00467 ***************************************************************************/ 00468 #define ithread_self pthread_self 00469 00470 /**************************************************************************** 00471 * Function: ithread_detach 00472 * 00473 * Description: 00474 * Makes a thread's resources reclaimed immediately 00475 * after it finishes 00476 * execution. 00477 * Returns: 00478 * 0 on success, Nonzero on failure. 00479 * See man page for pthread_detach 00480 ***************************************************************************/ 00481 #define ithread_detach pthread_detach 00482 00483 /**************************************************************************** 00484 * Function: ithread_join 00485 * 00486 * Description: 00487 * Suspends the currently running thread until the 00488 * specified thread 00489 * has finished. 00490 * Returns the return code of the thread, or ITHREAD_CANCELED 00491 * if the thread has been canceled. 00492 * Parameters: 00493 * ithread_t *thread (valid non null thread identifier) 00494 * void ** return (space for return code) 00495 * Returns: 00496 * 0 on success, Nonzero on failure. 00497 * See man page for pthread_join 00498 ***************************************************************************/ 00499 #define ithread_join pthread_join 00500 00501 00502 00503 /**************************************************************************** 00504 * Function: isleep 00505 * 00506 * Description: 00507 * Suspends the currently running thread for the specified number 00508 * of seconds 00509 * Always returns 0. 00510 * Parameters: 00511 * unsigned int seconds - number of seconds to sleep. 00512 * Returns: 00513 * 0 on success, Nonzero on failure. 00514 * See man page for sleep (man 3 sleep) 00515 *****************************************************************************/ 00516 #define isleep sleep 00517 00518 /**************************************************************************** 00519 * Function: isleep 00520 * 00521 * Description: 00522 * Suspends the currently running thread for the specified number 00523 * of milliseconds 00524 * Always returns 0. 00525 * Parameters: 00526 * unsigned int milliseconds - number of milliseconds to sleep. 00527 * Returns: 00528 * 0 on success, Nonzero on failure. 00529 * See man page for sleep (man 3 sleep) 00530 *****************************************************************************/ 00531 #define imillisleep(x) usleep(1000*x) 00532 00533 #ifdef WIN32 00534 #ifndef UPNP_STATIC_LIB 00535 #ifdef LIBUPNP_EXPORTS 00536 // set up declspec for dll export to make functions visible to library users 00537 #define EXPORT_SPEC __declspec(dllexport) 00538 #else 00539 #define EXPORT_SPEC __declspec(dllimport) 00540 #endif 00541 #else 00542 #define EXPORT_SPEC 00543 #endif 00544 #else 00545 #define EXPORT_SPEC 00546 #endif 00547 00548 00549 //NK: Added for satisfying the gcc compiler 00550 EXPORT_SPEC int pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind); 00551 00552 #ifdef __cplusplus 00553 } 00554 #endif 00555 00556 #endif //ITHREADH
1.6.1