Mac OS X and iOS Internals by Jonathan Levin

Mac OS X and iOS Internals by Jonathan Levin

Author:Jonathan Levin
Language: eng
Format: epub
Publisher: John Wiley & Sons
Published: 2012-07-02T16:00:00+00:00


Plugging this function into Listing 10-19 is straightforward. In a manner similar to this experiment, you can drill down further to the thread level by using the thread_info() function. This is but one of many thread APIs, discussed next.

Thread APIs

Much as it does for tasks, Mach provides a rich API for thread management. Most of these achieve the same functionality as the task APIs. Indeed, the task APIs often just iterate over the list of threads in each task, and apply these in turn. As can be expected, these calls (aside from mach_thread_self) are implemented over Mach messages (and generated by MIG subsystem 3600). Table 11.3 lists the thread APIs. All return a kern_return_t, unless otherwise noted.

Table 11.3 Mach Thread APIs

Mach Thread API Used for

thread_t mach_thread_self() Sends rights to thread's kernel port.

thread_terminate(thread_t thread) Terminates self.

[thread/act]_[get/set]_state (thread_t thread, int flavor, thread_state_t state, mach_msg_type_number_t *count)

Gets/sets thread context. The act functions disallow getting/setting the current thread, but otherwise fall through to the thread functions.

The thread_state_t is platform dependent. In OS X, it is an x86_thread_state_t (either 32- or 64-bit). In iOS, it is an arm_thread_state_t.

thread_suspend(thread_t thread)

thread_resume (thread_t thread) Suspends or resumes thread by incrementing/decrementing the suspend count. The thread may only execute if both its suspend count and its containing task suspend count is zero.

thread_abort[_safely] (thread_t thread)

Destroys another thread.

thread_depress_abort (thread_t thread)

Cancel thread depression (forced lowering of priority).

thread_[get/set]_special_port (thread_act_t thread, int which_port, thread special_port);

Gets or sets one of several special ports for the thread.

The only special port supported in XNU is THREAD_KERNEL_PORT.

thread_info(thread_t thread, thread_flavor_t flavor, thread_info_t tinfo_out, mach_msg_type_number_t *ti_count)

Queries information on thread according to flavor, and returns it in buffer specified by tinfo_out, which is ti_count bytes long.

GDB uses this call when you use the info task or info thread command.

thread_get_exception_ports

thread_set_exception_ports

thread_swap_exception_ports Queries, sets, or swaps between exception ports, which are where Mach exception messages will be sent. Discussed later under Exceptions.

thread_policy/thread_set_policy Obsolete; has been replaced by thread_policy_get/set.

thread_policy_[get/set] ( thread_t thread, thread_policy_flavor_t flavor, thread_policy_t policy_info, mach_msg_type_number_t *count, boolean_t *get_default))

Threads scheduling policy.

thread_policy_set is defined similarly (no get_default_argument, and count is an in parameter).

thread_sample Deprecated and removed. On CMU Mach, this allows the periodic sampling of a thread's program counter (IP/PC) and receiving of the samples using a receive_samples API.

etap_trace_thread Deprecated and removed in Leopard and later. Similar to thread_sample(), above, this once enabled tracing a thread using ETAP buffers.

thread_assign(thread_t thread, processor_set_t new_pset)) thread_assign_default (thread_t thread)

Assigns (=affine) thread to a particular processor set new_pset, or the default one. Unsupported (returns KERN_FAILURE).

thread_get_assigment (thread_t thread, processor_set_t *pset)

Returns current thread assignment to processor set (CPU affinity). Always returns a reference to pset0, the default processor set.

As an exercise, you might want to extend the listing in the previous experiment to also list threads. This can be done by calling task_threads() on the task port, and thread_info (with THREAD_BASIC_INFO) on each of the thread ports returned.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.