1/*
2
3threads.c
4
5gSOAP XML Web services tools
6Copyright (C) 2000-2005, Robert van Engelen, Genivia Inc., All Rights Reserved.
7This part of the software is released under one of the following licenses:
8GPL, the gSOAP public license, or Genivia's license for commercial use.
9--------------------------------------------------------------------------------
10gSOAP public license.
11
12The contents of this file are subject to the gSOAP Public License Version 1.3
13(the "License"); you may not use this file except in compliance with the
14License. You may obtain a copy of the License at
15http://www.cs.fsu.edu/~engelen/soaplicense.html
16Software distributed under the License is distributed on an "AS IS" basis,
17WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
18for the specific language governing rights and limitations under the License.
19
20The Initial Developer of the Original Code is Robert A. van Engelen.
21Copyright (C) 2000-2005, Robert van Engelen, Genivia Inc., All Rights Reserved.
22--------------------------------------------------------------------------------
23GPL license.
24
25This program is free software; you can redistribute it and/or modify it under
26the terms of the GNU General Public License as published by the Free Software
27Foundation; either version 2 of the License, or (at your option) any later
28version.
29
30This program is distributed in the hope that it will be useful, but WITHOUT ANY
31WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
32PARTICULAR PURPOSE. See the GNU General Public License for more details.
33
34You should have received a copy of the GNU General Public License along with
35this program; if not, write to the Free Software Foundation, Inc., 59 Temple
36Place, Suite 330, Boston, MA 02111-1307 USA
37
38Author contact information:
39engelen@genivia.com / engelen@acm.org
40
41This program is released under the GPL with the additional exemption that
42compiling, linking, and/or using OpenSSL is allowed.
43--------------------------------------------------------------------------------
44A commercial use license is available from Genivia, Inc., contact@genivia.com
45--------------------------------------------------------------------------------
46*/
47
48#include "threads.h"
49
50/******************************************************************************\
51 *
52 *	Emulation of POSIX condition variables for WIN32
53 *
54\******************************************************************************/
55
56#ifdef WIN32
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62int emulate_pthread_cond_init(COND_TYPE *cv)
63{
64  cv->waiters_count_ = 0;
65  cv->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
66
67  return 0;
68}
69
70int emulate_pthread_cond_destroy(COND_TYPE *cv)
71{
72  CloseHandle(cv->signal_event_);
73
74  return 0;
75}
76
77int emulate_pthread_cond_signal(COND_TYPE *cv)
78{
79  int have_waiters;
80
81  EnterCriticalSection(&cv->waiters_count_lock_);
82  have_waiters = cv->waiters_count_ > 0;
83  LeaveCriticalSection(&cv->waiters_count_lock_);
84
85  if (have_waiters)
86    return SetEvent(cv->signal_event_) == 0;
87
88  return 0;
89}
90
91int emulate_pthread_cond_wait(COND_TYPE *cv, MUTEX_TYPE *cs)
92{
93  int result;
94
95  EnterCriticalSection(&cv->waiters_count_lock_);
96  cv->waiters_count_++;
97  LeaveCriticalSection(&cv->waiters_count_lock_);
98
99  LeaveCriticalSection(cs);
100
101  result = (WaitForSingleObject(cv->signal_event_, INFINITE) == WAIT_FAILED);
102
103  EnterCriticalSection(&cv->waiters_count_lock_);
104  cv->waiters_count_--;
105  LeaveCriticalSection(&cv->waiters_count_lock_);
106
107  EnterCriticalSection(cs, INFINITE);
108
109  return result;
110}
111
112#ifdef __cplusplus
113}
114#endif
115
116#endif
117