1 | #ifndef MYSQL_SERVICE_THD_SPECIFICS_INCLUDED |
2 | /* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; version 2 of the License. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
16 | |
17 | /** |
18 | @file |
19 | |
20 | THD specific for plugin(s) |
21 | |
22 | This API provides pthread_getspecific like functionality to plugin authors. |
23 | This is a functional alternative to the declarative MYSQL_THDVAR |
24 | |
25 | A plugin should at init call thd_key_create that create a key that |
26 | will have storage in each THD. The key should be used by all threads |
27 | and can be used concurrently from all threads. |
28 | |
29 | A plugin should at deinit call thd_key_delete. |
30 | |
31 | Alternatively, a plugin can use thd_key_create_from_var(K,V) to create |
32 | a key that corresponds to a named MYSQL_THDVAR variable. |
33 | |
34 | This API is also safe when using pool-of-threads in which case |
35 | pthread_getspecific is not, because the actual OS thread may change. |
36 | |
37 | @note |
38 | |
39 | Normally one should prefer MYSQL_THDVAR declarative API. |
40 | |
41 | The benefits are: |
42 | |
43 | - It supports typed variables (int, char*, enum, etc), not only void*. |
44 | - The memory allocated for MYSQL_THDVAR is free'd automatically |
45 | (if PLUGIN_VAR_MEMALLOC is specified). |
46 | - Continuous loading and unloading of the same plugin does not allocate |
47 | memory for same variables over and over again. |
48 | |
49 | An example of using MYSQL_THDVAR for a thd local storage: |
50 | |
51 | MYSQL_THDVAR_STR(my_tls, |
52 | PLUGIN_VAR_MEMALLOC | PLUGIN_VAR_NOSYSVAR | PLUGIN_VAR_NOCMDOPT, |
53 | "thd local storage example", 0, 0, 0); |
54 | */ |
55 | |
56 | #ifdef __cplusplus |
57 | extern "C" { |
58 | #endif |
59 | |
60 | typedef int MYSQL_THD_KEY_T; |
61 | |
62 | extern struct thd_specifics_service_st { |
63 | int (*thd_key_create_func)(MYSQL_THD_KEY_T *key); |
64 | void (*thd_key_delete_func)(MYSQL_THD_KEY_T *key); |
65 | void *(*thd_getspecific_func)(MYSQL_THD thd, MYSQL_THD_KEY_T key); |
66 | int (*thd_setspecific_func)(MYSQL_THD thd, MYSQL_THD_KEY_T key, void *value); |
67 | } *thd_specifics_service; |
68 | |
69 | #define thd_key_create_from_var(K, V) do { *(K)= MYSQL_SYSVAR_NAME(V).offset; } while(0) |
70 | |
71 | #ifdef MYSQL_DYNAMIC_PLUGIN |
72 | |
73 | #define thd_key_create(K) (thd_specifics_service->thd_key_create_func(K)) |
74 | #define thd_key_delete(K) (thd_specifics_service->thd_key_delete_func(K)) |
75 | #define thd_getspecific(T, K) (thd_specifics_service->thd_getspecific_func(T, K)) |
76 | #define thd_setspecific(T, K, V) (thd_specifics_service->thd_setspecific_func(T, K, V)) |
77 | |
78 | #else |
79 | |
80 | /** |
81 | * create THD specific storage |
82 | * @return 0 on success |
83 | * else errno is returned |
84 | */ |
85 | int thd_key_create(MYSQL_THD_KEY_T *key); |
86 | |
87 | /** |
88 | * delete THD specific storage |
89 | */ |
90 | void thd_key_delete(MYSQL_THD_KEY_T *key); |
91 | |
92 | /** |
93 | * get/set thd specific storage |
94 | * - first time this is called from a thread it will return 0 |
95 | * - this call is thread-safe in that different threads may call this |
96 | * simultaneously if operating on different THDs. |
97 | * - this call acquires no mutexes and is implemented as an array lookup |
98 | */ |
99 | void* thd_getspecific(MYSQL_THD thd, MYSQL_THD_KEY_T key); |
100 | int thd_setspecific(MYSQL_THD thd, MYSQL_THD_KEY_T key, void *value); |
101 | |
102 | #endif |
103 | |
104 | #ifdef __cplusplus |
105 | } |
106 | #endif |
107 | |
108 | #define MYSQL_SERVICE_THD_SPECIFICS_INCLUDED |
109 | #endif |
110 | |
111 | |