You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

541 lines
17 KiB

This patch is a collection of patches from from Sanja, Sergei and Monty. Added logging and pinning of pages to block format. Integration of transaction manager, log handler. Better page cache intergration Split trnman.h into two files, so that we don't have to include my_atomic.h into C++ programs. Renaming of structures, more comments, more debugging etc. Fixed problem with small head block + long varchar. Added extra argument to delete_record() and update_record() (needed for UNDO logging) Small changes to interface of pagecache and log handler. Change initialization of log_record_type_descriptors to not be depending on enum order. Use array of LEX_STRING's to send data to log handler Added 'dummy' transaction option to MARIA_INFO so that we can always assume 'trn' exists. include/lf.h: Interface fixes Rename of structures (Patch from Sergei via Sanja) include/my_atomic.h: More comments include/my_global.h: Added MY_ERRPTR include/pagecache.h: Added undo LSN when unlocking pages mysql-test/r/maria.result: Updated results mysql-test/t/maria.test: Added autocommit around lock tables (Patch from Sanja) mysys/lf_alloc-pin.c: Post-review fixes, simple optimizations More comments Struct slot renames Check amount of memory on stack (Patch from Sergei) mysys/lf_dynarray.c: More comments mysys/lf_hash.c: More comments After review fixes (Patch from Sergei) storage/maria/ha_maria.cc: Split trnman.h into two files, so that we don't have to include my_atomic.h into the .cc program. (Temporary fix to avoid bug in gcc) Move out all deferencing of the transaction structure. Transaction manager integrated (Patch from Sergei) storage/maria/ha_maria.h: Added prototype for start_stmt() storage/maria/lockman.c: Function call rename storage/maria/ma_bitmap.c: Mark deleted pages free from page cache storage/maria/ma_blockrec.c: Offset -> rownr More debugging Fixed problem with small head block + long varchar Added logging of changed pages Added logging of undo (Including only loggging of changed fields in case of update) Added pinning/unpinning of all changed pages More comments Added free_full_pages() as the same code was used in several places. fill_rows_parts() renamed as fill_insert_undo_parts() offset -> rownr Added some optimization of not transactional tables _ma_update_block_record() has new parameter, as we need original row to do efficent undo for update storage/maria/ma_blockrec.h: Added ROW_EXTENTS_ON_STACK Changed prototype for update and delete of row storage/maria/ma_check.c: Added original row to delete_record() call storage/maria/ma_control_file.h: Added ifdefs for C++ storage/maria/ma_delete.c: Added original row to delete_record() call (Needed for efficent undo logging) storage/maria/ma_dynrec.c: Added extra argument to delete_record() and update_record() Removed not used variable storage/maria/ma_init.c: Initialize log handler storage/maria/ma_loghandler.c: Removed not used variable Change initialization of log_record_type_descriptors to not be depending on enum order Use array of LEX_STRING's to send data to log handler storage/maria/ma_loghandler.h: New defines Use array of LEX_STRING's to send data to log handler storage/maria/ma_open.c: Added 'dummy' transaction option to MARIA_INFO so that we can always assume 'trn' exists. Store in MARIA_SHARE->page_type if pages will have up to date LSN's storage/maria/ma_pagecache.c: Don't decrease number of readers when using pagecache_write()/pagecache_read() In pagecache_write() decrement request count if page was left pinned Added pagecache_delete_pages() Removed some casts Make trace output consistent with rest of code Simplify calling of DBUG_ASSERT(0) Only update LSN if the LSN is bigger than what's already on the page Added LSN parameter pagecache_unpin_page(), pagecache_unpin(), and pagecache_unlock() (Part of patch from Sanja) storage/maria/ma_static.c: Added 'dummy' transaction option to MARIA_INFO so that we can always assume 'trn' exists. Added default page cache storage/maria/ma_statrec.c: Added extra argument to delete_record() and update_record() storage/maria/ma_test1.c: Added option -T for transactions storage/maria/ma_test2.c: Added option -T for transactions storage/maria/ma_test_all.sh: Test with transactions storage/maria/ma_update.c: Changed prototype for update of row storage/maria/maria_def.h: Changed prototype for update & delete of row as block records need to access the old row Store in MARIA_SHARE->page_type if pages will have up to date LSN's Added MARIA_MAX_TREE_LEVELS to allow us to calculate the number of possible pinned pages we may need. Removed not used 'empty_bits_buffer' Added pointer to transaction object Added array for pinned pages Added log_row_parts array for logging of field data. Added MARIA_PINNED_PAGE to store pinned pages storage/maria/trnman.c: Added accessor functions to transaction object Added missing DBUG_RETURN() More debugging More comments Changed // comment of code to #ifdef NOT_USED Transaction manager integrated. Post review fixes Part of patch originally from Sergei storage/maria/trnman.h: Split trnman.h into two files, so that we don't have to include my_atomic.h into the .cc program. (Temporary fix to avoid bug in gcc) storage/maria/unittest/ma_pagecache_single.c: Added missing argument Added SKIP_BIG_TESTS (Patch from Sanja) storage/maria/unittest/ma_test_loghandler-t.c: Test logging with new LEX_STRING parameter (Patch from Sanja) storage/maria/unittest/ma_test_loghandler_multigroup-t.c: Test logging with new LEX_STRING parameter (Patch from Sanja) storage/maria/unittest/ma_test_loghandler_multithread-t.c: Test logging with new LEX_STRING parameter (Patch from Sanja) storage/maria/unittest/ma_test_loghandler_pagecache-t.c: Test logging with new LEX_STRING parameter (Patch from Sanja) storage/maria/unittest/trnman-t.c: Stack overflow detection (Patch from Sergei) unittest/unit.pl: Command-line options --big and --verbose (Patch from Sergei) unittest/mytap/tap.c: Detect --big (Patch from Sergei) unittest/mytap/tap.h: Skip_big_tests and SKIP_BIG_TESTS (Patch from Sergei) storage/maria/trnman_public.h: New BitKeeper file ``storage/maria/trnman_public.h''
19 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
WL#3064 - waiting threads - wait-for graph and deadlock detection client/mysqltest.c: compiler warnings configure.in: remove old tests for unused programs disable the use of gcc built-ins if smp assembler atomics were selected explictily. add waiting_threads.o to THREAD_LOBJECTS include/lf.h: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. constructor/destructor in lf-alloc include/my_pthread.h: shuffle set_timespec/set_timespec_nsec macros a bit to be able to fill several timeout structures with only one my_getsystime() call include/waiting_threads.h: waiting threads - wait-for graph and deadlock detection mysys/Makefile.am: add waiting_threads.c mysys/lf_alloc-pin.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. constructor/destructor in lf-alloc mysys/lf_hash.c: constructor/destructor in lf-alloc mysys/my_thr_init.c: remember end-of-stack pointer in the mysys_var mysys/waiting_threads.c: waiting threads - wait-for graph and deadlock detection storage/maria/ha_maria.cc: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/ma_commit.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/trnman.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/trnman_public.h: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/unittest/trnman-t.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. unittest/mysys/Makefile.am: add waiting_threads-t unittest/mysys/lf-t.c: factor out the common code for multi-threaded stress unit tests move lf tests to a separate file unittest/mysys/my_atomic-t.c: factor out the common code for multi-threaded stress unit tests move lf tests to a separate file unittest/mysys/thr_template.c: factor out the common code for multi-threaded stress unit tests unittest/mysys/waiting_threads-t.c: wt tests
18 years ago
WL#3064 - waiting threads - wait-for graph and deadlock detection client/mysqltest.c: compiler warnings configure.in: remove old tests for unused programs disable the use of gcc built-ins if smp assembler atomics were selected explictily. add waiting_threads.o to THREAD_LOBJECTS include/lf.h: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. constructor/destructor in lf-alloc include/my_pthread.h: shuffle set_timespec/set_timespec_nsec macros a bit to be able to fill several timeout structures with only one my_getsystime() call include/waiting_threads.h: waiting threads - wait-for graph and deadlock detection mysys/Makefile.am: add waiting_threads.c mysys/lf_alloc-pin.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. constructor/destructor in lf-alloc mysys/lf_hash.c: constructor/destructor in lf-alloc mysys/my_thr_init.c: remember end-of-stack pointer in the mysys_var mysys/waiting_threads.c: waiting threads - wait-for graph and deadlock detection storage/maria/ha_maria.cc: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/ma_commit.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/trnman.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/trnman_public.h: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/unittest/trnman-t.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. unittest/mysys/Makefile.am: add waiting_threads-t unittest/mysys/lf-t.c: factor out the common code for multi-threaded stress unit tests move lf tests to a separate file unittest/mysys/my_atomic-t.c: factor out the common code for multi-threaded stress unit tests move lf tests to a separate file unittest/mysys/thr_template.c: factor out the common code for multi-threaded stress unit tests unittest/mysys/waiting_threads-t.c: wt tests
18 years ago
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled Essentially, the problem is that safemalloc is excruciatingly slow as it checks all allocated blocks for overrun at each memory management primitive, yielding a almost exponential slowdown for the memory management functions (malloc, realloc, free). The overrun check basically consists of verifying some bytes of a block for certain magic keys, which catches some simple forms of overrun. Another minor problem is violation of aliasing rules and that its own internal list of blocks is prone to corruption. Another issue with safemalloc is rather the maintenance cost as the tool has a significant impact on the server code. Given the magnitude of memory debuggers available nowadays, especially those that are provided with the platform malloc implementation, maintenance of a in-house and largely obsolete memory debugger becomes a burden that is not worth the effort due to its slowness and lack of support for detecting more common forms of heap corruption. Since there are third-party tools that can provide the same functionality at a lower or comparable performance cost, the solution is to simply remove safemalloc. Third-party tools can provide the same functionality at a lower or comparable performance cost. The removal of safemalloc also allows a simplification of the malloc wrappers, removing quite a bit of kludge: redefinition of my_malloc, my_free and the removal of the unused second argument of my_free. Since free() always check whether the supplied pointer is null, redudant checks are also removed. Also, this patch adds unit testing for my_malloc and moves my_realloc implementation into the same file as the other memory allocation primitives. client/mysqldump.c: Pass my_free directly as its signature is compatible with the callback type -- which wasn't the case for free_table_ent.
16 years ago
WL#3064 - waiting threads - wait-for graph and deadlock detection client/mysqltest.c: compiler warnings configure.in: remove old tests for unused programs disable the use of gcc built-ins if smp assembler atomics were selected explictily. add waiting_threads.o to THREAD_LOBJECTS include/lf.h: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. constructor/destructor in lf-alloc include/my_pthread.h: shuffle set_timespec/set_timespec_nsec macros a bit to be able to fill several timeout structures with only one my_getsystime() call include/waiting_threads.h: waiting threads - wait-for graph and deadlock detection mysys/Makefile.am: add waiting_threads.c mysys/lf_alloc-pin.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. constructor/destructor in lf-alloc mysys/lf_hash.c: constructor/destructor in lf-alloc mysys/my_thr_init.c: remember end-of-stack pointer in the mysys_var mysys/waiting_threads.c: waiting threads - wait-for graph and deadlock detection storage/maria/ha_maria.cc: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/ma_commit.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/trnman.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/trnman_public.h: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. storage/maria/unittest/trnman-t.c: replace the end-of-stack pointer with the pointer to the end-of-stack pointer. the latter could be stored in THD (mysys_vars) and updated in pool-of-threads scheduler. unittest/mysys/Makefile.am: add waiting_threads-t unittest/mysys/lf-t.c: factor out the common code for multi-threaded stress unit tests move lf tests to a separate file unittest/mysys/my_atomic-t.c: factor out the common code for multi-threaded stress unit tests move lf tests to a separate file unittest/mysys/thr_template.c: factor out the common code for multi-threaded stress unit tests unittest/mysys/waiting_threads-t.c: wt tests
18 years ago
  1. /* QQ: TODO multi-pinbox */
  2. /* Copyright (C) 2006-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /*
  14. wait-free concurrent allocator based on pinning addresses
  15. It works as follows: every thread (strictly speaking - every CPU, but
  16. it's too difficult to do) has a small array of pointers. They're called
  17. "pins". Before using an object its address must be stored in this array
  18. (pinned). When an object is no longer necessary its address must be
  19. removed from this array (unpinned). When a thread wants to free() an
  20. object it scans all pins of all threads to see if somebody has this
  21. object pinned. If yes - the object is not freed (but stored in a
  22. "purgatory"). To reduce the cost of a single free() pins are not scanned
  23. on every free() but only added to (thread-local) purgatory. On every
  24. LF_PURGATORY_SIZE free() purgatory is scanned and all unpinned objects
  25. are freed.
  26. Pins are used to solve ABA problem. To use pins one must obey
  27. a pinning protocol:
  28. 1. Let's assume that PTR is a shared pointer to an object. Shared means
  29. that any thread may modify it anytime to point to a different object
  30. and free the old object. Later the freed object may be potentially
  31. allocated by another thread. If we're unlucky that other thread may
  32. set PTR to point to this object again. This is ABA problem.
  33. 2. Create a local pointer LOCAL_PTR.
  34. 3. Pin the PTR in a loop:
  35. do
  36. {
  37. LOCAL_PTR= PTR;
  38. pin(PTR, PIN_NUMBER);
  39. } while (LOCAL_PTR != PTR)
  40. 4. It is guaranteed that after the loop has ended, LOCAL_PTR
  41. points to an object (or NULL, if PTR may be NULL), that
  42. will never be freed. It is not guaranteed though
  43. that LOCAL_PTR == PTR (as PTR can change any time)
  44. 5. When done working with the object, remove the pin:
  45. unpin(PIN_NUMBER)
  46. 6. When copying pins (as in the list traversing loop:
  47. pin(CUR, 1);
  48. while ()
  49. {
  50. do // standard
  51. { // pinning
  52. NEXT=CUR->next; // loop
  53. pin(NEXT, 0); // see #3
  54. } while (NEXT != CUR->next); // above
  55. ...
  56. ...
  57. CUR=NEXT;
  58. pin(CUR, 1); // copy pin[0] to pin[1]
  59. }
  60. which keeps CUR address constantly pinned), note than pins may be
  61. copied only upwards (!!!), that is pin[N] to pin[M], M > N.
  62. 7. Don't keep the object pinned longer than necessary - the number of
  63. pins you have is limited (and small), keeping an object pinned
  64. prevents its reuse and cause unnecessary mallocs.
  65. Explanations:
  66. 3. The loop is important. The following can occur:
  67. thread1> LOCAL_PTR= PTR
  68. thread2> free(PTR); PTR=0;
  69. thread1> pin(PTR, PIN_NUMBER);
  70. now thread1 cannot access LOCAL_PTR, even if it's pinned,
  71. because it points to a freed memory. That is, it *must*
  72. verify that it has indeed pinned PTR, the shared pointer.
  73. 6. When a thread wants to free some LOCAL_PTR, and it scans
  74. all lists of pins to see whether it's pinned, it does it
  75. upwards, from low pin numbers to high. Thus another thread
  76. must copy an address from one pin to another in the same
  77. direction - upwards, otherwise the scanning thread may
  78. miss it.
  79. Implementation details:
  80. Pins are given away from a "pinbox". Pinbox is stack-based allocator.
  81. It used dynarray for storing pins, new elements are allocated by dynarray
  82. as necessary, old are pushed in the stack for reuse. ABA is solved by
  83. versioning a pointer - because we use an array, a pointer to pins is 16 bit,
  84. upper 16 bits are used for a version.
  85. It is assumed that pins belong to a THD and are not transferable
  86. between THD's (LF_PINS::stack_ends_here being a primary reason
  87. for this limitation).
  88. */
  89. #include <my_global.h>
  90. #include <my_sys.h>
  91. #include <lf.h>
  92. #define LF_PINBOX_MAX_PINS 65536
  93. static void _lf_pinbox_real_free(LF_PINS *pins);
  94. /*
  95. Initialize a pinbox. Normally called from lf_alloc_init.
  96. See the latter for details.
  97. */
  98. void lf_pinbox_init(LF_PINBOX *pinbox, uint free_ptr_offset,
  99. lf_pinbox_free_func *free_func, void *free_func_arg)
  100. {
  101. DBUG_ASSERT(free_ptr_offset % sizeof(void *) == 0);
  102. compile_time_assert(sizeof(LF_PINS) == 128);
  103. lf_dynarray_init(&pinbox->pinarray, sizeof(LF_PINS));
  104. pinbox->pinstack_top_ver= 0;
  105. pinbox->pins_in_array= 0;
  106. pinbox->free_ptr_offset= free_ptr_offset;
  107. pinbox->free_func= free_func;
  108. pinbox->free_func_arg= free_func_arg;
  109. }
  110. void lf_pinbox_destroy(LF_PINBOX *pinbox)
  111. {
  112. lf_dynarray_destroy(&pinbox->pinarray);
  113. }
  114. /*
  115. Get pins from a pinbox. Usually called via lf_alloc_get_pins() or
  116. lf_hash_get_pins().
  117. SYNOPSYS
  118. pinbox -
  119. DESCRIPTION
  120. get a new LF_PINS structure from a stack of unused pins,
  121. or allocate a new one out of dynarray.
  122. NOTE
  123. It is assumed that pins belong to a thread and are not transferable
  124. between threads.
  125. */
  126. LF_PINS *_lf_pinbox_get_pins(LF_PINBOX *pinbox)
  127. {
  128. struct st_my_thread_var *var;
  129. uint32 pins, next, top_ver;
  130. LF_PINS *el;
  131. /*
  132. We have an array of max. 64k elements.
  133. The highest index currently allocated is pinbox->pins_in_array.
  134. Freed elements are in a lifo stack, pinstack_top_ver.
  135. pinstack_top_ver is 32 bits; 16 low bits are the index in the
  136. array, to the first element of the list. 16 high bits are a version
  137. (every time the 16 low bits are updated, the 16 high bits are
  138. incremented). Versioniong prevents the ABA problem.
  139. */
  140. top_ver= pinbox->pinstack_top_ver;
  141. do
  142. {
  143. if (!(pins= top_ver % LF_PINBOX_MAX_PINS))
  144. {
  145. /* the stack of free elements is empty */
  146. pins= my_atomic_add32((int32 volatile*) &pinbox->pins_in_array, 1)+1;
  147. if (unlikely(pins >= LF_PINBOX_MAX_PINS))
  148. return 0;
  149. /*
  150. note that the first allocated element has index 1 (pins==1).
  151. index 0 is reserved to mean "NULL pointer"
  152. */
  153. el= (LF_PINS *)_lf_dynarray_lvalue(&pinbox->pinarray, pins);
  154. if (unlikely(!el))
  155. return 0;
  156. break;
  157. }
  158. el= (LF_PINS *)_lf_dynarray_value(&pinbox->pinarray, pins);
  159. next= el->link;
  160. } while (!my_atomic_cas32((int32 volatile*) &pinbox->pinstack_top_ver,
  161. (int32*) &top_ver,
  162. top_ver-pins+next+LF_PINBOX_MAX_PINS));
  163. /*
  164. set el->link to the index of el in the dynarray (el->link has two usages:
  165. - if element is allocated, it's its own index
  166. - if element is free, it's its next element in the free stack
  167. */
  168. el->link= pins;
  169. el->purgatory_count= 0;
  170. el->pinbox= pinbox;
  171. var= my_thread_var;
  172. /*
  173. Threads that do not call my_thread_init() should still be
  174. able to use the LF_HASH.
  175. */
  176. el->stack_ends_here= (var ? & var->stack_ends_here : NULL);
  177. return el;
  178. }
  179. /*
  180. Put pins back to a pinbox. Usually called via lf_alloc_put_pins() or
  181. lf_hash_put_pins().
  182. DESCRIPTION
  183. empty the purgatory (XXX deadlock warning below!),
  184. push LF_PINS structure to a stack
  185. */
  186. void _lf_pinbox_put_pins(LF_PINS *pins)
  187. {
  188. LF_PINBOX *pinbox= pins->pinbox;
  189. uint32 top_ver, nr;
  190. nr= pins->link;
  191. #ifdef MY_LF_EXTRA_DEBUG
  192. {
  193. int i;
  194. for (i= 0; i < LF_PINBOX_PINS; i++)
  195. DBUG_ASSERT(pins->pin[i] == 0);
  196. }
  197. #endif
  198. /*
  199. XXX this will deadlock if other threads will wait for
  200. the caller to do something after _lf_pinbox_put_pins(),
  201. and they would have pinned addresses that the caller wants to free.
  202. Thus: only free pins when all work is done and nobody can wait for you!!!
  203. */
  204. while (pins->purgatory_count)
  205. {
  206. _lf_pinbox_real_free(pins);
  207. if (pins->purgatory_count)
  208. {
  209. my_atomic_rwlock_wrunlock(&pins->pinbox->pinarray.lock);
  210. pthread_yield();
  211. my_atomic_rwlock_wrlock(&pins->pinbox->pinarray.lock);
  212. }
  213. }
  214. top_ver= pinbox->pinstack_top_ver;
  215. do
  216. {
  217. pins->link= top_ver % LF_PINBOX_MAX_PINS;
  218. } while (!my_atomic_cas32((int32 volatile*) &pinbox->pinstack_top_ver,
  219. (int32*) &top_ver,
  220. top_ver-pins->link+nr+LF_PINBOX_MAX_PINS));
  221. return;
  222. }
  223. static int ptr_cmp(void **a, void **b)
  224. {
  225. return *a < *b ? -1 : *a == *b ? 0 : 1;
  226. }
  227. #define add_to_purgatory(PINS, ADDR) \
  228. do \
  229. { \
  230. *(void **)((char *)(ADDR)+(PINS)->pinbox->free_ptr_offset)= \
  231. (PINS)->purgatory; \
  232. (PINS)->purgatory= (ADDR); \
  233. (PINS)->purgatory_count++; \
  234. } while (0)
  235. /*
  236. Free an object allocated via pinbox allocator
  237. DESCRIPTION
  238. add an object to purgatory. if necessary, call _lf_pinbox_real_free()
  239. to actually free something.
  240. */
  241. void _lf_pinbox_free(LF_PINS *pins, void *addr)
  242. {
  243. add_to_purgatory(pins, addr);
  244. if (pins->purgatory_count % LF_PURGATORY_SIZE)
  245. _lf_pinbox_real_free(pins);
  246. }
  247. struct st_harvester {
  248. void **granary;
  249. int npins;
  250. };
  251. /*
  252. callback for _lf_dynarray_iterate:
  253. scan all pins of all threads and accumulate all pins
  254. */
  255. static int harvest_pins(LF_PINS *el, struct st_harvester *hv)
  256. {
  257. int i;
  258. LF_PINS *el_end= el+min(hv->npins, LF_DYNARRAY_LEVEL_LENGTH);
  259. for (; el < el_end; el++)
  260. {
  261. for (i= 0; i < LF_PINBOX_PINS; i++)
  262. {
  263. void *p= el->pin[i];
  264. if (p)
  265. *hv->granary++= p;
  266. }
  267. }
  268. /*
  269. hv->npins may become negative below, but it means that
  270. we're on the last dynarray page and harvest_pins() won't be
  271. called again. We don't bother to make hv->npins() correct
  272. (that is 0) in this case.
  273. */
  274. hv->npins-= LF_DYNARRAY_LEVEL_LENGTH;
  275. return 0;
  276. }
  277. /*
  278. callback for _lf_dynarray_iterate:
  279. scan all pins of all threads and see if addr is present there
  280. */
  281. static int match_pins(LF_PINS *el, void *addr)
  282. {
  283. int i;
  284. LF_PINS *el_end= el+LF_DYNARRAY_LEVEL_LENGTH;
  285. for (; el < el_end; el++)
  286. for (i= 0; i < LF_PINBOX_PINS; i++)
  287. if (el->pin[i] == addr)
  288. return 1;
  289. return 0;
  290. }
  291. #if STACK_DIRECTION < 0
  292. #define available_stack_size(CUR,END) (long) ((char*)(CUR) - (char*)(END))
  293. #else
  294. #define available_stack_size(CUR,END) (long) ((char*)(END) - (char*)(CUR))
  295. #endif
  296. #define next_node(P, X) (*((uchar * volatile *)(((uchar *)(X)) + (P)->free_ptr_offset)))
  297. #define anext_node(X) next_node(&allocator->pinbox, (X))
  298. /*
  299. Scan the purgatory and free everything that can be freed
  300. */
  301. static void _lf_pinbox_real_free(LF_PINS *pins)
  302. {
  303. int npins;
  304. void *list;
  305. void **addr= NULL;
  306. void *first= NULL, *last= NULL;
  307. LF_PINBOX *pinbox= pins->pinbox;
  308. npins= pinbox->pins_in_array+1;
  309. #ifdef HAVE_ALLOCA
  310. if (pins->stack_ends_here != NULL)
  311. {
  312. int alloca_size= sizeof(void *)*LF_PINBOX_PINS*npins;
  313. /* create a sorted list of pinned addresses, to speed up searches */
  314. if (available_stack_size(&pinbox, *pins->stack_ends_here) > alloca_size)
  315. {
  316. struct st_harvester hv;
  317. addr= (void **) alloca(alloca_size);
  318. hv.granary= addr;
  319. hv.npins= npins;
  320. /* scan the dynarray and accumulate all pinned addresses */
  321. _lf_dynarray_iterate(&pinbox->pinarray,
  322. (lf_dynarray_func)harvest_pins, &hv);
  323. npins= hv.granary-addr;
  324. /* and sort them */
  325. if (npins)
  326. qsort(addr, npins, sizeof(void *), (qsort_cmp)ptr_cmp);
  327. }
  328. }
  329. #endif
  330. list= pins->purgatory;
  331. pins->purgatory= 0;
  332. pins->purgatory_count= 0;
  333. while (list)
  334. {
  335. void *cur= list;
  336. list= *(void **)((char *)cur+pinbox->free_ptr_offset);
  337. if (npins)
  338. {
  339. if (addr) /* use binary search */
  340. {
  341. void **a, **b, **c;
  342. for (a= addr, b= addr+npins-1, c= a+(b-a)/2; (b-a) > 1; c= a+(b-a)/2)
  343. if (cur == *c)
  344. a= b= c;
  345. else if (cur > *c)
  346. a= c;
  347. else
  348. b= c;
  349. if (cur == *a || cur == *b)
  350. goto found;
  351. }
  352. else /* no alloca - no cookie. linear search here */
  353. {
  354. if (_lf_dynarray_iterate(&pinbox->pinarray,
  355. (lf_dynarray_func)match_pins, cur))
  356. goto found;
  357. }
  358. }
  359. /* not pinned - freeing */
  360. if (last)
  361. last= next_node(pinbox, last)= (uchar *)cur;
  362. else
  363. first= last= (uchar *)cur;
  364. continue;
  365. found:
  366. /* pinned - keeping */
  367. add_to_purgatory(pins, cur);
  368. }
  369. if (last)
  370. pinbox->free_func(first, last, pinbox->free_func_arg);
  371. }
  372. /* lock-free memory allocator for fixed-size objects */
  373. LF_REQUIRE_PINS(1)
  374. /*
  375. callback for _lf_pinbox_real_free to free a list of unpinned objects -
  376. add it back to the allocator stack
  377. DESCRIPTION
  378. 'first' and 'last' are the ends of the linked list of nodes:
  379. first->el->el->....->el->last. Use first==last to free only one element.
  380. */
  381. static void alloc_free(uchar *first,
  382. uchar volatile *last,
  383. LF_ALLOCATOR *allocator)
  384. {
  385. /*
  386. we need a union here to access type-punned pointer reliably.
  387. otherwise gcc -fstrict-aliasing will not see 'tmp' changed in the loop
  388. */
  389. union { uchar * node; void *ptr; } tmp;
  390. tmp.node= allocator->top;
  391. do
  392. {
  393. anext_node(last)= tmp.node;
  394. } while (!my_atomic_casptr((void **)(char *)&allocator->top,
  395. (void **)&tmp.ptr, first) && LF_BACKOFF);
  396. }
  397. /*
  398. initialize lock-free allocator
  399. SYNOPSYS
  400. allocator -
  401. size a size of an object to allocate
  402. free_ptr_offset an offset inside the object to a sizeof(void *)
  403. memory that is guaranteed to be unused after
  404. the object is put in the purgatory. Unused by ANY
  405. thread, not only the purgatory owner.
  406. This memory will be used to link waiting-to-be-freed
  407. objects in a purgatory list.
  408. */
  409. void lf_alloc_init(LF_ALLOCATOR *allocator, uint size, uint free_ptr_offset)
  410. {
  411. lf_pinbox_init(&allocator->pinbox, free_ptr_offset,
  412. (lf_pinbox_free_func *)alloc_free, allocator);
  413. allocator->top= 0;
  414. allocator->mallocs= 0;
  415. allocator->element_size= size;
  416. allocator->constructor= 0;
  417. allocator->destructor= 0;
  418. DBUG_ASSERT(size >= sizeof(void*) + free_ptr_offset);
  419. }
  420. /*
  421. destroy the allocator, free everything that's in it
  422. NOTE
  423. As every other init/destroy function here and elsewhere it
  424. is not thread safe. No, this function is no different, ensure
  425. that no thread needs the allocator before destroying it.
  426. We are not responsible for any damage that may be caused by
  427. accessing the allocator when it is being or has been destroyed.
  428. Oh yes, and don't put your cat in a microwave.
  429. */
  430. void lf_alloc_destroy(LF_ALLOCATOR *allocator)
  431. {
  432. uchar *node= allocator->top;
  433. while (node)
  434. {
  435. uchar *tmp= anext_node(node);
  436. if (allocator->destructor)
  437. allocator->destructor(node);
  438. my_free(node);
  439. node= tmp;
  440. }
  441. lf_pinbox_destroy(&allocator->pinbox);
  442. allocator->top= 0;
  443. }
  444. /*
  445. Allocate and return an new object.
  446. DESCRIPTION
  447. Pop an unused object from the stack or malloc it is the stack is empty.
  448. pin[0] is used, it's removed on return.
  449. */
  450. void *_lf_alloc_new(LF_PINS *pins)
  451. {
  452. LF_ALLOCATOR *allocator= (LF_ALLOCATOR *)(pins->pinbox->free_func_arg);
  453. uchar *node;
  454. for (;;)
  455. {
  456. do
  457. {
  458. node= allocator->top;
  459. _lf_pin(pins, 0, node);
  460. } while (node != allocator->top && LF_BACKOFF);
  461. if (!node)
  462. {
  463. node= (void *)my_malloc(allocator->element_size, MYF(MY_WME));
  464. if (allocator->constructor)
  465. allocator->constructor(node);
  466. #ifdef MY_LF_EXTRA_DEBUG
  467. if (likely(node != 0))
  468. my_atomic_add32(&allocator->mallocs, 1);
  469. #endif
  470. break;
  471. }
  472. if (my_atomic_casptr((void **)(char *)&allocator->top,
  473. (void *)&node, anext_node(node)))
  474. break;
  475. }
  476. _lf_unpin(pins, 0);
  477. return node;
  478. }
  479. /*
  480. count the number of objects in a pool.
  481. NOTE
  482. This is NOT thread-safe !!!
  483. */
  484. uint lf_alloc_pool_count(LF_ALLOCATOR *allocator)
  485. {
  486. uint i;
  487. uchar *node;
  488. for (node= allocator->top, i= 0; node; node= anext_node(node), i++)
  489. /* no op */;
  490. return i;
  491. }