• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 #ifndef IWSTW_H
3 #define IWSTW_H
4 
5 /**************************************************************************************************
6  * Single thread worker.
7  *
8  * IOWOW library
9  *
10  * MIT License
11  *
12  * Copyright (c) 2012-2020 Softmotions Ltd <info@softmotions.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this software and associated documentation files (the "Software"), to deal
16  * in the Software without restriction, including without limitation the rights
17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  *  copies of the Software, and to permit persons to whom the Software is
19  * furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in all
22  * copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *************************************************************************************************/
32 
33 #include "basedefs.h"
34 
35 IW_EXTERN_C_START
36 
37 struct _IWSTW;
38 typedef struct _IWSTW *IWSTW;
39 
40 /**
41  * @brief Task to execute
42  */
43 typedef void (*iwstw_task_f)(void *arg);
44 
45 /**
46  * @brief Start single thread worker.
47  *        Function will block until start of worker thread.
48  *
49  * @param queue_limit Max length of pending tasks queue. Unlimited if zero.
50  * @param[out] stwp_out Pointer to worker handler to be initialized.
51  */
52 IW_EXPORT iwrc iwstw_start(int queue_limit, IWSTW *stwp_out);
53 
54 /**
55  * @brief Shutdowns worker and disposes all resources.
56  *        Function will wait until current task completes or
57  *        wait for all pednding tasks if `wait_for_all` is set to `true`.
58  *        No new tasks will be accepted during `iwstw_shutdown` call.
59  *
60  * @param stw Pointer to worker handler which should be destroyed.
61  * @param wait_for_all If true worker will wait for all pending tasks before shutdown.
62  */
63 IW_EXPORT void iwstw_shutdown(IWSTW *stwp, bool wait_for_all);
64 
65 /**
66  * @brief Schedule task for execution.
67  *        Task will be added to pending tasks queue.
68  *
69  * @note If tasks queue is reached its length limit `IW_ERROR_OVERFLOW` will be returned.
70  * @note If worker is in process of stopping `IW_ERROR_INVALID_STATE` will be returned.
71  */
72 IW_EXPORT iwrc iwstw_schedule(IWSTW stw, iwstw_task_f task, void *task_arg);
73 
74 IW_EXTERN_C_END
75 #endif
76