Merge branch 'mob' of git+ssh://repo.or.cz/srv/git/fmail
[fmail.git] / src / posix-sem.cpp
blobc9f28428bf58b398fc7a131bb1816217c59bc3ed
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 Semaphore::Semaphore(int value, int shared, int key){
33 os_sem = (OSSem*)malloc(sizeof(OSSem));
34 sem_init(&(os_sem->sem), 0, value);
36 Semaphore::~Semaphore(){
37 sem_destroy(&(os_sem->sem));
38 free(os_sem);
41 int Semaphore::TryWait(){
42 sem_trywait(&(os_sem->sem));
45 int Semaphore::Wait(){
46 sem_wait(&(os_sem->sem));
49 int Semaphore::Post(){
50 sem_post(&(os_sem->sem));
53 int Semaphore::getValue(){
54 int ret;
55 sem_getvalue(&(os_sem->sem), &ret);
57 return ret;