Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jonathan Wilkes
flext
Commits
c057774b
Commit
c057774b
authored
Jul 08, 2003
by
thomas
Browse files
no message
git-svn-id:
https://svn.grrrr.org/ext/trunk@665
4d9ac71a-51e6-0310-8455-cad1006bcd31
parent
b64c8578
Changes
2
Hide whitespace changes
Inline
Side-by-side
source/flsupport.h
View file @
c057774b
...
...
@@ -16,13 +16,7 @@ WARRANTIES, see the file, "license.txt," in this distribution.
#define __FLSUPPORT_H
#include "flstdc.h"
#include <time.h>
#if FLEXT_OS == FLEXT_OS_WIN
#include <sys/timeb.h>
#elif FLEXT_OS == FLEXT_OS_LINUX
#include <unistd.h>
#endif
class
FLEXT_SHARE
flext_base
;
...
...
@@ -752,10 +746,10 @@ public:
//! Lock thread mutex
bool
Lock
()
{
return
pthread_mutex_lock
(
&
mutex
)
==
0
;
}
/*!
\brief
Wait to lock thread mutex.
/*! Wait to lock thread mutex.
\todo Implement!
*/
bool
WaitForLock
(
double
tm
)
{
return
pthread_mutex_lock
(
&
mutex
)
==
0
;
}
//
bool WaitForLock(double tm) { return pthread_mutex_lock(&mutex) == 0; }
//! Try to lock, but don't wait
bool
TryLock
()
{
return
pthread_mutex_trylock
(
&
mutex
)
==
0
;
}
//! Unlock thread mutex
...
...
@@ -776,7 +770,7 @@ public:
//! Lock thread mutex
bool
Lock
()
{
return
MPEnterCriticalRegion
(
crit
,
kDurationForever
)
==
noErr
;
}
//! Wait to lock thread mutex
bool
WaitForLock
(
double
tm
)
{
return
MPEnterCriticalRegion
(
crit
,
tm
*
kDurationMicrosecond
*
1.e6
)
==
noErr
;
}
//
bool WaitForLock(double tm) { return MPEnterCriticalRegion(crit,tm*kDurationMicrosecond*1.e6) == noErr; }
//! Try to lock, but don't wait
bool
TryLock
()
{
return
MPEnterCriticalRegion
(
crit
,
kDurationImmediate
)
==
noErr
;
}
//! Unlock thread mutex
...
...
@@ -803,58 +797,19 @@ public:
~
ThrCond
()
{
pthread_cond_destroy
(
&
cond
);
}
//! Wait for condition
bool
Wait
()
{
Lock
();
bool
ret
=
pthread_cond_wait
(
&
cond
,
&
mutex
)
==
0
;
Unlock
();
return
ret
;
}
bool
Wait
();
/*!
\brief
Wait for condition (for a certain time).
/*! Wait for condition (for a certain time).
\param ftime Wait time in seconds
\ret 0 = signalled, 1 = timed out
\remark If ftime = 0 this may suck away your cpu if used in a signalled loop.
\remark The time resolution of the implementation is required to be at least ms.
*/
bool
TimedWait
(
double
ftime
)
{
timespec
tm
;
#if FLEXT_OS == FLEXT_OS_WIN
_timeb
tmb
;
_ftime
(
&
tmb
);
tm
.
tv_nsec
=
tmb
.
millitm
*
1000000
;
tm
.
tv_sec
=
tmb
.
time
;
#else
#if 0 // find out when the following is defined
clock_gettime(CLOCK_REALTIME,tm);
#else
struct
timeval
tp
;
gettimeofday
(
&
tp
,
NULL
);
tm
.
tv_nsec
=
tp
.
tv_usec
*
1000
;
tm
.
tv_sec
=
tp
.
tv_sec
;
#endif
#endif
tm
.
tv_nsec
+=
(
long
)((
ftime
-
(
long
)
ftime
)
*
1.e9
);
long
nns
=
tm
.
tv_nsec
%
1000000000
;
tm
.
tv_sec
+=
(
long
)
ftime
+
(
tm
.
tv_nsec
-
nns
)
/
1000000000
;
tm
.
tv_nsec
=
nns
;
Lock
();
bool
ret
=
pthread_cond_timedwait
(
&
cond
,
&
mutex
,
&
tm
)
==
0
;
Unlock
();
return
ret
;
}
bool
TimedWait
(
double
ftime
);
//! Signal condition
bool
Signal
()
{
Lock
();
bool
ret
=
pthread_cond_signal
(
&
cond
)
==
0
;
Unlock
();
return
ret
;
}
//! Broadcast condition
// int Broadcast() { return pthread_cond_broadcast(&cond); }
bool
Signal
();
protected:
pthread_cond_t
cond
;
};
...
...
@@ -876,8 +831,7 @@ public:
//! Signal condition
bool
Signal
()
{
return
MPSetEvent
(
ev
,
1
)
==
noErr
;
}
// one bit needs to be set at least
//! Broadcast condition
// int Broadcast() { return pthread_cond_broadcast(&cond); }
protected:
MPEventID
ev
;
};
...
...
source/flthr.cpp
View file @
c057774b
...
...
@@ -17,6 +17,16 @@ WARRANTIES, see the file, "license.txt," in this distribution.
#ifdef FLEXT_THREADS
#include <time.h>
#if FLEXT_OS == FLEXT_OS_WIN
#include <sys/timeb.h>
#elif FLEXT_OS == FLEXT_OS_LINUX
#include <sys/time.h>
#include <unistd.h>
#endif
#include <errno.h>
//! Thread id of system thread
...
...
@@ -492,4 +502,52 @@ flext_base::thr_entry::thr_entry(void (*m)(thr_params *),thr_params *p,thrid_t i
{}
#if FLEXT_THREADS == FLEXT_THR_POSIX
bool
flext
::
ThrCond
::
Wait
()
{
Lock
();
bool
ret
=
pthread_cond_wait
(
&
cond
,
&
mutex
)
==
0
;
Unlock
();
return
ret
;
}
bool
flext
::
ThrCond
::
TimedWait
(
double
ftime
)
{
timespec
tm
;
#if FLEXT_OS == FLEXT_OS_WIN
_timeb
tmb
;
_ftime
(
&
tmb
);
tm
.
tv_nsec
=
tmb
.
millitm
*
1000000
;
tm
.
tv_sec
=
tmb
.
time
;
#else
#if 0 // find out when the following is defined
clock_gettime(CLOCK_REALTIME,tm);
#else
struct
timeval
tp
;
gettimeofday
(
&
tp
,
NULL
);
tm
.
tv_nsec
=
tp
.
tv_usec
*
1000
;
tm
.
tv_sec
=
tp
.
tv_sec
;
#endif
#endif
tm
.
tv_nsec
+=
(
long
)((
ftime
-
(
long
)
ftime
)
*
1.e9
);
long
nns
=
tm
.
tv_nsec
%
1000000000
;
tm
.
tv_sec
+=
(
long
)
ftime
+
(
tm
.
tv_nsec
-
nns
)
/
1000000000
;
tm
.
tv_nsec
=
nns
;
Lock
();
bool
ret
=
pthread_cond_timedwait
(
&
cond
,
&
mutex
,
&
tm
)
==
0
;
Unlock
();
return
ret
;
}
bool
flext
::
ThrCond
::
Signal
()
{
Lock
();
bool
ret
=
pthread_cond_signal
(
&
cond
)
==
0
;
Unlock
();
return
ret
;
}
#endif
#endif // FLEXT_THREADS
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment