MDAT  1.0
 All Classes Files Functions Variables Typedefs Pages
lock.h
Go to the documentation of this file.
1 
8 /* Copyright (C) 2013 Eric Larson and Rochelle Palting
9  elarson@seattleu.edu
10 
11  This file is part of the MDAT infrastructure.
12 
13  This program is free software: you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  This program is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26 
27 #ifndef LOCK_H
28 #define LOCK_H
29 
30 #include <string>
31 #include <vector>
32 #include <deque>
33 #include <pthread.h>
34 using namespace std;
35 
46 {
47  public:
48 
57  void addLock(const char *name, pthread_mutex_t *lock);
58 
68  void acquireLock(int id, pthread_mutex_t *lock);
69 
80  void releaseLock(int id, pthread_mutex_t *lock);
81 
87  string getLockTable() const;
88 
89  private:
90 
91  /* Lock data type */
92  struct Lock {
93  pthread_mutex_t *lock; // corresponding lock variable
94  string name; // name of the lock
95  bool isLocked; // state of the lock
96  int threadHolding; // id of thread holding the lock (if held)
97  deque<int> waiting; // queue of threads waiting on lock
98  };
99 
100  vector<Lock> lockArray; // array of locks
101 
102  /* Finds and returns the desired lock record. */
103  Lock* findLock(pthread_mutex_t *lp) {
104  for (unsigned int i = 0; i < lockArray.size(); i++) {
105  if (lockArray[i].lock == lp) return &lockArray[i];
106  }
107  return NULL;
108  }
109  Lock* findLock(string name) {
110  for (unsigned int i = 0; i < lockArray.size(); i++) {
111  if (lockArray[i].name == name) return &lockArray[i];
112  }
113  return NULL;
114  }
115 
116 };
117 
121 extern LockManager locks;
122 
123 #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).