From 8171fe1f5fdd95d275832d5ed89f811d65af37f2 Mon Sep 17 00:00:00 2001 From: Carlos Daniel Ruvalcaba Valenzuela Date: Wed, 19 Mar 2008 17:12:03 -0700 Subject: [PATCH] Implemented a more general Thread Worker pattern --- Makefile | 6 ++-- docs/fmail-platform.svg | 5 ++- docs/ipc.tex | 10 ++++++ include/libfmail.h | 1 + include/libfmail/worker.h | 50 +++++++++++++++++++++++++++++ src/worker.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 include/libfmail/worker.h create mode 100644 src/worker.cpp diff --git a/Makefile b/Makefile index 1e4d7c9..f747f89 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # GPLv2 applied. CC= g++ -CFLAGS= -Wall -W -I./include +CFLAGS= -Wall -W -I./include -g .PHONY: clean all debug all: bin/libfmail.so bin/fmail-fsauth bin/fmail-fsmailbox bin/fmail-queue bin/fmail-pop3 bin/fmail-smtp @@ -29,7 +29,9 @@ bin/threadpool.o: src/threadpool.cpp $(CC) $^ $(CFLAGS) -c -o $@ bin/configuration.o: src/configuration.cpp $(CC) $^ $(CFLAGS) -c -o $@ -bin/libfmail.so: bin/baseserver.o bin/socket.o bin/socketipc.o bin/ipc.o bin/ipcmsg.o bin/lock.o bin/thread.o bin/threadpool.o bin/semaphore.o bin/configuration.o +bin/worker.o: src/worker.cpp + $(CC) $^ $(CFLAGS) -c -o $@ +bin/libfmail.so: bin/baseserver.o bin/socket.o bin/socketipc.o bin/ipc.o bin/ipcmsg.o bin/lock.o bin/thread.o bin/threadpool.o bin/semaphore.o bin/configuration.o bin/worker.o $(CC) $^ -lpthread -lpcrecpp -lstdc++ -shared -o $@ #echo "Compiling Testcases" diff --git a/docs/fmail-platform.svg b/docs/fmail-platform.svg index f60233a..440168b 100644 --- a/docs/fmail-platform.svg +++ b/docs/fmail-platform.svg @@ -16,7 +16,10 @@ inkscape:version="0.45.1" sodipodi:docbase="/home/clsdaniel/Development/fmail/docs" sodipodi:docname="fmail-platform.svg" - inkscape:output_extension="org.inkscape.output.svg.inkscape"> + inkscape:output_extension="org.inkscape.output.svg.inkscape" + inkscape:export-filename="/home/clsdaniel/Development/fmail/docs/fmail-platform.png" + inkscape:export-xdpi="90" + inkscape:export-ydpi="90"> #include #include +#include diff --git a/include/libfmail/worker.h b/include/libfmail/worker.h new file mode 100644 index 0000000..e17b0d9 --- /dev/null +++ b/include/libfmail/worker.h @@ -0,0 +1,50 @@ +/* +libfmail: Thread Worker Pattern Implementation + +Copyright (C) 2008 Carlos Daniel Ruvalcaba Valenzuela + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +class Job{ +private: +public: + virtual ~Job(){} + virtual int Run() = 0; +}; + +class Worker : public Thread, public Lock { +private: + Job *cJob; + Lock *mtx; + int onRun; +public: + Worker(); + ~Worker(); + int setJob(Job *job); + int wakeup(); + int Run(); +}; + +class Boss{ +private: + Worker **workers; + int nworkers; +public: + Boss(int n); + + int Dispatch(Job *jb); +}; + diff --git a/src/worker.cpp b/src/worker.cpp new file mode 100644 index 0000000..3709c7f --- /dev/null +++ b/src/worker.cpp @@ -0,0 +1,80 @@ +/* +libfmail: Thread Worker Pattern Implementation + +Copyright (C) 2008 Carlos Daniel Ruvalcaba Valenzuela + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include + +Worker::Worker() : Thread(), Lock(){ + cJob = NULL; + mtx = new Lock(); + mtx->Acquire(); +} + +Worker::~Worker(){ +} + +int Worker::setJob(Job *job){ + cJob = job; + return 0; +} +int Worker::wakeup(){ + return mtx->Release(); +} + +int Worker::Run(){ + onRun = 1; + while (onRun){ + mtx->Acquire(); + if (cJob){ + cJob->Run(); + } + cJob = NULL; + Release(); + } + return 0; +} + +Boss::Boss(int n){ + int i; + workers = (Worker**)malloc(sizeof(Worker*) * n); + for (i = 0; i < n; i++){ + workers[i] = new Worker(); + workers[i]->Start(); + } + nworkers = n; +} + +int Boss::Dispatch(Job *jb){ + int i = 0; + int onRun = 1; + + while(onRun){ + if (workers[i]->TryAcquire() == 0){ + workers[i]->setJob(jb); + workers[i]->wakeup(); + onRun = 0; + } + i++; + if (i == nworkers) + i = 0; + } + return 1; +} + + -- 2.11.4.GIT