From fe77fc6b869a71bf94d501a0762579f4ddbc5094 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 17 Nov 2006 23:15:04 +0000 Subject: [PATCH] Throw out anything polling related - they will go to libmspio eventually Remove some old files Redesign Application::main --- Package | 5 -- source/core/Module | 5 -- source/core/application.cpp | 127 ++++++++++++++---------------------- source/core/application.h | 36 +++++----- source/core/error.h | 4 +- source/core/event.cpp | 75 --------------------- source/core/event.h | 61 ----------------- source/core/main.cpp | 2 +- source/core/mutex.h | 2 +- source/core/pollable.cpp | 29 -------- source/core/pollable.h | 32 --------- source/core/poller.cpp | 102 ----------------------------- source/core/poller.h | 56 ---------------- source/core/semaphore.cpp | 2 +- source/core/semaphore.h | 2 +- source/core/thread.cpp | 2 +- source/core/thread.h | 2 +- source/core/types.h | 2 +- source/core/win32poll.h | 29 -------- source/time/Module | 1 - source/time/timedelta.cpp | 2 +- source/time/timedelta.h | 2 +- source/time/timer.cpp | 2 +- source/time/timer.h | 2 +- source/time/timestamp.h | 2 +- source/time/units.cpp | 2 +- source/time/units.h | 2 +- source/time/utils.cpp | 2 +- source/time/utils.h | 2 +- 29 files changed, 84 insertions(+), 510 deletions(-) delete mode 100644 Package delete mode 100644 source/core/Module delete mode 100644 source/core/event.cpp delete mode 100644 source/core/event.h delete mode 100644 source/core/pollable.cpp delete mode 100644 source/core/pollable.h delete mode 100644 source/core/poller.cpp delete mode 100644 source/core/poller.h delete mode 100644 source/core/win32poll.h delete mode 100644 source/time/Module diff --git a/Package b/Package deleted file mode 100644 index 42d52e4..0000000 --- a/Package +++ /dev/null @@ -1,5 +0,0 @@ -package="mspcore" -version="0.1" -description="Mikkosoft Productions core library" -requires=("mspmisc","pthread","sigc++-2.0") -tarballfile=("License.txt",) diff --git a/source/core/Module b/source/core/Module deleted file mode 100644 index 536d2d4..0000000 --- a/source/core/Module +++ /dev/null @@ -1,5 +0,0 @@ -type="library" -target="mspcore" -extradirs=("time",) -installheaders="core" -installtarget=1 diff --git a/source/core/application.cpp b/source/core/application.cpp index 01bb558..7e92dbd 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -1,10 +1,11 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ #include #include +#include "../time/units.h" #include "application.h" #include "error.h" @@ -12,35 +13,6 @@ using namespace std; namespace Msp { -Poller::Slot &Application::add_pollable(Pollable *obj, short events) -{ - if(!poller_) - poller_=new Poller; - - Poller::Slot &slot=poller_->add_pollable(obj, events); - // Interrupt a possible poll in progress -#ifndef WIN32 //XXX - pthread_kill(main_tid, SIGALRM); -#endif - return slot; -} - -EventManager::Event &Application::create_event() -{ - if(!ev_mgr_) - ev_mgr_=new EventManager(*this); - - return ev_mgr_->create_event(); -} - -Application::~Application() -{ - if(poller_) - delete poller_; - if(ev_mgr_) - delete ev_mgr_; -} - /** Constructs an instance of the registered application class and runs it. If the application throws a UsageError, the static usage() function is called. @@ -64,17 +36,13 @@ int Application::run(int argc, char **argv) return 126; } -#ifndef WIN32 //XXX - signal(SIGALRM, &sigalrm_); -#endif - try { app_=reg_app_->create_app(argc, argv); } catch(const UsageError &e) { - reg_app_->usage(argv[0], e.get_brief()); + reg_app_->usage(e.what(), argv[0], e.get_brief()); return 1; } @@ -87,63 +55,53 @@ int Application::run(int argc, char **argv) Prints a message describing the usage of the application. The default version will blame the programmer for being lazy. -@param argv0 The value of argv[0], to be used in the message -@param brief Whether to print a brief or long usage message +@param reason Why the function was called +@param argv0 The value of argv[0], to be used in the message +@param brief Whether to print a brief or long usage message */ -void Application::usage(const char *, bool) +void Application::usage(const char *reason, const char *, bool) { + if(reason) + cerr<poll(0); tick(); -#ifdef WIN32 - Sleep(0); -#else - //sched_yield(); - timespec ts={0,1000000}; - nanosleep(&ts, 0); -#endif + sleep(Time::msec); } - else + else if(loop_mode_==TICK_YIELD) { - if(poller_) - poller_->poll(-1); - else - { + tick(); #ifdef WIN32 - Sleep(1); + Sleep(0); #else - timespec ts={1000,0}; - nanosleep(&ts, 0); + sched_yield(); #endif - } - if(tick_mode_!=NONE) - tick(); } } @@ -158,12 +116,25 @@ void Application::catch_signal(int s) signal(s, &sighandler_); } -void Application::set_tick_mode(TickMode t) +/** +Changes the main loop mode. +*/ +void Application::set_loop_mode(LoopMode l) { - tick_mode_=t; -#ifndef WIN32 //XXX - pthread_kill(main_tid, SIGALRM); -#endif + LoopMode old_mode=loop_mode_; + loop_mode_=l; + if(old_mode==SLEEP) + sleep_sem_.signal(); +} + +/** +Causes the tick() function to be executed once if loop mode is SLEEP. Has no +effect with other loop modes. +*/ +void Application::induce_tick() +{ + if(loop_mode_==SLEEP) + sleep_sem_.signal(); } /** @@ -173,11 +144,13 @@ void Application::exit(int c) { done=true; exit_code=c; -#ifndef WIN32 //XXX - pthread_kill(main_tid, SIGALRM); -#endif + if(loop_mode_==SLEEP) + sleep_sem_.signal(); } +/** +Static wrapper function to call a member function of the Application instance. +*/ void Application::sighandler_(int s) { app_->sighandler(s); diff --git a/source/core/application.h b/source/core/application.h index 5f3968a..fa0c9c4 100644 --- a/source/core/application.h +++ b/source/core/application.h @@ -1,14 +1,12 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ #ifndef MSP_FRAMEWORK_APPLICATION_H_ #define MSP_FRAMEWORK_APPLICATION_H_ -#include "event.h" -#include "poller.h" -#include "types.h" +#include "semaphore.h" namespace Msp { @@ -19,25 +17,24 @@ member of type RegApp. class Application { public: - Poller::Slot &add_pollable(Pollable *, short); - EventManager::Event &create_event(); - virtual ~Application(); + virtual ~Application() { } static int run(int, char **); - static void usage(const char *, bool); + static void usage(const char *, const char *, bool); protected: - enum TickMode + enum LoopMode { - NONE, /// No ticks - AFTER_POLL, /// One tick after each poll - IDLE /// Constant torrent of ticks + NONE, /// No main loop - main() will just return + SLEEP, /// Only sleep in the main loop - useful for servers + TICK_SLEEP, /// Call tick every iteration, with a short sleep in between + TICK_YIELD /// Call tick every iteration, with sched_yield in between }; class RegBase { public: virtual Application *create_app(int, char **)=0; - virtual void usage(const char *, bool)=0; + virtual void usage(const char *, const char *, bool)=0; virtual ~RegBase() { } protected: RegBase(); @@ -48,7 +45,7 @@ protected: { public: Application *create_app(int argc, char **argv) { return new T(argc, argv); } - void usage(const char *a, bool b) { T::usage(a,b); } + void usage(const char *r, const char *a, bool b) { T::usage(r, a, b); } }; bool done; @@ -57,20 +54,19 @@ protected: Application(); virtual int main(); void catch_signal(int); - void set_tick_mode(TickMode); + void set_loop_mode(LoopMode); + void induce_tick(); void exit(int); virtual void tick() { } virtual void sighandler(int) { } private: - TickMode tick_mode_; - Poller *poller_; - EventManager *ev_mgr_; - ThreadHandle main_tid; + LoopMode loop_mode_; + Semaphore sleep_sem_; Application(const Application &); Application &operator=(const Application &); - static RegBase *reg_app_; + static RegBase *reg_app_; static Application *app_; static void sighandler_(int); diff --git a/source/core/error.h b/source/core/error.h index c311bcd..a8ad293 100644 --- a/source/core/error.h +++ b/source/core/error.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -13,7 +13,7 @@ namespace Msp { class UsageError: public Msp::Exception { public: - UsageError(bool b=true): Exception(""), brief(b) { } + UsageError(const std::string &r, bool b=true): Exception(r), brief(b) { } bool get_brief() const { return brief; } private: bool brief; diff --git a/source/core/event.cpp b/source/core/event.cpp deleted file mode 100644 index 01b8542..0000000 --- a/source/core/event.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#ifdef WIN32 -#include -#include -#endif - -#include "application.h" -#include "event.h" - -using namespace std; - -namespace Msp { - -EventManager::EventManager(Application &a): - app(a), - next_id(1) -{ - app.add_pollable(&pipe, POLLIN).signal_event.connect(sigc::mem_fun(this, &EventManager::data_available)); -} - -EventManager::Event &EventManager::create_event() -{ - events.push_back(Event(*this, next_id++)); - return events.back(); -} - -void EventManager::data_available(short) -{ - unsigned buf[1024]; - int len=pipe.read((char *)buf, sizeof(buf)); - for(unsigned i=0; i*sizeof(unsigned)<(unsigned)len; ++i) - { - for(list::iterator j=events.begin(); j!=events.end(); ++j) - if(j->get_id()==buf[i]) - j->signal_triggered.emit(); - } -} - -void EventManager::Event::trigger() -{ - mgr.pipe.write((char *)&id, sizeof(id)); -} - -EventManager::Pipe::Pipe() -{ -#ifdef WIN32 - _pipe(fd, 1024, _O_BINARY); -#else - ::pipe(fd); -#endif -} - -int EventManager::Pipe::write(char *buf, unsigned len) -{ -#ifdef WIN32 - return _write(fd[1], buf, len); -#else - return ::write(fd[1], buf, len); -#endif -} - -int EventManager::Pipe::read(char *buf, unsigned len) -{ -#ifdef WIN32 - return _read(fd[0], buf, len); -#else - return ::read(fd[0], buf, len); -#endif -} - -} // namespace Msp diff --git a/source/core/event.h b/source/core/event.h deleted file mode 100644 index 091d637..0000000 --- a/source/core/event.h +++ /dev/null @@ -1,61 +0,0 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#ifndef MSP_FRAMEWORK_EVENT_H_ -#define MSP_FRAMEWORK_EVENT_H_ - -#include -#include "pollable.h" - -namespace Msp { - -class Application; - -/** -Events can be used in multi-threaded applictions to trigger actions in the main -thread. -*/ -class EventManager -{ -public: - class Event - { - public: - sigc::signal signal_triggered; - - Event(EventManager &m, unsigned i): mgr(m), id(i) { } - unsigned get_id() const { return id; } - void trigger(); - private: - EventManager &mgr; - unsigned id; - }; - - EventManager(Application &); - Event &create_event(); -private: - class Pipe: public Pollable - { - public: - Pipe(); - int write(char *, unsigned); - int read(char *, unsigned); - private: - int fd[2]; - - int get_fd() { return fd[0]; } - }; - - Application &app; - Pipe pipe; - unsigned next_id; - std::list events; - - void data_available(short); -}; - -} // namespace Msp - -#endif diff --git a/source/core/main.cpp b/source/core/main.cpp index 4b187c3..de62f75 100644 --- a/source/core/main.cpp +++ b/source/core/main.cpp @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/core/mutex.h b/source/core/mutex.h index 9958510..146b982 100644 --- a/source/core/mutex.h +++ b/source/core/mutex.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/core/pollable.cpp b/source/core/pollable.cpp deleted file mode 100644 index 4db38dd..0000000 --- a/source/core/pollable.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#ifdef WIN32 -#include -#include "win32poll.h" -#else -#include -#endif -#include "pollable.h" - -namespace Msp { - -short Pollable::poll(short events, int timeout) -{ -#ifdef WIN32 - return 0; -#else - pollfd pfd={get_fd(), events, 0}; - int result=::poll(&pfd, 1, timeout); - if(result<=0) - return result; - return pfd.revents; -#endif -} - -} diff --git a/source/core/pollable.h b/source/core/pollable.h deleted file mode 100644 index f676d55..0000000 --- a/source/core/pollable.h +++ /dev/null @@ -1,32 +0,0 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#ifndef MSP_FRAMEWORK_POLLABLE_H_ -#define MSP_FRAMEWORK_POLLABLE_H_ - -#ifdef WIN32 -#include "win32poll.h" -#endif - -#include - -namespace Msp { - -class Pollable -{ -public: - sigc::signal signal_deleted; - - virtual short poll(short, int =0); - virtual ~Pollable() { signal_deleted.emit(); } -protected: - virtual int get_fd()=0; - - friend class Poller; -}; - -} - -#endif diff --git a/source/core/poller.cpp b/source/core/poller.cpp deleted file mode 100644 index 4a623d4..0000000 --- a/source/core/poller.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#include "pollable.h" -#include "poller.h" - -using namespace std; - -namespace Msp { - -Poller::Slot &Poller::add_pollable(Pollable *obj, short events) -{ - MutexLock sl(slots_mutex); - - slots.push_back(new Slot(obj, events)); - if(!pfd_mutex.trylock()) - { - rebuild_pfd(); - pfd_mutex.unlock(); - } - else - dirty=true; - return *slots.back(); -} - -int Poller::poll(int timeout) -{ -#ifdef WIN32 - return 0; -#else - slots_mutex.lock(); - for(list::iterator i=slots.begin(); i!=slots.end();) - { - if((*i)->get_object()) - ++i; - else - { - delete *i; - i=slots.erase(i); - dirty=true; - } - } - - pfd_mutex.lock(); - if(dirty) - { - rebuild_pfd(); - dirty=false; - } - slots_mutex.unlock(); - - int result=::poll(&pfd[0], pfd.size(), timeout); - - if(result>0) - { - list::iterator j=slots.begin(); - for(vector::iterator i=pfd.begin(); i!=pfd.end(); ++i) - { - if(i->revents&POLLNVAL) - dirty=true; - else if(i->revents) - { - while(j!=slots.end() && (!(*j)->get_object() || (*j)->get_object()->get_fd()!=i->fd)) - ++j; - if(j==slots.end()) - break; - (*j)->signal_event.emit(i->revents); - } - } - } - - pfd_mutex.unlock(); - - return result; -#endif -} - -void Poller::rebuild_pfd() -{ - pfd.clear(); - pfd.reserve(slots.size()); - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) - { - if(!(*i)->get_object() || (*i)->get_object()->get_fd()<0) - continue; - - pfd.push_back(pollfd()); - pfd.back().fd=(*i)->get_object()->get_fd(); - pfd.back().events=(*i)->get_events(); - } -} - -Poller::Slot::Slot(Pollable *o, short e): - obj(o), - events(e) -{ - obj->signal_deleted.connect(sigc::mem_fun(this, &Slot::obj_deleted)); -} - -} // namespace Msp diff --git a/source/core/poller.h b/source/core/poller.h deleted file mode 100644 index dcd3658..0000000 --- a/source/core/poller.h +++ /dev/null @@ -1,56 +0,0 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#ifndef MSP_FRAMEWORK_POLLER_H_ -#define MSP_FRAMEWORK_POLLER_H_ - -#ifdef WIN32 -#include "win32poll.h" -#else -#include -#endif -#include -#include -#include "mutex.h" - -namespace Msp { - -class Pollable; - -class Poller -{ -public: - class Slot - { - public: - sigc::signal signal_event; - - Slot(Pollable *, short); - Pollable *get_object() const { return obj; } - short get_events() const { return events; } - private: - Pollable *obj; - short events; - - void obj_deleted() { obj=0; } - }; - - Slot &add_pollable(Pollable *, short); - int poll(int =0); -private: - std::list slots; - std::vector pfd; - Mutex slots_mutex; - Mutex pfd_mutex; - bool dirty; - - void remove_stale_slots(); - void rebuild_pfd(); - void pollable_deleted(Pollable *); -}; - -}; - -#endif diff --git a/source/core/semaphore.cpp b/source/core/semaphore.cpp index 7006eaf..9107a76 100644 --- a/source/core/semaphore.cpp +++ b/source/core/semaphore.cpp @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/core/semaphore.h b/source/core/semaphore.h index b060947..1cf3cd6 100644 --- a/source/core/semaphore.h +++ b/source/core/semaphore.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/core/thread.cpp b/source/core/thread.cpp index 6858ce9..c7981cc 100644 --- a/source/core/thread.cpp +++ b/source/core/thread.cpp @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/core/thread.h b/source/core/thread.h index 2b0766c..937d161 100644 --- a/source/core/thread.h +++ b/source/core/thread.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/core/types.h b/source/core/types.h index bf08313..885c523 100644 --- a/source/core/types.h +++ b/source/core/types.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/core/win32poll.h b/source/core/win32poll.h deleted file mode 100644 index f39d53e..0000000 --- a/source/core/win32poll.h +++ /dev/null @@ -1,29 +0,0 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#ifndef MSP_FRAMEWORK_WIN32POLL_H_ -#define MSP_FRAMEWORK_WIN32POLL_H_ - -#ifdef WIN32 -// From Linux sys/poll.h -struct pollfd -{ - int fd; /* File descriptor to poll. */ - short int events; /* Types of events poller cares about. */ - short int revents; /* Types of events that actually occurred. */ -}; - -#ifndef POLLIN -// From Linux pth.h -#define POLLIN 0x0001 /* any readable data available */ -#endif - -#ifndef POLLNVAL -// From Linux pth.h -#define POLLNVAL 0x0020 /* requested events "invalid" */ -#endif - -#endif // Win32 -#endif diff --git a/source/time/Module b/source/time/Module deleted file mode 100644 index 9ccc1f2..0000000 --- a/source/time/Module +++ /dev/null @@ -1 +0,0 @@ -installheaders="time" diff --git a/source/time/timedelta.cpp b/source/time/timedelta.cpp index 891d560..acf9eef 100644 --- a/source/time/timedelta.cpp +++ b/source/time/timedelta.cpp @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/time/timedelta.h b/source/time/timedelta.h index 96dc760..5c5b633 100644 --- a/source/time/timedelta.h +++ b/source/time/timedelta.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/time/timer.cpp b/source/time/timer.cpp index 3d4aec5..e716d32 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/time/timer.h b/source/time/timer.h index ef7ea8b..369a78c 100644 --- a/source/time/timer.h +++ b/source/time/timer.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/time/timestamp.h b/source/time/timestamp.h index da4e457..984333d 100644 --- a/source/time/timestamp.h +++ b/source/time/timestamp.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/time/units.cpp b/source/time/units.cpp index d435d2e..c9d5d17 100644 --- a/source/time/units.cpp +++ b/source/time/units.cpp @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/time/units.h b/source/time/units.h index a157c29..98d2ae9 100644 --- a/source/time/units.h +++ b/source/time/units.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/time/utils.cpp b/source/time/utils.cpp index e8c0480..d6b6332 100644 --- a/source/time/utils.cpp +++ b/source/time/utils.cpp @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ diff --git a/source/time/utils.h b/source/time/utils.h index 9efe39a..e16cb65 100644 --- a/source/time/utils.h +++ b/source/time/utils.h @@ -1,5 +1,5 @@ /* -This file is part of libmspframework +This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ -- 2.43.0