• 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-2022 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 typedef void (*iwstw_on_task_discard_f)(iwstw_task_f task, void *arg);
46 
47 /**
48  * @brief Starts a single thread worker.
49  *        Function will block until start of worker thread.
50  *
51  * @param queue_limit Max length of pending tasks queue. Unlimited if zero.
52  * @param queue_blocking If true iwstw_schedule will block when queue reached its limit.
53  * @param[out] stwp_out Pointer to worker handler to be initialized.
54  */
55 IW_EXPORT iwrc iwstw_start(const char *thread_name, int queue_limit, bool queue_blocking, IWSTW *out_stw);
56 
57 /**
58  * @brief Shutdowns worker and disposes all resources.
59  *        Function will wait until current task completes or
60  *        wait for all enqueued tasks if `wait_for_all` is set to `true`.
61  *        No new tasks will be accepted during `iwstw_shutdown` call.
62  *
63  * @param stw Pointer to worker handler which should be destroyed.
64  * @param wait_for_all If true worker will wait for completion of all enqueued tasks before shutdown.
65  */
66 IW_EXPORT iwrc iwstw_shutdown(IWSTW *stwp, bool wait_for_all);
67 
68 /**
69  * @brief Schedule task for execution.
70  *        Task will be added to pending tasks queue.
71  *
72  * @note If tasks queue is reached its length limit
73  *        current thread will be blocked if `queue_blocking` is true
74  *        or `IW_ERROR_OVERFLOW` will be returned.
75  * @note If worker is in process of stopping `IW_ERROR_INVALID_STATE` will be returned.
76  */
77 IW_EXPORT iwrc iwstw_schedule(IWSTW stw, iwstw_task_f task, void *task_arg);
78 
79 /**
80  * @brief Schedule task for execution discading all pending tasks on queue.
81  * @note If worker is in process of stopping `IW_ERROR_INVALID_STATE` will be returned.
82  */
83 IW_EXPORT iwrc iwstw_schedule_only(IWSTW stw, iwstw_task_f task, void *task_arg);
84 
85 /**
86  * @brief Schedule task only if task queue is empty.
87  */
88 IW_EXPORT iwrc iwstw_schedule_empty_only(IWSTW stw, iwstw_task_f task, void *task_arg, bool *out_scheduled);
89 
90 /**
91  * @brief Set on task discard callback function.
92  * Called when pending task removed from queue and will not be executed.
93  */
94 IW_EXPORT void iwstw_set_on_task_discard(IWSTW stw, iwstw_on_task_discard_f on_task_discard);
95 
96 /**
97  * @brief Returns size of tasks queue.
98  */
99 IW_EXPORT int iwstw_queue_size(IWSTW stw);
100 
101 IW_EXTERN_C_END
102 #endif
103