Thread Local Storage
Thread Local Storage (TLS) is a method of storing information specific (and probably global) to a specific thread. Although not essential at the moment, it will be important for DGD-MP (multi-processor) as the alternatives will prevent multiple threads to run at once.
The important KernelLib class here is /kernel/lib/api/tls. In order to use it, inherit it into whatever object you wish to add use it. You will need to backcall tls' create() function. This will give you access to the following functions:
get_tlvar
- Description
- mixed get_tlvar(int index)
- Requires
- 0 <= index
- Returns
- Whatever has been stored in that index of the Thread Local Storage.
set_tlvar
- Description
- void set_tlvar(int index, mixed value)
- Requires
- 0 <= index
- Function
- Stores the value in the appropriate index of Thread Local Storage.
set_tls_size
- Description
- void set_tls_size(int size)
- Requires
- 0 <= size
- Function
- Sets the size of Thread Local Storage.
Example
An example of how you would use TLS to implement this_player() in an auto object.
private inherit tls "/kernel/lib/api/tls";
static void create() {
tls::create();
set_tls_size(1); /* one thread local variable, index 0 */
}
static void set_this_player(object player) {
set_tlvar(0, player);
}
/* return the current player */
static object this_player() {
return get_tlvar(0);
}
-- ErwinHarte - 20 Jan 2005
|