Skip to content

Adding simple processor thread interface #8

Merged
merged 12 commits into from
Nov 4, 2019
Prev Previous commit
Next Next commit
IO Processor interface
Greg committed Nov 3, 2019
commit 6973e6e9ade8f80cdfd6cff9a5a2453b23e1114a
7 changes: 6 additions & 1 deletion breadcrumbs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -35,6 +35,11 @@ configure_file (
include_directories("${CMAKE_INCLUDE_PATH}")
# Adding private include files from source tree
include_directories("${CMAKE_SOURCE_DIR}")

# puts all .cpp files inside src to the SOURCES variable
# TODO: replace this with a script for collecting cpp files
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/*.cpp")
message("Source files: ${SOURCES}")

# Adding executables
add_executable(Breadcrumbs ${CMAKE_SOURCE_DIR}/Breadcrumbs.cpp)
add_executable(Breadcrumbs ${SOURCES})
21 changes: 6 additions & 15 deletions breadcrumbs/src/Breadcrumbs.cpp
Original file line number Diff line number Diff line change
@@ -3,24 +3,15 @@
#include "io/out_procs/VirtualOutputProcessor.hpp"


#define THREADS 5


int main() {
printf("Hello World!\n");

SensorThread* procs[THREADS];
int targs[THREADS];
int threadID = 0;
IOProcessor *processor;
processor = new VirtualOutputProcessor();
processor->startThread(&threadID);

for (int i = 0; i < THREADS; i++) {
procs[i] = new VirtualOutputProcessor;
targs[i] = i + 1;
procs[i]->startThread(targs + i);
}
processor->waitForThread();
delete processor;

for (int i = 0; i < THREADS; i++) {
procs[i]->waitForThread();
delete procs[i];
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,58 @@
/*
*
* Stores the interface for building a sensor thread
* implementation.
*
*/

#include <Windows.h>
#include <stdio.h>


class SensorThread
{
public:

// Thread start 'caller' function
static DWORD __stdcall threadStart(LPVOID pUserData) {
((SensorThread*) pUserData)->threadRuntime();
return 0;
}
// Thread routine for implementation to override
virtual void threadRuntime() = 0;

// Async control
UINT8 startThread(LPVOID targs)s
{
threadArgs = targs;
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
threadStart, // thread function name
this, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
return TRUE;
};
BOOL waitForThread()
{
WaitForSingleObject(hThread, INFINITE);
return TRUE;
};

// Sync control
BOOLEAN bufferDataAvailable() { return TRUE; };
SIZE_T getBufferData(LPCSTR* bufferKeyArray, LPCSTR* bufferValueArray) { return 0; };
VOID setDataStoreValue(LPCSTR key, LPCVOID value, SIZE_T valueSize) {};
protected:
LPCVOID threadArgs;
DWORD dwThreadId;
HANDLE hThread;
};
/*
*
* Stores the interface for building a sensor thread
* implementation.
*
*/

#ifndef SENSOR_INTERFACE
#define SENSOR_INTERFACE


#include <Windows.h>
#include <stdio.h>


class IOProcessor
{
public:
// Thread routine for implementation to override
virtual VOID threadRuntime() = 0;
// Thread start 'caller' function
static DWORD __stdcall threadStart(LPVOID pUserData)
{
((IOProcessor*) pUserData)->threadRuntime();
return 0;
}

// Async control
UINT8 startThread(LPVOID targs)
{
this->threadArgs = targs;
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
threadStart, // thread function name
this, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
return TRUE;
};
BOOL waitForThread()
{
WaitForSingleObject(hThread, INFINITE);
return TRUE;
};

// Sync control
BOOLEAN bufferDataAvailable() { return TRUE; };
SIZE_T getBufferData(LPCSTR* bufferKeyArray, LPCSTR* bufferValueArray) { return 0; };
VOID setDataStoreValue(LPCSTR key, LPCVOID value, SIZE_T valueSize) {};
protected:
LPCVOID threadArgs;
DWORD dwThreadId;
HANDLE hThread;

};

#endif
2 changes: 1 addition & 1 deletion breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp
Original file line number Diff line number Diff line change
@@ -4,5 +4,5 @@

void VirtualOutputProcessor::threadRuntime()
{
printf("Hello from thread %d!\n", *((int*) threadArgs));
printf("VirtualOutputProcessor %d started.\n", *((int*) threadArgs));
}
11 changes: 8 additions & 3 deletions breadcrumbs/src/io/out_procs/VirtualOutputProcessor.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@

#include <stdio.h>

#include "../SensorInterface.hpp"
#include "../IOProcessor.hpp"


class VirtualOutputProcessor : public SensorThread
#ifndef VIRTUAL_OUTPUT_PROCESSOR_H
#define VIRTUAL_OUTPUT_PROCESSOR_H

class VirtualOutputProcessor : public IOProcessor
{
public:
virtual void threadRuntime();
void threadRuntime();
};

#endif