xref: /trunk/main/sal/osl/w32/semaphor.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "system.h"
29 
30 #include <osl/diagnose.h>
31 #include <osl/semaphor.h>
32 
33 /*
34     Implemetation notes:
35     The void* represented by oslSemaphore is used
36     to store a WIN32 HANDLE.
37 */
38 
39 
40 /*****************************************************************************/
41 /* osl_createSemaphore */
42 /*****************************************************************************/
43 oslSemaphore SAL_CALL osl_createSemaphore(sal_uInt32 initialCount)
44 {
45     oslSemaphore Semaphore;
46 
47     Semaphore= CreateSemaphore(0, initialCount, INT_MAX, 0);
48 
49     /* create failed? */
50     if((HANDLE)Semaphore == INVALID_HANDLE_VALUE)
51     {
52         Semaphore= 0;
53     }
54 
55     return Semaphore;
56 }
57 
58 /*****************************************************************************/
59 /* osl_destroySemaphore */
60 /*****************************************************************************/
61 void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore)
62 {
63 
64 
65     if(Semaphore != 0)
66     {
67         CloseHandle((HANDLE)Semaphore);
68     }
69 
70 }
71 
72 /*****************************************************************************/
73 /* osl_acquireSemaphore */
74 /*****************************************************************************/
75 sal_Bool SAL_CALL osl_acquireSemaphore(oslSemaphore Semaphore)
76 {
77     OSL_ASSERT(Semaphore != 0);
78 
79     switch ( WaitForSingleObject( (HANDLE)Semaphore, INFINITE ) )
80     {
81         case WAIT_OBJECT_0:
82             return sal_True;
83 
84         default:
85             return (sal_False);
86     }
87 }
88 
89 /*****************************************************************************/
90 /* osl_tryToAcquireSemaphore */
91 /*****************************************************************************/
92 sal_Bool SAL_CALL osl_tryToAcquireSemaphore(oslSemaphore Semaphore)
93 {
94     OSL_ASSERT(Semaphore != 0);
95     return (sal_Bool)(WaitForSingleObject((HANDLE)Semaphore, 0) == WAIT_OBJECT_0);
96 }
97 
98 
99 /*****************************************************************************/
100 /* osl_releaseSemaphore */
101 /*****************************************************************************/
102 sal_Bool SAL_CALL osl_releaseSemaphore(oslSemaphore Semaphore)
103 {
104     OSL_ASSERT(Semaphore != 0);
105 
106     /* increase count by one, not interested in previous count */
107     return (sal_Bool)(ReleaseSemaphore((HANDLE)Semaphore, 1, NULL) != FALSE);
108 }
109 
110 
111 
112