-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbakery.h
47 lines (34 loc) · 1.09 KB
/
bakery.h
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
#ifndef BAKERY_H_
#define BEKERY_H_
#include "algo.h"
/**
* bakery class is implementation of Lamport's bakery algorithm for mutual exclusion.
* It's base class is algo which is a virtual class containing lock() and unlock()
*/
class bakery : public algo {
/* variables for singleton pattern */
static bool instance;
static bakery *m;
/* array for doorway section */
volatile bool *choosing;
/* stores token */
volatile int *token;
/* number of processes */
int n;
/* initializes memory for n processes */
bakery(int i);
/* returns maximum value from already present tokens */
int max(volatile int *token);
public:
/* gives same object. If object is not present, it creates one */
static bakery* getLock(int);
/* Requests a lock for CS
pid: the process-id (thread-id) of requesting process */
void lock(int) override;
/* Releases previously acquired lock
pid: the process-id (thread-id) of the process */
void unlock(int) override;
/* frees memory initialized for n processes */
~bakery();
};
#endif