Motr  M0
threads.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2014-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 #include "ut/threads.h"
24 
25 #include "lib/memory.h" /* M0_ALLOC_ARR */
26 #include "lib/thread.h" /* m0_thread */
27 
28 M0_INTERNAL void m0_ut_threads_start(struct m0_ut_threads_descr *descr,
29  int thread_nr,
30  void *param_array,
31  size_t param_size)
32 {
33  int rc;
34  int i;
35 
36  M0_PRE(descr->utd_thread_nr == 0);
37  descr->utd_thread_nr = thread_nr;
38 
39  M0_ALLOC_ARR(descr->utd_thread, descr->utd_thread_nr);
40  M0_ASSERT(descr->utd_thread != NULL);
41 
42  for (i = 0; i < thread_nr; ++i) {
43  rc = M0_THREAD_INIT(&descr->utd_thread[i],
44  void *, NULL,
45  descr->utd_thread_func,
46  param_array + i * param_size,
47  "ut_thread%d", i);
48  M0_ASSERT(rc == 0);
49  }
50 }
51 
52 M0_INTERNAL void m0_ut_threads_stop(struct m0_ut_threads_descr *descr)
53 {
54  int rc;
55  int i;
56 
57  M0_PRE(descr->utd_thread_nr > 0);
58 
59  for (i = 0; i < descr->utd_thread_nr; ++i) {
60  rc = m0_thread_join(&descr->utd_thread[i]);
61  M0_ASSERT(rc == 0);
62  m0_thread_fini(&descr->utd_thread[i]);
63  }
64  m0_free(descr->utd_thread);
65 
66  descr->utd_thread = NULL;
67  descr->utd_thread_nr = 0;
68 }
69 
70 
71 /*
72  * Local variables:
73  * c-indentation-style: "K&R"
74  * c-basic-offset: 8
75  * tab-width: 8
76  * fill-column: 80
77  * scroll-step: 1
78  * End:
79  */
80 /*
81  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
82  */
#define M0_PRE(cond)
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
#define NULL
Definition: misc.h:38
int m0_thread_join(struct m0_thread *q)
Definition: kthread.c:169
#define M0_THREAD_INIT(thread, TYPE, init, func, arg, namefmt,...)
Definition: thread.h:139
int i
Definition: dir.c:1033
#define M0_ASSERT(cond)
void m0_thread_fini(struct m0_thread *q)
Definition: thread.c:92
M0_INTERNAL void m0_ut_threads_stop(struct m0_ut_threads_descr *descr)
Definition: threads.c:52
void(* utd_thread_func)(void *param)
Definition: threads.h:41
M0_INTERNAL void m0_ut_threads_start(struct m0_ut_threads_descr *descr, int thread_nr, void *param_array, size_t param_size)
Definition: threads.c:28
struct m0_thread * utd_thread
Definition: threads.h:42
void m0_free(void *data)
Definition: memory.c:146
int32_t rc
Definition: trigger_fop.h:47