SlideShare una empresa de Scribd logo
1 de 14
ПОТОКИ В PERL
Самунь Виктор
Зачем изучать?
• Механизмы работы
• Оптимальное использование
• Это интересно
• Поиск ошибок
Способы изучения
Читать/модифицировать исходники

Исходники: http://www.cpan.org/src/README.html
Структура исходников
• Корень:
   • sv.h av.h hv.h gv.h perl.h …
   • sv.c av.c hv.c gv.c perl.c malloc.c …
   • beos/ haiku/ os2/ plan9/ qnx/ win32/ …


• win32/
  • perlmain.c
  • Makefile
  • win32thread.c …
pp_sys.c
PP(pp_fork)
{
#ifdef HAS_FORK
    dVAR; dSP; dTARGET;
    Pid_t childpid;

      EXTEND(SP, 1);
      PERL_FLUSHALL_FOR_CHILD;
      childpid = PerlProc_fork();
      if (childpid < 0)
          RETSETUNDEF;
      if (!childpid) {
          GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
          if (tmpgv) {
              SvREADONLY_off(GvSV(tmpgv));
              sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
              SvREADONLY_on(GvSV(tmpgv));
          }
...
win32win32thread.h
#ifndef DONT_USE_CRITICAL_SECTION

/* Critical Sections used instead of mutexes:
lightweight,
 * but can't be communicated to child processes, and
can't get
 * HANDLE to it for use elsewhere.
 */
typedef CRITICAL_SECTION perl_mutex;
#define MUTEX_INIT(m) InitializeCriticalSection(m)
#define MUTEX_LOCK(m) EnterCriticalSection(m)
#define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
#define MUTEX_DESTROY(m) DeleteCriticalSection(m)

#else
...
win32win32thread.h
...

typedef HANDLE perl_mutex;
# define MUTEX_INIT(m) 
    STMT_START {                                                  
        if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL)        
            Perl_croak_nocontext("panic: MUTEX_INIT");            
    } STMT_END

#   define MUTEX_LOCK(m) 
     STMT_START {                                                 
         if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED)   
             Perl_croak_nocontext("panic: MUTEX_LOCK");           
     } STMT_END

#   define MUTEX_UNLOCK(m) 
     STMT_START {                                                 
         if (ReleaseMutex(*(m)) == 0)                             
             Perl_croak_nocontext("panic: MUTEX_UNLOCK");         
     } STMT_END

#   define MUTEX_DESTROY(m) 
     STMT_START {                                                 
         if (CloseHandle(*(m)) == 0)                              
             Perl_croak_nocontext("panic: MUTEX_DESTROY");        
     } STMT_END
win32win32thread.h
#if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER)
#define JOIN(t, avp)                                                   
    STMT_START {                                                       
        if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED)   
             || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0)     
             || (CloseHandle((t)->self) == 0))                         
            Perl_croak_nocontext("panic: JOIN");                       
        *avp = (AV *)((t)->i.retv);                                    
    } STMT_END
#else   /* !USE_RTL_THREAD_API || _MSC_VER */
#define JOIN(t, avp)                                                   
    STMT_START {                                                       
        if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED)   
             || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0)     
             || (CloseHandle((t)->self) == 0))                         
            Perl_croak_nocontext("panic: JOIN");                       
    } STMT_END
#endif /* !USE_RTL_THREAD_API || _MSC_VER */

#define YIELD                  Sleep(0)
win32perlhost.h
int
PerlProcFork(struct IPerlProc* piPerl)
{
    dTHX;
#ifdef USE_ITHREADS
    DWORD id;
    HANDLE handle;
    CPerlHost *h;

   if (w32_num_pseudo_children >= MAXIMUM_WAIT_OBJECTS) {
       errno = EAGAIN;
       return -1;
   }
   h = new CPerlHost(*(CPerlHost*)w32_internal_host);
   PerlInterpreter *new_perl = perl_clone_using((PerlInterpreter*)aTHX,
                                                CLONEf_COPY_STACKS,
                                                h->m_pHostperlMem,
                                                h->m_pHostperlMemShared,
                                                h->m_pHostperlMemParse,
                                                h->m_pHostperlEnv,
                                                h->m_pHostperlStdIO,
                                                h->m_pHostperlLIO,
                                                h->m_pHostperlDir,
                                                h->m_pHostperlSock,
                                                h->m_pHostperlProc
                                                );
win32perlhost.h
     new_perl->Isys_intern.internal_host = h;
     h->host_perl = new_perl;
#   ifdef PERL_SYNC_FORK
     id = win32_start_child((LPVOID)new_perl);
     PERL_SET_THX(aTHX);
#   else
     if (w32_message_hwnd == INVALID_HANDLE_VALUE)
         w32_message_hwnd = win32_create_message_window();
     new_perl->Isys_intern.message_hwnd = w32_message_hwnd;
     w32_pseudo_child_message_hwnds[w32_num_pseudo_children] =
         (w32_message_hwnd == NULL) ? (HWND)NULL : (HWND)INVALID_HANDLE_VALUE;
#     ifdef USE_RTL_THREAD_API
     handle = (HANDLE)_beginthreadex((void*)NULL, 0, win32_start_child,
                                     (void*)new_perl, 0, (unsigned*)&id);
#     else
     handle = CreateThread(NULL, 0, win32_start_child,
                           (LPVOID)new_perl, 0, &id);
#     endif
win32perlhost.h
    PERL_SET_THX(aTHX); /* XXX perl_clone*() set TLS */
    if (!handle) {
        errno = EAGAIN;
        return -1;
    }
    if (IsWin95()) {
        int pid = (int)id;
        if (pid < 0)
            id = -pid;
    }
    w32_pseudo_child_handles[w32_num_pseudo_children] = handle;
    w32_pseudo_child_pids[w32_num_pseudo_children] = id;
    ++w32_num_pseudo_children;
# endif
    return -(int)id;
#else
    Perl_croak(aTHX_ "fork() not implemented!n");
    return -1;
#endif /* USE_ITHREADS */
win32perlhost.h
CPerlHost::CPerlHost(CPerlHost& host)
{
    /* Construct a host from another host */
    InterlockedIncrement(&num_hosts);
    m_pVMem = new VMem();
    m_pVMemShared = host.GetMemShared();
    m_pVMemParse = host.GetMemParse();

   /* duplicate directory info */
   m_pvDir = new VDir(0);
   m_pvDir->Init(host.GetDir(), m_pVMem);
win32perlhost.h
CopyMemory(&m_hostperlMem, &perlMem, sizeof(perlMem));
CopyMemory(&m_hostperlMemShared, &perlMemShared,
sizeof(perlMemShared));
CopyMemory(&m_hostperlMemParse, &perlMemParse,
sizeof(perlMemParse));
CopyMemory(&m_hostperlEnv, &perlEnv, sizeof(perlEnv));
CopyMemory(&m_hostperlStdIO, &perlStdIO,
sizeof(perlStdIO));
CopyMemory(&m_hostperlLIO, &perlLIO, sizeof(perlLIO));
CopyMemory(&m_hostperlDir, &perlDir, sizeof(perlDir));
CopyMemory(&m_hostperlSock, &perlSock, sizeof(perlSock));
CopyMemory(&m_hostperlProc, &perlProc, sizeof(perlProc));

...
Спасибо за внимание!
     Вопросы?

Más contenido relacionado

La actualidad más candente

Gregor modules
Gregor modulesGregor modules
Gregor modules
skyshaw
 
20110424 action scriptを使わないflash勉強会
20110424 action scriptを使わないflash勉強会20110424 action scriptを使わないflash勉強会
20110424 action scriptを使わないflash勉強会
Hiroki Mizuno
 

La actualidad más candente (20)

Dip Your Toes in the Sea of Security (PHP Berkshire Nov 2015)
Dip Your Toes in the Sea of Security (PHP Berkshire Nov 2015)Dip Your Toes in the Sea of Security (PHP Berkshire Nov 2015)
Dip Your Toes in the Sea of Security (PHP Berkshire Nov 2015)
 
Synapseindia php development tutorial
Synapseindia php development tutorialSynapseindia php development tutorial
Synapseindia php development tutorial
 
Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)
 
Tai lieu ky thuat lap trinh
Tai lieu ky thuat lap trinhTai lieu ky thuat lap trinh
Tai lieu ky thuat lap trinh
 
A simple snake game project
A simple snake game projectA simple snake game project
A simple snake game project
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
Gregor modules
Gregor modulesGregor modules
Gregor modules
 
Sysprog17
Sysprog17Sysprog17
Sysprog17
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
Ping pong game
Ping pong  gamePing pong  game
Ping pong game
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges
 
Message in a bottle
Message in a bottleMessage in a bottle
Message in a bottle
 
Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
 
Отладка в GDB
Отладка в GDBОтладка в GDB
Отладка в GDB
 
The most exciting features of PHP 7.1
The most exciting features of PHP 7.1The most exciting features of PHP 7.1
The most exciting features of PHP 7.1
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
Qt Rest Server
Qt Rest ServerQt Rest Server
Qt Rest Server
 
Flow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidFlow of events during Media Player creation in Android
Flow of events during Media Player creation in Android
 
20110424 action scriptを使わないflash勉強会
20110424 action scriptを使わないflash勉強会20110424 action scriptを使わないflash勉強会
20110424 action scriptを使わないflash勉強会
 
Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perl
 

Destacado

Tale Comenius Paco for Bori
Tale Comenius Paco for BoriTale Comenius Paco for Bori
Tale Comenius Paco for Bori
fimarcab
 
Использование Mojolicious::Plugin::AnyData в тестовом режиме проекта
Использование Mojolicious::Plugin::AnyData в тестовом режиме проектаИспользование Mojolicious::Plugin::AnyData в тестовом режиме проекта
Использование Mojolicious::Plugin::AnyData в тестовом режиме проекта
Ilya Zelenchuk
 

Destacado (9)

Tale Comenius Paco for Bori
Tale Comenius Paco for BoriTale Comenius Paco for Bori
Tale Comenius Paco for Bori
 
Wcf faq
Wcf faqWcf faq
Wcf faq
 
Nginx.pm
Nginx.pmNginx.pm
Nginx.pm
 
Использование Mojolicious::Plugin::AnyData в тестовом режиме проекта
Использование Mojolicious::Plugin::AnyData в тестовом режиме проектаИспользование Mojolicious::Plugin::AnyData в тестовом режиме проекта
Использование Mojolicious::Plugin::AnyData в тестовом режиме проекта
 
Perl и SPDY
Perl и SPDYPerl и SPDY
Perl и SPDY
 
Контрактное программирование
Контрактное программированиеКонтрактное программирование
Контрактное программирование
 
Wanas Strategic Sustainability Prez
Wanas Strategic Sustainability PrezWanas Strategic Sustainability Prez
Wanas Strategic Sustainability Prez
 
Функциональные тесты на Perl
Функциональные тесты на PerlФункциональные тесты на Perl
Функциональные тесты на Perl
 
Coro - реальные потоки в Perl
Coro - реальные потоки в PerlCoro - реальные потоки в Perl
Coro - реальные потоки в Perl
 

Similar a Потоки в перле изнутри

cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
gordienaysmythe
 
참고 코드(Shelllilst filtering)
참고 코드(Shelllilst filtering)참고 코드(Shelllilst filtering)
참고 코드(Shelllilst filtering)
문서 사해
 
R57shell
R57shellR57shell
R57shell
ady36
 

Similar a Потоки в перле изнутри (20)

[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis
[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis
[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis
 
include.docx
include.docxinclude.docx
include.docx
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
 
Mythread.h
Mythread.hMythread.h
Mythread.h
 
Aodv routing protocol code in ns2
Aodv routing protocol code in ns2Aodv routing protocol code in ns2
Aodv routing protocol code in ns2
 
Marat-Slides
Marat-SlidesMarat-Slides
Marat-Slides
 
Jamming attack in wireless network
Jamming attack in wireless networkJamming attack in wireless network
Jamming attack in wireless network
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
 
참고 코드(Shelllilst filtering)
참고 코드(Shelllilst filtering)참고 코드(Shelllilst filtering)
참고 코드(Shelllilst filtering)
 
Php7 hashtable
Php7 hashtablePhp7 hashtable
Php7 hashtable
 
Climbing the Abstract Syntax Tree (DPC 2017)
Climbing the Abstract Syntax Tree (DPC 2017)Climbing the Abstract Syntax Tree (DPC 2017)
Climbing the Abstract Syntax Tree (DPC 2017)
 
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
 
Climbing the Abstract Syntax Tree (IPC Fall 2017)
Climbing the Abstract Syntax Tree (IPC Fall 2017)Climbing the Abstract Syntax Tree (IPC Fall 2017)
Climbing the Abstract Syntax Tree (IPC Fall 2017)
 
Climbing the Abstract Syntax Tree (Forum PHP 2017)
Climbing the Abstract Syntax Tree (Forum PHP 2017)Climbing the Abstract Syntax Tree (Forum PHP 2017)
Climbing the Abstract Syntax Tree (Forum PHP 2017)
 
Ns2programs
Ns2programsNs2programs
Ns2programs
 
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
 
R57shell
R57shellR57shell
R57shell
 
Climbing the Abstract Syntax Tree (phpDay 2017)
Climbing the Abstract Syntax Tree (phpDay 2017)Climbing the Abstract Syntax Tree (phpDay 2017)
Climbing the Abstract Syntax Tree (phpDay 2017)
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」
 
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
 

Último

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

Потоки в перле изнутри

  • 2. Зачем изучать? • Механизмы работы • Оптимальное использование • Это интересно • Поиск ошибок
  • 4. Структура исходников • Корень: • sv.h av.h hv.h gv.h perl.h … • sv.c av.c hv.c gv.c perl.c malloc.c … • beos/ haiku/ os2/ plan9/ qnx/ win32/ … • win32/ • perlmain.c • Makefile • win32thread.c …
  • 5. pp_sys.c PP(pp_fork) { #ifdef HAS_FORK dVAR; dSP; dTARGET; Pid_t childpid; EXTEND(SP, 1); PERL_FLUSHALL_FOR_CHILD; childpid = PerlProc_fork(); if (childpid < 0) RETSETUNDEF; if (!childpid) { GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV); if (tmpgv) { SvREADONLY_off(GvSV(tmpgv)); sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid()); SvREADONLY_on(GvSV(tmpgv)); } ...
  • 6. win32win32thread.h #ifndef DONT_USE_CRITICAL_SECTION /* Critical Sections used instead of mutexes: lightweight, * but can't be communicated to child processes, and can't get * HANDLE to it for use elsewhere. */ typedef CRITICAL_SECTION perl_mutex; #define MUTEX_INIT(m) InitializeCriticalSection(m) #define MUTEX_LOCK(m) EnterCriticalSection(m) #define MUTEX_UNLOCK(m) LeaveCriticalSection(m) #define MUTEX_DESTROY(m) DeleteCriticalSection(m) #else ...
  • 7. win32win32thread.h ... typedef HANDLE perl_mutex; # define MUTEX_INIT(m) STMT_START { if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) Perl_croak_nocontext("panic: MUTEX_INIT"); } STMT_END # define MUTEX_LOCK(m) STMT_START { if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) Perl_croak_nocontext("panic: MUTEX_LOCK"); } STMT_END # define MUTEX_UNLOCK(m) STMT_START { if (ReleaseMutex(*(m)) == 0) Perl_croak_nocontext("panic: MUTEX_UNLOCK"); } STMT_END # define MUTEX_DESTROY(m) STMT_START { if (CloseHandle(*(m)) == 0) Perl_croak_nocontext("panic: MUTEX_DESTROY"); } STMT_END
  • 8. win32win32thread.h #if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER) #define JOIN(t, avp) STMT_START { if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) || (CloseHandle((t)->self) == 0)) Perl_croak_nocontext("panic: JOIN"); *avp = (AV *)((t)->i.retv); } STMT_END #else /* !USE_RTL_THREAD_API || _MSC_VER */ #define JOIN(t, avp) STMT_START { if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) || (CloseHandle((t)->self) == 0)) Perl_croak_nocontext("panic: JOIN"); } STMT_END #endif /* !USE_RTL_THREAD_API || _MSC_VER */ #define YIELD Sleep(0)
  • 9. win32perlhost.h int PerlProcFork(struct IPerlProc* piPerl) { dTHX; #ifdef USE_ITHREADS DWORD id; HANDLE handle; CPerlHost *h; if (w32_num_pseudo_children >= MAXIMUM_WAIT_OBJECTS) { errno = EAGAIN; return -1; } h = new CPerlHost(*(CPerlHost*)w32_internal_host); PerlInterpreter *new_perl = perl_clone_using((PerlInterpreter*)aTHX, CLONEf_COPY_STACKS, h->m_pHostperlMem, h->m_pHostperlMemShared, h->m_pHostperlMemParse, h->m_pHostperlEnv, h->m_pHostperlStdIO, h->m_pHostperlLIO, h->m_pHostperlDir, h->m_pHostperlSock, h->m_pHostperlProc );
  • 10. win32perlhost.h new_perl->Isys_intern.internal_host = h; h->host_perl = new_perl; # ifdef PERL_SYNC_FORK id = win32_start_child((LPVOID)new_perl); PERL_SET_THX(aTHX); # else if (w32_message_hwnd == INVALID_HANDLE_VALUE) w32_message_hwnd = win32_create_message_window(); new_perl->Isys_intern.message_hwnd = w32_message_hwnd; w32_pseudo_child_message_hwnds[w32_num_pseudo_children] = (w32_message_hwnd == NULL) ? (HWND)NULL : (HWND)INVALID_HANDLE_VALUE; # ifdef USE_RTL_THREAD_API handle = (HANDLE)_beginthreadex((void*)NULL, 0, win32_start_child, (void*)new_perl, 0, (unsigned*)&id); # else handle = CreateThread(NULL, 0, win32_start_child, (LPVOID)new_perl, 0, &id); # endif
  • 11. win32perlhost.h PERL_SET_THX(aTHX); /* XXX perl_clone*() set TLS */ if (!handle) { errno = EAGAIN; return -1; } if (IsWin95()) { int pid = (int)id; if (pid < 0) id = -pid; } w32_pseudo_child_handles[w32_num_pseudo_children] = handle; w32_pseudo_child_pids[w32_num_pseudo_children] = id; ++w32_num_pseudo_children; # endif return -(int)id; #else Perl_croak(aTHX_ "fork() not implemented!n"); return -1; #endif /* USE_ITHREADS */
  • 12. win32perlhost.h CPerlHost::CPerlHost(CPerlHost& host) { /* Construct a host from another host */ InterlockedIncrement(&num_hosts); m_pVMem = new VMem(); m_pVMemShared = host.GetMemShared(); m_pVMemParse = host.GetMemParse(); /* duplicate directory info */ m_pvDir = new VDir(0); m_pvDir->Init(host.GetDir(), m_pVMem);
  • 13. win32perlhost.h CopyMemory(&m_hostperlMem, &perlMem, sizeof(perlMem)); CopyMemory(&m_hostperlMemShared, &perlMemShared, sizeof(perlMemShared)); CopyMemory(&m_hostperlMemParse, &perlMemParse, sizeof(perlMemParse)); CopyMemory(&m_hostperlEnv, &perlEnv, sizeof(perlEnv)); CopyMemory(&m_hostperlStdIO, &perlStdIO, sizeof(perlStdIO)); CopyMemory(&m_hostperlLIO, &perlLIO, sizeof(perlLIO)); CopyMemory(&m_hostperlDir, &perlDir, sizeof(perlDir)); CopyMemory(&m_hostperlSock, &perlSock, sizeof(perlSock)); CopyMemory(&m_hostperlProc, &perlProc, sizeof(perlProc)); ...