diff --git a/widget/src/beos/nsToolkit.cpp b/widget/src/beos/nsToolkit.cpp index 2eb65546ccc1..476a085972bd 100644 --- a/widget/src/beos/nsToolkit.cpp +++ b/widget/src/beos/nsToolkit.cpp @@ -29,6 +29,12 @@ #include "nsSwitchToUIThread.h" #include "plevent.h" +// +// Static thread local storage index of the Toolkit +// object associated with a given thread... +// +static PRUintn gToolkitTLSIndex = 0; + //NS_IMPL_ISUPPORTS(nsToolkit, NS_ITOOLKIT_IID) //------------------------------------------------------------------------- // @@ -152,6 +158,7 @@ nsToolkit::nsToolkit() nsToolkit::~nsToolkit() { Kill(); + PR_SetThreadPrivate(gToolkitTLSIndex, nsnull); } void nsToolkit::Kill() @@ -268,3 +275,52 @@ void nsToolkit::CallMethodAsync(MethodInfo *info) id.sync = false; write_port_etc(eventport, WM_CALLMETHOD, &id, sizeof(id), B_TIMEOUT, 0); } + +//------------------------------------------------------------------------- +// +// Return the nsIToolkit for the current thread. If a toolkit does not +// yet exist, then one will be created... +// +//------------------------------------------------------------------------- +NS_METHOD NS_GetCurrentToolkit(nsIToolkit* *aResult) +{ + nsIToolkit* toolkit = nsnull; + nsresult rv = NS_OK; + PRStatus status; + + // Create the TLS index the first time through... + if (0 == gToolkitTLSIndex) { + status = PR_NewThreadPrivateIndex(&gToolkitTLSIndex, NULL); + if (PR_FAILURE == status) { + rv = NS_ERROR_FAILURE; + } + } + + if (NS_SUCCEEDED(rv)) { + toolkit = (nsIToolkit*)PR_GetThreadPrivate(gToolkitTLSIndex); + + // + // Create a new toolkit for this thread... + // + if (!toolkit) { + toolkit = new nsToolkit(); + + if (!toolkit) { + rv = NS_ERROR_OUT_OF_MEMORY; + } else { + NS_ADDREF(toolkit); + toolkit->Init(PR_GetCurrentThread()); + // + // The reference stored in the TLS is weak. It is removed in the + // nsToolkit destructor... + // + PR_SetThreadPrivate(gToolkitTLSIndex, (void*)toolkit); + } + } else { + NS_ADDREF(toolkit); + } + *aResult = toolkit; + } + + return rv; +}