DUDS
Distributed Update of Data from Something
ChipSelectManager.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
10 #ifndef CHIPSELECTMANAGER_HPP
11 #define CHIPSELECTMANAGER_HPP
12 
13 #include <mutex>
14 #include <condition_variable>
15 #include <memory>
16 #include <boost/noncopyable.hpp>
17 
18 namespace duds { namespace hardware { namespace interface {
19 
20 class ChipAccess;
21 
42 class ChipSelectManager : boost::noncopyable,
43 public std::enable_shared_from_this<ChipSelectManager> {
47  friend class ChipAccess;
48 protected:
54  std::mutex block;
55 private:
59  std::condition_variable selwait;
69  int waiting;
77  void retire(ChipAccess *ca);
78 protected:
83  int cid;
89  virtual void select() = 0;
95  virtual void deselect() = 0;
111  void changeChip(int chipId);
117  void baseAccess(std::unique_lock<std::mutex> &lock, int chipId);
124  void shutdown();
125 public:
132  virtual ~ChipSelectManager() = 0;
136  bool inUse() const {
137  return curacc;
138  }
150  virtual bool validChip(int chipId) const noexcept = 0;
168  std::unique_ptr<ChipAccess> access(int chipId);
191  void access(ChipAccess &acc, int chipId);
208  std::unique_ptr<ChipAccess> select(int chipId);
230  void select(ChipAccess &acc, int chipId);
231 };
232 
233 } } }
234 #endif // #ifndef CHIPSELECTMANAGER_HPP
235 
void changeChip(int chipId)
Changes the chip in use while continuing to use an existing access object.
int waiting
A count of the threads waiting to access chips.
void retire(ChipAccess *ca)
Called by ChipAccess::~ChipAccess() to indicate that the access object is no longer in use...
ChipAccess * curacc
The currently in use access object, or nullptr.
virtual void deselect()=0
Deselects the chip identified by cid.
virtual void select()=0
Selects the chip identified by cid.
std::mutex block
Used to synchonize access.
An object used to provide chip select control to a single user at a time.
Definition: ChipAccess.hpp:22
void baseAccess(std::unique_lock< std::mutex > &lock, int chipId)
Obtains the resources for providing an access object, but does not make an access object...
int cid
Selected chip ID, or -1 to terminate.
std::unique_ptr< ChipAccess > access(int chipId)
Acquires access to the requested chip and issues a ChipAccess object.
bool inUse() const
Returns true if an access object provided by this manager exists.
std::condition_variable selwait
Used to awaken threads waiting on a chip select.
virtual bool validChip(int chipId) const noexcept=0
Returns true if chipId references a valid chip for this manager.
virtual ~ChipSelectManager()=0
Derived non-abstract classes must call shutdown() in thier destructor.
void shutdown()
Waits on a ChipAccess object if one is in use, then begins forcing any threads waiting on access to w...
The base class for all chip selection managers, the classes that handle the output state to select a ...