The Pedigree Project  0.1
ProducerConsumer.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_UTILITIES_PRODUCERCONSUMER_H
21 #define KERNEL_UTILITIES_PRODUCERCONSUMER_H
22 
23 #if defined(THREADS) || defined(UTILITY_LINUX)
24 #define PRODUCERCONSUMER_ASYNCHRONOUS 1
25 #else
26 #define PRODUCERCONSUMER_ASYNCHRONOUS 0
27 #endif
28 
29 #include "pedigree/kernel/compiler.h"
30 #include "pedigree/kernel/utilities/new"
31 
32 #if PRODUCERCONSUMER_ASYNCHRONOUS
33 #include "pedigree/kernel/process/ConditionVariable.h"
34 #include "pedigree/kernel/process/Mutex.h"
35 #include "pedigree/kernel/processor/types.h"
36 #include "pedigree/kernel/utilities/List.h"
37 #endif
38 
53 {
54  public:
56  virtual ~ProducerConsumer();
57 
58  bool initialise();
59 
60  void produce(
61  uint64_t p0 = 0, uint64_t p1 = 0, uint64_t p2 = 0, uint64_t p3 = 0,
62  uint64_t p4 = 0, uint64_t p5 = 0, uint64_t p6 = 0, uint64_t p7 = 0,
63  uint64_t p8 = 0);
64 
65  private:
66  virtual void consume(
67  uint64_t p0, uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4,
68  uint64_t p5, uint64_t p6, uint64_t p7, uint64_t p8) = 0;
69 
70  void consumerThread();
71 
72  static int thread(void *p);
73 
74  struct Task
75  {
76  uint64_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
77  };
78 
79 #if PRODUCERCONSUMER_ASYNCHRONOUS
80  Mutex m_Lock;
81  ConditionVariable m_Condition;
82  List<Task *> m_Tasks;
83 
84  void *m_pThreadHandle = nullptr;
85  bool m_Running = false;
86 #endif
87 };
88 
89 #endif
Definition: Mutex.h:58
Definition: List.h:64