Applied Wang Cong Patch, char pointer const fix.
[fmail.git] / src / posix-sem.cpp
blobd9d8a6e38605e4d02f03ba0b342e374a06df8610
1 /*
2 libfmail: Semaphore class
4 Copyright (C) 2007 Carlos Daniel Ruvalcaba Valenzuela <clsdaniel@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <string.h>
23 #include <malloc.h>
25 #include <semaphore.h>
26 #include <libfmail/semaphore.h>
28 struct _ossem{
29 sem_t sem;
32 /* TODO: Add support for shared semaphores */
33 Semaphore::Semaphore(int value, int shared, int key){
34 (void)shared;
35 (void)key;
36 os_sem = (OSSem*)malloc(sizeof(OSSem));
37 sem_init(&(os_sem->sem), 0, value);
39 Semaphore::~Semaphore(){
40 sem_destroy(&(os_sem->sem));
41 free(os_sem);
44 int Semaphore::TryWait(){
45 return sem_trywait(&(os_sem->sem));
48 int Semaphore::Wait(){
49 return sem_wait(&(os_sem->sem));
52 int Semaphore::Post(){
53 return sem_post(&(os_sem->sem));
56 int Semaphore::getValue(){
57 int ret;
58 sem_getvalue(&(os_sem->sem), &ret);
60 return ret;