Motr  M0
thread_pool.h
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2015-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #pragma once
24 
25 #ifndef __MOTR_LIB_THREAD_POOL_H__
26 #define __MOTR_LIB_THREAD_POOL_H__
27 
75 #include "lib/queue.h"
76 #include "lib/mutex.h"
77 #include "lib/thread.h"
78 #include "lib/semaphore.h"
79 
80 /* pool */
81 
83 struct m0_parallel_queue;
84 
88  bool pp_done;
89 
90  int (*pp_process)(void *job);
93 
97 
98  int pp_state;
100 };
101 
102 M0_INTERNAL int m0_parallel_pool_init(struct m0_parallel_pool *pool,
103  int thread_nr, int qlinks_nr);
104 M0_INTERNAL void m0_parallel_pool_fini(struct m0_parallel_pool *pool);
105 M0_INTERNAL int m0_parallel_pool_job_add(struct m0_parallel_pool *pool,
106  void *job);
107 M0_INTERNAL int m0_parallel_pool_wait(struct m0_parallel_pool *pool);
108 M0_INTERNAL void m0_parallel_pool_start(struct m0_parallel_pool *pool,
109  int (*process)(void *job));
110 M0_INTERNAL void m0_parallel_pool_terminate_wait(struct m0_parallel_pool *pool);
111 M0_INTERNAL int m0_parallel_pool_rc_next(struct m0_parallel_pool *pool,
112  void **job, int *rc);
113 
114 /* parallel for macro */
115 
116 #define M0_PARALLEL_FOR(name, pool, list, process) \
117 ({ \
118  int rc = -EINVAL; \
119  typeof (name ## _tlist_head(NULL)) obj; \
120  m0_tl_for(name, list, obj) { \
121  rc = m0_parallel_pool_job_add(pool, obj); \
122  if (rc == -EFBIG) { \
123  m0_parallel_pool_start(pool, process); \
124  rc = m0_parallel_pool_wait(pool); \
125  if (rc != 0) \
126  break; \
127  rc = m0_parallel_pool_job_add(pool, obj); \
128  M0_POST(rc == 0); \
129  } \
130  } m0_tl_endfor; \
131  if (rc == 0) { \
132  m0_parallel_pool_start(pool, process); \
133  rc = m0_parallel_pool_wait(pool); \
134  } \
135  (rc); \
136 })
137 
139 #endif /* __MOTR_LIB_THREAD_POOL_H__ */
140 
141 /*
142  * Local variables:
143  * c-indentation-style: "K&R"
144  * c-basic-offset: 8
145  * tab-width: 8
146  * fill-column: 80
147  * scroll-step: 1
148  * End:
149  */
150 /*
151  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
152  */
M0_INTERNAL int m0_parallel_pool_job_add(struct m0_parallel_pool *pool, void *item)
Definition: thread_pool.c:264
M0_INTERNAL int m0_parallel_pool_rc_next(struct m0_parallel_pool *pool, void **job, int *rc)
Definition: thread_pool.c:281
struct m0_parallel_queue_link * pp_qlinks
Definition: thread_pool.h:95
M0_INTERNAL int m0_parallel_pool_wait(struct m0_parallel_pool *pool)
Definition: thread_pool.c:229
struct m0_parallel_queue * pp_queue
Definition: thread_pool.h:94
M0_INTERNAL void m0_parallel_pool_fini(struct m0_parallel_pool *pool)
Definition: thread_pool.c:204
struct m0_semaphore pp_ready
Definition: thread_pool.h:87
M0_INTERNAL int m0_parallel_pool_init(struct m0_parallel_pool *pool, int thread_nr, int qlinks_nr)
Definition: thread_pool.c:189
M0_INTERNAL void m0_parallel_pool_start(struct m0_parallel_pool *pool, int(*process)(void *job))
M0_INTERNAL void m0_parallel_pool_terminate_wait(struct m0_parallel_pool *pool)
Definition: thread_pool.c:247
static struct m0_pool pool
Definition: iter_ut.c:58
int(* pp_process)(void *job)
Definition: thread_pool.h:90
struct m0_semaphore pp_sync
Definition: thread_pool.h:86
int32_t rc
Definition: trigger_fop.h:47
struct m0_thread * pp_threads
Definition: thread_pool.h:91