2024-12-01 19:35:24 +00:00
|
|
|
//| Copyright: (C) 2020-2024 Kevin Larke <contact AT larke DOT org>
|
|
|
|
//| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
|
2019-12-19 03:24:12 +00:00
|
|
|
#ifndef cwThread_H
|
|
|
|
#define cwThread_H
|
|
|
|
|
|
|
|
namespace cw
|
|
|
|
{
|
2019-12-24 15:05:24 +00:00
|
|
|
namespace thread
|
2019-12-19 03:24:12 +00:00
|
|
|
{
|
2024-02-18 13:37:33 +00:00
|
|
|
const int kDefaultStateTimeOutMicros=100000;
|
|
|
|
const int kDefaultPauseMicros = 10000;
|
2019-12-24 15:05:24 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
kNotInitThId,
|
|
|
|
kPausedThId,
|
|
|
|
kRunningThId,
|
|
|
|
kExitedThId
|
|
|
|
} stateId_t;
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
typedef handle<struct thread_str> handle_t;
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2024-02-10 16:35:34 +00:00
|
|
|
// Return false to indicate that the thread should terminate.
|
2019-12-24 15:05:24 +00:00
|
|
|
typedef bool (*cbFunc_t)( void* arg );
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2020-01-27 22:50:57 +00:00
|
|
|
typedef unsigned long long thread_id_t;
|
|
|
|
|
2024-02-10 16:35:34 +00:00
|
|
|
// The thread is in the 'paused' state after it is created.
|
2019-12-24 15:05:24 +00:00
|
|
|
// stateMicros = total time out duration for switching to the exit state or for switching in/out of pause state.
|
|
|
|
// pauseMicros = duration of thread sleep interval when in paused state.
|
2024-02-18 13:37:33 +00:00
|
|
|
rc_t create( handle_t& hRef,
|
|
|
|
cbFunc_t func,
|
|
|
|
void* funcArg,
|
|
|
|
const char* label, // Assign a label which will show up via `top -H` or `ps -T`.
|
|
|
|
int stateTimeOutMicros=kDefaultStateTimeOutMicros,
|
|
|
|
int pauseMicros=kDefaultPauseMicros );
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
rc_t destroy( handle_t& hRef );
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2024-09-13 21:21:21 +00:00
|
|
|
|
|
|
|
// 'cycleCnt' gives a limit on the count of time the thread function should be called
|
|
|
|
// when the thread exits the pause state. It is ignored if kPauseFl is set in 'cmdFlags'
|
2019-12-24 15:05:24 +00:00
|
|
|
enum { kPauseFl=0x01, kWaitFl=0x02 };
|
2024-09-13 21:21:21 +00:00
|
|
|
rc_t pause( handle_t h, unsigned cmdFlags = kWaitFl, unsigned cycleCnt=0 );
|
|
|
|
rc_t unpause( handle_t h, unsigned cycleCnt=0 ); // same as threadPause(h,kWaitFl)
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
stateId_t state( handle_t h );
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
// Return the thread id of the calling context.
|
2020-01-27 22:50:57 +00:00
|
|
|
thread_id_t id();
|
2024-02-18 13:37:33 +00:00
|
|
|
const char* label( handle_t h );
|
2024-02-14 16:30:02 +00:00
|
|
|
|
|
|
|
unsigned stateTimeOutMicros( handle_t h);
|
|
|
|
unsigned pauseMicros( handle_t h );
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
2019-12-19 03:24:12 +00:00
|
|
|
rc_t threadTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|