File: semaphore.h

package info (click to toggle)
bonnie%2B%2B 2.00a%2Bnmu3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 384 kB
  • sloc: cpp: 4,413; perl: 164; sh: 117; makefile: 76
file content (51 lines) | stat: -rw-r--r-- 1,137 bytes parent folder | download | duplicates (7)
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
#ifndef SEMAPHORE_H
#define SEMAPHORE_H

#include <sys/ipc.h>
#include <sys/sem.h>

#ifndef SEMUN_IN_SEM_H
union semun
{
  int val;                    /* value for SETVAL */
  struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
  unsigned short int *array;  /* array for GETALL, SETALL */
  struct seminfo *__buf;      /* buffer for IPC_INFO */
};
#endif

class Semaphore
{
public:

  // numSems is the number of semaphores to be in the set
  // semKey is the ID number for the semaphore set
  // val is the initial value for the semaphores, no values will be assigned
  // if the default (0) is specified.
  Semaphore(int semKey, int numSems = 1, int val = 0);

  // clear the semaphores and return an error code.
  int clear_sem();

  // create the semaphores
  // count is the initial value assigned to each semaphore
  int create(int count);

  // get the handle to a semaphore set previously created
  int get_semid();

  int decrement_and_wait(int nr_sem);
  int get_mutex();
  int put_mutex();

private:
  union semun m_arg;
  int m_semid;
  int m_semflg;
  bool m_semopen;
  int m_semKey;
  int m_numSems;
};

#endif