contrib/python-zstandard/zstd/common/threading.h
changeset 37495 b1fb341d8a61
parent 30895 c32454d69b85
child 43994 de7838053207
equal deleted inserted replaced
37494:1ce7a55b09d1 37495:b1fb341d8a61
     1 
       
     2 /**
     1 /**
     3  * Copyright (c) 2016 Tino Reichardt
     2  * Copyright (c) 2016 Tino Reichardt
     4  * All rights reserved.
     3  * All rights reserved.
     5  *
     4  *
     6  * This source code is licensed under the BSD-style license found in the
     5  * This source code is licensed under both the BSD-style license (found in the
     7  * LICENSE file in the root directory of this source tree. An additional grant
     6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
     8  * of patent rights can be found in the PATENTS file in the same directory.
     7  * in the COPYING file in the root directory of this source tree).
     9  *
     8  *
    10  * You can contact the author at:
     9  * You can contact the author at:
    11  * - zstdmt source repository: https://github.com/mcmilk/zstdmt
    10  * - zstdmt source repository: https://github.com/mcmilk/zstdmt
    12  */
    11  */
    13 
    12 
    36 
    35 
    37 #ifndef WIN32_LEAN_AND_MEAN
    36 #ifndef WIN32_LEAN_AND_MEAN
    38 #  define WIN32_LEAN_AND_MEAN
    37 #  define WIN32_LEAN_AND_MEAN
    39 #endif
    38 #endif
    40 
    39 
       
    40 #undef ERROR   /* reported already defined on VS 2015 (Rich Geldreich) */
    41 #include <windows.h>
    41 #include <windows.h>
       
    42 #undef ERROR
       
    43 #define ERROR(name) ZSTD_ERROR(name)
       
    44 
    42 
    45 
    43 /* mutex */
    46 /* mutex */
    44 #define pthread_mutex_t           CRITICAL_SECTION
    47 #define ZSTD_pthread_mutex_t           CRITICAL_SECTION
    45 #define pthread_mutex_init(a,b)   InitializeCriticalSection((a))
    48 #define ZSTD_pthread_mutex_init(a, b)  ((void)(b), InitializeCriticalSection((a)), 0)
    46 #define pthread_mutex_destroy(a)  DeleteCriticalSection((a))
    49 #define ZSTD_pthread_mutex_destroy(a)  DeleteCriticalSection((a))
    47 #define pthread_mutex_lock(a)     EnterCriticalSection((a))
    50 #define ZSTD_pthread_mutex_lock(a)     EnterCriticalSection((a))
    48 #define pthread_mutex_unlock(a)   LeaveCriticalSection((a))
    51 #define ZSTD_pthread_mutex_unlock(a)   LeaveCriticalSection((a))
    49 
    52 
    50 /* condition variable */
    53 /* condition variable */
    51 #define pthread_cond_t             CONDITION_VARIABLE
    54 #define ZSTD_pthread_cond_t             CONDITION_VARIABLE
    52 #define pthread_cond_init(a, b)    InitializeConditionVariable((a))
    55 #define ZSTD_pthread_cond_init(a, b)    ((void)(b), InitializeConditionVariable((a)), 0)
    53 #define pthread_cond_destroy(a)    /* No delete */
    56 #define ZSTD_pthread_cond_destroy(a)    ((void)(a))
    54 #define pthread_cond_wait(a, b)    SleepConditionVariableCS((a), (b), INFINITE)
    57 #define ZSTD_pthread_cond_wait(a, b)    SleepConditionVariableCS((a), (b), INFINITE)
    55 #define pthread_cond_signal(a)     WakeConditionVariable((a))
    58 #define ZSTD_pthread_cond_signal(a)     WakeConditionVariable((a))
    56 #define pthread_cond_broadcast(a)  WakeAllConditionVariable((a))
    59 #define ZSTD_pthread_cond_broadcast(a)  WakeAllConditionVariable((a))
    57 
    60 
    58 /* pthread_create() and pthread_join() */
    61 /* ZSTD_pthread_create() and ZSTD_pthread_join() */
    59 typedef struct {
    62 typedef struct {
    60     HANDLE handle;
    63     HANDLE handle;
    61     void* (*start_routine)(void*);
    64     void* (*start_routine)(void*);
    62     void* arg;
    65     void* arg;
    63 } pthread_t;
    66 } ZSTD_pthread_t;
    64 
    67 
    65 int pthread_create(pthread_t* thread, const void* unused,
    68 int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
    66                    void* (*start_routine) (void*), void* arg);
    69                    void* (*start_routine) (void*), void* arg);
    67 
    70 
    68 #define pthread_join(a, b) _pthread_join(&(a), (b))
    71 int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
    69 int _pthread_join(pthread_t* thread, void** value_ptr);
       
    70 
    72 
    71 /**
    73 /**
    72  * add here more wrappers as required
    74  * add here more wrappers as required
    73  */
    75  */
    74 
    76 
    75 
    77 
    76 #elif defined(ZSTD_MULTITHREAD)   /* posix assumed ; need a better detection mathod */
    78 #elif defined(ZSTD_MULTITHREAD)   /* posix assumed ; need a better detection method */
    77 /* ===   POSIX Systems   === */
    79 /* ===   POSIX Systems   === */
    78 #  include <pthread.h>
    80 #  include <pthread.h>
       
    81 
       
    82 #define ZSTD_pthread_mutex_t            pthread_mutex_t
       
    83 #define ZSTD_pthread_mutex_init(a, b)   pthread_mutex_init((a), (b))
       
    84 #define ZSTD_pthread_mutex_destroy(a)   pthread_mutex_destroy((a))
       
    85 #define ZSTD_pthread_mutex_lock(a)      pthread_mutex_lock((a))
       
    86 #define ZSTD_pthread_mutex_unlock(a)    pthread_mutex_unlock((a))
       
    87 
       
    88 #define ZSTD_pthread_cond_t             pthread_cond_t
       
    89 #define ZSTD_pthread_cond_init(a, b)    pthread_cond_init((a), (b))
       
    90 #define ZSTD_pthread_cond_destroy(a)    pthread_cond_destroy((a))
       
    91 #define ZSTD_pthread_cond_wait(a, b)    pthread_cond_wait((a), (b))
       
    92 #define ZSTD_pthread_cond_signal(a)     pthread_cond_signal((a))
       
    93 #define ZSTD_pthread_cond_broadcast(a)  pthread_cond_broadcast((a))
       
    94 
       
    95 #define ZSTD_pthread_t                  pthread_t
       
    96 #define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
       
    97 #define ZSTD_pthread_join(a, b)         pthread_join((a),(b))
    79 
    98 
    80 #else  /* ZSTD_MULTITHREAD not defined */
    99 #else  /* ZSTD_MULTITHREAD not defined */
    81 /* No multithreading support */
   100 /* No multithreading support */
    82 
   101 
    83 #define pthread_mutex_t int   /* #define rather than typedef, as sometimes pthread support is implicit, resulting in duplicated symbols */
   102 typedef int ZSTD_pthread_mutex_t;
    84 #define pthread_mutex_init(a,b)
   103 #define ZSTD_pthread_mutex_init(a, b)   ((void)(a), (void)(b), 0)
    85 #define pthread_mutex_destroy(a)
   104 #define ZSTD_pthread_mutex_destroy(a)   ((void)(a))
    86 #define pthread_mutex_lock(a)
   105 #define ZSTD_pthread_mutex_lock(a)      ((void)(a))
    87 #define pthread_mutex_unlock(a)
   106 #define ZSTD_pthread_mutex_unlock(a)    ((void)(a))
    88 
   107 
    89 #define pthread_cond_t int
   108 typedef int ZSTD_pthread_cond_t;
    90 #define pthread_cond_init(a,b)
   109 #define ZSTD_pthread_cond_init(a, b)    ((void)(a), (void)(b), 0)
    91 #define pthread_cond_destroy(a)
   110 #define ZSTD_pthread_cond_destroy(a)    ((void)(a))
    92 #define pthread_cond_wait(a,b)
   111 #define ZSTD_pthread_cond_wait(a, b)    ((void)(a), (void)(b))
    93 #define pthread_cond_signal(a)
   112 #define ZSTD_pthread_cond_signal(a)     ((void)(a))
    94 #define pthread_cond_broadcast(a)
   113 #define ZSTD_pthread_cond_broadcast(a)  ((void)(a))
    95 
   114 
    96 /* do not use pthread_t */
   115 /* do not use ZSTD_pthread_t */
    97 
   116 
    98 #endif /* ZSTD_MULTITHREAD */
   117 #endif /* ZSTD_MULTITHREAD */
    99 
   118 
   100 #if defined (__cplusplus)
   119 #if defined (__cplusplus)
   101 }
   120 }