1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
32 #include <osl/semaphor.h>
33 #include <osl/diagnose.h>
35 /* This is the (default) POSIX thread-local semaphore variant */
39 The void* represented by oslSemaphore is used
40 as a pointer to an sem_t struct
43 /*****************************************************************************/
44 /* osl_createSemaphore */
45 /*****************************************************************************/
47 oslSemaphore SAL_CALL
osl_createSemaphore(sal_uInt32 initialCount
)
50 oslSemaphore Semaphore
;
52 Semaphore
= malloc(sizeof(sem_t
));
54 OSL_ASSERT(Semaphore
); /* ptr valid? */
61 /* unnamed semaphore, not shared between processes */
63 ret
= sem_init((sem_t
*)Semaphore
, 0, initialCount
);
68 OSL_TRACE("osl_createSemaphore failed. Errno: %d; %s\n",
79 /*****************************************************************************/
80 /* osl_destroySemaphore */
81 /*****************************************************************************/
82 void SAL_CALL
osl_destroySemaphore(oslSemaphore Semaphore
)
84 if(Semaphore
) /* ptr valid? */
86 sem_destroy((sem_t
*)Semaphore
);
91 /*****************************************************************************/
92 /* osl_acquireSemaphore */
93 /*****************************************************************************/
94 sal_Bool SAL_CALL
osl_acquireSemaphore(oslSemaphore Semaphore
) {
96 OSL_ASSERT(Semaphore
!= 0); /* abort in debug mode */
98 if (Semaphore
!= 0) /* be tolerant in release mode */
100 return (sem_wait((sem_t
*)Semaphore
) == 0);
106 /*****************************************************************************/
107 /* osl_tryToAcquireSemaphore */
108 /*****************************************************************************/
109 sal_Bool SAL_CALL
osl_tryToAcquireSemaphore(oslSemaphore Semaphore
) {
111 OSL_ASSERT(Semaphore
!= 0); /* abort in debug mode */
112 if (Semaphore
!= 0) /* be tolerant in release mode */
114 return (sem_trywait((sem_t
*)Semaphore
) == 0);
120 /*****************************************************************************/
121 /* osl_releaseSemaphore */
122 /*****************************************************************************/
123 sal_Bool SAL_CALL
osl_releaseSemaphore(oslSemaphore Semaphore
) {
125 OSL_ASSERT(Semaphore
!= 0); /* abort in debug mode */
127 if (Semaphore
!= 0) /* be tolerant in release mode */
129 return (sem_post((sem_t
*)Semaphore
) == 0);
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */