pthread.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Code for simulating pthreads API on Windows. This is Git-specific,
  3. * but it is enough for Numexpr needs too.
  4. *
  5. * Copyright (C) 2009 Andrzej K. Haczewski <ahaczewski@gmail.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. * DISCLAIMER: The implementation is Git-specific, it is subset of original
  26. * Pthreads API, without lots of other features that Git doesn't use.
  27. * Git also makes sure that the passed arguments are valid, so there's
  28. * no need for double-checking.
  29. */
  30. #ifndef PTHREAD_H
  31. #define PTHREAD_H
  32. #ifndef WIN32_LEAN_AND_MEAN
  33. #define WIN32_LEAN_AND_MEAN
  34. #endif
  35. #include <windows.h>
  36. /*
  37. * Defines that adapt Windows API threads to pthreads API
  38. */
  39. #define pthread_mutex_t CRITICAL_SECTION
  40. #define pthread_mutex_init(a,b) InitializeCriticalSection((a))
  41. #define pthread_mutex_destroy(a) DeleteCriticalSection((a))
  42. #define pthread_mutex_lock EnterCriticalSection
  43. #define pthread_mutex_unlock LeaveCriticalSection
  44. /*
  45. * Implement simple condition variable for Windows threads, based on ACE
  46. * implementation.
  47. *
  48. * See original implementation: http://bit.ly/1vkDjo
  49. * ACE homepage: http://www.cse.wustl.edu/~schmidt/ACE.html
  50. * See also: http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
  51. */
  52. typedef struct {
  53. LONG waiters;
  54. int was_broadcast;
  55. CRITICAL_SECTION waiters_lock;
  56. HANDLE sema;
  57. HANDLE continue_broadcast;
  58. } pthread_cond_t;
  59. extern int pthread_cond_init(pthread_cond_t *cond, const void *unused);
  60. extern int pthread_cond_destroy(pthread_cond_t *cond);
  61. extern int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex);
  62. extern int pthread_cond_signal(pthread_cond_t *cond);
  63. extern int pthread_cond_broadcast(pthread_cond_t *cond);
  64. /*
  65. * Simple thread creation implementation using pthread API
  66. */
  67. typedef struct {
  68. HANDLE handle;
  69. void *(*start_routine)(void*);
  70. void *arg;
  71. } pthread_t;
  72. extern int pthread_create(pthread_t *thread, const void *unused,
  73. void *(*start_routine)(void*), void *arg);
  74. /*
  75. * To avoid the need of copying a struct, we use small macro wrapper to pass
  76. * pointer to win32_pthread_join instead.
  77. */
  78. #define pthread_join(a, b) win32_pthread_join(&(a), (b))
  79. extern int win32_pthread_join(pthread_t *thread, void **value_ptr);
  80. #endif /* PTHREAD_H */