#include <stdio.h>
#include <iostream>
+
/** \page MTasker
Simple system for implementing cooperative multitasking of functions, with
support for waiting on events which can return values.
w.key=key;
d_waiters.insert(w);
-
+#ifdef MTASKERTIMING
+ unsigned int diff=d_threads[d_tid].dt.ndiff()/1000;
+ d_threads[d_tid].totTime+=diff;
+#endif
if(swapcontext(d_waiters.find(key)->context,&d_kernel)) { // 'A' will return here when 'key' has arrived, hands over control to kernel first
perror("swapcontext");
exit(EXIT_FAILURE); // no way we can deal with this
}
+#ifdef MTASKERTIMING
+ d_threads[d_tid].dt.start();
+#endif
if(val && d_waitstatus==Answer)
*val=d_waitval;
d_tid=w.tid;
{
if(!d_runQueue.empty()) {
d_tid=d_runQueue.front();
+#ifdef MTASKERTIMING
+ d_threads[d_tid].dt.start();
+#endif
if(swapcontext(&d_kernel, d_threads[d_tid].context)) {
perror("swapcontext in schedule");
exit(EXIT_FAILURE);
return d_tid;
}
-
//! Returns the maximum stack usage so far of this MThread
template<class Key, class Val>unsigned int MTasker<Key,Val>::getMaxStackUsage()
{
return d_threads[d_tid].startOfStack - d_threads[d_tid].highestStackSeen;
}
+
+//! Returns the maximum stack usage so far of this MThread
+template<class Key, class Val>unsigned int MTasker<Key,Val>::getUsec()
+{
+#ifdef MTASKERTIMING
+ return d_threads[d_tid].totTime + d_threads[d_tid].dt.ndiff()/1000;
+#else
+ return 0;
+#endif
+}
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include "namespaces.hh"
+#include "misc.hh"
using namespace ::boost::multi_index;
+// #define MTASKERTIMING 1
+
struct KeyTag {};
//! The main MTasker class
ucontext_t* context;
char* startOfStack;
char* highestStackSeen;
+#ifdef MTASKERTIMING
+ CPUTime dt;
+ unsigned int totTime;
+#endif
};
typedef std::map<int, ThreadInfo> mthreads_t;
unsigned int numProcesses();
int getTid();
unsigned int getMaxStackUsage();
+ unsigned int getUsec();
private:
static void threadWrapper(uint32_t self1, uint32_t self2, tfunc_t *tf, int tid, uint32_t val1, uint32_t val2);