SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?
Transcript of SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?
![Page 1: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/1.jpg)
S B
Implementing ScriptBasicMulti- Thread
How to embed ScriptBasicmulti-thread?
![Page 2: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/2.jpg)
S B 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
![Page 3: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/3.jpg)
S B 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
![Page 4: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/4.jpg)
S B 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
![Page 5: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/5.jpg)
S B Issues regarding threads
• Don’t use global variables (except for constant values, like ScriptBasic syntax definition tables)
• Carefully design locking mechanisms
• Free memory yourself, don’t rely on OS
![Page 6: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/6.jpg)
S B 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)
![Page 7: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/7.jpg)
S B 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)
![Page 8: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/8.jpg)
S B 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)
• Can’t and should not share support function table
![Page 9: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/9.jpg)
S B Dependent threads
• Threads access shared data• Example:
– session handling in web server– Long-life application „global”
variables– Locks (not flock, but memory
mutexes)
![Page 10: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/10.jpg)
S B
Pseu
do In
terp
rete
r (con
fig
, glo
bal S
T)
Thread life-time and ST object life time
PR
OC
ES
S
Basic
thre
ad
Mod
ule
work
er th
read
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
ST
STST
STST ST
STST
![Page 11: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/11.jpg)
S B 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
![Page 12: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/12.jpg)
S B
Mod
ule
work
er th
read
Module life-time
PR
OC
ES
S
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
Module instance counter
2
PR
OC
ES
S
1
1
21
3
21
0
![Page 13: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/13.jpg)
S B 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
![Page 14: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/14.jpg)
S B MT special macros and variables
#define SUPPORT_MULTITHREAD \ static MUTEX mxThreadCounter,mxInit;\ static int iFirst;\ static long lThreadCounter;
static void *pProcessLevelMemorySegment;
![Page 15: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/15.jpg)
S B Initialization code in bootmodu
INC_THREAD_COUNTERINITLOCK if( iFirst ){ /* inherit malloc and free from pMemorySegment */ pProcessLevelMemorySegment = besINIT_SEGMENT(pSt->pEo->pMemorySegment,NULL);
besCreateThread(&hT,worker_thread, pSt->pEo->pSTI);
iFirst = 0 } INITUNLO /* unlock the init mutex */
![Page 16: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/16.jpg)
S B New control function
besSUB_KEEP long lTC;
GET_THREAD_COUNTER(lTC); if( lTC == 0 ){ INC_THREAD_COUNTER } return lTC ? 1 : 0;besEND
![Page 17: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/17.jpg)
S B
Mod
ule
work
er th
read
Module life-time using besSUB_KEEP
PR
OC
ES
S
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
Basic
thre
ad
Module instance counter
2
PR
OC
ES
S
1
1
21
3
21
1
![Page 18: SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?](https://reader036.fdocuments.net/reader036/viewer/2022082612/56649ea05503460f94ba271e/html5/thumbnails/18.jpg)
S B
Thank you for your attention