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