1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Ehsan Akhgari <ehsan@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : // Cross-platform lightweight thread local data wrappers
40 :
41 : #if defined(XP_WIN)
42 : // This file will get included in any file that wants to add
43 : // a profiler mark. In order to not bring <windows.h> together
44 : // we could include windef.h and winbase.h which are sufficient
45 : // to get the prototypes for the Tls* functions.
46 : // # include <windef.h>
47 : // # include <winbase.h>
48 : // Unfortunately, even including these headers causes
49 : // us to add a bunch of ugly to our namespace. e.g #define CreateEvent CreateEventW
50 : extern "C" {
51 : __declspec(dllimport) void * __stdcall TlsGetValue(unsigned long);
52 : __declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void *);
53 : __declspec(dllimport) unsigned long __stdcall TlsAlloc();
54 : };
55 : #else
56 : # include <pthread.h>
57 : # include <signal.h>
58 : #endif
59 :
60 : namespace mozilla {
61 :
62 : #if defined(XP_WIN)
63 : typedef unsigned long sig_safe_t;
64 : #else
65 : typedef sig_atomic_t sig_safe_t;
66 : #endif
67 :
68 : namespace tls {
69 :
70 : #if defined(XP_WIN)
71 :
72 : typedef unsigned long key;
73 :
74 : template <typename T>
75 : inline T* get(key mykey) {
76 : return (T*) TlsGetValue(mykey);
77 : }
78 :
79 : template <typename T>
80 : inline bool set(key mykey, const T* value) {
81 : return TlsSetValue(mykey, const_cast<T*>(value));
82 : }
83 :
84 : inline bool create(key* mykey) {
85 : key newkey = TlsAlloc();
86 : if (newkey == (unsigned long)0xFFFFFFFF /* TLS_OUT_OF_INDEXES */) {
87 : return false;
88 : }
89 : *mykey = newkey;
90 : return true;
91 : }
92 :
93 : #else
94 :
95 : typedef pthread_key_t key;
96 :
97 : template <typename T>
98 23 : inline T* get(key mykey) {
99 23 : return (T*) pthread_getspecific(mykey);
100 : }
101 :
102 : template <typename T>
103 : inline bool set(key mykey, const T* value) {
104 : return !pthread_setspecific(mykey, value);
105 : }
106 :
107 : inline bool create(key* mykey) {
108 : return !pthread_key_create(mykey, NULL);
109 : }
110 :
111 : #endif
112 :
113 : }
114 :
115 : }
116 :
|