summaryrefslogtreecommitdiff
path: root/src/libnml/os_intf/sem.cc
blob: 98cd2a96eb52d6f4dd4b44f09924e738f013913a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/********************************************************************
* Description: sem.cc
*
*   Derived from a work by Fred Proctor & Will Shackleford
*
* Author:
* License: LGPL Version 2
* System: Linux
*    
* Copyright (c) 2004 All rights reserved.
*
* Last change: 
********************************************************************/

extern "C" {
#include <stdio.h>		/* NULL */
#include "_sem.h"
}
#include "sem.hh"
#include "rcs_print.hh"		// rcs_print_debug(),
				// PRINT_SEMAPHORE_ACTIVITY
#include "timer.hh"		// etime()
RCS_SEMAPHORE::RCS_SEMAPHORE(key_t _id, int _oflag, double _time,
    int _mode, int _state)
{
    /* save the constructor args */
    id = _id;
    oflag = _oflag;
    mode = _mode;
    state = _state;
    timeout = _time;

    if (oflag & RCS_SEMAPHORE_CREATE) {
	sem = rcs_sem_create(id, mode, state);
    } else {
	sem = rcs_sem_open(id, 0);
    }

    if (sem == NULL) {
	rcs_print_error
	    ("can't create semaphore (id = %lu, oflag = %d, timeout = %f, mode = 0x%X, state = %d)\n",
	    id, oflag, timeout, mode, state);
    }
}

int
  RCS_SEMAPHORE::valid()
{
    return (sem != NULL);
}

RCS_SEMAPHORE::~RCS_SEMAPHORE()
{
    if (sem == NULL)
	return;

    /* need to destroy the semaphore before closing our copy */
    if (oflag & RCS_SEMAPHORE_CREATE) {
	rcs_sem_destroy(sem);
    }
    rcs_sem_close(sem);
    sem = NULL;
}

int
  RCS_SEMAPHORE::wait()
{
    int retval;
    if (sem == NULL)
	return -1;
    retval = rcs_sem_wait(sem, timeout);
    return retval;
}

int RCS_SEMAPHORE::trywait()
{
    if (sem == NULL)
	return -1;
    return rcs_sem_trywait(sem);
}

int RCS_SEMAPHORE::post()
{
    if (sem == NULL)
	return -1;
    return rcs_sem_post(sem);
}

int RCS_SEMAPHORE::flush()
{
    if (sem == NULL)
	return -1;
    return rcs_sem_flush(sem);
}

int RCS_SEMAPHORE::setflag(int _oflag)
{
    oflag = _oflag;		/* we can reset whether this was the one who
				   created the semaphore */
    return (0);
}

int RCS_SEMAPHORE::clear()
{
    return rcs_sem_clear(sem);
}

// This constructor declared private to prevent copying.
RCS_SEMAPHORE::RCS_SEMAPHORE(RCS_SEMAPHORE & sem)
{
}