build/stlport/src/lock_free_slist.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 1997-1999
michael@0 3 * Silicon Graphics Computer Systems, Inc.
michael@0 4 *
michael@0 5 * Copyright (c) 1999
michael@0 6 * Boris Fomitchev
michael@0 7 *
michael@0 8 * This material is provided "as is", with absolutely no warranty expressed
michael@0 9 * or implied. Any use is at your own risk.
michael@0 10 *
michael@0 11 * Permission to use or copy this software for any purpose is hereby granted
michael@0 12 * without fee, provided the above notices are retained on all copies.
michael@0 13 * Permission to modify the code and to distribute modified code is granted,
michael@0 14 * provided the above notices are retained, and a notice that the code was
michael@0 15 * modified is included with the above copyright notice.
michael@0 16 *
michael@0 17 */
michael@0 18
michael@0 19 #ifndef _STLP_LOCK_FREE_SLIST_H
michael@0 20 #define _STLP_LOCK_FREE_SLIST_H
michael@0 21
michael@0 22 #if defined(_STLP_PTHREADS)
michael@0 23 # include <pthread.h>
michael@0 24
michael@0 25 # if defined (__GNUC__) && defined (__i386__)
michael@0 26
michael@0 27 # define _STLP_HAS_ATOMIC_FREELIST
michael@0 28 /**
michael@0 29 * Class that implements a non-blocking and thread-safe freelist.
michael@0 30 * It is used for the lock-free node allocation engine.
michael@0 31 *
michael@0 32 * @author felixw@inin.com
michael@0 33 */
michael@0 34 class _STLP_atomic_freelist {
michael@0 35 public:
michael@0 36 /**
michael@0 37 * Type representing items of the freelist
michael@0 38 */
michael@0 39 struct item {
michael@0 40 item* _M_next;
michael@0 41 };
michael@0 42
michael@0 43 _STLP_atomic_freelist() {
michael@0 44 // Statically assert layout of member is as expected by assembly code
michael@0 45 _STLP_STATIC_ASSERT(sizeof(_M) == 8)
michael@0 46 _M._M_data._M_top = 0;
michael@0 47 _M._M_data._M_sequence = 0;
michael@0 48 }
michael@0 49
michael@0 50 /**
michael@0 51 * Atomically pushes the specified item onto the freelist.
michael@0 52 *
michael@0 53 * @param __item [in] Item to add to the front of the list
michael@0 54 */
michael@0 55 void push(item* __item) {
michael@0 56 // NOTE: GCC uses ebx as the PIC register for globals in shared libraries.
michael@0 57 // The GCC version I'm using (3.4.1) won't temporarily spill it if it's
michael@0 58 // used as input, output, or clobber. Instead, it complains with a
michael@0 59 // "can't find a register in class `BREG' while reloading `asm'" error.
michael@0 60 // This is probably a compiler bug, but as the cmpxchg8b instruction
michael@0 61 // requires ebx, I work around this here by using ecx for the '__item'
michael@0 62 // input and spilling ebx into edi. This also precludes us from using
michael@0 63 // a "m" operand for the cmpxchg8b argument (GCC might think it can make
michael@0 64 // it relative to ebx). Instead, we're using esi for the address of _M_data.
michael@0 65 //
michael@0 66 int __tmp1; // These dummy variables are used to tell GCC that the eax, ecx,
michael@0 67 int __tmp2; // and edx registers will not have the same value as their input.
michael@0 68 int __tmp3; // The optimizer will remove them as their values are not used.
michael@0 69 __asm__ __volatile__
michael@0 70 (" movl %%ebx, %%edi\n\t"
michael@0 71 " movl %%ecx, %%ebx\n\t"
michael@0 72 "L1_%=: movl %%eax, (%%ebx)\n\t" // __item._M_next = _M._M_data._M_top
michael@0 73 " leal 1(%%edx),%%ecx\n\t" // new sequence = _M._M_data._M_sequence + 1
michael@0 74 "lock; cmpxchg8b (%%esi)\n\t"
michael@0 75 " jne L1_%=\n\t" // Failed, retry! (edx:eax now contain most recent _M_sequence:_M_top)
michael@0 76 " movl %%edi, %%ebx"
michael@0 77 :"=a" (__tmp1), "=d" (__tmp2), "=c" (__tmp3)
michael@0 78 :"a" (_M._M_data._M_top), "d" (_M._M_data._M_sequence), "c" (__item), "S" (&_M._M_data)
michael@0 79 :"edi", "memory", "cc");
michael@0 80 }
michael@0 81
michael@0 82 /**
michael@0 83 * Atomically removes the topmost item from the freelist and returns a
michael@0 84 * pointer to it. Returns NULL if the list is empty.
michael@0 85 *
michael@0 86 * @return Item that was removed from front of list; NULL if list empty
michael@0 87 */
michael@0 88 item* pop() {
michael@0 89 item* __result;
michael@0 90 int __tmp;
michael@0 91 __asm__ __volatile__
michael@0 92 (" movl %%ebx, %%edi\n\t"
michael@0 93 "L1_%=: testl %%eax, %%eax\n\t" // _M_top == NULL?
michael@0 94 " je L2_%=\n\t" // If yes, we're done
michael@0 95 " movl (%%eax), %%ebx\n\t" // new top = _M._M_data._M_top->_M_next
michael@0 96 " leal 1(%%edx),%%ecx\n\t" // new sequence = _M._M_data._M_sequence + 1
michael@0 97 "lock; cmpxchg8b (%%esi)\n\t"
michael@0 98 " jne L1_%=\n\t" // We failed, retry! (edx:eax now contain most recent _M_sequence:_M_top)
michael@0 99 "L2_%=: movl %%edi, %%ebx"
michael@0 100 :"=a" (__result), "=d" (__tmp)
michael@0 101 :"a" (_M._M_data._M_top), "d" (_M._M_data._M_sequence), "S" (&_M._M_data)
michael@0 102 :"edi", "ecx", "memory", "cc");
michael@0 103 return __result;
michael@0 104 }
michael@0 105
michael@0 106 /**
michael@0 107 * Atomically detaches all items from the list and returns a pointer to the
michael@0 108 * topmost item. The items are still chained and may be traversed safely as
michael@0 109 * they're now "owned" by the calling thread.
michael@0 110 *
michael@0 111 * @return Pointer to topmost item in the list; NULL if list empty
michael@0 112 */
michael@0 113 item* clear() {
michael@0 114 item* __result;
michael@0 115 int __tmp;
michael@0 116 __asm__ __volatile__
michael@0 117 (" movl %%ebx, %%edi\n\t"
michael@0 118 "L1_%=: testl %%eax, %%eax\n\t" // _M_top == NULL?
michael@0 119 " je L2_%=\n\t" // If yes, we're done
michael@0 120 " xorl %%ebx, %%ebx\n\t" // We're attempting to set _M_top to NULL
michael@0 121 " leal 1(%%edx),%%ecx\n\t" // new sequence = _M._M_data._M_sequence + 1
michael@0 122 "lock; cmpxchg8b (%%esi)\n\t"
michael@0 123 " jne L1_%=\n\t" // Failed, retry! (edx:eax now contain most recent _M_sequence:_M_top)
michael@0 124 "L2_%=: movl %%edi, %%ebx"
michael@0 125 :"=a" (__result), "=d" (__tmp)
michael@0 126 :"a" (_M._M_data._M_top), "d" (_M._M_data._M_sequence), "S" (&_M._M_data)
michael@0 127 :"edi", "ecx", "memory", "cc");
michael@0 128 return __result;
michael@0 129 }
michael@0 130
michael@0 131 private:
michael@0 132 union {
michael@0 133 long long _M_align;
michael@0 134 struct {
michael@0 135 item* _M_top; // Topmost element in the freelist
michael@0 136 unsigned int _M_sequence; // Sequence counter to prevent "ABA problem"
michael@0 137 } _M_data;
michael@0 138 } _M;
michael@0 139
michael@0 140 _STLP_atomic_freelist(const _STLP_atomic_freelist&);
michael@0 141 _STLP_atomic_freelist& operator=(const _STLP_atomic_freelist&);
michael@0 142 };
michael@0 143
michael@0 144 # endif /* if defined(__GNUC__) && defined(__i386__) */
michael@0 145
michael@0 146 #elif defined (_STLP_WIN32THREADS)
michael@0 147
michael@0 148 # if !defined (_WIN64)
michael@0 149 # define _STLP_USE_ASM_IMPLEMENTATION
michael@0 150 # endif
michael@0 151
michael@0 152 // Here are the compiler/platform requirements for the thread safe and
michael@0 153 // lock free singly linked list implementation:
michael@0 154 # if defined (_STLP_USE_ASM_IMPLEMENTATION)
michael@0 155 // For the asm version:
michael@0 156 # if defined (_STLP_MSVC) && defined (_M_IX86) && (_M_IX86 >= 500)
michael@0 157 # define _STLP_HAS_ATOMIC_FREELIST
michael@0 158 # endif
michael@0 159 # else
michael@0 160 // For the API based version:
michael@0 161 # if defined (_STLP_NEW_PLATFORM_SDK) && (!defined (WINVER) || (WINVER >= 0x0501)) && \
michael@0 162 (!defined (_WIN32_WINNT) || (_WIN32_WINNT >= 0x0501))
michael@0 163 # define _STLP_HAS_ATOMIC_FREELIST
michael@0 164 # endif
michael@0 165 # endif
michael@0 166
michael@0 167 # if defined (_STLP_HAS_ATOMIC_FREELIST)
michael@0 168 # if defined (_STLP_USE_ASM_IMPLEMENTATION)
michael@0 169 # if defined (_STLP_MSVC) && (_STLP_MSVC < 1300) || defined (__ICL)
michael@0 170 # pragma warning (push)
michael@0 171 # pragma warning (disable : 4035) //function has no return value
michael@0 172 # endif
michael@0 173 # endif
michael@0 174 /**
michael@0 175 * Class that implements a non-blocking and thread-safe freelist.
michael@0 176 * It is used for the lock-free node allocation engine.
michael@0 177 *
michael@0 178 * @author felixw@inin.com
michael@0 179 */
michael@0 180 class _STLP_atomic_freelist {
michael@0 181 public:
michael@0 182 /**
michael@0 183 * Type representing items of the freelist
michael@0 184 */
michael@0 185 # if defined (_STLP_USE_ASM_IMPLEMENTATION)
michael@0 186 struct item {
michael@0 187 item* _M_next;
michael@0 188 };
michael@0 189 # else
michael@0 190 typedef SLIST_ENTRY item;
michael@0 191 # endif
michael@0 192
michael@0 193 _STLP_atomic_freelist() {
michael@0 194 // Statically assert layout of member is as expected by assembly code
michael@0 195 # if defined (_STLP_USE_ASM_IMPLEMENTATION)
michael@0 196 _STLP_STATIC_ASSERT((sizeof(item) == sizeof(item*)) && (sizeof(_M) == 8))
michael@0 197 _M._M_data._M_top = 0;
michael@0 198 _M._M_data._M_sequence = 0;
michael@0 199 # else
michael@0 200 InitializeSListHead(&_M_head);
michael@0 201 # endif
michael@0 202 }
michael@0 203
michael@0 204 /**
michael@0 205 * Atomically pushes the specified item onto the freelist.
michael@0 206 *
michael@0 207 * @param __item [in] Item to add to the front of the list
michael@0 208 */
michael@0 209 void push(item* __item) {
michael@0 210 # if defined (_STLP_USE_ASM_IMPLEMENTATION)
michael@0 211 __asm
michael@0 212 {
michael@0 213 mov esi, this
michael@0 214 mov ebx, __item
michael@0 215 mov eax, [esi] // _M._M_data._M_top
michael@0 216 mov edx, [esi+4] // _M._M_data._M_sequence
michael@0 217 L1: mov [ebx], eax // __item._M_next = _M._M_data._M_top
michael@0 218 lea ecx, [edx+1] // new sequence = _M._M_data._M_sequence + 1
michael@0 219 lock cmpxchg8b qword ptr [esi]
michael@0 220 jne L1 // Failed, retry! (edx:eax now contain most recent _M_sequence:_M_top)
michael@0 221 }
michael@0 222 # else
michael@0 223 InterlockedPushEntrySList(&_M_head, __item);
michael@0 224 # endif
michael@0 225 }
michael@0 226
michael@0 227 /**
michael@0 228 * Atomically removes the topmost item from the freelist and returns a
michael@0 229 * pointer to it. Returns NULL if the list is empty.
michael@0 230 *
michael@0 231 * @return Item that was removed from front of list; NULL if list empty
michael@0 232 */
michael@0 233 item* pop() {
michael@0 234 # if defined (_STLP_USE_ASM_IMPLEMENTATION)
michael@0 235 __asm
michael@0 236 {
michael@0 237 mov esi, this
michael@0 238 mov eax, [esi] // _M._M_data._M_top
michael@0 239 mov edx, [esi+4] // _M._M_data._M_sequence
michael@0 240 L1: test eax, eax // _M_top == NULL?
michael@0 241 je L2 // Yes, we're done
michael@0 242 mov ebx, [eax] // new top = _M._M_data._M_top->_M_next
michael@0 243 lea ecx, [edx+1] // new sequence = _M._M_data._M_sequence + 1
michael@0 244 lock cmpxchg8b qword ptr [esi]
michael@0 245 jne L1 // Failed, retry! (edx:eax now contain most recent _M_sequence:_M_top)
michael@0 246 L2:
michael@0 247 }
michael@0 248 # else
michael@0 249 return InterlockedPopEntrySList(&_M_head);
michael@0 250 # endif
michael@0 251 }
michael@0 252
michael@0 253 /**
michael@0 254 * Atomically detaches all items from the list and returns pointer to the
michael@0 255 * topmost. The items are still chained and may be traversed safely as
michael@0 256 * they're now "owned" by the calling thread.
michael@0 257 *
michael@0 258 * @return Pointer to topmost item in the list; NULL if list empty
michael@0 259 */
michael@0 260 item* clear() {
michael@0 261 # if defined (_STLP_USE_ASM_IMPLEMENTATION)
michael@0 262 __asm
michael@0 263 {
michael@0 264 mov esi, this
michael@0 265 mov eax, [esi] // _M._M_data._M_top
michael@0 266 mov edx, [esi+4] // _M._M_data._M_sequence
michael@0 267 L1: test eax, eax // _M_top == NULL?
michael@0 268 je L2 // Yes, we're done
michael@0 269 xor ebx,ebx // We're attempting to set _M._M_data._M_top to NULL
michael@0 270 lea ecx, [edx+1] // new sequence = _M._M_data._M_sequence + 1
michael@0 271 lock cmpxchg8b qword ptr [esi]
michael@0 272 jne L1 // Failed, retry! (edx:eax now contain most recent _M_sequence:_M_top)
michael@0 273 L2:
michael@0 274 }
michael@0 275 # else
michael@0 276 return InterlockedFlushSList(&_M_head);
michael@0 277 # endif
michael@0 278 }
michael@0 279
michael@0 280 private:
michael@0 281 # if defined (_STLP_USE_ASM_IMPLEMENTATION)
michael@0 282 union {
michael@0 283 __int64 _M_align;
michael@0 284 struct {
michael@0 285 item* _M_top; // Topmost element in the freelist
michael@0 286 unsigned int _M_sequence; // Sequence counter to prevent "ABA problem"
michael@0 287 } _M_data;
michael@0 288 } _M;
michael@0 289 # else
michael@0 290 SLIST_HEADER _M_head;
michael@0 291 # endif
michael@0 292
michael@0 293 _STLP_atomic_freelist(const _STLP_atomic_freelist&);
michael@0 294 _STLP_atomic_freelist& operator = (const _STLP_atomic_freelist&);
michael@0 295 };
michael@0 296
michael@0 297 # if defined (_STLP_USE_ASM_IMPLEMENTATION)
michael@0 298 # if defined (_STLP_MSVC) && (_STLP_MSVC < 1300) || defined (__ICL)
michael@0 299 # pragma warning (pop)
michael@0 300 # endif
michael@0 301 # endif
michael@0 302
michael@0 303 # endif /* _STLP_HAS_ATOMIC_FREELIST */
michael@0 304
michael@0 305 #endif
michael@0 306
michael@0 307 #endif /* _STLP_LOCK_FREE_SLIST_H */

mercurial