01baaa40c6331f9a16aa9b24aed39c2df8153aae
[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 #define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
27   if(pthread_key_create(&(k),0)) {\
28     PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
29     exit(1);\
30   }\
31 } STMT_END
32 #ifdef OLD_PTHREADS_API
33 #define PERL_THREAD_DETACH(t) pthread_detach(&(t))
34 #define PERL_THREAD_GETSPECIFIC(k,v) pthread_getspecific(k,&v)
35 #else
36 #define PERL_THREAD_DETACH(t) pthread_detach((t))
37 #define PERL_THREAD_GETSPECIFIC(k,v) v = pthread_getspecific(k)
38 #endif
39 #endif
40
41 typedef struct {
42   PerlInterpreter *interp;    /* The threads interpreter */
43   I32 tid;              /* Our thread */
44   perl_mutex mutex;             /* our mutex */
45   I32 count;                    /* how many threads have a reference to us */
46   signed char detached;         /* are we detached ? */
47   SV* init_function;
48   SV* params;
49 #ifdef WIN32
50   DWORD thr;
51   HANDLE handle;
52 #else
53   pthread_t thr;
54 #endif
55 } ithread;
56
57
58
59 static perl_mutex create_mutex;  /* protects the creation of threads ??? */
60
61
62
63 I32 tid_counter = 1;
64 shared_sv* threads;
65
66 perl_key self_key;
67
68
69
70
71 /* internal functions */
72 #ifdef WIN32
73 THREAD_RET_TYPE Perl_thread_run(LPVOID arg);
74 #else
75 void* Perl_thread_run(void * arg);
76 #endif
77 void Perl_thread_destruct(ithread* thread);
78
79 /* Perl mapped functions to iThread:: */
80 SV* Perl_thread_create(char* class, SV* function_to_call, SV* params);
81 I32 Perl_thread_tid (SV* obj);
82 void Perl_thread_join(SV* obj);
83 void Perl_thread_detach(SV* obj);
84 SV* Perl_thread_self (char* class);
85
86
87
88
89
90
91
92
93