kadpeer team mailing list archive
-
kadpeer team
-
Mailing list archive
-
Message #00006
r17 committed - Add Singleton threadpool by Devil Wang
*
*
---------- Forwarded message ----------
From: <kadpeer@xxxxxxxxxxxxxx>
Date: Tue, Jul 13, 2010 at 3:03 PM
Subject: [kadpeer] r17 committed - Add Singleton threadpool by Devil Wang
To: wxjeacen@xxxxxxxxx
Revision: 17
Author: wxjeacen
Date: Tue Jul 13 00:02:19 2010
Log: Add Singleton threadpool by Devil Wang
http://code.google.com/p/kadpeer/source/detail?r=17
Added:
/trunk/COPYING
/trunk/INSTALL
/trunk/common/TThread.cpp
/trunk/common/TThreadPool.cpp
/trunk/common/sha1.cpp
/trunk/include/common/Singleton.h
/trunk/include/common/TThread.hh
/trunk/include/common/TThreadPool.hh
/trunk/include/common/sha1.h
/trunk/include/common/sll.hh
/trunk/src/TRNG.cc
/trunk/src/TRNG.hh
/trunk/src/TTimer.cc
/trunk/src/TTimer.hh
/trunk/src/main.cc
Modified:
/trunk/Makefile
/trunk/common/BigInt.cpp
/trunk/common/Makefile
/trunk/include/common/BigInt.h
/trunk/madmedia/Makefile
/trunk/src/main.cpp
=======================================
--- /dev/null
+++ /trunk/COPYING Tue Jul 13 00:02:19 2010
@@ -0,0 +1,340 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+ 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange;
or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free
Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show
w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ <signature of Ty Coon>, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Library General
+Public License instead of this License.
=======================================
--- /dev/null
+++ /trunk/INSTALL Tue Jul 13 00:02:19 2010
@@ -0,0 +1,5 @@
+1. type 'make' to compile the package.
+
+2. type 'make' to install the program.
+
+3. You can remove the package files from the sourcecode dir by typing 'make
uninstall'.
=======================================
--- /dev/null
+++ /trunk/common/TThread.cpp Tue Jul 13 00:02:19 2010
@@ -0,0 +1,244 @@
+//
+// Project : ThreadPool
+// File : TThread.cc
+// Author : Ronald Kriemann
+// Purpose : baseclass for a thread-able class
+//
+// arch-tag: aec71576-f8d5-4573-ad31-f3dbddc59934
+//
+
+#include <unistd.h>
+#include <signal.h>
+#include <time.h>
+#include <sched.h>
+#include <string.h>
+
+#include <iostream>
+#include <cmath>
+
+#include "TThread.hh"
+
+//
+// routine to call TThread::run() method
+//
+extern "C"
+void *
+_run_thread ( void *arg )
+{
+ if (arg != NULL)
+ {
+ ((TThread*) arg)->run();
+ ((TThread*) arg)->reset_running();
+ }// if
+
+ return NULL;
+}
+
+////////////////////////////////////////////
+//
+// constructor and destructor
+//
+
+TThread::TThread ( const int thread_no )
+ : _running( false ), _thread_no(thread_no)
+{
+}
+
+TThread::~TThread ()
+{
+ // request cancellation of the thread if running
+ if ( _running )
+ cancel();
+}
+
+////////////////////////////////////////////
+//
+// access local data
+//
+
+void
+TThread::set_thread_no ( const int n )
+{
+ _thread_no = n;
+}
+
+////////////////////////////////////////////
+//
+// thread management
+//
+
+//
+// create thread (actually start it)
+//
+void
+TThread::create ( const bool detached, const bool sscope )
+{
+ if ( ! _running )
+ {
+ int status;
+
+ if ((status = pthread_attr_init( & _thread_attr )) != 0)
+ {
+ std::cerr << "(TThread) create : pthread_attr_init ("
+ << strerror( status ) << ")" << std::endl;
+ return;
+ }// if
+
+ if ( detached )
+ {
+ // detache created thread from calling thread
+ if ((status = pthread_attr_setdetachstate( & _thread_attr,
+
PTHREAD_CREATE_DETACHED )) != 0)
+ {
+ std::cerr << "(TThread) create :
pthread_attr_setdetachstate ("
+ << strerror( status ) << ")" << std::endl;
+ return;
+ }// if
+ }// if
+
+ if ( sscope )
+ {
+ // use system-wide scheduling for thread
+ if ((status = pthread_attr_setscope( & _thread_attr,
PTHREAD_SCOPE_SYSTEM )) != 0 )
+ {
+ std::cerr << "(TThread) create : pthread_attr_setscope ("
+ << strerror( status ) << ")" << std::endl;
+ return;
+ }// if
+ }// if
+
+#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && defined(SUNOS)
+ //
+ // adjust thread-scheduling for Solaris
+ //
+
+ struct sched_param t_param;
+
+ t_param.sched_priority = sched_get_priority_min( SCHED_RR );
+
+ if ((status = pthread_attr_setschedpolicy( & _thread_attr,
SCHED_RR )) != 0)
+ std::cerr << "(TThread) create : pthread_attr_setschedpolicy ("
+ << strerror( status ) << ")" << std::endl;
+
+ if ((status = pthread_attr_setschedparam( & _thread_attr, &
t_param )) != 0)
+ std::cerr << "(TThread) create : pthread_attr_setschedparam ("
+ << strerror( status ) << ")" << std::endl;
+
+ if ((status = pthread_attr_setinheritsched( & _thread_attr,
PTHREAD_EXPLICIT_SCHED )) != 0)
+ std::cerr << "(TThread) create : pthread_attr_setinheritsched
("
+ << strerror( status ) << ")" << std::endl;
+#endif
+
+#ifdef HPUX
+ // on HP-UX we increase the stack-size for a stable behaviour
+ // (need much memory for this !!!)
+ pthread_attr_setstacksize( & _thread_attr, 32 * 1024 * 1024 );
+#endif
+
+ if ((status = pthread_create( & _thread_id, & _thread_attr,
_run_thread, this ) != 0))
+ std::cerr << "(TThread) create : pthread_create ("
+ << strerror( status ) << ")" << std::endl;
+ else
+ _running = true;
+ }// if
+ else
+ std::cout << "(TThread) create : thread is already running" <<
std::endl;
+}
+
+//
+// detach thread
+//
+void
+TThread::detach ()
+{
+ if ( _running )
+ {
+ int status;
+
+ // detach thread
+ if ((status = pthread_detach( _thread_id )) != 0)
+ std::cerr << "(TThread) detach : pthread_detach ("
+ << strerror( status ) << ")" << std::endl;
+ }// if
+}
+
+//
+// synchronise with thread (wait until finished)
+//
+void
+TThread::join ()
+{
+ if ( _running )
+ {
+ int status;
+
+ // wait for thread to finish
+ if ((status = pthread_join( _thread_id, NULL )) != 0)
+ std::cerr << "(TThread) join : pthread_join ("
+ << strerror( status ) << ")" << std::endl;
+
+ _running = false;
+ }// if
+}
+
+//
+// request cancellation of thread
+//
+void
+TThread::cancel ()
+{
+ if ( _running )
+ {
+ int status;
+
+ if ((status = pthread_cancel( _thread_id )) != 0)
+ std::cerr << "(TThread) cancel : pthread_cancel ("
+ << strerror( status ) << ")" << std::endl;
+ }// if
+}
+
+////////////////////////////////////////////
+//
+// functions to be called by a thread itself
+//
+
+//
+// terminate thread
+//
+void
+TThread::exit ()
+{
+ if ( _running && (pthread_self() == _thread_id))
+ {
+ void * ret_val = NULL;
+
+ pthread_exit( ret_val );
+
+ _running = false;
+ }// if
+}
+
+//
+// put thread to sleep (milli + nano seconds)
+//
+void
+TThread::sleep ( const double sec )
+{
+ if ( _running )
+ {
+ struct timespec interval;
+
+ if ( sec <= 0.0 )
+ {
+ interval.tv_sec = 0;
+ interval.tv_nsec = 0;
+ }// if
+ else
+ {
+ interval.tv_sec = time_t( std::floor( sec ) );
+ interval.tv_nsec = long( (sec - interval.tv_sec) * 1e6 );
+ }// else
+
+ nanosleep( & interval, 0 );
+ }// if
+}
=======================================
--- /dev/null
+++ /trunk/common/TThreadPool.cpp Tue Jul 13 00:02:19 2010
@@ -0,0 +1,418 @@
+//
+// Project : ThreadPool
+// File : TThreadPool.cc
+// Author : Ronald Kriemann
+// Purpose : class for managing a pool of threads
+//
+// arch-tag: d4739323-4e22-42d9-9fcb-f98f5890d77b
+//
+
+#include <pthread.h>
+
+#include "TThreadPool.hh"
+
+//
+// set to one to enable sequential execution
+//
+#define THR_SEQUENTIAL 0
+
+//
+// global thread-pool
+//
+
+TThreadPool * thread_pool = NULL;
+
+//////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////
+//
+// thread handled by threadpool
+//
+//////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////
+
+//
+// thread handled by threadpool
+//
+
+class TPoolThr : public TThread
+{
+protected:
+ // pool we are in
+ TThreadPool * _pool;
+
+ // job to run and data for it
+ TThreadPool::TJob * _job;
+ void * _data_ptr;
+
+ // should the job be deleted upon completion
+ bool _del_job;
+
+ // condition for job-waiting
+ TCondition _work_cond;
+
+ // indicates end-of-thread
+ bool _end;
+
+ // mutex for preventing premature deletion
+ TMutex _del_mutex;
+
+public:
+ //
+ // constructor
+ //
+ TPoolThr ( const int n, TThreadPool * p )
+ : TThread(n), _pool(p), _job(NULL), _data_ptr(NULL),
_del_job(false), _end(false)
+ {}
+
+ ~TPoolThr () {}
+
+ //
+ // parallel running method
+ //
+ void run ()
+ {
+ _del_mutex.lock();
+
+ while ( ! _end )
+ {
+ //
+ // append thread to idle-list and wait for work
+ //
+
+ _pool->append_idle( this );
+
+ _work_cond.lock();
+
+ while (( _job == NULL ) && ! _end )
+ _work_cond.wait();
+
+ _work_cond.unlock();
+
+ //
+ // look if we really have a job to do
+ // and handle it
+ //
+
+ if ( _job != NULL )
+ {
+ // execute job
+ _job->run( _data_ptr );
+ _job->unlock();
+
+ if ( _del_job )
+ delete _job;
+
+ // reset data
+ _work_cond.lock();
+ _job = NULL;
+ _data_ptr = NULL;
+ _work_cond.unlock();
+ }// if
+ }// while
+
+ _del_mutex.unlock();
+ }
+
+ //
+ // set and run job with optional data
+ //
+ void run_job ( TThreadPool::TJob * j, void * p, const bool del = false
)
+ {
+ _work_cond.lock();
+
+ _job = j;
+ _data_ptr = p;
+ _del_job = del;
+
+ _work_cond.signal();
+ _work_cond.unlock();
+ }
+
+ //
+ // give access to delete mutex
+ //
+
+ TMutex & del_mutex ()
+ {
+ return _del_mutex;
+ }
+
+ //
+ // quit thread (reset data and wake up)
+ //
+
+ void quit ()
+ {
+ _work_cond.lock();
+
+ _end = true;
+ _job = NULL;
+ _data_ptr = NULL;
+
+ _work_cond.signal();
+ _work_cond.unlock();
+ }
+};
+
+//////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////
+//
+// ThreadPool - implementation
+//
+//////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////
+
+//
+// constructor and destructor
+//
+
+TThreadPool::TThreadPool ( const uint max_p )
+{
+ //
+ // create max_p threads for pool
+ //
+
+ _max_parallel = max_p;
+
+ _threads = new TPoolThr*[ _max_parallel ];
+
+ if ( _threads == NULL )
+ {
+ _max_parallel = 0;
+ std::cerr << "(TThreadPool) TThreadPool : could not allocate thread
array" << std::endl;
+ }// if
+
+ for ( uint i = 0; i < _max_parallel; i++ )
+ {
+ _threads[i] = new TPoolThr( i, this );
+
+ if ( _threads == NULL )
+ std::cerr << "(TThreadPool) TThreadPool : could not allocate
thread" << std::endl;
+ else
+ _threads[i]->create( true, true );
+ }// for
+
+ // tell the scheduling system, how many threads to expect
+ // (commented out since not needed on most systems)
+// if ( pthread_setconcurrency( _max_parallel +
pthread_getconcurrency() ) != 0 )
+// std::cerr << "(TThreadPool) TThreadPool : pthread_setconcurrency
("
+// << strerror( status ) << ")" << std::endl;
+}
+
+TThreadPool::~TThreadPool ()
+{
+ // wait till all threads have finished
+ sync_all();
+
+ // finish all thread
+ for ( uint i = 0; i < _max_parallel; i++ )
+ _threads[i]->quit();
+
+ // cancel and delete all threads (not really safe !)
+ for ( uint i = 0; i < _max_parallel; i++ )
+ {
+ _threads[i]->del_mutex().lock();
+ delete _threads[i];
+ }// for
+
+ delete[] _threads;
+}
+
+///////////////////////////////////////////////
+//
+// run, stop and synch with job
+//
+
+void
+TThreadPool::run ( TThreadPool::TJob * job, void * ptr, const bool del )
+{
+ if ( job == NULL )
+ return;
+
+#if THR_SEQUENTIAL == 1
+ //
+ // run in calling thread
+ //
+
+ job->run( ptr );
+
+ if ( del )
+ delete job;
+
+#else
+ //
+ // run in parallel thread
+ //
+
+ TPoolThr * thr = get_idle();
+
+ // lock job for synchronisation
+ job->lock();
+
+ // attach job to thread
+ thr->run_job( job, ptr, del );
+#endif
+}
+
+//
+// wait until <job> was executed
+//
+void
+TThreadPool::sync ( TJob * job )
+{
+ if ( job == NULL )
+ return;
+
+ job->lock();
+ job->unlock();
+}
+
+//
+// wait until all jobs have been executed
+//
+void
+TThreadPool::sync_all ()
+{
+ while ( true )
+ {
+ _idle_cond.lock();
+
+ // wait until next thread becomes idle
+ if ( _idle_threads.size() < _max_parallel )
+ _idle_cond.wait();
+ else
+ {
+ _idle_cond.unlock();
+ break;
+ }// else
+
+ _idle_cond.unlock();
+ }// while
+}
+
+///////////////////////////////////////////////
+//
+// manage pool threads
+//
+
+//
+// return idle thread form pool
+//
+TPoolThr *
+TThreadPool::get_idle ()
+{
+ while ( true )
+ {
+ //
+ // wait for an idle thread
+ //
+
+ _idle_cond.lock();
+
+ while ( _idle_threads.size() == 0 )
+ _idle_cond.wait();
+
+ //
+ // get first idle thread
+ //
+
+ if ( _idle_threads.size() > 0 )
+ {
+ TPoolThr * t = _idle_threads.behead();
+
+ _idle_cond.unlock();
+
+ return t;
+ }// if
+
+ _idle_cond.unlock();
+ }// while
+}
+
+//
+// append recently finished thread to idle list
+//
+void
+TThreadPool::append_idle ( TPoolThr * t )
+{
+ //
+ // CONSISTENCY CHECK: if given thread is already in list
+ //
+
+ _idle_cond.lock();
+
+ for ( sll< TPoolThr * >::iterator iter = _idle_threads.first(); !
iter.eol(); ++iter )
+ {
+ if ( iter() == t )
+ {
+ _idle_cond.unlock();
+ return;
+ }// if
+ }// while
+
+ _idle_threads.push_back( t );
+
+ // wake a blocked thread for job execution
+ _idle_cond.signal();
+
+ _idle_cond.unlock();
+}
+
+///////////////////////////////////////////////////
+//
+// to access global thread-pool
+//
+///////////////////////////////////////////////////
+
+//
+// init global thread_pool
+//
+void
+tp_init ( const uint p )
+{
+ if ( thread_pool != NULL )
+ delete thread_pool;
+
+ if ((thread_pool = new TThreadPool( p )) == NULL)
+ std::cerr << "(init_thread_pool) could not allocate thread pool" <<
std::endl;
+}
+
+//
+// run job
+//
+void
+tp_run ( TThreadPool::TJob * job, void * ptr, const bool del )
+{
+ if ( job == NULL )
+ return;
+
+ thread_pool->run( job, ptr, del );
+}
+
+//
+// synchronise with specific job
+//
+void
+tp_sync ( TThreadPool::TJob * job )
+{
+ thread_pool->sync( job );
+}
+
+//
+// synchronise with all jobs
+//
+void
+tp_sync_all ()
+{
+ thread_pool->sync_all();
+}
+
+//
+// finish thread pool
+//
+void
+tp_done ()
+{
+ delete thread_pool;
+}
+
=======================================
--- /dev/null
+++ /trunk/common/sha1.cpp Tue Jul 13 00:02:19 2010
@@ -0,0 +1,330 @@
+#include "sha1.h"
+#include "BigInt.h"
+
+SHA1::SHA1()
+{
+ Reset();
+}
+
+SHA1::~SHA1()
+{
+ // The destructor does nothing
+}
+
+void SHA1::Reset()
+{
+ Length_Low = 0;
+ Length_High = 0;
+ Message_Block_Index = 0;
+
+ H[0] = 0x67452301;
+ H[1] = 0xEFCDAB89;
+ H[2] = 0x98BADCFE;
+ H[3] = 0x10325476;
+ H[4] = 0xC3D2E1F0;
+
+ Computed = false;
+ Corrupted = false;
+}
+
+
+bool SHA1::Result(unsigned *message_digest_array)
+{
+ int i; // Counter
+
+ if (Corrupted)
+ {
+ return false;
+ }
+
+ if (!Computed)
+ {
+ PadMessage();
+ Computed = true;
+ }
+
+ for(i = 0; i < 5; i++)
+ {
+ message_digest_array[i] = H[i];
+ }
+
+ return true;
+}
+#include<iostream>
+#include<sstream>
+#include<exception>
+using namespace std;
+bool SHA1::Result( BigInt::Rossi &bint)
+{
+ unsigned int digest[5];
+ std::stringstream tmp;
+ //string tmp;
+ //ostream os;
+ try{
+ if ( ! Result(digest) )
+ {
+ throw exception();
+ }
+ }catch(...)
+ {
+ cerr<<"Result error"<<endl;
+ return 0;
+ }
+ ios::fmtflags flags =
cout.setf(ios::hex|ios::uppercase,ios::basefield);
+ tmp.setf(ios::uppercase);
+ tmp.setf(flags);
+ for(int i = 0; i < 5 ; i++)
+ {
+ //stringstream s;
+ tmp<<digest[i];
+ cout<<tmp<<endl;
+ //s>>tmp;
+ }
+// tmp.setf(flags);
+ string ret;
+ tmp>>ret;
+ const string out=ret;
+ bint=BigInt::Rossi(out,16);
+ return true;
+}
+
+
+void SHA1::Input( const unsigned char *message_array,
+ unsigned length)
+{
+ if (!length)
+ {
+ return;
+ }
+
+ if (Computed || Corrupted)
+ {
+ Corrupted = true;
+ return;
+ }
+
+ while(length-- && !Corrupted)
+ {
+ Message_Block[Message_Block_Index++] = (*message_array & 0xFF);
+
+ Length_Low += 8;
+ Length_Low &= 0xFFFFFFFF; // Force it to 32 bits
+ if (Length_Low == 0)
+ {
+ Length_High++;
+ Length_High &= 0xFFFFFFFF; // Force it to 32 bits
+ if (Length_High == 0)
+ {
+ Corrupted = true; // Message is too long
+ }
+ }
+
+ if (Message_Block_Index == 64)
+ {
+ ProcessMessageBlock();
+ }
+
+ message_array++;
+ }
+}
+
+void SHA1::Input( const char *message_array,
+ unsigned length)
+{
+ Input((unsigned char *) message_array, length);
+}
+
+void SHA1::Input(unsigned char message_element)
+{
+ Input(&message_element, 1);
+}
+
+void SHA1::Input(char message_element)
+{
+ Input((unsigned char *) &message_element, 1);
+}
+
+
+SHA1& SHA1::operator<<(const char *message_array)
+{
+ const char *p = message_array;
+
+ while(*p)
+ {
+ Input(*p);
+ p++;
+ }
+
+ return *this;
+}
+
+SHA1& SHA1::operator<<(const unsigned char *message_array)
+{
+ const unsigned char *p = message_array;
+
+ while(*p)
+ {
+ Input(*p);
+ p++;
+ }
+
+ return *this;
+}
+
+
+SHA1& SHA1::operator<<(const char message_element)
+{
+ Input((unsigned char *) &message_element, 1);
+
+ return *this;
+}
+
+SHA1& SHA1::operator<<(const unsigned char message_element)
+{
+ Input(&message_element, 1);
+
+ return *this;
+}
+
+void SHA1::ProcessMessageBlock()
+{
+ const unsigned K[] = { // Constants defined for SHA-1
+ 0x5A827999,
+ 0x6ED9EBA1,
+ 0x8F1BBCDC,
+ 0xCA62C1D6
+ };
+ int t; // Loop counter
+ unsigned temp; // Temporary word value
+ unsigned W[80]; // Word sequence
+ unsigned A, B, C, D, E; // Word buffers
+
+ /*
+ * Initialize the first 16 words in the array W
+ */
+ for(t = 0; t < 16; t++)
+ {
+ W[t] = ((unsigned) Message_Block[t * 4]) << 24;
+ W[t] |= ((unsigned) Message_Block[t * 4 + 1]) << 16;
+ W[t] |= ((unsigned) Message_Block[t * 4 + 2]) << 8;
+ W[t] |= ((unsigned) Message_Block[t * 4 + 3]);
+ }
+
+ for(t = 16; t < 80; t++)
+ {
+ W[t] = CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
+ }
+
+ A = H[0];
+ B = H[1];
+ C = H[2];
+ D = H[3];
+ E = H[4];
+
+ for(t = 0; t < 20; t++)
+ {
+ temp = CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] +
K[0];
+ temp &= 0xFFFFFFFF;
+ E = D;
+ D = C;
+ C = CircularShift(30,B);
+ B = A;
+ A = temp;
+ }
+
+ for(t = 20; t < 40; t++)
+ {
+ temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
+ temp &= 0xFFFFFFFF;
+ E = D;
+ D = C;
+ C = CircularShift(30,B);
+ B = A;
+ A = temp;
+ }
+
+ for(t = 40; t < 60; t++)
+ {
+ temp = CircularShift(5,A) +
+ ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
+ temp &= 0xFFFFFFFF;
+ E = D;
+ D = C;
+ C = CircularShift(30,B);
+ B = A;
+ A = temp;
+ }
+
+ for(t = 60; t < 80; t++)
+ {
+ temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
+ temp &= 0xFFFFFFFF;
+ E = D;
+ D = C;
+ C = CircularShift(30,B);
+ B = A;
+ A = temp;
+ }
+
+ H[0] = (H[0] + A) & 0xFFFFFFFF;
+ H[1] = (H[1] + B) & 0xFFFFFFFF;
+ H[2] = (H[2] + C) & 0xFFFFFFFF;
+ H[3] = (H[3] + D) & 0xFFFFFFFF;
+ H[4] = (H[4] + E) & 0xFFFFFFFF;
+
+ Message_Block_Index = 0;
+}
+
+
+void SHA1::PadMessage()
+{
+ /*
+ * Check to see if the current message block is too small to hold
+ * the initial padding bits and length. If so, we will pad the
+ * block, process it, and then continue padding into a second block.
+ */
+ if (Message_Block_Index > 55)
+ {
+ Message_Block[Message_Block_Index++] = 0x80;
+ while(Message_Block_Index < 64)
+ {
+ Message_Block[Message_Block_Index++] = 0;
+ }
+
+ ProcessMessageBlock();
+
+ while(Message_Block_Index < 56)
+ {
+ Message_Block[Message_Block_Index++] = 0;
+ }
+ }
+ else
+ {
+ Message_Block[Message_Block_Index++] = 0x80;
+ while(Message_Block_Index < 56)
+ {
+ Message_Block[Message_Block_Index++] = 0;
+ }
+
+ }
+
+ /*
+ * Store the message length as the last 8 octets
+ */
+ Message_Block[56] = (Length_High >> 24) & 0xFF;
+ Message_Block[57] = (Length_High >> 16) & 0xFF;
+ Message_Block[58] = (Length_High >> 8) & 0xFF;
+ Message_Block[59] = (Length_High) & 0xFF;
+ Message_Block[60] = (Length_Low >> 24) & 0xFF;
+ Message_Block[61] = (Length_Low >> 16) & 0xFF;
+ Message_Block[62] = (Length_Low >> 8) & 0xFF;
+ Message_Block[63] = (Length_Low) & 0xFF;
+
+ ProcessMessageBlock();
+}
+
+
+unsigned SHA1::CircularShift(int bits, unsigned word)
+{
+ return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >>
(32-bits));
+}
=======================================
--- /dev/null
+++ /trunk/include/common/Singleton.h Tue Jul 13 00:02:19 2010
@@ -0,0 +1,45 @@
+#ifndef __SINGLETON_H_
+#define __SINGLETON_H_
+#include<iostream>
+template <class Type>
+class Singleton
+{
+public:
+ static Singleton *getInstance();
+ virtual ~Singleton();
+ Type getVal();
+ void setVal(Type v);
+protected:
+ Singleton();
+ static Singleton *instance;
+ Type val;
+};
+
+template<class Type>
+Singleton<Type>::Singleton(){
+}
+template<class Type>
+Singleton<Type>::~Singleton()
+{
+ delete instance;
+}
+template<class Type>
+Singleton<Type>* Singleton<Type>::instance=new Singleton<Type>();
+
+template<class Type>
+Singleton<Type>* Singleton<Type>::getInstance()
+{
+ return instance;
+}
+template<class Type>
+void Singleton<Type>::setVal(Type v)
+{
+ val=v;
+}
+
+template<class Type>
+Type Singleton<Type>::getVal()
+{
+ return val;
+}
+#endif
=======================================
--- /dev/null
+++ /trunk/include/common/TThread.hh Tue Jul 13 00:02:19 2010
@@ -0,0 +1,195 @@
+#ifndef __TTHREAD_HH
+#define __TTHREAD_HH
+//
+// Project : ThreadPool
+// File : TThread.hh
+// Author : Ronald Kriemann
+// Purpose : baseclass for a thread-able class
+//
+// arch-tag: d09c570a-520a-48ce-b612-a813b50e87b4
+//
+
+#include <stdio.h>
+#include <pthread.h>
+
+////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////
+//
+// baseclass for all threaded classes
+// - defines basic interface
+//
+////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////
+
+class TThread
+{
+protected:
+ // thread-specific things
+ pthread_t _thread_id;
+ pthread_attr_t _thread_attr;
+
+ // is the thread running or not
+ bool _running;
+
+ // no of thread
+ int _thread_no;
+
+public:
+ ////////////////////////////////////////////
+ //
+ // constructor and destructor
+ //
+
+ TThread ( const int thread_no = -1 );
+
+ virtual ~TThread ();
+
+ ////////////////////////////////////////////
+ //
+ // access local data
+ //
+
+ int thread_no () const { return _thread_no; }
+ int proc_no () const { return _thread_no; }
+
+ void set_thread_no ( const int n );
+
+ // compare if given proc-no is local one
+ bool on_proc ( const int p ) const { return ((p == -1) || (_thread_no
== -1) || (p == _thread_no)); }
+
+ // resets running-status (used in _run_proc, see TThread.cc)
+ void reset_running () { _running = false; }
+
+ ////////////////////////////////////////////
+ //
+ // user-interface
+ //
+
+ // actually method the thread executes
+ virtual void run () = 0;
+
+ ////////////////////////////////////////////
+ //
+ // thread management
+ //
+
+ // create thread (actually start it)
+ void create ( const bool detached = false, const bool sscope = false );
+
+ // detach thread
+ void detach ();
+
+ // synchronise with thread (wait until finished)
+ void join ();
+
+ // request cancellation of thread
+ void cancel ();
+
+protected:
+ ////////////////////////////////////////////
+ //
+ // functions to be called by a thread itself
+ //
+
+ // terminate thread
+ void exit ();
+
+ // put thread to sleep for <sec> seconds
+ void sleep ( const double sec );
+};
+
+
+
+////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////
+//
+// wrapper for pthread_mutex
+//
+////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////
+
+class TMutex
+{
+protected:
+ // the mutex itself and the mutex-attr
+ pthread_mutex_t _mutex;
+ pthread_mutexattr_t _mutex_attr;
+
+public:
+ /////////////////////////////////////////////////
+ //
+ // constructor and destructor
+ //
+
+ TMutex ()
+ {
+ pthread_mutexattr_init( & _mutex_attr );
+ pthread_mutex_init( & _mutex, & _mutex_attr );
+ }
+
+ ~TMutex ()
+ {
+ pthread_mutex_destroy( & _mutex );
+ pthread_mutexattr_destroy( & _mutex_attr );
+ }
+
+ /////////////////////////////////////////////////
+ //
+ // usual behavior of a mutex
+ //
+
+ // lock and unlock mutex (return 0 on success)
+ int lock () { return pthread_mutex_lock( & _mutex ); }
+ int unlock () { return pthread_mutex_unlock( & _mutex ); }
+
+ // return true if mutex is locked, otherwise false
+ bool is_locked ()
+ {
+ if ( pthread_mutex_trylock( & _mutex ) != 0 )
+ return true;
+ else
+ {
+ unlock();
+ return false;
+ }// else
+ }
+};
+
+
+
+////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////
+//
+// class for a condition variable
+// - derived from mutex to allow locking of condition
+// to inspect or modify the predicate
+//
+////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////
+
+class TCondition : public TMutex
+{
+protected:
+ // our condition variable
+ pthread_cond_t _cond;
+
+public:
+ /////////////////////////////////////////////////
+ //
+ // constructor and destructor
+ //
+
+ TCondition () { pthread_cond_init( & _cond, NULL ); }
+ ~TCondition () { pthread_cond_destroy( & _cond ); }
+
+ /////////////////////////////////////////////////
+ //
+ // condition variable related methods
+ //
+
+ void wait () { pthread_cond_wait( & _cond, & _mutex ); }
+ void signal () { pthread_cond_signal( & _cond ); }
+ void broadcast () { pthread_cond_broadcast( & _cond ); }
+};
+
+#endif // __TTHREAD_HH
=======================================
--- /dev/null
+++ /trunk/include/common/TThreadPool.hh Tue Jul 13 00:02:19 2010
@@ -0,0 +1,143 @@
+#ifndef __TTHREADPOOL_HH
+#define __TTHREADPOOL_HH
+//
+// Project : ThreadPool
+// File : TThreadPool.hh
+// Author : Ronald Kriemann
+// Purpose : class for managing a pool of threads
+//
+// arch-tag: 7b363e2f-5623-471f-b0dd-55d7ac55b93e
+//
+
+#include "sll.hh"
+#include "TThread.hh"
+typedef unsigned int uint;
+
+// no specific processor
+#define NO_PROC ((uint) -1)
+
+// forward decl. for internal class
+class TPoolThr;
+
+//
+// takes jobs and executes them in threads
+//
+class TThreadPool
+{
+ friend class TPoolThr;
+
+public:
+ ///////////////////////////////////////////
+ //
+ // class for a job in the pool
+ //
+
+ class TJob
+ {
+ protected:
+ // number of processor this job was assigned to
+ const uint _job_no;
+
+ // mutex for synchronisation
+ TMutex _sync_mutex;
+
+ public:
+ // constructor
+ TJob ( const uint n = NO_PROC ) : _job_no(n) {}
+
+ virtual ~TJob ()
+ {
+ if ( _sync_mutex.is_locked() )
+ std::cerr << "(TJob) destructor : job is still running!" <<
std::endl;
+ }
+
+ // running method
+ virtual void run ( void * ptr ) = 0;
+
+ // access processor-number
+ uint job_no () const { return _job_no; }
+
+ // and mutex
+ void lock () { _sync_mutex.lock(); }
+ void unlock () { _sync_mutex.unlock(); }
+
+ // compare if given proc-no is local one
+ bool on_proc ( const uint p ) const
+ { return ((p == NO_PROC) || (_job_no == NO_PROC) || (p ==
_job_no)); }
+ };
+
+protected:
+ // maximum degree of parallelism
+ uint _max_parallel;
+
+ // array of threads, handled by pool
+ TPoolThr ** _threads;
+
+ // list of idle threads
+ sll< TPoolThr * > _idle_threads;
+
+ // condition for synchronisation of idle list
+ TCondition _idle_cond;
+
+public:
+ ///////////////////////////////////////////////
+ //
+ // constructor and destructor
+ //
+
+ TThreadPool ( const uint max_p );
+
+ ~TThreadPool ();
+
+ ///////////////////////////////////////////////
+ //
+ // access local variables
+ //
+
+ uint max_parallel () const { return _max_parallel; }
+
+ ///////////////////////////////////////////////
+ //
+ // run, stop and synch with job
+ //
+
+ void run ( TJob * job, void * ptr = NULL, const bool del = false );
+ void sync ( TJob * job );
+
+ void sync_all ();
+
+protected:
+ ///////////////////////////////////////////////
+ //
+ // manage pool threads
+ //
+
+ // return idle thread form pool
+ TPoolThr * get_idle ();
+
+ // insert idle thread into pool
+ void append_idle ( TPoolThr * t );
+};
+
+///////////////////////////////////////////////////
+//
+// to access the global thread-pool
+//
+///////////////////////////////////////////////////
+
+// init global thread_pool
+void tp_init ( const uint p );
+
+// run job
+void tp_run ( TThreadPool::TJob * job, void * ptr = NULL, const bool del =
false );
+
+// synchronise with specific job
+void tp_sync ( TThreadPool::TJob * job );
+
+// synchronise with all jobs
+void tp_sync_all ();
+
+// finish thread pool
+void tp_done ();
+
+#endif // __TTHREADPOOL_HH
=======================================
--- /dev/null
+++ /trunk/include/common/sha1.h Tue Jul 13 00:02:19 2010
@@ -0,0 +1,68 @@
+#ifndef _SHA1_H_
+#define _SHA1_H_
+
+#include "BigInt.h"
+
+class SHA1
+{
+
+public:
+
+ SHA1();
+ virtual ~SHA1();
+
+ /*
+ * Re-initialize the class
+ */
+ void Reset();
+
+ /*
+ * Returns the message digest
+ */
+ bool Result(unsigned *message_digest_array);
+ bool Result( BigInt::Rossi & bint);
+ /*
+ * Provide input to SHA1
+ */
+ void Input( const unsigned char *message_array,
+ unsigned length);
+ void Input( const char *message_array,
+ unsigned length);
+ void Input(unsigned char message_element);
+ void Input(char message_element);
+ SHA1& operator<<(const char *message_array);
+ SHA1& operator<<(const unsigned char *message_array);
+ SHA1& operator<<(const char message_element);
+ SHA1& operator<<(const unsigned char message_element);
+
+private:
+
+ /*
+ * Process the next 512 bits of the message
+ */
+ void ProcessMessageBlock();
+
+ /*
+ * Pads the current message block to 512 bits
+ */
+ void PadMessage();
+
+ /*
+ * Performs a circular left shift operation
+ */
+ inline unsigned CircularShift(int bits, unsigned word);
+
+ unsigned H[5]; // Message digest buffers
+
+ unsigned Length_Low; // Message length in bits
+ unsigned Length_High; // Message length in bits
+
+ unsigned char Message_Block[64]; // 512-bit message blocks
+ int Message_Block_Index; // Index into message block array
+
+ bool Computed; // Is the digest computed?
+ bool Corrupted; // Is the message digest corruped?
+
+};
+
+#endif
=======================================
--- /dev/null
+++ /trunk/include/common/sll.hh Tue Jul 13 00:02:19 2010
@@ -0,0 +1,368 @@
+#ifndef __SLL_HH
+#define __SLL_HH
+//
+// Project : ThreadPool
+// File : sll.hh
+// Purpose : Template class for a single concatenated list
+// Author : Ronald Kriemann
+//
+// arch-tag: ec87ea39-c827-428a-8377-d68641f4c26a
+//
+
+#include <assert.h>
+
+#include <iostream>
+
+////////////////////////////////////////////////////
+//
+// Template class list
+//
+template <class T>
+class sll
+{
+public:
+ ////////////////////////////////////////////////////
+ //
+ // class for a single item in the list
+ //
+
+ class item
+ {
+ public:
+ T _data;
+ item * _next;
+
+ public:
+ item ( const T & e,
+ item * n = NULL )
+ : _data( e ), _next( n )
+ {}
+ };
+
+ //////////////////////////////////////////////////////////
+ //
+ // class for an iterator for the previous list
+ //
+
+ class iterator
+ {
+ friend class sll<T>;
+
+ protected:
+ // currently selected item
+ item * _item;
+
+ public:
+ //
+ // constructor and destructor
+ //
+
+ iterator () : _item(NULL) {}
+ iterator ( item * item ) : _item(item) {}
+ iterator ( const iterator & iter ) : _item(iter._item) {}
+
+ // get status (end-of-list)
+ bool eol () const { return (_item == NULL); }
+
+ // increment
+ iterator & next () { return ++*this; }
+
+ // access actual element
+ T & data () { assert(_item != NULL); return
_item->_data; }
+ const T & data () const { assert(_item != NULL); return
_item->_data; }
+
+ //
+ // usual operators
+ //
+
+ // copy
+ iterator & operator = ( const iterator & i ) { _item = i._item;
return *this; }
+
+ // access items
+ T & operator () () { assert(_item != NULL); return
_item->_data; }
+ const T & operator () () const { assert(_item != NULL); return
_item->_data; }
+
+ // iterate (prefix/postfix)
+ iterator & operator ++ () { if (_item) _item = _item->_next;
return *this; }
+ iterator operator ++ (int) { iterator tmp(*this); ++*this; return
tmp; }
+
+ // dereference
+ T & operator * () { assert(_item != NULL); return
_item->_data; }
+ T * operator -> () { assert(_item != NULL); return &
_item->_data; }
+
+ // compare
+ bool operator == ( const iterator & i ) const { return
(_item == i._item); }
+ bool operator != ( const iterator & i ) const { return
(_item != i._item); }
+
+ protected:
+ // handle case when no item is present
+ void no_item () const
+ {
+ std::cout << "sll::iterator::no_item : no item in iterator
present" << std::endl;
+ }
+ };
+
+protected:
+ // first and last element of list
+ item * _first;
+ item * _last;
+
+ // size of the list aka number of stored items
+ unsigned int _size;
+
+public:
+ /////////////////////////////////////////////////
+ //
+ // constructors and destructors
+ //
+
+ sll () : _first(NULL), _last(NULL), _size(0) {}
+ sll ( const sll<T> & l ) : _first(NULL), _last(NULL), _size(0) { copy(
l ); }
+
+ ~sll () { if (_size > 0) clear(); }
+
+
+ /////////////////////////////////////////////////
+ //
+ // access member variables
+ //
+
+ unsigned int size () const { return _size; }
+
+ iterator first () { return _first; }
+ const iterator first () const { return _first; }
+
+ iterator last () { return _last; }
+ const iterator last () const { return _last; }
+
+
+ // same as above but access real data
+ T & first_item () { assert(_first != NULL); return
_first->_data; }
+ const T & first_item () const { assert(_first != NULL); return
_first->_data; }
+
+ T & last_item () { assert(_last != NULL); return
_last->_data; }
+ const T & last_item () const { assert(_last != NULL); return
_last->_data; }
+
+ /////////////////////////////////////////////////
+ //
+ // methods for manipulating list
+ //
+
+ //
+ // add a new object to the front of list
+ //
+ sll<T> & push_front ( const T & elem )
+ {
+ item * tmp = new item( elem, _first );
+
+ _first = tmp;
+ _size++;
+
+ if (_last == NULL)
+ _last = tmp;
+
+ return *this;
+ }
+
+ //
+ // add a new object to the back of list
+ //
+ sll<T> & push_back ( const T & elem )
+ {
+ item * tmp = new item( elem );
+
+ if (_last)
+ _last->_next = tmp;
+
+ _last = tmp;
+
+ if ( ! _first )
+ _first = tmp;
+
+ _size++;
+
+ return *this;
+ }
+
+ //
+ // remove given element from list
+ //
+ void remove ( const T & elem )
+ {
+ item * cur = _first;
+ item * old = NULL;
+
+ while (cur != NULL)
+ {
+ if (cur->_data == elem)
+ {
+ if (cur == _first)
+ _first = cur->_next;
+ else // (cur != first) => (old != NULL)
+ old->_next = cur->_next;
+
+ if (cur == _last)
+ _last = old;
+
+ delete cur;
+ _size--;
+
+ if ( old == NULL )
+ cur = _first;
+ else
+ cur = old->_next;
+ }// if
+ else
+ {
+ old = cur;
+ cur = cur->_next;
+ }// else
+ }// while
+ }
+
+ //
+ // remove given element (by iterator) from list
+ void
+ remove ( iterator & i )
+ {
+ assert(i._item != NULL);
+ remove(i._item->_data);
+ }
+
+ //
+ // remove first element of list
+ //
+ void pop_front ()
+ {
+ if (_first)
+ {
+ item * tmp = _first->_next;
+
+ delete _first;
+
+ if (tmp == NULL)
+ _last = NULL;
+
+ _first = tmp;
+
+ _size--;
+ }// if
+ }
+
+ //
+ // return first item and remove it from list
+ //
+ T behead ()
+ {
+ assert( _first != NULL );
+
+ T ret = _first->_data;
+ item * tmp = _first->_next;
+
+ delete _first;
+
+ if (tmp == NULL)
+ _last = NULL;
+
+ _first = tmp;
+
+ _size--;
+
+ return ret;
+ }
+
+ //
+ // remove all elements from list
+ //
+ void clear ()
+ {
+ if (_size == 0)
+ return;
+
+ item * tmp = _first;
+ item * next;
+
+ while (tmp != NULL)
+ {
+ next = tmp->_next;
+ delete tmp;
+ tmp = next;
+ }// while
+
+ _first = _last = NULL;
+ _size = 0;
+ }
+
+ //
+ // copy given list into local
+ //
+ void copy ( const sll< T > & list )
+ {
+ item * tmp = list._first;
+ item * old = NULL;
+ item * cur = list._first;
+
+ clear();
+
+ while ( tmp != NULL )
+ {
+ cur = new item( tmp->_data );
+
+ if ( _first == NULL )
+ _first = cur;
+
+ if ( old != NULL )
+ old->_next = cur;
+
+ old = cur;
+ tmp = tmp->_next;
+ }// while
+
+ _last = old;
+ _size = list._size;
+ }
+
+ /////////////////////////////////////////////////
+ //
+ // search/sort related methods
+ //
+
+ //
+ // look if element is in list
+ //
+ bool is_in ( const T & elem ) const
+ {
+ item * tmp = _first;
+
+ while ( tmp )
+ {
+ if (tmp->_data == elem)
+ return true;
+
+ tmp = tmp->_next;
+ }// while
+
+ return false;
+ }
+
+ /////////////////////////////////////////////////
+ //
+ // misc.
+ //
+
+ //
+ // assignment
+ //
+ sll< T > & operator = ( const sll< T > & list ) { copy( list ); return
*this; }
+
+protected:
+ //
+ // handle case when no item is present
+ //
+ void no_item () const
+ {
+ std::cout << "sll::no_item : I miss an item to play with" <<
std::endl;
+ }
+};
+
+#endif // __SLL_HH
=======================================
--- /dev/null
+++ /trunk/src/TRNG.cc Tue Jul 13 00:02:19 2010
@@ -0,0 +1,163 @@
+//
+// Project : misc
+// File : TRNG.cc
+// Author : Ronald Kriemann
+// Purpose : class for a random-number-generator
+//
+// arch-tag: bfc157b0-9bbc-4ed8-84e8-b71ed20a1d75
+//
+
+#include "TRNG.hh"
+
+/* A C-program for MT19937, with initialization improved 2002/2/10.*/
+/* Coded by Takuji Nishimura and Makoto Matsumoto. */
+/* This is a faster version by taking Shawn Cokus's optimization, */
+/* Matthe Bellew's simplification, Isaku Wada's real version. */
+
+/* Before using, initialize the state by using init_genrand(seed) */
+/* or init_by_array(init_key, key_length). */
+
+/* This library is free software. */
+/* 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. */
+
+/* Copyright (C) 1997, 2002 Makoto Matsumoto and Takuji Nishimura. */
+/* Any feedback is very welcome. */
+/* http://www.math.keio.ac.jp/matumoto/emt.html */
+/* email: matumoto@xxxxxxxxxxxxxxx */
+
+//
+// some defines/numbers wich are used in the RNG
+//
+
+#define N 624
+#define M 397
+#define MATRIX_A 0x9908b0dfUL // constant vector a
+#define UMASK 0x80000000UL // most significant w-r bits
+#define LMASK 0x7fffffffUL // least significant r bits
+#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
+#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
+
+////////////////////////////////////////////
+//
+// access rng
+//
+
+//
+// return random number between 0 and max
+//
+double
+TRNG::rand ( double max )
+{
+ unsigned long y;
+
+ if ( --_left == 0 )
+ next_state();
+
+ y = *_next++;
+
+ /* Tempering */
+ y ^= (y >> 11);
+ y ^= (y << 7) & 0x9d2c5680UL;
+ y ^= (y << 15) & 0xefc60000UL;
+ y ^= (y >> 18);
+
+ /* divided by 2^32-1 */
+ return max * double( y ) * (1.0/4294967295.0);
+}
+
+//
+// initialise rng
+//
+void
+TRNG::init ( unsigned long seed )
+{
+ _state[0] = seed & 0xffffffffUL;
+
+ for ( int j = 1; j < N; j++ )
+ {
+ _state[j] = (1812433253UL * (_state[j-1] ^ (_state[j-1] >> 30)) +
j);
+
+ // See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
+ // In the previous versions, MSBs of the seed affect
+ // only MSBs of the array state[].
+ // 2002/01/09 modified by Makoto Matsumoto
+ _state[j] &= 0xffffffffUL; // for >32 bit machines
+ }// for
+
+ _left = 1;
+}
+
+//
+// initialise rng by array
+//
+void
+TRNG::init ( unsigned long init_key[], unsigned long key_length )
+{
+ int i, j, k;
+
+ init( 19650218UL );
+
+ i = 1;
+ j = 0;
+ k = (int) (N > key_length ? N : key_length);
+
+ for ( ; k; k-- )
+ {
+ _state[i] = ((_state[i] ^ ((_state[i-1] ^ (_state[i-1] >> 30)) *
1664525UL))
+ + init_key[j] + j); // non linear
+ _state[i] &= 0xffffffffUL; // for WORDSIZE > 32 machines
+
+ i++;
+ j++;
+
+ if (i >= N)
+ {
+ _state[0] = _state[N-1];
+ i = 1;
+ }// if
+
+ if (((unsigned long) j) >= key_length )
+ j = 0;
+ }// for
+
+ for ( k = N-1; k; k-- )
+ {
+ _state[i] = ((_state[i] ^ ((_state[i-1] ^ (_state[i-1] >> 30)) *
1566083941UL))
+ - i); // non linear
+ _state[i] &= 0xffffffffUL; // for WORDSIZE > 32 machines
+
+ i++;
+
+ if (i >= N)
+ {
+ _state[0] = _state[N-1];
+ i = 1;
+ }// if
+ }// for
+
+ _state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array
+ _left = 1;
+}
+
+//
+// set next state in RNG
+//
+void
+TRNG::next_state ()
+{
+ unsigned long * p = _state;
+ int j;
+
+ _left = N;
+ _next = _state;
+
+ for ( j = N-M+1; --j; p++ )
+ *p = p[M] ^ TWIST(p[0], p[1]);
+
+ for ( j = M; --j; p++ )
+ *p = p[M-N] ^ TWIST(p[0], p[1]);
+
+ *p = p[M-N] ^ TWIST(p[0], _state[0]);
+}
=======================================
--- /dev/null
+++ /trunk/src/TRNG.hh Tue Jul 13 00:02:19 2010
@@ -0,0 +1,53 @@
+#ifndef __TRNG_HH
+#define __TRNG_HH
+//
+// Project : misc
+// File : TRNG.hh
+// Author : Ronald Kriemann
+// Purpose : class for a random-number-generator
+//
+// arch-tag: 21171758-77bd-4aaa-a499-aa8564e31e50
+//
+
+//
+//
+//
+class TRNG
+{
+protected:
+ // state of the RNG (624 == N from TRNG.cc)
+ unsigned long _state[624];
+ int _left;
+ unsigned long * _next;
+
+public:
+ ////////////////////////////////////////////
+ //
+ // constructor and destructor
+ //
+
+ TRNG ( unsigned long seed = 5489UL ) { if ( seed != 0 ) init( seed ); }
+
+ ////////////////////////////////////////////
+ //
+ // access rng
+ //
+
+ // return random number in the interval [0,max]
+ double rand ( double max = 1.0 );
+
+ // abbr. for rand
+ double operator () ( double max = 1.0 ) { return rand(max); }
+
+ // initialise rng by single seed (seed must not equal 0)
+ void init ( unsigned long seed );
+
+ // initialise rng by array
+ void init ( unsigned long init_key[], unsigned long length );
+
+protected:
+ // set next state in RNG
+ void next_state ();
+};
+
+#endif // __TRANDOM_HH
=======================================
--- /dev/null
+++ /trunk/src/TTimer.cc Tue Jul 13 00:02:19 2010
@@ -0,0 +1,109 @@
+//
+// Project : misc
+// File : timer.cc
+// Author : Ronald Kriemann
+// Purpose : class for a timer (for speed tests)
+//
+// arch-tag: ba44c609-187f-4fdf-9fc4-5112d138b760
+//
+
+#include <sys/times.h>
+#include <limits.h>
+
+#include <cmath>
+
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "TTimer.hh"
+
+//
+// get time of system (usertime or real)
+//
+double
+TTimer::system_time ()
+{
+ double sec, usec;
+
+ switch ( _type )
+ {
+ case REAL_TIME :
+ gettimeofday( & _timeval_data, NULL );
+
+ sec = _timeval_data.tv_sec;
+ usec = _timeval_data.tv_usec;
+
+ break;
+
+ case CPU_TIME :
+#ifdef SUNOS
+ _lwp_info( & _lwpinfo_data );
+ sec = _lwpinfo_data.lwp_utime.tv_sec +
_lwpinfo_data.lwp_stime.tv_sec;
+ usec = _lwpinfo_data.lwp_utime.tv_nsec +
_lwpinfo_data.lwp_stime.tv_nsec;
+ usec /= 1000;
+#else
+ getrusage( RUSAGE_SELF, & _rusage_data );
+ sec = _rusage_data.ru_utime.tv_sec +
_rusage_data.ru_stime.tv_sec;
+ usec = _rusage_data.ru_utime.tv_usec +
_rusage_data.ru_stime.tv_usec;
+#endif
+
+ break;
+
+ case USER_TIME :
+#ifdef SUNOS
+ _lwp_info( & _lwpinfo_data );
+ sec = _lwpinfo_data.lwp_utime.tv_sec;
+ usec = _lwpinfo_data.lwp_utime.tv_nsec;
+ usec /= 1000;
+#else
+ getrusage( RUSAGE_SELF, & _rusage_data );
+ sec = _rusage_data.ru_utime.tv_sec;
+ usec = _rusage_data.ru_utime.tv_usec;
+#endif
+ break;
+
+ case SYSTEM_TIME :
+#ifdef SUNOS
+ _lwp_info( & _lwpinfo_data );
+ sec = _lwpinfo_data.lwp_stime.tv_sec;
+ usec = _lwpinfo_data.lwp_stime.tv_nsec;
+ usec /= 1000;
+#else
+ getrusage( RUSAGE_SELF, & _rusage_data );
+ sec = _rusage_data.ru_stime.tv_sec;
+ usec = _rusage_data.ru_stime.tv_usec;
+#endif
+ break;
+
+ default :
+ cout << "TTimer::system_time : unsupported type of time" <<
std::endl;
+ sec = usec = 0.0;
+ }// switch
+
+ return sec + (usec * 1e-6);
+}
+
+//
+// stream output (only the time)
+//
+std::ostream &
+operator << ( std::ostream & os, const TTimer & timer )
+{
+ double time;
+ long seconds, mseconds;
+
+ time = 0 > timer.diff() ? 0 : timer.diff();
+ seconds = long( floor( time ) );
+ mseconds = long(floor( (time - double(seconds)) * 1000.0 + 0.5 ));
+
+ os << seconds << ',';
+
+ if (mseconds < 10) os << "00";
+ else if (mseconds < 100) os << "0";
+
+ os << mseconds << "s";
+
+ return os;
+}
=======================================
--- /dev/null
+++ /trunk/src/TTimer.hh Tue Jul 13 00:02:19 2010
@@ -0,0 +1,86 @@
+#ifndef __TIMER_HH
+#define __TIMER_HH
+//
+// Project : misc
+// File : timer.hh
+// Author : Ronald Kriemann
+// Purpose : class for a timer (for speed tests)
+//
+// arch-tag: b0ec2652-fb34-4496-97c1-5f83c1151f1b
+//
+
+#include <sys/time.h>
+#include <sys/resource.h>
+
+#ifdef SUNOS
+#include <sys/lwp.h>
+#endif
+
+#include <unistd.h>
+
+#include <iostream>
+
+// type for different timer-measurment
+typedef enum { REAL_TIME, CPU_TIME, USER_TIME, SYSTEM_TIME } timetype_t;
+
+//
+// the timer class
+//
+class TTimer
+{
+public:
+
+protected:
+ // start/stop times
+ double _start, _stop;
+
+ // temp structs
+ struct timeval _timeval_data;
+#ifdef SUNOS
+ struct lwpinfo _lwpinfo_data;
+#else
+ struct rusage _rusage_data;
+#endif
+
+ // what kind of time we should stop
+ timetype_t _type;
+
+public:
+ /////////////////////////////////////////////////
+ //
+ // constructor
+ //
+
+ TTimer ( timetype_t type = CPU_TIME ) : _start(0), _stop(0),
_type(type) {}
+
+ /////////////////////////////////////////////////
+ //
+ // compute times
+ //
+
+ // sets first/second timer (cpu- or real-time by _real-field)
+ TTimer & start () { _stop = _start = system_time(); return *this; }
+ TTimer & stop () { _stop = system_time(); return *this; }
+
+ // returns time between start and end in seconds
+ float diff () const { return _stop - _start; }
+
+ // get time of system (usertime or real)
+ double system_time ();
+
+ /////////////////////////////////////////////////
+ //
+ // output
+ //
+
+ // set type of time to measure and return *this (for output)
+ TTimer & real () { _type = REAL_TIME; return *this; }
+ TTimer & cpu () { _type = CPU_TIME; return *this; }
+ TTimer & user () { _type = USER_TIME; return *this; }
+ TTimer & sys () { _type = SYSTEM_TIME; return *this; }
+
+ // stream output (only the time)
+ friend std::ostream & operator << ( std::ostream & os, const TTimer
& timer );
+};
+
+#endif // __TIMER_HH
=======================================
--- /dev/null
+++ /trunk/src/main.cc Tue Jul 13 00:02:19 2010
@@ -0,0 +1,199 @@
+//
+// arch-tag: 2dcf5618-b51f-4a04-9f3a-86ab829bc7d8
+//
+
+#include <stdlib.h>
+#include <cmath>
+
+#include "TThreadPool.hh"
+#include "TTimer.hh"
+#include "TRNG.hh"
+
+//
+// for some benchmarking
+//
+
+static int job_number = 0;
+
+class TBenchJob : public TThreadPool::TJob
+{
+protected:
+ int _size;
+
+public:
+ TBenchJob ( int i, int s ) : TThreadPool::TJob( i ), _size(s) {}
+
+ virtual void run ( void * )
+ {
+ //std::cout << " starting thread (" << job_number++ << "), size =
" << _size << std::endl;
+
+ double * _matrix = new double[ _size * _size ];
+
+ for ( int i = 0; i < _size; i++ )
+ {
+ for ( int j = 0; j < _size; j++ )
+ {
+ _matrix[ (i*_size) + j ] = std::sin( double(j) * M_PI *
std::cos( double(i) ));
+ }// for
+ }// for
+
+ delete _matrix;
+ }
+};
+
+#define MAX_SIZE 1000
+#define MAX_RAND 500
+
+void
+recursion ( int level, TRNG & rng )
+{
+ if ( level == 0 )
+ {
+ TBenchJob * job = new TBenchJob( -1, int(rng.rand( MAX_RAND )) +
MAX_SIZE );
+
+ tp_run( job, NULL, true );
+ }// if
+ else
+ {
+ recursion( level-1, rng );
+ recursion( level-1, rng );
+ recursion( level-1, rng );
+ recursion( level-1, rng );
+ }// else
+}
+
+void
+bench1 ( int argc, char ** argv )
+{
+ TRNG rng;
+ int i = 1;
+ int thr_count = 16;
+ int rec_depth = 6;
+
+ if ( argc > 1 ) thr_count = atoi( argv[1] );
+ if ( argc > 2 ) rec_depth = atoi( argv[2] );
+
+ for ( int j = 0; j < rec_depth; j++ )
+ i *= 4;
+
+ tp_init( thr_count );
+
+ std::cout << "executing " << i << " jobs using " << thr_count << "
thread(s)" << std::endl;
+
+ TTimer timer( REAL_TIME );
+
+ timer.start();
+
+ recursion( rec_depth, rng );
+
+ tp_sync_all();
+
+ timer.stop();
+ std::cout << "time for recursion = " << timer << std::endl;
+}
+
+class TBench2Job : public TThreadPool::TJob
+{
+public:
+ TBench2Job ( int i ) : TThreadPool::TJob( i ) {}
+
+ virtual void run ( void * )
+ {
+ // do nothing
+ }
+};
+
+class TBench2Thr : public TThread
+{
+public:
+ TBench2Thr ( int i ) : TThread( i ) {}
+
+ virtual void run ()
+ {
+ // do nothing
+ }
+};
+
+void
+bench2 ( int argc, char ** argv )
+{
+ int max_jobs = 10000;
+
+ tp_init( 4 );
+
+ TTimer timer( REAL_TIME );
+
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Job * job = new TBench2Job( i );
+ delete job;
+ }
+
+ timer.stop();
+ std::cout << "time to create jobs = " << timer << std::endl;
+
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Thr * job = new TBench2Thr( i );
+ delete job;
+ }
+
+ timer.stop();
+ std::cout << "time to create threads = " << timer << std::endl;
+
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Job * job = new TBench2Job( i );
+
+ tp_run( job );
+ tp_sync( job );
+
+ delete job;
+ }
+
+ timer.stop();
+ std::cout << "time for thread pool = " << timer << std::endl;
+
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Thr * job = new TBench2Thr( i );
+
+ job->create( false, false );
+ job->join();
+
+ delete job;
+ }
+
+ timer.stop();
+
+ std::cout << "time for lwp-threads = " << timer << std::endl;
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Thr * job = new TBench2Thr( i );
+
+ job->create( false, true );
+ job->join();
+
+ delete job;
+ }
+
+ timer.stop();
+ std::cout << "time for hwp-threads = " << timer << std::endl;
+}
+
+int
+main ( int argc, char ** argv )
+{
+ bench1( argc, argv );
+ // bench2( argc, argv );
+}
=======================================
--- /trunk/Makefile Wed Jul 7 18:38:12 2010
+++ /trunk/Makefile Tue Jul 13 00:02:19 2010
@@ -1,13 +1,17 @@
CC=gcc
CXX=g++
MAKE=gmake
-all:
+all : clean
@[ -d lib ] || mkdir lib
@[ -d bin ] || mkdir bin
$(MAKE) -C common
$(MAKE) -C src
-clean:
+ @cp -f lib/* bin/
+ @cp -f lib/* ./
+clean :
$(MAKE) -C common clean
$(MAKE) -C src clean
@[ ! -d lib ] || rm -rf lib
@[ ! -d bin ] || rm -rf bin
+ @[ ! -f *.so ] || rm -f *.so
+ @[ ! -f kadpeer ] || rm -f kadpeer
=======================================
--- /trunk/common/BigInt.cpp Wed Jul 7 05:23:52 2010
+++ /trunk/common/BigInt.cpp Tue Jul 13 00:02:19 2010
@@ -1,5 +1,5 @@
#include "BigInt.h"
-
+#include <exception>
// =================
// Constants
@@ -1986,3 +1986,23 @@
return o_os << i_ins.toStrDec();
//return o_os << i_ins.toStr0xHex();
}
+
+int rossiLog( const BigInt::Rossi & bint)
+{
+ BigInt::Rossi tmp(bint);
+ try {
+ if ( tmp <= BigInt::Rossi( 0 ))
+ throw std::exception();
+ int count=0;
+ while ( tmp > BigInt::Rossi( 0 ))
+ {
+ tmp>>=1;
+ count++;
+ }
+ return count-1;
+ }catch (...)
+ {
+ std::cerr<<"Log operator error"<<std::endl;
+ exit(1);
+ }
+}
=======================================
--- /trunk/common/Makefile Wed Jul 7 18:38:12 2010
+++ /trunk/common/Makefile Tue Jul 13 00:02:19 2010
@@ -1,9 +1,9 @@
CC=gcc
CXX=g++
-CFLAGES= -g3 -Wno-deprecated
+CFLAGES= -Wno-deprecated -fPIC
INCDIR= -I../include/\
-I../include/common
-LIBS=
+LIBS=-lpthread
SRC=$(shell ls *.cpp)
OBJ=$(SRC:.cpp=.o)
LIBOUTPUT=libcommon.so
=======================================
--- /trunk/include/common/BigInt.h Thu Jul 1 22:40:43 2010
+++ /trunk/include/common/BigInt.h Tue Jul 13 00:02:19 2010
@@ -404,7 +404,9 @@
bool operator> (const Rossi& i_arg) const;
bool operator<= (const Rossi& i_arg) const;
bool operator>= (const Rossi& i_arg) const;
-
+
+ // --- Log fucntion
+ friend int rossiLog( const BigInt::Rossi & bint);
// --- Show functions ---
std::string toStrHex (const std::string& i_prefix = "") const;
std::string toStr0xHex () const;
=======================================
--- /trunk/madmedia/Makefile Wed Jul 7 05:23:52 2010
+++ /trunk/madmedia/Makefile Tue Jul 13 00:02:19 2010
@@ -1,15 +1,15 @@
CC=gcc
CXX=g++
-CFLAGES= -g3 -Wno-deprecated
+CFLAGES= -g3 -Wno-deprecated -fPIC
INCDIR= -I../include/\
-I../include/common
LIBS=
SRC=$(shell ls *.cpp)
OBJ=$(SRC:.cpp=.o)
-COMLIB=libmadmelia.so
-all:clean $(COMLIB)
-$(COMLIB):$(OBJ)
- $(CXX) $(CFLAGES) $(INCDIR) -shared -o $(COMLIB) $^ $(LIBS)
+LIBMADMELIA=libmadmelia.so
+all:clean $(LIBMADMELIA)
+$(LIBMADMELIA):$(OBJ)
+ $(CXX) $(CFLAGES) $(INCDIR) -shared -o $(LIBMADMELIA) $^ $(LIBS)
@cp -f *.so ../lib
%.o:%.cpp
$(CXX) $(CFLAGES) $(INCDIR) -c $< -o $@ $(LIBS)
@@ -20,5 +20,5 @@
clean:
@ rm -rf *.o
@ rm -rf *.so
- @ rm -f ../lib/$(COMLIB)
- @ rm -f $(COMLIB)
+ @ rm -f ../lib/$(LIBMADMELIA)
+ @ rm -f $(LIBMADMELIA)
=======================================
--- /trunk/src/main.cpp Fri Jul 2 21:55:10 2010
+++ /trunk/src/main.cpp Tue Jul 13 00:02:19 2010
@@ -1,31 +1,159 @@
-#include<iostream>
-#include<string>
-#include"common/BigInt.h"
-#include"common/HashMap.h"
-#include<ctime>
+/*
+ * shatest.cpp
+ *
+ * Copyright (C) 1998, 2009
+ * Paul E. Jones <paulej@xxxxxxxxxxxxxx>
+ * All Rights Reserved
+ *
+
*****************************************************************************
+ * $Id: shatest.cpp 12 2009-06-22 19:34:25Z paulej $
+
*****************************************************************************
+ *
+ * Description:
+ * This file will exercise the SHA1 class and perform the three
+ * tests documented in FIPS PUB 180-1.
+ *
+ * Portability Issues:
+ * None.
+ *
+ */
+
+#include <iostream>
+#include "sha1.h"
+
using namespace std;
+
+/*
+ * Define patterns for testing
+ */
+#define TESTA "abc"
+#define TESTB "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+
+/*
+ * Function prototype
+ */
+void DisplayMessageDigest(unsigned *message_digest);
+
+/*
+ * main
+ *
+ * Description:
+ * This is the entry point for the program
+ *
+ * Parameters:
+ * None.
+ *
+ * Returns:
+ * Nothing.
+ *
+ * Comments:
+ *
+ */
int main()
{
- clock_t start,end;
- start=clock();
- BigInt::Rossi ret(1);
- BigInt::Rossi sum(0);
- for( int i=2;i<=1000;i++)
- {
- ret =ret*i;
- sum+=ret;
- }
- cout<<sum<<endl;
- end=clock();
- StringMap<int> kmap;
- kmap.set("1",10);
- kmap.set("2",20);
- kmap.set("2",30);
- StringMap<int>::const_iterator it;
- for( it = kmap.begin(); it!=kmap.end(); it++ )
- {
- cout<<(it->first)<<"\t"<<it->second->getValue()<<endl;
- }
- cout<<"Run Time:"<<(end-start)*1.0/CLOCKS_PER_SEC<<endl;
+ SHA1 sha;
+ unsigned message_digest[5];
+
+ /*
+ * Perform test A
+ */
+ cout << endl << "Test A: 'abc'" << endl;
+
+ sha.Reset();
+ sha << TESTA;
+
+ if (!sha.Result(message_digest))
+ {
+ cerr << "ERROR-- could not compute message digest" << endl;
+ }
+
+ else
+ {
+ DisplayMessageDigest(message_digest);
+ cout << "Should match:" << endl;
+ cout << '\t' << "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D" <<
endl;
+
+
+ }
+ sha.Reset();
+ sha<<TESTA;
+ BigInt::Rossi digest;
+ if ( sha.Result(digest) )
+ {
+ cout<<digest.toStrHex()<<endl;
+ cout<<digest.toStrDec()<<endl;
+ }
+ /*
+ * Perform test B
+ */
+ cout << endl << "Test B: " << TESTB << endl;
+
+ sha.Reset();
+ sha << TESTB;
+
+ if (!sha.Result(message_digest))
+ {
+ cerr << "ERROR-- could not compute message digest" << endl;
+ }
+ else
+ {
+ DisplayMessageDigest(message_digest);
+ cout << "Should match:" << endl;
+ cout << '\t' << "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1" <<
endl;
+ }
+
+ /*
+ * Perform test C
+ */
+ cout << endl << "Test C: One million 'a' characters" << endl;
+
+ sha.Reset();
+ for(int i = 1; i <= 1000000; i++) sha.Input('a');
+
+ if (!sha.Result(message_digest))
+ {
+ cerr << "ERROR-- could not compute message digest" << endl;
+ }
+ else
+ {
+ DisplayMessageDigest(message_digest);
+ cout << "Should match:" << endl;
+ cout << '\t' << "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F" <<
endl;
+ }
+
return 0;
}
+
+/*
+ * DisplayMessageDigest
+ *
+ * Description:
+ * Display Message Digest array
+ *
+ * Parameters:
+ * None.
+ *
+ * Returns:
+ * Nothing.
+ *
+ * Comments:
+ *
+ */
+void DisplayMessageDigest(unsigned *message_digest)
+{
+ ios::fmtflags flags;
+
+ cout << '\t';
+
+ flags = cout.setf(ios::hex|ios::uppercase,ios::basefield);
+ cout.setf(ios::uppercase);
+
+ for(int i = 0; i < 5 ; i++)
+ {
+ cout << message_digest[i] << ' ';
+ }
+
+ cout << endl;
+
+ cout.setf(flags);
+}
--
Thanks & Regards
Linux Developer : Devil Wang