[Jsmooth-cvs] jsmooth/skeletons/util-core Log.h, NONE, 1.1 Log.cpp, NONE, 1.1 Thread.cpp, NONE, 1.1
Status: Beta
Brought to you by:
reyes
From: Rodrigo R. <re...@us...> - 2007-04-09 17:17:07
|
Update of /cvsroot/jsmooth/jsmooth/skeletons/util-core In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv8985 Added Files: Log.h Log.cpp Thread.cpp Thread.h Log Message: added new classes --- NEW FILE: Log.cpp --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "Log.h" #include <windows.h> #include <winbase.h> Log::Log() { m_out = 0; } Log::Log(const std::string& filepath) { m_out = fopen(filepath.c_str(), "a"); } Log::~Log() { if (m_out != 0) fclose(m_out); m_out = 0; } void Log::out(const std::string& msg) { if (m_out != 0) { SYSTEMTIME time; GetSystemTime(&time); char timestamp[64]; sprintf(timestamp, "%02d:%02d.%02d", time.wHour, time.wMinute, time.wSecond); std::string m = std::string(timestamp) + " :" + msg; fprintf(m_out, "%s\n", m.c_str()); fflush(m_out); } } --- NEW FILE: Log.h --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef _LOG_H_ #define _LOG_H_ #include <string> #include <stdio.h> class Log { protected: FILE* m_out; public: Log(); Log(const std::string& filepath); ~Log(); void out(const std::string& msg); }; #endif --- NEW FILE: Thread.h --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef __JSMOOTHCORETHREAD_H_ #define __JSMOOTHCORETHREAD_H_ #include <process.h> #include <windows.h> #include <winbase.h> #include <stdio.h> /** * Provides basic string operations. * * @author Rodrigo Reyes <re...@ch...> */ class Thread { protected: unsigned long m_threadId; void (*m_callback)(void*); void* m_param; bool m_stopRequested; public: Thread(); /** * Starts the thread, and execute the run() method. */ void start(); /** * Starts the thread and execute the run method. If the run method * is not overloaded, it executes the callback fonction, and pass * the param as parameter to the function. */ void start(void (*callback)(void*), void* param); /** * The method executed by the thread. */ void run(); /* * Waits for the thread to complete */ void join(); /* * Waits for the thread to complete. If the timeout (millis) is * reached, returns false. Returns true if the thread ended. */ bool join(int millis); /** * Waits a number of milliseconds. */ static void sleep(int millis); bool isStopRequested(); void setStopRequested(bool stop=true); }; #endif --- NEW FILE: Thread.cpp --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "Thread.h" void thread_support_fn(void* param) { Thread *t = (Thread*) param; try { t->run(); } catch (...) { // } _endthread(); } Thread::Thread() { m_callback = 0; } void Thread::start() { m_threadId = _beginthread((void( __cdecl * )( void * ))thread_support_fn, 0, (void*) this); } void Thread::start(void (*callback)(void*), void* param) { m_callback = callback; m_param = param; m_threadId = _beginthread((void( __cdecl * )( void * ))thread_support_fn, 0, (void*) this); } void Thread::run() { if (m_callback != 0) { (*m_callback)(m_param); } } void Thread::sleep(int millis) { Sleep(millis); } void Thread::join() { WaitForSingleObject ( (void*)m_threadId, INFINITE ); } bool Thread::join(int millis) { int result = WaitForSingleObject( (void*)m_threadId, millis); switch(result) { case WAIT_ABANDONED: printf("WAIT_ABANDONED\n"); break; case WAIT_OBJECT_0: printf("WAIT_OBJECT_0"); return true; break; case WAIT_TIMEOUT: printf("WAIT_TIMEOUT"); break; } return false; } bool Thread::isStopRequested() { return m_stopRequested; } void Thread::setStopRequested(bool stop) { m_stopRequested = stop; } |