| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
``grn_thread_*`` |
| 4 |
================ |
| 5 |
|
| 6 |
Summary |
| 7 |
------- |
| 8 |
|
| 9 |
Groonga provides thread related APIs with ``grn_thread_`` prefix. |
| 10 |
|
| 11 |
Normally, you don't need to use these APIs. |
| 12 |
|
| 13 |
You may want to use these APIs when you write a Groonga server. |
| 14 |
|
| 15 |
Example |
| 16 |
------- |
| 17 |
|
| 18 |
Here is a real word use case of ``grn_thread_*`` APIs by |
| 19 |
:doc:`/reference/executables/groonga`. :doc:`/reference/executables/groonga` |
| 20 |
increases its thread pool size when the max number of threads is |
| 21 |
increased. :doc:`/reference/executables/groonga` decreases its thread |
| 22 |
pool size and stops too many threads when the max number of threads is |
| 23 |
decreased. |
| 24 |
|
| 25 |
.. code-block :: c |
| 26 |
|
| 27 |
static grn_mutex q_mutex; |
| 28 |
static grn_cond q_cond; |
| 29 |
static uint32_t nfthreads; |
| 30 |
static uint32_t max_nfthreads; |
| 31 |
|
| 32 |
static uint32_t |
| 33 |
groonga_get_thread_limit(void *data) |
| 34 |
{ |
| 35 |
return max_nfthreads; |
| 36 |
} |
| 37 |
|
| 38 |
static void |
| 39 |
groonga_set_thread_limit(uint32_t new_limit, void *data) |
| 40 |
{ |
| 41 |
uint32_t i; |
| 42 |
uint32_t current_nfthreads; |
| 43 |
|
| 44 |
MUTEX_LOCK(q_mutex); |
| 45 |
current_nfthreads = nfthreads; |
| 46 |
max_nfthreads = new_limit; |
| 47 |
MUTEX_UNLOCK(q_mutex); |
| 48 |
|
| 49 |
if (current_nfthreads > new_limit) { |
| 50 |
for (i = 0; i < current_nfthreads; i++) { |
| 51 |
MUTEX_LOCK(q_mutex); |
| 52 |
COND_SIGNAL(q_cond); |
| 53 |
MUTEX_UNLOCK(q_mutex); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
int |
| 59 |
main(int argc, char *argv) |
| 60 |
{ |
| 61 |
/* ... */ |
| 62 |
grn_thread_set_get_limit_func(groonga_get_thread_limit, NULL); |
| 63 |
grn_thread_set_set_limit_func(groonga_set_thread_limit, NULL); |
| 64 |
|
| 65 |
grn_init(); |
| 66 |
|
| 67 |
/* ... */ |
| 68 |
} |
| 69 |
|
| 70 |
Reference |
| 71 |
--------- |
| 72 |
|
| 73 |
.. note:: |
| 74 |
We are currently switching to automatic generation using Doxygen. |