summaryrefslogtreecommitdiff
path: root/ext/threads/threads.h
blob: 72a4872485e5723d5602c96ea81dddb307168f99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <stdio.h>
#include <stdlib.h>

#ifdef WIN32
#include <windows.h>
#include <win32thread.h>
#define PERL_THREAD_DETACH(t) 
#define PERL_THREAD_SETSPECIFIC(k,v) TlsSetValue(k,v)
#define PERL_THREAD_GETSPECIFIC(k,v) v = TlsGetValue(k)
#define PERL_THREAD_ALLOC_SPECIFIC(k) \
STMT_START {\
  if((k = TlsAlloc()) == TLS_OUT_OF_INDEXES) {\
    PerlIO_printf(PerlIO_stderr(),"panic threads.h: TlsAlloc");\
    exit(1);\
  }\
} STMT_END
#else
#include <pthread.h>
#include <thread.h>

#define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
#ifdef OLD_PTHREADS_API
#define PERL_THREAD_DETACH(t) pthread_detach(&(t))
#define PERL_THREAD_GETSPECIFIC(k,v) pthread_getspecific(k,&v)
#define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
  if(pthread_keycreate(&(k),0)) {\
    PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
    exit(1);\
  }\
} STMT_END
#else
#define PERL_THREAD_DETACH(t) pthread_detach((t))
#define PERL_THREAD_GETSPECIFIC(k,v) v = pthread_getspecific(k)
#define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
  if(pthread_key_create(&(k),0)) {\
    PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
    exit(1);\
  }\
} STMT_END
#endif
#endif

typedef struct {
  PerlInterpreter *interp;    /* The threads interpreter */
  I32 tid;              /* Our thread */
  perl_mutex mutex; 		/* our mutex */
  I32 count;			/* how many threads have a reference to us */
  signed char detached;		/* are we detached ? */
  SV* init_function;
  SV* params;
#ifdef WIN32
  DWORD	thr;
  HANDLE handle;
#else
  pthread_t thr;
#endif
} ithread;



static perl_mutex create_mutex;  /* protects the creation of threads ??? */



I32 tid_counter = 1;
shared_sv* threads;

perl_key self_key;




/* internal functions */
#ifdef WIN32
THREAD_RET_TYPE Perl_thread_run(LPVOID arg);
#else
void* Perl_thread_run(void * arg);
#endif
void Perl_thread_destruct(ithread* thread);

/* Perl mapped functions to iThread:: */
SV* Perl_thread_create(char* class, SV* function_to_call, SV* params);
I32 Perl_thread_tid (SV* obj);
void Perl_thread_join(SV* obj);
void Perl_thread_detach(SV* obj);
SV* Perl_thread_self (char* class);