0
|
1
|
|
2 /* Digital Mars DMDScript source code.
|
|
3 * Copyright (c) 2000-2002 by Chromium Communications
|
|
4 * D version Copyright (c) 2004-2005 by Digital Mars
|
|
5 * All Rights Reserved
|
|
6 * written by Walter Bright
|
|
7 * www.digitalmars.com
|
|
8 * Use at your own risk. There is no warranty, express or implied.
|
|
9 * License for redistribution is by the GNU General Public License in gpl.txt.
|
|
10 *
|
|
11 * A binary, non-exclusive license for commercial use can be
|
|
12 * purchased from www.digitalmars.com/dscript/buy.html.
|
|
13 *
|
|
14 * DMDScript is implemented in the D Programming Language,
|
|
15 * www.digitalmars.com/d/
|
|
16 *
|
|
17 * For a C++ implementation of DMDScript, including COM support,
|
|
18 * see www.digitalmars.com/dscript/cppscript.html.
|
|
19 */
|
|
20
|
|
21
|
3
|
22 module dmdscript_tango.threadcontext;
|
0
|
23
|
|
24 import std.thread;
|
|
25
|
|
26 alias std.thread.Thread Thread;
|
|
27
|
3
|
28 import dmdscript_tango.script;
|
|
29 import dmdscript_tango.program;
|
|
30 import dmdscript_tango.dmath;
|
|
31 import dmdscript_tango.dobject;
|
|
32 import dmdscript_tango.dfunction;
|
0
|
33
|
|
34 // These are our per-thread global variables
|
|
35
|
|
36 struct ThreadContext
|
|
37 {
|
|
38 Thread threadid; // identifier of current thread
|
|
39
|
|
40 // Tables where the prototype and constructor object are stored.
|
|
41 Dobject[d_string] protoTable;
|
|
42 Dfunction[d_string] ctorTable;
|
|
43
|
|
44 // Table where object initializers go
|
|
45 static void function(ThreadContext*)[] initTable;
|
|
46
|
|
47 // Values from here to the end of the struct are 0'd by dobject_term()
|
|
48 Program program; // associated data
|
|
49
|
|
50 Dfunction Dobject_constructor;
|
|
51 Dobject Dobject_prototype;
|
|
52
|
|
53 Dfunction Dfunction_constructor;
|
|
54 Dobject Dfunction_prototype;
|
|
55
|
|
56 Dfunction Darray_constructor;
|
|
57 Dobject Darray_prototype;
|
|
58
|
|
59 Dfunction Dstring_constructor;
|
|
60 Dobject Dstring_prototype;
|
|
61
|
|
62 Dfunction Dboolean_constructor;
|
|
63 Dobject Dboolean_prototype;
|
|
64
|
|
65 Dfunction Dnumber_constructor;
|
|
66 Dobject Dnumber_prototype;
|
|
67
|
|
68 Dfunction Derror_constructor;
|
|
69 Dobject Derror_prototype;
|
|
70
|
|
71 Dfunction Ddate_constructor;
|
|
72 Dobject Ddate_prototype;
|
|
73
|
|
74 Dfunction Dregexp_constructor;
|
|
75 Dobject Dregexp_prototype;
|
|
76
|
|
77 Dfunction Denumerator_constructor;
|
|
78 Dobject Denumerator_prototype;
|
|
79
|
|
80 Dmath Dmath_object;
|
|
81
|
|
82 /***********************************************
|
|
83 * Get ThreadContext associated with this thread.
|
|
84 */
|
|
85
|
|
86 static ThreadContext[Thread] threadtable;
|
|
87
|
|
88 static Thread cache_ti;
|
|
89 static ThreadContext* cache_cc;
|
|
90
|
|
91 static ThreadContext* getThreadContext()
|
|
92 {
|
|
93 /* This works by creating an array of ThreadContext's, one
|
|
94 * for each thread. We match up by thread id.
|
|
95 */
|
|
96
|
|
97 Thread ti;
|
|
98 ThreadContext *cc;
|
|
99
|
|
100 //writef("ThreadContext.getThreadContext()\n");
|
|
101
|
|
102 ti = Thread.getThis();
|
|
103
|
|
104 synchronized
|
|
105 {
|
|
106 // Used cached version if we can
|
|
107 if (ti == cache_ti)
|
|
108 {
|
|
109 cc = cache_cc;
|
|
110 //exception(L"getThreadContext(): cache x%x", ti);
|
|
111 }
|
|
112 else
|
|
113 {
|
|
114 cc = ti in threadtable;
|
|
115 if (!cc)
|
|
116 {
|
|
117 threadtable[ti] = ThreadContext.init;
|
|
118 cc = &threadtable[ti];
|
|
119 }
|
|
120
|
|
121 cc.threadid = ti;
|
|
122
|
|
123 // Cache for next time
|
|
124 cache_ti = ti;
|
|
125 cache_cc = cc;
|
|
126 }
|
|
127 }
|
|
128 return cc;
|
|
129 }
|
|
130 }
|
|
131
|
|
132
|