Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "system.h"
31 : :
32 : : #include <osl/semaphor.h>
33 : : #include <osl/diagnose.h>
34 : :
35 : : /* This is the (default) POSIX thread-local semaphore variant */
36 : :
37 : : /*
38 : : Implemetation notes:
39 : : The void* represented by oslSemaphore is used
40 : : as a pointer to an sem_t struct
41 : : */
42 : :
43 : : /*****************************************************************************/
44 : : /* osl_createSemaphore */
45 : : /*****************************************************************************/
46 : :
47 : 0 : oslSemaphore SAL_CALL osl_createSemaphore(sal_uInt32 initialCount)
48 : : {
49 : 0 : int ret = 0;
50 : : oslSemaphore Semaphore;
51 : :
52 : 0 : Semaphore= malloc(sizeof(sem_t));
53 : :
54 : : OSL_ASSERT(Semaphore); /* ptr valid? */
55 : :
56 [ # # ]: 0 : if ( Semaphore == 0 )
57 : : {
58 : 0 : return 0;
59 : : }
60 : :
61 : : /* unnamed semaphore, not shared between processes */
62 : :
63 : 0 : ret= sem_init((sem_t*)Semaphore, 0, initialCount);
64 : :
65 : : /* create failed? */
66 [ # # ]: 0 : if (ret != 0)
67 : : {
68 : : OSL_TRACE("osl_createSemaphore failed. Errno: %d; %s\n",
69 : : errno,
70 : : strerror(errno));
71 : :
72 : 0 : free(Semaphore);
73 : 0 : Semaphore = NULL;
74 : : }
75 : :
76 : 0 : return Semaphore;
77 : : }
78 : :
79 : : /*****************************************************************************/
80 : : /* osl_destroySemaphore */
81 : : /*****************************************************************************/
82 : 0 : void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore)
83 : : {
84 [ # # ]: 0 : if(Semaphore) /* ptr valid? */
85 : : {
86 : 0 : sem_destroy((sem_t*)Semaphore);
87 : 0 : free(Semaphore);
88 : : }
89 : 0 : }
90 : :
91 : : /*****************************************************************************/
92 : : /* osl_acquireSemaphore */
93 : : /*****************************************************************************/
94 : 0 : sal_Bool SAL_CALL osl_acquireSemaphore(oslSemaphore Semaphore) {
95 : :
96 : : OSL_ASSERT(Semaphore != 0); /* abort in debug mode */
97 : :
98 [ # # ]: 0 : if (Semaphore != 0) /* be tolerant in release mode */
99 : : {
100 : 0 : return (sem_wait((sem_t*)Semaphore) == 0);
101 : : }
102 : :
103 : 0 : return sal_False;
104 : : }
105 : :
106 : : /*****************************************************************************/
107 : : /* osl_tryToAcquireSemaphore */
108 : : /*****************************************************************************/
109 : 0 : sal_Bool SAL_CALL osl_tryToAcquireSemaphore(oslSemaphore Semaphore) {
110 : :
111 : : OSL_ASSERT(Semaphore != 0); /* abort in debug mode */
112 [ # # ]: 0 : if (Semaphore != 0) /* be tolerant in release mode */
113 : : {
114 : 0 : return (sem_trywait((sem_t*)Semaphore) == 0);
115 : : }
116 : :
117 : 0 : return sal_False;
118 : : }
119 : :
120 : : /*****************************************************************************/
121 : : /* osl_releaseSemaphore */
122 : : /*****************************************************************************/
123 : 0 : sal_Bool SAL_CALL osl_releaseSemaphore(oslSemaphore Semaphore) {
124 : :
125 : : OSL_ASSERT(Semaphore != 0); /* abort in debug mode */
126 : :
127 [ # # ]: 0 : if (Semaphore != 0) /* be tolerant in release mode */
128 : : {
129 : 0 : return (sem_post((sem_t*)Semaphore) == 0);
130 : : }
131 : :
132 : 0 : return sal_False;
133 : : }
134 : :
135 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|