MDAT  1.0
 All Classes Files Functions Variables Typedefs Pages
sem.h
Go to the documentation of this file.
1 
9 /* Copyright (C) 2013 Eric Larson and Rochelle Palting
10  elarson@seattleu.edu
11 
12  This file is part of the MDAT infrastructure.
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27 
28 #ifndef SEM_H
29 #define SEM_H
30 
31 #include <string>
32 #include <vector>
33 #include <deque>
34 #include <semaphore.h>
35 using namespace std;
36 
47 {
48  public:
49 
59  void addSemaphore(const char *name, sem_t *sem, int value);
60 
71  void waitSemaphore(int id, sem_t *sem);
72 
82  void signalSemaphore(int id, sem_t *sem);
83 
89  string getSemaphoreTable() const;
90 
91  private:
92 
93  /* Semaphore data type */
94  struct Semaphore {
95  sem_t *sem; // corresponding semaphore variable
96  string name; // name of the semaphore
97  int value; // value of the semaphore
98  deque<int> waiting; // queue of threads waiting on semaphore
99  };
100 
101  vector<Semaphore> semArray; // array of semaphores
102 
103 
104  /* Finds and returns the desired semaphore record. */
105  Semaphore* findSemaphore(sem_t *sp) {
106  for (unsigned int i = 0; i < semArray.size(); i++) {
107  if (semArray[i].sem == sp) return &semArray[i];
108  }
109  return NULL;
110  }
111  Semaphore* findSemaphore(string name) {
112  for (unsigned int i = 0; i < semArray.size(); i++) {
113  if (semArray[i].name == name) return &semArray[i];
114  }
115  return NULL;
116  }
117 
118 };
119 
123 extern SemaphoreManager sems;
124 
125 #endif
This is a personal WEB site developed and maintained by an individual and not by Seattle University. The content and link(s) provided on this site do not represent or reflect the view(s) of Seattle University. The individual who authored this site is solely responsible for the site's content. This site and its author are subject to applicable University policies including the Computer Acceptable Use Policy (www.seattleu.edu/policies).