@@ -17,6 +17,7 @@ use ptr;
17
17
use libc:: consts:: os:: posix01:: { PTHREAD_CREATE_JOINABLE , PTHREAD_STACK_MIN } ;
18
18
use libc;
19
19
use thunk:: Thunk ;
20
+ use ffi:: CString ;
20
21
21
22
use sys_common:: stack:: RED_ZONE ;
22
23
use sys_common:: thread:: * ;
@@ -206,6 +207,29 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
206
207
native
207
208
}
208
209
210
+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
211
+ pub unsafe fn set_name ( name : & str ) {
212
+ // Using prctl() rather than pthread_setname_np(),
213
+ // because pthread_setname_np() wasn't added until glibc 2.12
214
+ // PR_SET_NAME since Linux 2.6.9
215
+ let cname = CString :: from_slice ( name. as_bytes ( ) ) ;
216
+ prctl ( 15i32 /* = PR_SET_NAME */ , cname. as_ptr ( ) as u64 , 0u64 , 0u64 , 0u64 ) ;
217
+ }
218
+
219
+ #[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" ) ) ]
220
+ pub unsafe fn set_name ( name : & str ) {
221
+ // pthread_set_name_np() since almost forever on all BSDs
222
+ let cname = CString :: from_slice ( name. as_bytes ( ) ) ;
223
+ pthread_set_name_np ( pthread_self ( ) , cname. as_ptr ( ) ) ;
224
+ }
225
+
226
+ #[ cfg( target_os = "macos" ) ]
227
+ pub unsafe fn set_name ( name : & str ) {
228
+ // pthread_setname_np() since OS X 10.6
229
+ let cname = CString :: from_slice ( name. as_bytes ( ) ) ;
230
+ pthread_setname_np ( cname. as_ptr ( ) ) ;
231
+ }
232
+
209
233
pub unsafe fn join ( native : rust_thread ) {
210
234
assert_eq ! ( pthread_join( native, ptr:: null_mut( ) ) , 0 ) ;
211
235
}
@@ -246,6 +270,15 @@ fn min_stack_size(_: *const libc::pthread_attr_t) -> libc::size_t {
246
270
PTHREAD_STACK_MIN
247
271
}
248
272
273
+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
274
+ extern {
275
+ fn prctl ( option : libc:: c_int ,
276
+ arg2 : libc:: c_ulong ,
277
+ arg3 : libc:: c_ulong ,
278
+ arg4 : libc:: c_ulong ,
279
+ arg5 : libc:: c_ulong ) -> libc:: c_int ;
280
+ }
281
+
249
282
#[ cfg( any( target_os = "linux" ) ) ]
250
283
extern {
251
284
pub fn pthread_self ( ) -> libc:: pthread_t ;
@@ -258,11 +291,18 @@ extern {
258
291
stacksize : * mut libc:: size_t ) -> libc:: c_int ;
259
292
}
260
293
294
+ #[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" ) ) ]
295
+ extern {
296
+ pub fn pthread_self ( ) -> libc:: pthread_t ;
297
+ fn pthread_set_name_np ( tid : libc:: pthread_t , name : * const c_char ) ;
298
+ }
299
+
261
300
#[ cfg( target_os = "macos" ) ]
262
301
extern {
263
302
pub fn pthread_self ( ) -> libc:: pthread_t ;
264
303
pub fn pthread_get_stackaddr_np ( thread : libc:: pthread_t ) -> * mut libc:: c_void ;
265
304
pub fn pthread_get_stacksize_np ( thread : libc:: pthread_t ) -> libc:: size_t ;
305
+ fn pthread_setname_np ( name : * const c_char ) -> libc:: c_int ;
266
306
}
267
307
268
308
extern {
0 commit comments