Split out core of sv_magic() into sv_magicext().
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.h
1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #ifdef WIN32
9 #include <windows.h>
10 #include <win32thread.h>
11 #define PERL_THREAD_DETACH(t) 
12 #define PERL_THREAD_SETSPECIFIC(k,v) TlsSetValue(k,v)
13 #define PERL_THREAD_GETSPECIFIC(k,v) v = TlsGetValue(k)
14 #define PERL_THREAD_ALLOC_SPECIFIC(k) \
15 STMT_START {\
16   if((k = TlsAlloc()) == TLS_OUT_OF_INDEXES) {\
17     PerlIO_printf(PerlIO_stderr(),"panic threads.h: TlsAlloc");\
18     exit(1);\
19   }\
20 } STMT_END
21 #else
22 #include <pthread.h>
23 #include <thread.h>
24
25 #define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
26 #ifdef OLD_PTHREADS_API
27 #define PERL_THREAD_DETACH(t) pthread_detach(&(t))
28 #define PERL_THREAD_GETSPECIFIC(k,v) pthread_getspecific(k,&v)
29 #define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
30   if(pthread_keycreate(&(k),0)) {\
31     PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
32     exit(1);\
33   }\
34 } STMT_END
35 #else
36 #define PERL_THREAD_DETACH(t) pthread_detach((t))
37 #define PERL_THREAD_GETSPECIFIC(k,v) v = pthread_getspecific(k)
38 #define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
39   if(pthread_key_create(&(k),0)) {\
40     PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
41     exit(1);\
42   }\
43 } STMT_END
44 #endif
45 #endif
46
47 typedef struct {
48   PerlInterpreter *interp;    /* The threads interpreter */
49   I32 tid;              /* Our thread */
50   perl_mutex mutex;             /* our mutex */
51   I32 count;                    /* how many threads have a reference to us */
52   signed char detached;         /* are we detached ? */
53   SV* init_function;
54   SV* params;
55 #ifdef WIN32
56   DWORD thr;
57   HANDLE handle;
58 #else
59   pthread_t thr;
60 #endif
61 } ithread;
62
63
64
65 static perl_mutex create_mutex;  /* protects the creation of threads ??? */
66
67
68
69 I32 tid_counter = 1;
70 shared_sv* threads;
71
72 perl_key self_key;
73
74
75
76
77 /* internal functions */
78 #ifdef WIN32
79 THREAD_RET_TYPE Perl_thread_run(LPVOID arg);
80 #else
81 void* Perl_thread_run(void * arg);
82 #endif
83 void Perl_thread_destruct(ithread* thread);
84
85 /* Perl mapped functions to iThread:: */
86 SV* Perl_thread_create(char* class, SV* function_to_call, SV* params);
87 I32 Perl_thread_tid (SV* obj);
88 void Perl_thread_join(SV* obj);
89 void Perl_thread_detach(SV* obj);
90 SV* Perl_thread_self (char* class);
91
92
93
94
95
96
97
98
99