Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  * Version:  6.5.2
  4.  *
  5.  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a
  8.  * copy of this software and associated documentation files (the "Software"),
  9.  * to deal in the Software without restriction, including without limitation
  10.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11.  * and/or sell copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included
  15.  * in all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  21.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25.  
  26. /*
  27.  * Thread support for gl dispatch.
  28.  *
  29.  * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
  30.  *                and Christoph Poliwoda (poliwoda@volumegraphics.com)
  31.  * Revised by Keith Whitwell
  32.  * Adapted for new gl dispatcher by Brian Paul
  33.  * Modified for use in mapi by Chia-I Wu
  34.  */
  35.  
  36. /*
  37.  * If this file is accidentally included by a non-threaded build,
  38.  * it should not cause the build to fail, or otherwise cause problems.
  39.  * In general, it should only be included when needed however.
  40.  */
  41.  
  42. #ifndef _U_THREAD_H_
  43. #define _U_THREAD_H_
  44.  
  45. #include "u_compiler.h"
  46.  
  47.  
  48. /*
  49.  * POSIX threads. This should be your choice in the Unix world
  50.  * whenever possible.  When building with POSIX threads, be sure
  51.  * to enable any compiler flags which will cause the MT-safe
  52.  * libc (if one exists) to be used when linking, as well as any
  53.  * header macros for MT-safe errno, etc.  For Solaris, this is the -mt
  54.  * compiler flag.  On Solaris with gcc, use -D_REENTRANT to enable
  55.  * proper compiling for MT-safe libc etc.
  56.  */
  57. #if defined(PTHREADS)
  58. #include <pthread.h> /* POSIX threads headers */
  59.  
  60. struct u_tsd {
  61.    pthread_key_t key;
  62.    int initMagic;
  63. };
  64.  
  65. typedef pthread_mutex_t u_mutex;
  66.  
  67. #define u_mutex_declare_static(name) \
  68.    static u_mutex name = PTHREAD_MUTEX_INITIALIZER
  69.  
  70. #define u_mutex_init(name)    pthread_mutex_init(&(name), NULL)
  71. #define u_mutex_destroy(name) pthread_mutex_destroy(&(name))
  72. #define u_mutex_lock(name)    (void) pthread_mutex_lock(&(name))
  73. #define u_mutex_unlock(name)  (void) pthread_mutex_unlock(&(name))
  74.  
  75. #endif /* PTHREADS */
  76.  
  77.  
  78. /*
  79.  * Windows threads. Should work with Windows NT and 95.
  80.  * IMPORTANT: Link with multithreaded runtime library when THREADS are
  81.  * used!
  82.  */
  83. #undef WIN32
  84.  
  85. #ifdef WIN32
  86. #include <windows.h>
  87.  
  88. struct u_tsd {
  89.    DWORD key;
  90.    int   initMagic;
  91. };
  92.  
  93. typedef CRITICAL_SECTION u_mutex;
  94.  
  95. /* http://locklessinc.com/articles/pthreads_on_windows/ */
  96. #define u_mutex_declare_static(name) \
  97.    /* static */ u_mutex name = {(void*)-1, -1, 0, 0, 0, 0}
  98.  
  99. #define u_mutex_init(name)    InitializeCriticalSection(&name)
  100. #define u_mutex_destroy(name) DeleteCriticalSection(&name)
  101. #define u_mutex_lock(name)    EnterCriticalSection(&name)
  102. #define u_mutex_unlock(name)  LeaveCriticalSection(&name)
  103.  
  104. #endif /* WIN32 */
  105.  
  106.  
  107. /*
  108.  * BeOS threads. R5.x required.
  109.  */
  110. #ifdef BEOS_THREADS
  111.  
  112. /* Problem with OS.h and this file on haiku */
  113. #ifndef __HAIKU__
  114. #include <kernel/OS.h>
  115. #endif
  116.  
  117. #include <support/TLS.h>
  118.  
  119. /* The only two typedefs required here
  120.  * this is cause of the OS.h problem
  121.  */
  122. #ifdef __HAIKU__
  123. typedef int32 thread_id;
  124. typedef int32 sem_id;
  125. #endif
  126.  
  127. struct u_tsd {
  128.    int32        key;
  129.    int          initMagic;
  130. };
  131.  
  132. /* Use Benaphore, aka speeder semaphore */
  133. typedef struct {
  134.     int32   lock;
  135.     sem_id  sem;
  136. } benaphore;
  137. typedef benaphore u_mutex;
  138.  
  139. #define u_mutex_declare_static(name) \
  140.    static u_mutex name = { 0, 0 }
  141.  
  142. #define u_mutex_init(name) \
  143.    name.sem = create_sem(0, #name"_benaphore"), \
  144.    name.lock = 0
  145.  
  146. #define u_mutex_destroy(name) \
  147.    delete_sem(name.sem), \
  148.    name.lock = 0
  149.  
  150. #define u_mutex_lock(name) \
  151.    if (name.sem == 0) \
  152.       u_mutex_init(name); \
  153.    if (atomic_add(&(name.lock), 1) >= 1) \
  154.       acquire_sem(name.sem)
  155.  
  156. #define u_mutex_unlock(name) \
  157.    if (atomic_add(&(name.lock), -1) > 1) \
  158.       release_sem(name.sem)
  159.  
  160. #endif /* BEOS_THREADS */
  161.  
  162.  
  163. /*
  164.  * THREADS not defined
  165.  */
  166. #ifndef THREADS
  167.  
  168. struct u_tsd {
  169.    int initMagic;
  170. };
  171.  
  172. typedef unsigned u_mutex;
  173.  
  174. #define u_mutex_declare_static(name)   static u_mutex name = 0
  175. #define u_mutex_init(name)             (void) name
  176. #define u_mutex_destroy(name)          (void) name
  177. #define u_mutex_lock(name)             (void) name
  178. #define u_mutex_unlock(name)           (void) name
  179.  
  180. #endif /* THREADS */
  181.  
  182.  
  183. unsigned long
  184. u_thread_self(void);
  185.  
  186. void
  187. u_tsd_init(struct u_tsd *tsd);
  188.  
  189. void
  190. u_tsd_destroy(struct u_tsd *tsd); /* WIN32 only */
  191.  
  192. void *
  193. u_tsd_get(struct u_tsd *tsd);
  194.  
  195. void
  196. u_tsd_set(struct u_tsd *tsd, void *ptr);
  197.  
  198. #endif /* _U_THREAD_H_ */
  199.