1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
2 | // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: |
3 | #ident "$Id$" |
4 | /*====== |
5 | This file is part of PerconaFT. |
6 | |
7 | |
8 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
9 | |
10 | PerconaFT is free software: you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License, version 2, |
12 | as published by the Free Software Foundation. |
13 | |
14 | PerconaFT is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | GNU General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU General Public License |
20 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
21 | |
22 | ---------------------------------------- |
23 | |
24 | PerconaFT is free software: you can redistribute it and/or modify |
25 | it under the terms of the GNU Affero General Public License, version 3, |
26 | as published by the Free Software Foundation. |
27 | |
28 | PerconaFT is distributed in the hope that it will be useful, |
29 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31 | GNU Affero General Public License for more details. |
32 | |
33 | You should have received a copy of the GNU Affero General Public License |
34 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
35 | ======= */ |
36 | |
37 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
38 | |
39 | #pragma once |
40 | |
41 | #include <stdio.h> |
42 | |
43 | // A toku_thread is toku_pthread that can be cached. |
44 | struct toku_thread; |
45 | |
46 | // Run a function f on a thread |
47 | // This function setups up the thread to run function f with argument arg and then wakes up |
48 | // the thread to run it. |
49 | void toku_thread_run(struct toku_thread *thread, void *(*f)(void *arg), void *arg); |
50 | |
51 | // A toku_thread_pool is a pool of toku_threads. These threads can be allocated from the pool |
52 | // and can run an arbitrary function. |
53 | struct toku_thread_pool; |
54 | |
55 | typedef struct toku_thread_pool *THREADPOOL; |
56 | |
57 | // Create a new threadpool |
58 | // Effects: a new threadpool is allocated and initialized. the number of threads in the threadpool is limited to max_threads. |
59 | // If max_threads == 0 then there is no limit on the number of threads in the pool. |
60 | // Initially, there are no threads in the pool. Threads are allocated by the _get or _run functions. |
61 | // Returns: if there are no errors, the threadpool is set and zero is returned. Otherwise, an error number is returned. |
62 | int toku_thread_pool_create(struct toku_thread_pool **threadpoolptr, int max_threads); |
63 | |
64 | // Destroy a threadpool |
65 | // Effects: the calling thread joins with all of the threads in the threadpool. |
66 | // Effects: the threadpool memory is freed. |
67 | // Returns: the threadpool is set to null. |
68 | void toku_thread_pool_destroy(struct toku_thread_pool **threadpoolptr); |
69 | |
70 | // Get the current number of threads in the thread pool |
71 | int toku_thread_pool_get_current_threads(struct toku_thread_pool *pool); |
72 | |
73 | // Get one or more threads from the thread pool |
74 | // dowait indicates whether or not the caller blocks waiting for threads to free up |
75 | // nthreads on input determines the number of threads that are wanted |
76 | // nthreads on output indicates the number of threads that were allocated |
77 | // toku_thread_return on input supplies an array of thread pointers (all NULL). This function returns the threads |
78 | // that were allocated in the array. |
79 | int toku_thread_pool_get(struct toku_thread_pool *pool, int dowait, int *nthreads, struct toku_thread **toku_thread_return); |
80 | |
81 | // Run a function f on one or more threads allocated from the thread pool |
82 | int toku_thread_pool_run(struct toku_thread_pool *pool, int dowait, int *nthreads, void *(*f)(void *arg), void *arg); |
83 | |
84 | // Print the state of the thread pool |
85 | void toku_thread_pool_print(struct toku_thread_pool *pool, FILE *out); |
86 | |