Implementing ScriptBasic Multi Thread - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Implementing ScriptBasic Multi Thread

Description:

Basic thread. Module worker thread. Basic thread. Basic thread. Basic thread. Basic thread ... Basic thread. Basic thread. Basic thread. Basic thread. Basic ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 19
Provided by: peterv1
Category:

less

Transcript and Presenter's Notes

Title: Implementing ScriptBasic Multi Thread


1
Implementing ScriptBasicMulti- Thread
  • How to embed ScriptBasic
  • multi-thread?

2
Contents
  • Who this presentation is for
  • What is multi-thread, issues
  • 2 ways of mt ScriptBasic
  • Issues when implementing dependent threads
  • Solutions for the issues

3
Who this presentation is for
  • Curious (why things happen?)
  • Want to learn and understand how ScriptBasic
    works
  • Want to embed ScriptBasic into multi threaded
    application
  • NOT for those, who just
  • want to program in scriba

4
What is multi-thread?
  • A single process runs several threads
    simultaneous
  • A thread has its own stack and call sequence
  • Threads share memory and other process resources
  • UNIX thread is just a process w/o separate data

5
Issues regarding threads
  • Dont use global variables (except for constant
    values, like ScriptBasic syntax definition
    tables)
  • Carefully design locking mechanisms
  • Free memory yourself, dont rely on OS

6
Enabling MT ScriptBasic
  • No global variables
  • No memory leaking
  • Object oriented approach (although it is C and
    not C)
  • Thread and lock primitives for portability
    (thread.c)

7
Two way of MT embedding
  • Independent threads (ex Eszter SB Engine
    v1.0b22)
  • Dependent threads, long-life worker threads(ex
    Eszter SB Engine v1.0b23)

8
Independent threads
  • Threads run independent of each other
  • Possibility and advised to share configuration
    file
  • Possibility and advised to cache program code in
    memory (though even v1.0b22 does not)
  • Cant and should not share support function table

9
Dependent threads
  • Threads access shared data
  • Example
  • session handling in web server
  • Long-life application global variables
  • Locks (not flock, but memory mutexes)

10
Thread life-time and ST object life time
ST
ST
ST
Basic thread
ST
ST
ST
Basic thread
Basic thread
ST
Basic thread
ST
Basic thread
PROCESS
Pseudo Interpreter (config, global
ST)
Module worker thread
Basic thread
Basic thread
11
Initializing and using global ST
  • InitModuleInterface
  • Initializes the module Support Function Table
  • InheritModuleInterface
  • Sets the Support Function Inherit pointer but has
    NO effect on the object Support Function Table

12
Module life-time
Module instance counter
1
Basic thread
2
Basic thread
1
PROCESS
PROCESS
0
Module worker thread
1
2
Basic thread
3
Basic thread
Basic thread
2
1
13
Worker thread initialization
  • DllMain (W32) or _init (UNIX) has no ST
  • bootmodu is called for each thread
  • Solution use bootmodu with the help of
  • basext.h macros
  • Process level global variables

14
MT special macros and variables
  • define SUPPORT_MULTITHREAD \
  • static MUTEX mxThreadCounter,mxInit\
  • static int iFirst\
  • static long lThreadCounter
  • static void pProcessLevelMemorySegment

15
Initialization code in bootmodu
  • INC_THREAD_COUNTER
  • INITLOCK
  • if( iFirst )
  • / inherit malloc and free from
    pMemorySegment /
  • pProcessLevelMemorySegment
  • besINIT_SEGMENT(pSt-gtpEo-gtpMemorySegment,NU
    LL)
  • besCreateThread(hT,worker_thread,
  • pSt-gtpEo-gtpSTI)
  • iFirst 0
  • INITUNLO / unlock the init mutex /

16
New control function
  • besSUB_KEEP
  • long lTC
  • GET_THREAD_COUNTER(lTC)
  • if( lTC 0 )
  • INC_THREAD_COUNTER
  • return lTC ? 1 0
  • besEND

17
Module life-time using besSUB_KEEP
Module instance counter
1
Basic thread
2
Basic thread
1
PROCESS
PROCESS
1
Module worker thread
1
2
Basic thread
3
Basic thread
Basic thread
2
1
18
Thank you for your attention
Write a Comment
User Comments (0)
About PowerShow.com