xref: /aosp_15_r20/external/libopenapv/src/oapv_tpool.h (revision abb65b4b03b69e1d508d4d9a44dcf199df16e7c3)
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the copyright owner, nor the names of its contributors
16  *   may be used to endorse or promote products derived from this software
17  *   without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef __OAPV_TPOOL_H__
33 #define __OAPV_TPOOL_H__
34 
35 typedef void *oapv_thread_t;
36 typedef int (*oapv_fn_thread_entry_t)(void *arg);
37 typedef struct oapv_tpool oapv_tpool_t;
38 typedef void *oapv_sync_obj_t;
39 
40 //  Salient points  ****************************************************
41 //  Thread Controller object will create, run and destroy***************
42 //  threads. Thread Controller has to be initialised *******************
43 //  before invoking handler functions.  Thread controller***************
44 //  should be de-initialized to release handler functions***************
45 //
46 
47 typedef enum {
48     TPOOL_SUCCESS = 0,
49     TPOOL_OUT_OF_MEMORY,
50     TPOOL_INVALID_ARG,
51     TPOOL_INVALID_STATE,
52     TPOOL_UNKNOWN_ERROR
53 } tpool_result_t;
54 
55 typedef enum {
56     TPOOL_SUSPENDED = 0,
57     TPOOL_RUNNING,
58     TPOOL_TERMINATED
59 } tpool_status_t;
60 
61 struct oapv_tpool {
62     // Handler function to create requested thread, thread created is in suspended state
63     oapv_thread_t (*create)(oapv_tpool_t *tp, int thread_id);
64     // Handler function to wake up suspended thread and assign task to complete
65     tpool_result_t (*run)(oapv_thread_t thread_id, oapv_fn_thread_entry_t entry, void *arg);
66     // Handler function to get result from the task assigned to the thread in consideration
67     tpool_result_t (*join)(oapv_thread_t thread_id, int *res);
68     // Handler function to terminate a thread in consideration
69     tpool_result_t (*release)(oapv_thread_t *thread_id);
70     // handle for mask number of allowed thread
71     int max_task_cnt;
72 };
73 
74 tpool_result_t oapv_tpool_init(oapv_tpool_t *tp, int maxtask);
75 tpool_result_t oapv_tpool_deinit(oapv_tpool_t *tp);
76 
77 oapv_sync_obj_t oapv_tpool_sync_obj_create();
78 tpool_result_t oapv_tpool_sync_obj_delete(oapv_sync_obj_t *sobj);
79 int oapv_tpool_spinlock_wait(volatile int *addr, int val);
80 void threadsafe_assign(volatile int *addr, int val);
81 
82 void oapv_tpool_enter_cs(oapv_sync_obj_t sobj);
83 void oapv_tpool_leave_cs(oapv_sync_obj_t sobj);
84 
85 #endif // __OAPV_TPOOL_H__
86