A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PDBBuzzer.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 /*
19  * File: PDBBuzzer.h
20  * Author: Chris
21  *
22  * Created on September 25, 2015, 5:10 PM
23  */
24 
25 #ifndef PDBBUZZER_H
26 #define PDBBUZZER_H
27 
28 #include <memory>
29 using namespace std;
30 
31 // create a smart pointer for PDBBuzzer objects
32 class PDBBuzzer;
33 typedef shared_ptr<PDBBuzzer> PDBBuzzerPtr;
34 
35 // A buzzer is an object given by someone who wants work done by a worker.
36 //
37 // The buzzer's constructor takes as input a function to handle the buzzer being signaled.
38 //
39 // Typically, someone who wants work done will create a buzzer, and then give it to a bunch
40 // of workers, then will call wait () to sleep until work is completed.
41 //
42 // When a piece of work is completed, the woker then calls buzz (), which invokes the
43 // function stored inside of the buzzer.
44 //
45 // One is guarnateed that each call to buzz () will result in a separate invocation of
46 // the function.
47 
48 #include "PDBAlarm.h"
49 #include <pthread.h>
50 #include <string>
51 #include <functional>
52 
53 
54 class PDBBuzzer {
55 public:
56  // sounds the buzzer, causing noStringFunc () to be called
57  void buzz(PDBAlarm withMe);
58 
59  // sounds the buzzer, causing stringFunc () to be called
60  void buzz(PDBAlarm withMe, string message);
61 
62  // sounds the buzzer, causing workFunc () to be called
63  void buzz(PDBAlarm withMe, int& counter);
64 
65  // blocks until someone calls buzz
66  void wait();
67 
68  // constructor, destructor
69  PDBBuzzer();
70  PDBBuzzer(std::nullptr_t nullp);
71  PDBBuzzer(std::function<void(PDBAlarm)>);
72  PDBBuzzer(std::function<void(PDBAlarm, string)>);
73  PDBBuzzer(std::function<void(PDBAlarm, int&)>);
74  ~PDBBuzzer();
75 
76 
77 private:
78  pthread_mutex_t waitingMutex;
79  pthread_cond_t waitingSignal;
80  bool signalSent = false;
81  std::function<void(PDBAlarm)> noStringFunc = nullptr;
82  std::function<void(PDBAlarm, std::string)> stringFunc = nullptr;
83  std::function<void(PDBAlarm, int&)> intFunc = nullptr;
84 };
85 
86 
87 #endif /* PDBBUZZER_H */
pthread_mutex_t waitingMutex
Definition: PDBBuzzer.h:78
shared_ptr< PDBBuzzer > PDBBuzzerPtr
Definition: PDBBuzzer.h:32
PDBAlarm
Definition: PDBAlarm.h:28
pthread_cond_t waitingSignal
Definition: PDBBuzzer.h:79