Bump for 3.6-28
[LibreOffice.git] / sal / osl / unx / semaphor.c
blobb38113b34fd4191815633db4484fbc79fe3d6094
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 ************************************************************************/
30 #include "system.h"
32 #include <osl/semaphor.h>
33 #include <osl/diagnose.h>
35 /* This is the (default) POSIX thread-local semaphore variant */
38 Implemetation notes:
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)
49 int ret = 0;
50 oslSemaphore Semaphore;
52 Semaphore= malloc(sizeof(sem_t));
54 OSL_ASSERT(Semaphore); /* ptr valid? */
56 if ( Semaphore == 0 )
58 return 0;
61 /* unnamed semaphore, not shared between processes */
63 ret= sem_init((sem_t*)Semaphore, 0, initialCount);
65 /* create failed? */
66 if (ret != 0)
68 OSL_TRACE("osl_createSemaphore failed. Errno: %d; %s\n",
69 errno,
70 strerror(errno));
72 free(Semaphore);
73 Semaphore = NULL;
76 return Semaphore;
79 /*****************************************************************************/
80 /* osl_destroySemaphore */
81 /*****************************************************************************/
82 void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore)
84 if(Semaphore) /* ptr valid? */
86 sem_destroy((sem_t*)Semaphore);
87 free(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);
103 return sal_False;
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);
117 return sal_False;
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);
132 return sal_False;
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */