2 * WinPR: Windows Portable Runtime
3 * Synchronization Functions
5 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <winpr/crt.h>
29 #include <winpr/synch.h>
32 #include "../thread/thread.h"
33 #include <winpr/thread.h>
37 * WaitForSingleObjectEx
38 * WaitForMultipleObjectsEx
44 #include "../handle/handle.h"
46 DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
51 if (!winpr_Handle_GetInfo(hHandle, &Type, &Object))
54 if (Type == HANDLE_TYPE_THREAD)
58 void* thread_status = NULL;
60 if (dwMilliseconds != INFINITE)
61 fprintf(stderr, "WaitForSingleObject: timeout not implemented for thread wait\n");
63 thread = (WINPR_THREAD*) Object;
65 status = pthread_join(thread->thread, &thread_status);
68 fprintf(stderr, "WaitForSingleObject: pthread_join failure: %d\n", status);
71 thread->dwExitCode = ((DWORD) (size_t) thread_status);
73 else if (Type == HANDLE_TYPE_MUTEX)
77 mutex = (WINPR_MUTEX*) Object;
79 if (dwMilliseconds != INFINITE)
80 fprintf(stderr, "WaitForSingleObject: timeout not implemented for mutex wait\n");
82 pthread_mutex_lock(&mutex->mutex);
84 else if (Type == HANDLE_TYPE_EVENT)
89 struct timeval timeout;
91 event = (WINPR_EVENT*) Object;
94 FD_SET(event->pipe_fd[0], &rfds);
95 ZeroMemory(&timeout, sizeof(timeout));
97 if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0))
99 timeout.tv_usec = dwMilliseconds * 1000;
102 status = select(event->pipe_fd[0] + 1, &rfds, NULL, NULL,
103 (dwMilliseconds == INFINITE) ? NULL : &timeout);
111 else if (Type == HANDLE_TYPE_SEMAPHORE)
113 WINPR_SEMAPHORE* semaphore;
115 semaphore = (WINPR_SEMAPHORE*) Object;
117 #ifdef WINPR_PIPE_SEMAPHORE
118 if (semaphore->pipe_fd[0] != -1)
123 struct timeval timeout;
126 FD_SET(semaphore->pipe_fd[0], &rfds);
127 ZeroMemory(&timeout, sizeof(timeout));
129 if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0))
131 timeout.tv_usec = dwMilliseconds * 1000;
134 status = select(semaphore->pipe_fd[0] + 1, &rfds, 0, 0,
135 (dwMilliseconds == INFINITE) ? NULL : &timeout);
143 length = read(semaphore->pipe_fd[0], &length, 1);
150 #if defined __APPLE__
151 semaphore_wait(*((winpr_sem_t*) semaphore->sem));
153 sem_wait((winpr_sem_t*) semaphore->sem);
160 fprintf(stderr, "WaitForSingleObject: unknown handle type %lu\n", Type);
163 return WAIT_OBJECT_0;
166 DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable)
168 return WAIT_OBJECT_0;
171 DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds)
180 struct timeval timeout;
186 ZeroMemory(&timeout, sizeof(timeout));
189 fprintf(stderr, "WaitForMultipleObjects: bWaitAll not yet implemented\n");
191 for (index = 0; index < nCount; index++)
193 if (!winpr_Handle_GetInfo(lpHandles[index], &Type, &Object))
196 if (Type == HANDLE_TYPE_EVENT)
198 fd = ((WINPR_EVENT*) Object)->pipe_fd[0];
200 else if (Type == HANDLE_TYPE_SEMAPHORE)
202 #ifdef WINPR_PIPE_SEMAPHORE
203 fd = ((WINPR_SEMAPHORE*) Object)->pipe_fd[0];
219 if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0))
221 timeout.tv_usec = dwMilliseconds * 1000;
224 status = select(maxfd + 1, &fds, 0, 0,
225 (dwMilliseconds == INFINITE) ? NULL : &timeout);
233 for (index = 0; index < nCount; index++)
235 winpr_Handle_GetInfo(lpHandles[index], &Type, &Object);
237 if (Type == HANDLE_TYPE_EVENT)
238 fd = ((WINPR_EVENT*) Object)->pipe_fd[0];
239 else if (Type == HANDLE_TYPE_SEMAPHORE)
240 fd = ((WINPR_SEMAPHORE*) Object)->pipe_fd[0];
242 if (FD_ISSET(fd, &fds))
244 if (Type == HANDLE_TYPE_SEMAPHORE)
246 int length = read(fd, &length, 1);
252 return (WAIT_OBJECT_0 + index);
259 DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds, BOOL bAlertable)
264 DWORD SignalObjectAndWait(HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, DWORD dwMilliseconds, BOOL bAlertable)