Skip to content

Commit

Permalink
Revert "ST: Remove context switch callback."
Browse files Browse the repository at this point in the history
This reverts commit 708bca4.
  • Loading branch information
winlinvip committed Aug 20, 2024
1 parent a0b49ae commit 61516ad
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions trunk/3rdparty/st-srs/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ void st_clist_insert_after(_st_clist_t *e, _st_clist_t *l)

void _st_switch_context(_st_thread_t *thread)
{
ST_SWITCH_OUT_CB(thread);

if (!_st_md_cxt_save(thread->context)) {
_st_vp_schedule();
}

ST_DEBUG_ITERATE_THREADS();
ST_SWITCH_IN_CB(thread);
}

void _st_restore_context(_st_thread_t *thread)
Expand Down
23 changes: 23 additions & 0 deletions trunk/3rdparty/st-srs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ typedef struct _st_vp {

_st_thread_t *sleep_q; /* sleep queue for this vp */
int sleepq_size; /* number of threads on sleep queue */

#ifdef ST_SWITCH_CB
st_switch_cb_t switch_out_cb; /* called when a thread is switched out */
st_switch_cb_t switch_in_cb; /* called when a thread is switched in */
#endif
} _st_vp_t;


Expand Down Expand Up @@ -296,6 +301,24 @@ extern __thread _st_eventsys_t *_st_eventsys;
#define ST_DEBUG_ITERATE_THREADS()
#endif

#ifdef ST_SWITCH_CB
#define ST_SWITCH_OUT_CB(_thread) \
if (_st_this_vp.switch_out_cb != NULL && \
_thread != _st_this_vp.idle_thread && \
_thread->state != _ST_ST_ZOMBIE) { \
_st_this_vp.switch_out_cb(); \
}
#define ST_SWITCH_IN_CB(_thread) \
if (_st_this_vp.switch_in_cb != NULL && \
_thread != _st_this_vp.idle_thread && \
_thread->state != _ST_ST_ZOMBIE) { \
_st_this_vp.switch_in_cb(); \
}
#else
#define ST_SWITCH_OUT_CB(_thread)
#define ST_SWITCH_IN_CB(_thread)
#endif

/*
* Switch away from the current thread context by saving its state and
* calling the thread scheduler
Expand Down
73 changes: 73 additions & 0 deletions trunk/3rdparty/st-srs/docs/reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <H2>State Threads Library Reference</H2>
<DL><DD><A HREF=#mutex_t>st_mutex_t</A></DD></DL>
<DL><DD><A HREF=#utime_t>st_utime_t</A></DD></DL>
<DL><DD><A HREF=#netfd_t>st_netfd_t</A></DD></DL>
<DL><DD><A HREF=#switch_cb_t>st_switch_cb_t</A></DD></DL>
<P>
<DD><A HREF=#errors><B>Error Handling</B></A></DD>
<P>
Expand All @@ -27,6 +28,10 @@ <H2>State Threads Library Reference</H2>
<DL><DD><A HREF=#timecache_set>st_timecache_set()</A></DD></DL>
<DL><DD><A HREF=#randomize_stacks>st_randomize_stacks()</A></DD></DL>
<P>
<DL><DD><A HREF=#switch_cb_t><B>st_switch_cb_t</B> type</A></DD></DL>
<DL><DD><A HREF=#set_switch_in_cb>st_set_switch_in_cb()</A></DD></DL>
<DL><DD><A HREF=#set_switch_out_cb>st_set_switch_out_cb()</A></DD></DL>
<P>
<DD><A HREF=#threads><B>Thread Control and Identification</B></A></DD>
<P>
<DL><DD><A HREF=#thread_t><B>st_thread_t</B> type</A></DD></DL>
Expand Down Expand Up @@ -256,6 +261,31 @@ <H5>Description</H5>
<HR>
<P>

<A NAME="switch_cb_t">
<H4>st_switch_cb_t</H4>
</A>
Context switch callback function type.
<P>
<H5>Syntax</H5>

<PRE>
#include &lt;st.h&gt;

typedef void (*st_switch_cb_t)(void);
</PRE>
<P>
<H5>Description</H5>

This datatype is a convenience type for describing a pointer
to a function that will be called when a thread is set to stop
or set to run.
This feature is available only when <TT>ST_SWITCH_CB</TT> is defined
in <TT>&lt;st.h&gt;</TT>.

<P>
<HR>
<P>

<A NAME="errors">
<H2>Error Handling</H2>
</A>
Expand All @@ -282,6 +312,11 @@ <H2>Library Initialization</H2>
<DD><A HREF=#set_eventsys>st_set_eventsys()</A></DD>
<DD><A HREF=#get_eventsys>st_get_eventsys()</A></DD>
<DD><A HREF=#get_eventsys_name>st_get_eventsys_name()</A></DD>
<P>
These functions operate on a callback function of type
<A HREF=#switch_cb_t><B>st_switch_cb_t</B></A>:
<DD><A HREF=#set_switch_in_cb>st_set_switch_in_cb()</A></DD>
<DD><A HREF=#set_switch_out_cb>st_set_switch_out_cb()</A></DD>
</DL>
<P>
<HR>
Expand Down Expand Up @@ -500,6 +535,44 @@ <H5>Description</H5>
<HR>
<P>

<A NAME="set_switch_in_cb">
<H4>st_set_switch_in_cb()</H4>
</A>
<A NAME="set_switch_out_cb">
<H4>st_set_switch_out_cb()</H4>
</A>
Set the optional callback function for thread switches.
<P>
<H5>Syntax</H5>

<PRE>
#include &lt;st.h&gt;

st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb);
st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb);
</PRE>
<P>
<H5>Parameters</H5>
<TT>st_set_switch_in_cb()</TT> and <TT>st_set_switch_out_cb()</TT> have the
following parameter:<P>
<TT>cb</TT><P>
A function to be called when a thread is resumed and stopped respectively.<P>
<H5>Returns</H5>
The previous callback function pointer.
<P>
<H5>Description</H5>
These functions set the callback for when a thread is resumed and stopped
respectively. After being called any thread switch will call the callback.
Use a <TT>NULL</TT> pointer to disable the callback (this is the default).
Use <A HREF=#thread_self>st_thread_self()</A> or <A HREF=#priv>thread
specific data</A> to differentiate between threads.<P>
These functions can be called at any time.<P>
This feature is available only when <TT>ST_SWITCH_CB</TT> is defined
in <TT>&lt;st.h&gt;</TT>.
<P>
<HR>
<P>

<A NAME="threads">
<H2>Thread Control and Identification</H2>
</A>
Expand Down
11 changes: 11 additions & 0 deletions trunk/3rdparty/st-srs/public.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
#define ST_VERSION_MAJOR 1
#define ST_VERSION_MINOR 9

/* Undefine this to remove the context switch callback feature. */
#define ST_SWITCH_CB

#ifndef ETIME
#define ETIME ETIMEDOUT
#endif
Expand All @@ -76,6 +79,9 @@ typedef struct _st_thread * st_thread_t;
typedef struct _st_cond * st_cond_t;
typedef struct _st_mutex * st_mutex_t;
typedef struct _st_netfd * st_netfd_t;
#ifdef ST_SWITCH_CB
typedef void (*st_switch_cb_t)(void);
#endif

extern int st_init(void);
extern int st_getfdlimit(void);
Expand All @@ -84,6 +90,11 @@ extern int st_set_eventsys(int eventsys);
extern int st_get_eventsys(void);
extern const char *st_get_eventsys_name(void);

#ifdef ST_SWITCH_CB
extern st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb);
extern st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb);
#endif

extern st_thread_t st_thread_self(void);
extern void st_thread_exit(void *retval);
extern int st_thread_join(st_thread_t thread, void **retvalp);
Expand Down
17 changes: 17 additions & 0 deletions trunk/3rdparty/st-srs/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,23 @@ void st_destroy(void)
}


#ifdef ST_SWITCH_CB
st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb)
{
st_switch_cb_t ocb = _st_this_vp.switch_in_cb;
_st_this_vp.switch_in_cb = cb;
return ocb;
}

st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb)
{
st_switch_cb_t ocb = _st_this_vp.switch_out_cb;
_st_this_vp.switch_out_cb = cb;
return ocb;
}
#endif


/*
* Start function for the idle thread
*/
Expand Down

0 comments on commit 61516ad

Please sign in to comment.