Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

TLS in Tango

Moderators: larsivi kris

Posted: 03/08/07 19:25:29

I'm writing a wrapper around Mike Cowlishaw's decNumber library for Binary Coded Decimals.

I'm making it a work alike of Python's Decimals module. To do this, I need to implement the getcontext() and setcontext() functions. These are very simple functions: getcontext() returns the default decimal context, setcontext() changes the default context. Contexts, BTW, are just D classes with some wrapped C structs and function calls.

The only caveat is that getcontext() and setcontext() need to work off of Thread Local Storage (TLS). Different threads need to have different default contexts, and when one thread's context changes another's must not.

I see that there is a ThreadLocal? class in Tango, but it's not obvious to me how to use it (I'm just learning D now). Can someone give me an example of allocating a class from TLS with Tango?

Thanx,

Brian

Author Message

Posted: 03/10/07 01:21:44

Heh,

Ok, not many people using TLS in D. Well, I can't say I'm shocked.

I got it working though. Here's what I did:

class rdbms_decimal {...}

alias ThreadLocal?!(rdbms_decimalcontext) decCtxLocal; rdbms_decimalcontext getcontext() {

return rdbms_decimal._local.val();

}

void setcontext(rdbms_decimalcontext context) {

rdbms_decimal._local.val(context);

}

class rdbms_decimal {

private static rdbms_decimalcontext defCtx; private static decCtxLocal _local;

static this() {

defCtx = new rdbms_decimalcontext(21, 11); _local = new decCtxLocal(defCtx);

}

... }

Any suggestions for improvement?

Brian

Posted: 03/11/07 18:41:26

Nope, that looks about right :-)