synch.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * WinPR: Windows Portable Runtime
  3. * Synchronization Functions
  4. *
  5. * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifndef WINPR_SYNCH_PRIVATE_H
  20. #define WINPR_SYNCH_PRIVATE_H
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include <winpr/platform.h>
  25. #include <winpr/synch.h>
  26. #include "../handle/handle.h"
  27. #ifndef _WIN32
  28. #define WINPR_PIPE_SEMAPHORE 1
  29. #if defined __APPLE__
  30. #include <pthread.h>
  31. #include <sys/time.h>
  32. #include <semaphore.h>
  33. #include <mach/mach.h>
  34. #include <mach/semaphore.h>
  35. #include <mach/task.h>
  36. #define winpr_sem_t semaphore_t
  37. #else
  38. #include <pthread.h>
  39. #include <semaphore.h>
  40. #define winpr_sem_t sem_t
  41. #endif
  42. struct winpr_mutex
  43. {
  44. WINPR_HANDLE_DEF();
  45. char* name;
  46. pthread_mutex_t mutex;
  47. };
  48. typedef struct winpr_mutex WINPR_MUTEX;
  49. struct winpr_semaphore
  50. {
  51. WINPR_HANDLE_DEF();
  52. int pipe_fd[2];
  53. winpr_sem_t* sem;
  54. };
  55. typedef struct winpr_semaphore WINPR_SEMAPHORE;
  56. struct winpr_event
  57. {
  58. WINPR_HANDLE_DEF();
  59. int pipe_fd[2];
  60. BOOL bAttached;
  61. BOOL bManualReset;
  62. char* name;
  63. };
  64. typedef struct winpr_event WINPR_EVENT;
  65. #ifdef HAVE_SYS_TIMERFD_H
  66. #include <stdio.h>
  67. #include <unistd.h>
  68. #include <fcntl.h>
  69. #include <sys/timerfd.h>
  70. #endif
  71. #if defined(__APPLE__)
  72. #include <dispatch/dispatch.h>
  73. #endif
  74. struct winpr_timer
  75. {
  76. WINPR_HANDLE_DEF();
  77. int fd;
  78. BOOL bInit;
  79. LONG lPeriod;
  80. BOOL bManualReset;
  81. PTIMERAPCROUTINE pfnCompletionRoutine;
  82. LPVOID lpArgToCompletionRoutine;
  83. #ifdef WITH_POSIX_TIMER
  84. timer_t tid;
  85. struct itimerspec timeout;
  86. #endif
  87. #if defined(__APPLE__)
  88. dispatch_queue_t queue;
  89. dispatch_source_t source;
  90. int pipe[2];
  91. BOOL running;
  92. #endif
  93. char* name;
  94. };
  95. typedef struct winpr_timer WINPR_TIMER;
  96. typedef struct winpr_timer_queue_timer WINPR_TIMER_QUEUE_TIMER;
  97. struct winpr_timer_queue
  98. {
  99. WINPR_HANDLE_DEF();
  100. pthread_t thread;
  101. pthread_attr_t attr;
  102. pthread_mutex_t mutex;
  103. pthread_cond_t cond;
  104. pthread_mutex_t cond_mutex;
  105. struct sched_param param;
  106. BOOL bCancelled;
  107. WINPR_TIMER_QUEUE_TIMER* activeHead;
  108. WINPR_TIMER_QUEUE_TIMER* inactiveHead;
  109. };
  110. typedef struct winpr_timer_queue WINPR_TIMER_QUEUE;
  111. struct winpr_timer_queue_timer
  112. {
  113. WINPR_HANDLE_DEF();
  114. ULONG Flags;
  115. DWORD DueTime;
  116. DWORD Period;
  117. PVOID Parameter;
  118. WAITORTIMERCALLBACK Callback;
  119. int FireCount;
  120. struct timespec StartTime;
  121. struct timespec ExpirationTime;
  122. WINPR_TIMER_QUEUE* timerQueue;
  123. WINPR_TIMER_QUEUE_TIMER* next;
  124. };
  125. #endif
  126. #endif /* WINPR_SYNCH_PRIVATE_H */