@@ -40,12 +40,14 @@ extern crate alloc;
40
40
41
41
#[ cfg( CONFIG_RUST_ALLOC ) ]
42
42
use alloc:: boxed:: Box ;
43
- use core:: { cell:: UnsafeCell , ffi:: { c_int, c_void} , mem} ;
43
+ use core:: { cell:: UnsafeCell , ffi:: { c_int, c_void, CStr } , mem} ;
44
44
45
45
use zephyr_sys:: {
46
+ k_tid_t,
46
47
k_thread,
47
48
k_thread_entry_t,
48
49
k_thread_create,
50
+ k_thread_name_set,
49
51
z_thread_stack_element,
50
52
ZR_STACK_ALIGN ,
51
53
ZR_STACK_RESERVED ,
@@ -179,6 +181,8 @@ pub struct Thread {
179
181
priority : c_int ,
180
182
/// Options given to thread creation.
181
183
options : u32 ,
184
+ /// The name to be given to the thread, if desired.
185
+ name : Option < & ' static CStr > ,
182
186
}
183
187
184
188
/// A statically defined thread.
@@ -195,6 +199,7 @@ impl Wrapped for StaticKernelObject<k_thread> {
195
199
stack,
196
200
priority : 0 ,
197
201
options : 0 ,
202
+ name : None ,
198
203
}
199
204
}
200
205
}
@@ -210,15 +215,23 @@ impl Thread {
210
215
self . options = options;
211
216
}
212
217
218
+ /// Set a name for this thread.
219
+ ///
220
+ /// Attempts to set the name of this thread, if Zephyr if configured to do so. Has no effect
221
+ /// otherwise.
222
+ pub fn set_name ( & mut self , name : & ' static CStr ) {
223
+ self . name = Some ( name) ;
224
+ }
225
+
213
226
/// Simple thread spawn. This is unsafe because of the raw values being used. This can be
214
227
/// useful in systems without an allocator defined.
215
- pub unsafe fn simple_spawn ( self ,
228
+ pub unsafe fn simple_spawn ( mut self ,
216
229
child : k_thread_entry_t ,
217
230
p1 : * mut c_void ,
218
231
p2 : * mut c_void ,
219
232
p3 : * mut c_void )
220
233
{
221
- k_thread_create (
234
+ let tid = k_thread_create (
222
235
self . raw ,
223
236
self . stack . base ,
224
237
self . stack . size ,
@@ -229,21 +242,23 @@ impl Thread {
229
242
self . priority ,
230
243
self . options ,
231
244
K_NO_WAIT ) ;
245
+
246
+ self . set_thread_name ( tid) ;
232
247
}
233
248
234
249
#[ cfg( CONFIG_RUST_ALLOC ) ]
235
250
/// Spawn a thread, with a closure.
236
251
///
237
252
/// This requires allocation to be able to safely pass the closure to the other thread.
238
- pub fn spawn < F : FnOnce ( ) + Send + ' static > ( & self , child : F ) {
253
+ pub fn spawn < F : FnOnce ( ) + Send + ' static > ( mut self , child : F ) {
239
254
use core:: ptr:: null_mut;
240
255
241
256
let child: closure:: Closure = Box :: new ( child) ;
242
257
let child = Box :: into_raw ( Box :: new ( closure:: ThreadData {
243
258
closure : child,
244
259
} ) ) ;
245
260
unsafe {
246
- k_thread_create (
261
+ let tid = k_thread_create (
247
262
self . raw ,
248
263
self . stack . base ,
249
264
self . stack . size ,
@@ -254,6 +269,16 @@ impl Thread {
254
269
self . priority ,
255
270
self . options ,
256
271
K_NO_WAIT ) ;
272
+
273
+ self . set_thread_name ( tid) ;
274
+ }
275
+ }
276
+
277
+ fn set_thread_name ( & mut self , tid : k_tid_t ) {
278
+ if let Some ( name) = self . name {
279
+ unsafe {
280
+ k_thread_name_set ( tid, name. as_ptr ( ) ) ;
281
+ }
257
282
}
258
283
}
259
284
}
0 commit comments