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#include <config.h>
23
24#if THREADS || UTILITY_LINUX
25#define PRODUCERCONSUMER_ASYNCHRONOUS 1
26#else
27#define PRODUCERCONSUMER_ASYNCHRONOUS 0
28#endif
29
30#include "pedigree/kernel/compiler.h"
31#include "pedigree/kernel/utilities/new"
32
33#if PRODUCERCONSUMER_ASYNCHRONOUS
34#include "pedigree/kernel/process/ConditionVariable.h"
35#include "pedigree/kernel/process/Mutex.h"
36#include "pedigree/kernel/processor/types.h"
37#include "pedigree/kernel/utilities/List.h"
38#endif
39
53class EXPORTED_PUBLIC ProducerConsumer {
54 public:
56 virtual ~ProducerConsumer();
57
58 bool initialise();
59
60 void produce(uint64_t p0 = 0, uint64_t p1 = 0, uint64_t p2 = 0, uint64_t p3 = 0, uint64_t p4 = 0,
61 uint64_t p5 = 0, uint64_t p6 = 0, uint64_t p7 = 0, uint64_t p8 = 0);
62
63 protected:
68 void destroy();
69
70 private:
71 virtual void consume(uint64_t p0, uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4, uint64_t p5,
72 uint64_t p6, uint64_t p7, uint64_t p8) = 0;
73
74 void consumerThread();
75
76 static int thread(void* p);
77
78 struct Task {
79 uint64_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
80 };
81
82#if PRODUCERCONSUMER_ASYNCHRONOUS
83 Mutex m_Lock;
84 ConditionVariable m_Condition;
85 List<Task*> m_Tasks;
86
87 void* m_pThreadHandle = nullptr;
88 bool m_Running = false;
89 bool m_Destroyed = false;
90#endif
91};
92
93#endif
Definition List.h:61
Definition Mutex.h:56