Skip to content

Commit 3694a85

Browse files
committed
gclib code sync update
1 parent 526866c commit 3694a85

5 files changed

Lines changed: 455 additions & 189 deletions

File tree

gclib/GThreads.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ freely, subject to the following restrictions:
3131
#endif
3232
#include <string.h>
3333

34-
//namespace tthread {
35-
3634
//------------------------------------------------------------------------------
3735
// condition_variable
3836
//------------------------------------------------------------------------------
@@ -181,7 +179,7 @@ struct _thread_start_info {
181179
GThread * mThread; ///< Pointer to the thread object.
182180
*/
183181
GThreadData threadData;
184-
//void (*mFunction)(void *, GThread*);
182+
//void (*mFunction)(void *, GThread*);
185183
void (*mFunction)(void *); ///< Pointer to the function to be executed.
186184
void (*gtFunction)(GThreadData&); //custom variant, passing GThreadData
187185
//handy constructors:
@@ -190,12 +188,12 @@ struct _thread_start_info {
190188
gtFunction=NULL;
191189
}
192190
_thread_start_info(GThread* t, void (*aFunc)(void *), void* udata):
193-
threadData(udata, t) {
191+
threadData(udata, t) {
194192
mFunction=aFunc;
195193
gtFunction=NULL;
196194
}
197195
_thread_start_info(GThread* t, void (*gtFunc)(GThreadData &), void* udata):
198-
threadData(udata, t) {
196+
threadData(udata, t) {
199197
mFunction=NULL;
200198
gtFunction=gtFunc;
201199
}
@@ -225,8 +223,8 @@ void * GThread::wrapper_function(void * aArg)
225223
}
226224
*/
227225
//ti->mFunction(ti->mArg, ti->mThread);
228-
229-
//cheap trick to pass current GThread pointer
226+
227+
//cheap trick to pass current GThread pointer
230228
//when the user doesn't pass anything
231229
if (ti->gtFunction) {
232230
ti->gtFunction(ti->threadData);
@@ -297,7 +295,7 @@ void GThread::initStart(void* tidata, size_t stacksize) {
297295

298296
GThread::GThread(void (*aFunction)(void *), void * aArg, size_t stacksize): mId(0), mHandle(0), mNotAThread(true)
299297
#if defined(_GTHREADS_WIN32_)
300-
, mWin32ThreadID(0)
298+
, mWin32ThreadID(0)
301299
#endif
302300
{
303301
kickStart(aFunction, aArg, stacksize);
@@ -312,8 +310,8 @@ void GThread::kickStart(void (*aFunction)(void *), void * aArg, size_t stacksize
312310
initStart(ti, stacksize);
313311
}
314312

315-
//custom alternate constructor (non-C++11 compatible), passing GThreadData back to the
316-
//user function in order to easily retrieve current GThread object
313+
//custom alternate constructor (non-C++11 compatible), passing GThreadData back to the
314+
//user function in order to easily retrieve current GThread object
317315
//(better alternative to this_thread)
318316
GThread::GThread(void (*gFunction)(GThreadData& thread_data), void * aArg, size_t stacksize) {
319317
kickStart(gFunction, aArg, stacksize);
@@ -375,7 +373,7 @@ void GThread::detach()
375373

376374
void GThread::wait_all() {
377375
while (GThread::num_running()>0)
378-
this_thread::sleep_for(chrono::milliseconds(4));
376+
current_thread::sleep_for(2);
379377
}
380378

381379

@@ -394,7 +392,7 @@ int GThread::get_id() const
394392
return 0; //FIXME: don't use this
395393
else
396394
return mId;
397-
/*
395+
/*
398396
#if defined(_GTHREADS_WIN32_)
399397
return id((unsigned long int) mWin32ThreadID);
400398
#elif defined(_GTHREADS_POSIX_)
@@ -421,12 +419,13 @@ unsigned GThread::hardware_concurrency()
421419
}
422420

423421

422+
423+
424424
//------------------------------------------------------------------------------
425-
// this_thread
425+
// current_thread
426426
//------------------------------------------------------------------------------
427427
/*
428-
int this_thread::get_id()
429-
{
428+
int current_thread::get_id() {
430429
#if defined(_GTHREADS_WIN32_)
431430
return thread::id((unsigned long int) GetCurrentThreadId());
432431
#elif defined(_GTHREADS_POSIX_)
@@ -435,3 +434,22 @@ int this_thread::get_id()
435434
}
436435
*/
437436

437+
void current_thread::yield() {
438+
#if defined(_GTHREADS_WIN32_)
439+
Sleep(0);
440+
#else
441+
sched_yield();
442+
#endif
443+
}
444+
// Blocks the calling thread for a certain time (given in milliseconds)
445+
// Example usage:
446+
// // Sleep for 100 milliseconds:
447+
// current_thread::sleep_for(100);
448+
void current_thread::sleep_for(const int32_t mstime) {
449+
#if defined(_GTHREADS_WIN32_)
450+
Sleep(mstime);
451+
#else
452+
usleep((useconds_t)(mstime*1000));
453+
#endif
454+
}
455+

gclib/GThreads.h

Lines changed: 18 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
#ifndef _GTHREADS_
2+
#define _GTHREADS_
3+
14
/*
2-
GThread - multi-platform thread support
3-
this is heavily based on the source code of TinyThread++ 1.0 package by Marcus Geelnard
4-
(with only minor modifications and namespace changes)
5+
GThread - multi-platform threads utility class
6+
This is heavily based on the source code of TinyThread++ 1.0 package
7+
by Marcus Geelnard (with only minor modifications),
8+
so all merits for the code go to Marcus, except the bugs which
9+
were probably introduced by me.
510
6-
Original Copyright notice below
11+
Original Copyright notice below
712
*/
813

914
/*
@@ -30,9 +35,6 @@ freely, subject to the following restrictions:
3035
*/
3136

3237

33-
#ifndef _GTHREADS_
34-
#define _GTHREADS_
35-
3638
/// @file
3739
/// @mainpage TinyThread++ API Reference
3840
///
@@ -167,13 +169,6 @@ freely, subject to the following restrictions:
167169
#define GTHREADS_NO_TLS
168170
#endif
169171

170-
171-
/// Main name space for TinyThread++.
172-
/// This namespace is more or less equivalent to the \c std namespace for the
173-
/// C++11 thread classes. For instance, the tthread::mutex class corresponds to
174-
/// the std::mutex class.
175-
//namespace tthread {
176-
177172
void gthreads_errExit(int err, const char* msg=NULL);
178173

179174
#define pthreads_err(msg) \
@@ -813,85 +808,26 @@ class GThread {
813808
#endif
814809
};
815810

816-
/// Minimal implementation of the \c ratio class. This class provides enough
817-
/// functionality to implement some basic \c chrono classes.
818-
template <int64_t N, int64_t D = 1> class ratio {
819-
public:
820-
static double _as_double() { return double(N) / double(D); }
821-
};
822-
823-
/// Minimal implementation of the \c chrono namespace.
824-
/// The \c chrono namespace provides types for specifying time intervals.
825-
namespace chrono {
826-
/// Duration template class. This class provides enough functionality to
827-
/// implement \c this_thread::sleep_for().
828-
template <class _Rep, class _Period = ratio<1> > class duration {
829-
private:
830-
_Rep rep_;
831-
public:
832-
typedef _Rep rep;
833-
typedef _Period period;
834-
835-
/// Construct a duration object with the given duration.
836-
template <class _Rep2>
837-
explicit duration(const _Rep2& r) : rep_(r) {};
838-
839-
/// Return the value of the duration object.
840-
rep count() const
841-
{
842-
return rep_;
843-
}
844-
};
845-
846-
// Standard duration types.
847-
typedef duration<int64_t, ratio<1, 1000000000> > nanoseconds; ///< Duration with the unit nanoseconds.
848-
typedef duration<int64_t, ratio<1, 1000000> > microseconds; ///< Duration with the unit microseconds.
849-
typedef duration<int64_t, ratio<1, 1000> > milliseconds; ///< Duration with the unit milliseconds.
850-
typedef duration<int64_t> seconds; ///< Duration with the unit seconds.
851-
typedef duration<int64_t, ratio<60> > minutes; ///< Duration with the unit minutes.
852-
typedef duration<int64_t, ratio<3600> > hours; ///< Duration with the unit hours.
853-
}
854811

855-
/// The namespace \c this_thread provides methods for dealing with the
812+
/// The namespace "current_thread" provides methods for dealing with the
856813
/// calling thread.
857-
namespace this_thread {
814+
namespace current_thread {
858815
/// Return the thread ID of the calling thread.
859816
//thread::id get_id(); //this can be slow, better not use it
860817

861818
/// Yield execution to another thread.
862819
/// Offers the operating system the opportunity to schedule another thread
863820
/// that is ready to run on the current processor.
864-
inline void yield()
865-
{
866-
#if defined(_GTHREADS_WIN32_)
867-
Sleep(0);
868-
#else
869-
sched_yield();
870-
#endif
871-
}
872-
873-
/// Blocks the calling thread for a period of time.
874-
/// @param[in] aTime Minimum time to put the thread to sleep.
875-
/// Example usage:
876-
/// @code
877-
/// // Sleep for 100 milliseconds
878-
/// this_thread::sleep_for(chrono::milliseconds(100));
879-
/// @endcode
880-
/// @note Supported duration types are: nanoseconds, microseconds,
881-
/// milliseconds, seconds, minutes and hours.
882-
template <class _Rep, class _Period> void sleep_for(const chrono::duration<_Rep, _Period>& aTime)
883-
{
884-
#if defined(_GTHREADS_WIN32_)
885-
Sleep(int(double(aTime.count()) * (1000.0 * _Period::_as_double()) + 0.5));
886-
#else
887-
usleep(int(double(aTime.count()) * (1000000.0 * _Period::_as_double()) + 0.5));
888-
#endif
889-
}
821+
void yield();
822+
823+
// Blocks the calling thread for a certain time (given in milliseconds)
824+
// Example usage:
825+
// // Sleep for 100 milliseconds:
826+
// current_thread::sleep_for(100);
827+
void sleep_for(const int32_t mstime);
890828
}
891829

892830
// Define/macro cleanup
893831
#undef _GTHREADS_DISABLE_ASSIGNMENT
894832

895-
896-
897833
#endif // _GTHREADS_

0 commit comments

Comments
 (0)