CPN
Computational Process Networks
Kernel.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
25 #ifndef CPN_KERNEL_H
26 #define CPN_KERNEL_H
27 #pragma once
28 #include <cpn/common.h>
29 #include <cpn/KernelAttr.h>
30 #include <cpn/NodeAttr.h>
31 #include <cpn/QueueAttr.h>
32 #include <cpn/bits/KernelBase.h>
35 #include <cpn/utils/Logger.h>
36 #include <cpn/Context.h>
37 #include <cpn/bits/NodeLoader.h>
38 #include <string>
39 #include <map>
40 #include <vector>
41 
42 class Pthread;
43 
44 namespace CPN {
45 
55  class CPN_API Kernel : public KernelBase {
57  INITIALIZED, RUNNING, TERMINATE, DONE
58  };
59  public:
60 
65  Kernel(const KernelAttr &kattr);
66  ~Kernel();
67 
74  void Wait();
75 
81  void Terminate();
82 
85  bool IsTerminated();
86 
91  void CheckTerminated();
92 
101  Key_t CreateNode(const NodeAttr &attr);
107  Key_t CreateNode(const std::string &name_, const std::string &nodetype_) {
108  return CreateNode(NodeAttr(name_,nodetype_));
109  }
110 
120  template<typename Function>
121  Key_t CreateFunctionNode(const std::string &nodename, Function func);
122  template<typename Function, typename Argument1>
123  Key_t CreateFunctionNode(const std::string &nodename, Function func, Argument1 arg1);
124  template<typename Function, typename Argument1, typename Argument2>
125  Key_t CreateFunctionNode(const std::string &nodename, Function func, Argument1 arg1, Argument2 arg2);
126  template<typename Function, typename Argument1, typename Argument2, typename Argument3>
127  Key_t CreateFunctionNode(const std::string &nodename, Function func, Argument1 arg1, Argument2 arg2, Argument3 arg3);
128  template<typename Function, typename Argument1, typename Argument2, typename Argument3, typename Argument4>
129  Key_t CreateFunctionNode(const std::string &nodename, Function func, Argument1 arg1, Argument2 arg2, Argument3 arg3, Argument4 arg4);
130  template<typename Function, typename Argument1, typename Argument2, typename Argument3, typename Argument4, typename Argument5>
131  Key_t CreateFunctionNode(const std::string &nodename, Function func, Argument1 arg1, Argument2 arg2, Argument3 arg3, Argument4 arg4, Argument5 arg5);
132  template<typename Function, typename Argument1, typename Argument2, typename Argument3, typename Argument4, typename Argument5, typename Argument6>
133  Key_t CreateFunctionNode(const std::string &nodename, Function func, Argument1 arg1, Argument2 arg2, Argument3 arg3, Argument4 arg4, Argument5 arg5, Argument6 arg6);
134  template<typename Function, typename Argument1, typename Argument2, typename Argument3, typename Argument4, typename Argument5, typename Argument6, typename Argument7>
135  Key_t CreateFunctionNode(const std::string &nodename, Function func, Argument1 arg1, Argument2 arg2, Argument3 arg3, Argument4 arg4, Argument5 arg5, Argument6 arg6, Argument7 arg7);
136  template<typename Function, typename Argument1, typename Argument2, typename Argument3, typename Argument4, typename Argument5, typename Argument6, typename Argument7, typename Argument8>
137  Key_t CreateFunctionNode(const std::string &nodename, Function func, Argument1 arg1, Argument2 arg2, Argument3 arg3, Argument4 arg4, Argument5 arg5, Argument6 arg6, Argument7 arg7, Argument8 arg8);
138  template<typename Function, typename Argument1, typename Argument2, typename Argument3, typename Argument4, typename Argument5, typename Argument6, typename Argument7, typename Argument8, typename Argument9>
139  Key_t CreateFunctionNode(const std::string &nodename, Function func, Argument1 arg1, Argument2 arg2, Argument3 arg3, Argument4 arg4, Argument5 arg5, Argument6 arg6, Argument7 arg7, Argument8 arg8, Argument9 arg9);
161  void CreateExternalReader(const std::string &name);
163  void CreateExternalWriter(const std::string &name);
164 
170  shared_ptr<QueueWriter> GetExternalOQueue(const std::string &name);
171 
177  shared_ptr<QueueReader> GetExternalIQueue(const std::string &name);
178 
185  void DestroyExternalEndpoint(const std::string &name);
186 
193  void WaitForNode(const std::string &nodename);
196  void WaitForAllNodes();
200  void WaitForNodeStart(const std::string &nodename);
201 
209  void CreateQueue(const QueueAttr &attr);
210 
214  LoggerOutput *GetLogger() { return &logger; }
215 
219  const std::string GetName() const { return kernelname; }
220 
223  Key_t GetKey() const { return kernelkey; }
224 
227  shared_ptr<Context> GetContext() const { return context; }
228 
232  void NodeTerminated(Key_t key);
233 
239  void NotifyTerminate();
240  void RemoteCreateWriter(SimpleQueueAttr attr);
241  void RemoteCreateReader(SimpleQueueAttr attr);
242  void RemoteCreateQueue(SimpleQueueAttr attr);
243  void RemoteCreateNode(NodeAttr attr);
254  void LoadSharedLib(const std::string &libname) { nodeloader.LoadSharedLib(libname); }
255 
258  void LoadNodeList(const std::string &filename) { nodeloader.LoadNodeList(filename); }
259 
262  void SearchDirectory(const std::string &dirname) { nodeloader.SearchDirectory(dirname); }
263 
276  NodeFactory *GetNodeFactory(const std::string &nodetype) { return nodeloader.GetFactory(nodetype); }
277 
281  void RegisterNodeFactory(shared_ptr<NodeFactory> factory) { nodeloader.RegisterFactory(factory); }
282 
293  bool UseD4R();
294  bool UseD4R(bool u);
295 
300  bool GrowQueueMaxThreshold();
301  bool GrowQueueMaxThreshold(bool grow);
302 
307  bool SwallowBrokenQueueExceptions();
308  bool SwallowBrokenQueueExceptions(bool sbqe);
309 
317  unsigned CalculateGrowSize(unsigned currentsize, unsigned request) {
318  return context->CalculateGrowSize(currentsize, request);
319  }
320 
321  private:
322  // Not copyable
323  Kernel(const Kernel&);
324  Kernel &operator=(const Kernel&);
325 
326  void CreateExternalEndpoint(const std::string &name, bool iswriter);
327 
328  void CreateReaderEndpoint(const SimpleQueueAttr &attr);
329  void CreateWriterEndpoint(const SimpleQueueAttr &attr);
330  void CreateLocalQueue(const SimpleQueueAttr &attr);
331  void InternalCreateNode(NodeAttr &nodeattr);
332  void ClearGarbage();
333 
334  void *EntryPoint();
335 
336  void SendWakeup();
337 
338  // Function to be called from gdb
339  void LogState();
340 
342 
343  // Read only after construction or contain own synchronization,
344  // no explicit synchronization required
345  auto_ptr<Pthread> thread;
346  const std::string kernelname;
349  shared_ptr<Context> context;
350  auto_ptr<ConnectionServer> server;
351  auto_ptr<RemoteQueueHolder> remotequeueholder;
352  bool useremote;
354 
355  typedef std::map<Key_t, shared_ptr<PseudoNode> > NodeMap;
356  typedef std::vector< shared_ptr<PseudoNode> > NodeList;
357 
358  // nodelock serializes access to nodemap
365 
367  bool useD4R;
370  };
371 }
372 
373 #include <cpn/bits/FunctionNode.h>
374 
375 #endif
Logger object that is used for forwarding log messages.
Definition: Logger.h:57
unsigned CalculateGrowSize(unsigned currentsize, unsigned request)
Calculate the new queue size when a queue needs to grow.
Definition: Kernel.h:317
KernelStatus_t
Definition: Kernel.h:56
bool useD4R
Definition: Kernel.h:367
shared_ptr< Context > GetContext() const
Definition: Kernel.h:227
NodeLoader nodeloader
Definition: Kernel.h:353
shared_ptr< Context > context
Definition: Kernel.h:349
Attributes for a node.
Definition: NodeAttr.h:58
NodeList garbagenodes
Definition: Kernel.h:364
std::vector< shared_ptr< PseudoNode > > NodeList
Definition: Kernel.h:356
Sync::ReentrantLock garbagelock
Definition: Kernel.h:363
The attribute for the Kernel.
Definition: KernelAttr.h:40
void LoadNodeList(const std::string &filename)
Definition: Kernel.h:258
const std::string kernelname
Definition: Kernel.h:346
void SearchDirectory(const std::string &dirname)
Definition: Kernel.h:262
bool useremote
Definition: Kernel.h:352
Sync::StatusHandler< KernelStatus_t > status
Definition: Kernel.h:341
Definition of the queue attributes.
Definition for node attributes.
The Context abstract data type.
auto_ptr< ConnectionServer > server
Definition: Kernel.h:350
uint64_t Key_t
Definition: common.h:79
bool swallowbrokenqueue
Definition: Kernel.h:368
NodeMap nodemap
Definition: Kernel.h:362
Declaration and implementation for the StatusHandler.
void RegisterNodeFactory(shared_ptr< NodeFactory > factory)
A function that lets others register node factories.
Definition: Kernel.h:281
Abstract base class for logger outputers. Any object who wishes to be a place for logging messages to...
Definition: Logger.h:37
Key_t kernelkey
Definition: Kernel.h:347
Logger logger
Definition: Kernel.h:348
A very simple logging interface.
Key_t CreateNode(const std::string &name_, const std::string &nodetype_)
Same as CreateNode that takes a NodeAttr except only the required parameters are set.
Definition: Kernel.h:107
std::map< Key_t, shared_ptr< PseudoNode > > NodeMap
Definition: Kernel.h:355
Definition of the kernel attributes.
auto_ptr< Pthread > thread
Definition: Kernel.h:345
Sync::ReentrantCondition nodecond
Definition: Kernel.h:360
auto_ptr< RemoteQueueHolder > remotequeueholder
Definition: Kernel.h:351
LoggerOutput * GetLogger()
Definition: Kernel.h:214
Works just like a pthread condition but works with the ReentrantLock.
void LoadSharedLib(const std::string &libname)
Attempts to load the given dynamic library and make the symbols inside available to be searched for n...
Definition: Kernel.h:254
The attributes for a queue.
Definition: QueueAttr.h:55
#define CPN_API
Definition: common.h:36
Sync::ReentrantLock datalock
Definition: Kernel.h:366
Sync::ReentrantLock nodelock
Definition: Kernel.h:359
The node factory provides a method for the kernel to create arbitrary user defined Nodes...
Definition: NodeFactory.h:37
NodeFactory * GetNodeFactory(const std::string &nodetype)
Return a pointer to the node factory that produces the given node type. May load a shared library to ...
Definition: Kernel.h:276
bool growmaxthresh
Definition: Kernel.h:369
The Kernel declaration.
Definition: Kernel.h:55
This is a simplified internal representation of the queue attributes needed to create a queue...
Definition: QueueAttr.h:237
const std::string GetName() const
Definition: Kernel.h:219
A reentrant lock implementation.
Key_t GetKey() const
Definition: Kernel.h:223
bool nodecond_signal
Definition: Kernel.h:361