Skip to content

Commit

Permalink
ST: Replace macros with explicit code for better understanding. v7.0.7 (
Browse files Browse the repository at this point in the history
#4149)

Improvements for ST(State Threads):

1. ST: Use g++ for CXX compiler.
2. ST: Remove macros for clist.
3. ST: Remove macros for global thread and vp.
4. ST: Remove macros for vp queue operations.
5. ST: Remove macros for context switch.
6. ST: Remove macros for setjmp/longjmp.
7. ST: Remove macro for stack pad.
8. ST: Refine macro for valgrind.

---------

Co-authored-by: Jacob Su <suzp1984@gmail.com>
  • Loading branch information
winlinvip and suzp1984 authored Aug 22, 2024
1 parent 0d76081 commit ff6a608
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 285 deletions.
19 changes: 8 additions & 11 deletions trunk/3rdparty/st-srs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ VERSION = 1.9
##########################

CC = cc
CXX = g++
AR = ar
LD = ld
RANLIB = ranlib
Expand Down Expand Up @@ -207,7 +208,8 @@ OBJS = $(TARGETDIR)/sched.o \
$(TARGETDIR)/sync.o \
$(TARGETDIR)/key.o \
$(TARGETDIR)/io.o \
$(TARGETDIR)/event.o
$(TARGETDIR)/event.o \
$(TARGETDIR)/common.o
OBJS += $(EXTRA_OBJS)
HEADER = $(TARGETDIR)/st.h
SLIBRARY = $(TARGETDIR)/libst.a
Expand Down Expand Up @@ -270,21 +272,16 @@ $(HEADER): public.h
rm -f $@
cp public.h $@

$(TARGETDIR)/md_linux.o: md_linux.S
$(CC) $(CFLAGS) -c $< -o $@

$(TARGETDIR)/md_linux2.o: md_linux2.S
$(CC) $(CFLAGS) -c $< -o $@

$(TARGETDIR)/md_darwin.o: md_darwin.S
$(CC) $(CFLAGS) -c $< -o $@

$(TARGETDIR)/md_cygwin64.o: md_cygwin64.S
$(TARGETDIR)/%.o: %.S
$(CC) $(CFLAGS) -c $< -o $@

$(TARGETDIR)/%.o: %.c common.h md.h
$(CC) $(CFLAGS) -c $< -o $@

# Note that we use C++98 standard for the C++ files.
$(TARGETDIR)/%.o: %.cc common.h md.h
$(CXX) $(CFLAGS) -c $< -o $@ -std=c++98

clean:
rm -rf *_OPT *_DBG obj st.pc

Expand Down
23 changes: 23 additions & 0 deletions trunk/3rdparty/st-srs/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2021-2022 The SRS Authors */

#include "common.h"

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)
{
_st_this_thread = thread;
_st_md_cxt_restore(thread->context, 1);
}

161 changes: 38 additions & 123 deletions trunk/3rdparty/st-srs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,8 @@
#include "public.h"
#include "md.h"

/* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */
#ifndef MD_VALGRIND
#ifndef NVALGRIND
#define NVALGRIND
#endif
#else
#undef NVALGRIND
#ifdef __cplusplus
extern "C" {
#endif


Expand All @@ -88,58 +83,37 @@ typedef struct _st_clist {
struct _st_clist *prev;
} _st_clist_t;

/* Insert element "_e" into the list, before "_l" */
#define ST_INSERT_BEFORE(_e,_l) \
ST_BEGIN_MACRO \
(_e)->next = (_l); \
(_e)->prev = (_l)->prev; \
(_l)->prev->next = (_e); \
(_l)->prev = (_e); \
ST_END_MACRO

/* Insert element "_e" into the list, after "_l" */
#define ST_INSERT_AFTER(_e,_l) \
ST_BEGIN_MACRO \
(_e)->next = (_l)->next; \
(_e)->prev = (_l); \
(_l)->next->prev = (_e); \
(_l)->next = (_e); \
ST_END_MACRO

/* Return the element following element "_e" */
#define ST_NEXT_LINK(_e) ((_e)->next)

/* Append an element "_e" to the end of the list "_l" */
#define ST_APPEND_LINK(_e,_l) ST_INSERT_BEFORE(_e,_l)

/* Insert an element "_e" at the head of the list "_l" */
#define ST_INSERT_LINK(_e,_l) ST_INSERT_AFTER(_e,_l)

/* Return the head/tail of the list */
#define ST_LIST_HEAD(_l) (_l)->next
#define ST_LIST_TAIL(_l) (_l)->prev
/* Initialize a circular list */
static inline void st_clist_init(_st_clist_t *l)
{
l->next = l;
l->prev = l;
}

/* Remove the element "_e" from it's circular list */
#define ST_REMOVE_LINK(_e) \
ST_BEGIN_MACRO \
(_e)->prev->next = (_e)->next; \
(_e)->next->prev = (_e)->prev; \
ST_END_MACRO

/* Return non-zero if the given circular list "_l" is empty, */
/* zero if the circular list is not empty */
#define ST_CLIST_IS_EMPTY(_l) \
((_l)->next == (_l))
static inline void st_clist_remove(_st_clist_t *e)
{
e->prev->next = e->next;
e->next->prev = e->prev;
}

/* Initialize a circular list */
#define ST_INIT_CLIST(_l) \
ST_BEGIN_MACRO \
(_l)->next = (_l); \
(_l)->prev = (_l); \
ST_END_MACRO
/* Insert element "_e" into the list, before "_l" */
static inline void st_clist_insert_before(_st_clist_t *e, _st_clist_t *l)
{
e->next = l;
e->prev = l->prev;
l->prev->next = e;
l->prev = e;
}

#define ST_INIT_STATIC_CLIST(_l) \
{(_l), (_l)}
/* Insert element "_e" into the list, after "_l" */
static inline void st_clist_insert_after(_st_clist_t *e, _st_clist_t *l)
{
e->next = l->next;
e->prev = l;
l->next->prev = e;
l->next = e;
}


/*****************************************
Expand All @@ -158,7 +132,7 @@ typedef struct _st_stack {
char *stk_top; /* Highest address of stack's usable portion */
void *sp; /* Stack pointer from C's point of view */
/* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */
#ifndef NVALGRIND
#ifdef MD_VALGRIND
/* id returned by VALGRIND_STACK_REGISTER */
/* http://valgrind.org/docs/manual/manual-core-adv.html */
unsigned long valgrind_stack_id;
Expand Down Expand Up @@ -270,48 +244,6 @@ extern __thread _st_vp_t _st_this_vp;
extern __thread _st_thread_t *_st_this_thread;
extern __thread _st_eventsys_t *_st_eventsys;

#define _ST_CURRENT_THREAD() (_st_this_thread)
#define _ST_SET_CURRENT_THREAD(_thread) (_st_this_thread = (_thread))

#define _ST_LAST_CLOCK (_st_this_vp.last_clock)

#define _ST_RUNQ (_st_this_vp.run_q)
#define _ST_IOQ (_st_this_vp.io_q)
#define _ST_ZOMBIEQ (_st_this_vp.zombie_q)
#ifdef DEBUG
#define _ST_THREADQ (_st_this_vp.thread_q)
#endif

#define _ST_PAGE_SIZE (_st_this_vp.pagesize)

#define _ST_SLEEPQ (_st_this_vp.sleep_q)
#define _ST_SLEEPQ_SIZE (_st_this_vp.sleepq_size)

#define _ST_VP_IDLE() (*_st_eventsys->dispatch)()


/*****************************************
* vp queues operations
*/

#define _ST_ADD_IOQ(_pq) ST_APPEND_LINK(&_pq.links, &_ST_IOQ)
#define _ST_DEL_IOQ(_pq) ST_REMOVE_LINK(&_pq.links)

#define _ST_ADD_RUNQ(_thr) ST_APPEND_LINK(&(_thr)->links, &_ST_RUNQ)
#define _ST_INSERT_RUNQ(_thr) ST_INSERT_LINK(&(_thr)->links, &_ST_RUNQ)
#define _ST_DEL_RUNQ(_thr) ST_REMOVE_LINK(&(_thr)->links)

#define _ST_ADD_SLEEPQ(_thr, _timeout) _st_add_sleep_q(_thr, _timeout)
#define _ST_DEL_SLEEPQ(_thr) _st_del_sleep_q(_thr)

#define _ST_ADD_ZOMBIEQ(_thr) ST_APPEND_LINK(&(_thr)->links, &_ST_ZOMBIEQ)
#define _ST_DEL_ZOMBIEQ(_thr) ST_REMOVE_LINK(&(_thr)->links)

#ifdef DEBUG
#define _ST_ADD_THREADQ(_thr) ST_APPEND_LINK(&(_thr)->tlink, &_ST_THREADQ)
#define _ST_DEL_THREADQ(_thr) ST_REMOVE_LINK(&(_thr)->tlink)
#endif


/*****************************************
* Thread states and flags
Expand Down Expand Up @@ -411,39 +343,18 @@ extern __thread _st_eventsys_t *_st_eventsys;
* Switch away from the current thread context by saving its state and
* calling the thread scheduler
*/
#define _ST_SWITCH_CONTEXT(_thread) \
ST_BEGIN_MACRO \
ST_SWITCH_OUT_CB(_thread); \
if (!MD_SETJMP((_thread)->context)) { \
_st_vp_schedule(); \
} \
ST_DEBUG_ITERATE_THREADS(); \
ST_SWITCH_IN_CB(_thread); \
ST_END_MACRO
void _st_switch_context(_st_thread_t *thread);

/*
* Restore a thread context that was saved by _ST_SWITCH_CONTEXT or
* Restore a thread context that was saved by _st_switch_context or
* initialized by _ST_INIT_CONTEXT
*/
#define _ST_RESTORE_CONTEXT(_thread) \
ST_BEGIN_MACRO \
_ST_SET_CURRENT_THREAD(_thread); \
MD_LONGJMP((_thread)->context, 1); \
ST_END_MACRO

/*
* Initialize the thread context preparing it to execute _main
*/
#ifdef MD_INIT_CONTEXT
#define _ST_INIT_CONTEXT MD_INIT_CONTEXT
#else
#error Unknown OS
#endif
void _st_restore_context(_st_thread_t *thread);

/*
* Number of bytes reserved under the stack "bottom"
*/
#define _ST_STACK_PAD_SIZE MD_STACK_PAD_SIZE
#define _ST_STACK_PAD_SIZE 128


/*****************************************
Expand Down Expand Up @@ -471,5 +382,9 @@ ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t time
int st_poll(struct pollfd *pds, int npds, st_utime_t timeout);
_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size);

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* !__ST_COMMON_H__ */

2 changes: 1 addition & 1 deletion trunk/3rdparty/st-srs/docs/timeout_heap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ appropriate for ST than introducing a separate array.
Thus, the new ST timeout heap works by organizing the existing
_st_thread_t objects in a balanced binary tree, just as they were
previously organized into a doubly-linked, sorted list. The global
_ST_SLEEPQ variable, formerly a linked list head, is now simply a
_st_this_vp.sleep_q variable, formerly a linked list head, is now simply a
pointer to the root of this tree, and the root node of the tree is the
thread with the earliest timeout. Each thread object has two child
pointers, "left" and "right", pointing to threads with later timeouts.
Expand Down
Loading

0 comments on commit ff6a608

Please sign in to comment.