/[eiffelstudio]/branches/eth/eve/Src/library/thread/classic/thread.e
ViewVC logotype

Contents of /branches/eth/eve/Src/library/thread/classic/thread.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 92811 - (show annotations)
Fri Jul 26 04:35:53 2013 UTC (6 years, 2 months ago) by jasonw
File size: 5831 byte(s)
<<Merged from trunk#92810.>>
1 note
2 description: "Class defining an Eiffel thread."
3 legal: "See notice at end of class."
4 status: "See notice at end of class."
5 date: "$Date$"
6 revision: "$Revision$"
7
8 deferred class
9 THREAD
10
11 inherit
12 THREAD_CONTROL
13
14 feature {NONE} -- Initialization
15
16 make
17 -- Initialize Current.
18 do
19 create launch_mutex.make
20 end
21
22 feature -- Access
23
24 thread_id: POINTER
25 -- Thread-id of the current thread object.
26 --| Updates have to be protected with `launch_mutex'.
27
28 terminated: BOOLEAN
29 -- True if the thread has terminated.
30 --| Updates have to be protected with `launch_mutex'.
31
32 feature -- Basic operations
33
34 execute
35 -- Routine executed when thread is launched.
36 deferred
37 end
38
39 frozen launch
40 -- Initialize a new thread running `execute'.
41 -- Set `is_last_launch_successful' to True if successful, False otherwise.
42 require
43 thread_capable: {PLATFORM}.is_thread_capable
44 is_launchable: is_launchable
45 do
46 launch_with_attributes (create {THREAD_ATTRIBUTES}.make)
47 end
48
49 frozen launch_with_attributes (attr: THREAD_ATTRIBUTES)
50 -- Initialize a new thread running `execute', using attributes.
51 -- Set `is_last_launch_successful' to True if successful, False otherwise.
52 require
53 thread_capable: {PLATFORM}.is_thread_capable
54 is_launchable: is_launchable
55 do
56 launch_mutex.lock
57 if not is_launchable then
58 -- This happens if multiple threads call `launch' or `launch_with_attributes'
59 -- on the same THREAD object.
60 is_last_launch_successful_cell.put (False)
61 else
62 create_thread_with_attr (Current, $thr_main, attr.item)
63 thread_id := last_created_thread
64 is_last_launch_successful_cell.put (True)
65 end
66 launch_mutex.unlock
67 rescue
68 is_last_launch_successful_cell.put (False)
69 launch_mutex.unlock
70 end
71
72 exit
73 -- Exit calling thread. Must be called from the thread itself.
74 require
75 is_exit_supported: is_exit_supported
76 self: current_thread_id = thread_id
77 external
78 "C use %"eif_threads.h%""
79 alias
80 "eif_thr_exit"
81 end
82
83 sleep (nanoseconds: INTEGER_64)
84 -- Suspend thread execution for interval specified in
85 -- `nanoseconds' (1 nanosecond = 10^(-9) second).
86 require
87 self: current_thread_id = thread_id
88 non_negative_nanoseconds: nanoseconds >= 0
89 do
90 (create {EXECUTION_ENVIRONMENT}).sleep (nanoseconds)
91 end
92
93 feature -- Status report
94
95 is_exit_supported: BOOLEAN
96 -- Can `exit' be called?
97 do
98 -- True for classic Eiffel, False in .NET
99 Result := True
100 end
101
102 is_launchable: BOOLEAN
103 -- Can we launch a new thread?
104 do
105 Result := launch_mutex.is_set and
106 thread_id = default_pointer and not terminated
107 end
108
109 is_last_launch_successful: BOOLEAN
110 -- Was the last call to `launch' or `launch_with_attributes' in current thread
111 -- of execution successful?
112 do
113 Result := is_last_launch_successful_cell.item
114 end
115
116 feature -- Synchronization
117
118 join
119 -- The calling thread waits for the current child thread to terminate.
120 do
121 -- Optimization
122 if not terminated then
123 thread_wait (Current)
124 end
125 end
126
127 join_with_timeout (a_timeout_ms: NATURAL_64): BOOLEAN
128 -- The calling thread waits for the current child thread to
129 -- terminate for at most `a_timeout_ms' milliseconds.
130 -- True if wait terminates within `a_timeout_ms', False otherwise.
131 do
132 if terminated then
133 Result := True
134 else
135 Result := thread_wait_with_timeout (Current, a_timeout_ms)
136 end
137 end
138
139 feature {NONE} -- Implementation
140
141 frozen thr_main (a_logical_id: INTEGER)
142 -- Call thread routine.
143 do
144 -- This ensures that `thread_id' has been properly initialized.
145 launch_mutex.lock
146 launch_mutex.unlock
147
148 -- Execute the thread
149 execute
150
151 -- To enable us to restart a new thread using the same object
152 launch_mutex.lock
153 terminated := True
154 thread_id := default_pointer
155 launch_mutex.unlock
156 rescue
157 launch_mutex.lock
158 terminated := True
159 thread_id := default_pointer
160 launch_mutex.unlock
161 end
162
163 launch_mutex: MUTEX
164 -- Mutex used to ensure that no two threads call `launch' or `launch_with_attributes'
165 -- on the same object. This ensures the validity of querying `thread_id' from
166 -- the launch routines.
167
168 is_last_launch_successful_cell: CELL [BOOLEAN]
169 -- Internal storage for `is_last_launch_successful'.
170 -- It is a once per thread and not an attribute because if you have multiple threads
171 -- calling `launch' on the same object, one will set it to True, and the other will
172 -- override the value with False.
173 once
174 create Result.put (False)
175 end
176
177 feature {NONE} -- Externals
178
179 create_thread_with_attr (current_obj: THREAD; init_func, attr: POINTER)
180 -- Initialize and start thread, after setting its priority
181 -- and stack size.
182 external
183 "C signature (EIF_OBJECT, EIF_PROCEDURE, EIF_POINTER) use %"eif_threads.h%""
184 alias
185 "eif_thr_create_with_attr"
186 end
187
188 thread_wait (term: THREAD)
189 -- The calling C thread waits for the current Eiffel thread to
190 -- terminate.
191 external
192 "C use %"eif_threads.h%""
193 alias
194 "eif_thr_wait"
195 end
196
197 thread_wait_with_timeout (term: THREAD; a_timeout_ms: NATURAL_64): BOOLEAN
198 -- The calling C thread waits for the current Eiffel thread to
199 -- terminate.
200 external
201 "C use %"eif_threads.h%""
202 alias
203 "eif_thr_wait_with_timeout"
204 end
205
206 last_created_thread: POINTER
207 -- Returns a pointer to the thread-id of the last created thread.
208 external
209 "C use %"eif_threads.h%""
210 alias
211 "eif_thr_last_thread"
212 end
213
214 note
215 copyright: "Copyright (c) 1984-2013, Eiffel Software and others"
216 license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
217 source: "[
218 Eiffel Software
219 5949 Hollister Ave., Goleta, CA 93117 USA
220 Telephone 805-685-1006, Fax 805-685-6869
221 Website http://www.eiffel.com
222 Customer support http://support.eiffel.com
223 ]"
224
225 end
226

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23