The Pedigree Project  0.1
ProducerConsumer.cc
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 #include "pedigree/kernel/utilities/ProducerConsumer.h"
21 
22 #if PRODUCERCONSUMER_ASYNCHRONOUS
23 #include "pedigree/kernel/LockGuard.h"
24 #include "pedigree/kernel/utilities/pocketknife.h"
25 #endif
26 
27 ProducerConsumer::ProducerConsumer() = default;
28 
29 ProducerConsumer::~ProducerConsumer()
30 {
31 #if PRODUCERCONSUMER_ASYNCHRONOUS
32  m_Lock.acquire();
33  if (m_Running)
34  {
35  m_Running = false;
36  m_Condition.signal();
37  m_Lock.release();
38 
39  pocketknife::attachTo(m_pThreadHandle);
40  }
41  else
42  {
43  m_Lock.release();
44  }
45 
46  // Clean up tasks that didn't get executed.
47  for (auto it : m_Tasks)
48  {
49  delete it;
50  }
51 #endif
52 }
53 
54 bool ProducerConsumer::initialise()
55 {
56 #if PRODUCERCONSUMER_ASYNCHRONOUS
57  LockGuard<Mutex> guard(m_Lock);
58 
59  if (m_Running)
60  {
61  return true;
62  }
63 
64  m_Running = true;
65  m_pThreadHandle = pocketknife::runConcurrentlyAttached(thread, this);
66  return m_pThreadHandle != nullptr;
67 #else
68  return true;
69 #endif
70 }
71 
72 void ProducerConsumer::produce(
73  uint64_t p0, uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4,
74  uint64_t p5, uint64_t p6, uint64_t p7, uint64_t p8)
75 {
76 #if PRODUCERCONSUMER_ASYNCHRONOUS
77  Task *task = new Task;
78  task->p0 = p0;
79  task->p1 = p1;
80  task->p2 = p2;
81  task->p3 = p3;
82  task->p4 = p4;
83  task->p5 = p5;
84  task->p6 = p6;
85  task->p7 = p7;
86  task->p8 = p8;
87 
88  m_Lock.acquire();
89  m_Tasks.pushBack(task);
90  m_Condition.signal();
91  m_Lock.release();
92 #else
93  consume(p0, p1, p2, p3, p4, p5, p6, p7, p8);
94 #endif
95 }
96 
98 {
99  LockGuard<Mutex> guard(m_Lock);
100 
101  while (m_Running)
102  {
104  m_Condition.wait(m_Lock);
105 
106  if (!m_Running)
107  {
108  break;
109  }
110  else if (!m_Tasks.size())
111  {
112  continue;
113  }
114 
115  Task *task = m_Tasks.popFront();
116 
117  // Don't hold lock while we actually perform the consume operation.
118  m_Lock.release();
119 
120  consume(
121  task->p0, task->p1, task->p2, task->p3, task->p4, task->p5,
122  task->p6, task->p7, task->p8);
123 
124  delete task;
125 
126  m_Lock.acquire();
127  }
128 }
129 
130 int ProducerConsumer::thread(void *p)
131 {
132  ProducerConsumer *pc = reinterpret_cast<ProducerConsumer *>(p);
133  pc->consumerThread();
134 
135  return 0;
136 }
EXPORTED_PUBLIC void * runConcurrentlyAttached(int(*func)(void *), void *param)
Definition: pocketknife.cc:40
bool acquire(size_t n=1, size_t timeoutSecs=0, size_t timeoutUsecs=0)
Definition: Semaphore.h:62
void release(size_t n=1)
Definition: Semaphore.cc:239
EXPORTED_PUBLIC int attachTo(void *handle)
Definition: pocketknife.cc:52
WaitResult wait(Mutex &mutex, Time::Timestamp &timeout)