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