yumapro  24.10-1
YumaPro SDK
Loading...
Searching...
No Matches
thd.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 - 2012, Andy Bierman, All Rights Reserved.
3 * Copyright (c) 2012 - 2021, YumaWorks, Inc., All Rights Reserved.
4 *
5 * Unless required by applicable law or agreed to in writing,
6 * software distributed under the License is distributed on an
7 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the
9 * specific language governing permissions and limitations
10 * under the License.
11 */
12
13#ifdef PTHREADS
14#ifndef _H_thd
15#define _H_thd
16
17/* FILE: thd.h
18*********************************************************************
19* *
20* P U R P O S E *
21* *
22*********************************************************************/
23
29/*********************************************************************
30* *
31* C H A N G E H I S T O R Y *
32* *
33*********************************************************************
34
35date init comment
36----------------------------------------------------------------------
3714-Jan-13 mts begun, borrowed from log.h: Pthread feature set
38*/
39
40#include <stdio.h>
41#include <xmlstring.h>
42#include <pthread.h>
43
44#ifdef WINDOWS
45#include <winsock2.h>
46#include <windows.h>
47#else
48#include <sys/signal.h>
49#endif
50
51#ifdef HAS_SIGNALFD
52#include <sys/signalfd.h>
53#endif
54
55#include "procdefs.h"
56#include "status.h"
57#include "ses.h"
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
63/********************************************************************
64* *
65* M A C R O S *
66* *
67*********************************************************************/
68
69
70/********************************************************************
71* *
72* C O N S T A N T S *
73* *
74*********************************************************************/
75#undef DEBUG_THD
76/*
77 * Not strictly necessary, but this gives us the flexibility to
78 * force thd debugs separately into a special (non-DEBUG) build.
79 */
80#ifdef DEBUG
81#define DEBUG_THD // DEBUG compile only thread-related debugs
82#endif
83
84#define LOCK_BANNER "[LOCK]"
85
86/* For use with thd_cleanup() */
87#define THD_CLEANUP_PHASE1 TRUE
88#define THD_CLEANUP_PHASE2 FALSE
89
90/* For use with thd_find_tcb_by_sid() ('strict' param) */
91#define THD_STRICT_LOOKUP TRUE
92#define THD_SIMPLE_LOOKUP !THD_STRICT_LOOKUP
93
94#define NULL_PTHREAD (pthread_t)NULL
95
96#define INIT_TID (-1) /* For use during init */
97#define INIT_SID (-1) /* For use during init */
98
99#define MAINLINE_TID 0 /* To distinguish between MAINLINE and thread */
100#define MAINLINE_SID 0 /* To distinguish between MAINLINE and thread */
101
102#define NULL_TID 0 /* For use to show "none" */
103#define NULL_SID SES_NULL_SID /* For use to show "none" */
104
105
106/* Thread capability bits */
107#define THD_CAP_CANCEL_CC bit0 /* Background thread for CC cancel */
108#define THD_CAP_NEXT_AVAIL bit1
109
110#define THD_INC_THD_CNT ENTER_FAST_CS; /* ++++++++++++++++++++++++ */ \
111 thread_count++; \
112 EXIT_FAST_CS; /* ------------------------ */
113
114#define THD_DEC_THD_CNT ENTER_FAST_CS; /* ++++++++++++++++++++++++ */ \
115 thread_count--; \
116 EXIT_FAST_CS; /* ------------------------ */
117
118#ifdef HAS_SIGNALFD
119#define THD_SIGNAL SIGVTALRM
120#else
121#define THD_SIGNAL SIGTERM
122#endif
123
124#define THD_LOGROTATE_SIG SIGUSR2
125
126
127/********************************************************************
128* *
129* T Y P E S *
130* *
131*********************************************************************/
132
145typedef boolean (*test_cdx_void_t) (void);
146typedef boolean (*test_cdx_int_t) (int);
147
149typedef enum thd_tcb_state_t_ {
154 THD_TCB_STATE_LIMIT
156
157#define THD_TCB_STATE_MIN (THD_TCB_STATE_NONE + 1)
158#define THD_TCB_STATE_MAX (THD_TCB_STATE_LIMIT - 1)
159
161typedef enum thd_type_t_ {
168 THD_TYPE_LIMIT
169} thd_type_t;
170
171#define THD_TYPE_MIN (THD_TYPE_NONE + 1)
172#define THD_TYPE_MAX (THD_TYPE_LIMIT - 1)
173
175typedef struct thd_tcb_t_ {
176
177 dlq_hdr_t qhdr;
179 pthread_t handle;
181 uint thd_cap;
182 const char *name;
185 // ses_cb_t *scb; /* NOT USED anymore, use SID instead*/
186 int tid;
187 int run;
188 int fd;
189 int sfd;
190 fd_set tcb_fd_set;
191 int sleep;
196 void *rwlock_locked[NCX_CFGID_CNT];
197
199 boolean rwlock_wrlocked[NCX_CFGID_CNT];
200
202 int rwlock_lock_level[NCX_CFGID_CNT];
203
205 boolean rwlock_req_needed[NCX_CFGID_CNT];
206
208 boolean rwlock_req_wrlock[NCX_CFGID_CNT];
209
211 boolean rwlock_req_locked[NCX_CFGID_CNT];
212
214 boolean rwlock_req_error[NCX_CFGID_CNT];
215
218
221
224
227
229 boolean cancel_cc;
230
233
237 struct agt_not_subscription_t_ *sub; /* back-ptr to subscription */
238 boolean snmp_notif;
240} thd_tcb_t;
241
242
243
244/********************************************************************
245* *
246* V A R I A B L E S *
247* *
248*********************************************************************/
249extern boolean thd_init_called;
250extern boolean thd_init_successful;
251
252extern int thread_count;
253extern boolean thd_cs_mutex_init;
254
255extern pthread_attr_t pthread_attr;
256
257
258/********************************************************************
259* *
260* F U N C T I O N S *
261* *
262*********************************************************************/
263
271extern boolean
272 thd_rcv_signal_setup(sigset_t *sigmask,
273 thd_tcb_t *tcb);
274
275
286extern void
287 thd_check_shutdown_signal (sigset_t *sigmask,
288 thd_tcb_t *tcb);
289
301extern boolean
302 thd_yield_for_cdx (int max_yield_count,
303 uint usec,
304 int ival,
305 test_cdx_int_t sid_test_func);
306
307
317extern void thd_set_main_pthread_attr (void);
318
319
325extern status_t thd_master_init ( void );
326
327
337extern void thd_set_main_handle ( void );
338
339
347extern thd_tcb_t *thd_new_tcb ( thd_type_t type );
348
349
355extern void thd_clear_locks(thd_tcb_t *tcb);
356
357
367extern void thd_mark_tcb_deleted( thd_tcb_t *tcb);
368
369
374extern void thd_shutdown (void);
375
376
392extern void thd_cleanup( boolean phase1 );
393
394
401extern thd_tcb_t *thd_find_tcb_by_handle( pthread_t handle );
402
403
410extern thd_tcb_t *thd_find_tcb_by_capability( uint cap );
411
412
419extern thd_tcb_t *
421
422
428extern int
430
431
439extern thd_tcb_t *thd_find_tcb_by_sid( ses_id_t sid, boolean strict );
440
441
442
452extern ses_id_t thd_find_sess_id_by_handle( pthread_t handle );
453
454
463extern ses_id_t thd_find_sess_id ( void );
464
465
474extern int thd_find_thread_id ( void );
475
476
482extern thd_tcb_t *thd_get_thread_tcb ( void );
483
484
493extern void thd_get_ids (int *tid, ses_id_t *sid);
494
495
502extern void thd_set_ready (void);
503
504
511extern int thd_lock_recursive_log_cs_mutex (void);
512
513
520extern int thd_unlock_recursive_log_cs_mutex (void);
521
522
529extern int thd_lock_ses_sync_mutex (void);
530
531
538extern int thd_unlock_ses_sync_mutex (void);
539
540
547extern int thd_lock_inreadyQ_mutex (ses_id_t sid);
548
549
556extern int thd_unlock_inreadyQ_mutex (ses_id_t sid);
557
558
568
569
579
580
587
588
595extern thd_tcb_t *
596 thd_get_first_tcb (void);
597
598
606extern thd_tcb_t *
608
609
613#ifdef __cplusplus
614} /* end extern 'C' */
615#endif
616
617#endif /* _H_thd */
618#endif // PTHREADS
status_t
global error return code
Definition: status_enum.h:210
ses_term_reason_t
Session Termination reason.
Definition: ses.h:417
uint32 ses_id_t
Session ID.
Definition: ses.h:335
status_t thd_module_db_wrlock(thd_tcb_t *tcb)
Request write access to the module DB ("object tree").
Definition: thd.c:1399
thd_tcb_t * thd_find_tcb_by_handle(pthread_t handle)
Find a thread by its OS pthread handle.
Definition: thd.c:757
ses_id_t thd_find_sess_id(void)
Find the session ID represented by the current thread.
Definition: thd.c:951
void thd_shutdown(void)
Begin thread ermination by signalling.
Definition: thd.c:1817
thd_tcb_t * thd_get_first_tcb(void)
Get the first thread control block.
Definition: thd.c:2003
int thd_lock_recursive_log_cs_mutex(void)
Request lock on recursive logger CS (critical section) mutex.
Definition: thd.c:1219
void thd_set_main_handle(void)
Record the thread handle of "main" (running initially before any threads are created) for reference.
Definition: thd.c:502
ses_id_t thd_find_sess_id_by_handle(pthread_t handle)
Find the session ID represented by the thread handle.
Definition: thd.c:912
void thd_get_ids(int *tid, ses_id_t *sid)
Return the TID and SID of the current running thread.
Definition: thd.c:1025
int thd_unlock_ses_sync_mutex(void)
Unlock session sync mutex (enforce session synchronization)
Definition: thd.c:1112
void thd_cleanup(boolean phase1)
Terminate threads and deallocate associated resources, including thread control blocks.
Definition: thd.c:1864
void thd_check_shutdown_signal(sigset_t *sigmask, thd_tcb_t *tcb)
The agt_shutdown_requested == TRUE at this point.
Definition: thd.c:1767
thd_tcb_t * thd_new_tcb(thd_type_t type)
Allocate a new thread control block.
Definition: thd.c:548
void thd_set_ready(void)
Netconfd will set thd_ready when initialization is complete, but before creating threads.
Definition: thd.c:528
int thd_unlock_recursive_log_cs_mutex(void)
Unlock recursive logger CS (critical section) mutex.
Definition: thd.c:1252
int thd_unlock_inreadyQ_mutex(ses_id_t sid)
Unlock mutex for access to the inreadyQ (pending input).
Definition: thd.c:1312
boolean thd_rcv_signal_setup(sigset_t *sigmask, thd_tcb_t *tcb)
Init thread to receive omnibus signal.
Definition: thd.c:1710
void thd_mark_tcb_deleted(thd_tcb_t *tcb)
Cleanup (release resources associated with a TCB) and free it.
Definition: thd.c:661
status_t thd_module_db_unlock(thd_tcb_t *tcb)
Unlock (release access) to the module DB ("object tree").
Definition: thd.c:1453
thd_tcb_t * thd_find_tcb_by_sid(ses_id_t sid, boolean strict)
Lookup TCB by session ID.
Definition: thd.c:708
int thd_count_ses_threads(void)
Count the number of session threads.
Definition: thd.c:871
int thd_lock_ses_sync_mutex(void)
Request lock on session sync mutex (enforce session synchronization)
Definition: thd.c:1072
int thd_find_thread_id(void)
Find the thread ID represented by the current thread.
Definition: thd.c:972
thd_tcb_t * thd_get_thread_tcb(void)
Return the TCB of the current thread.
Definition: thd.c:998
boolean thd_yield_for_cdx(int max_yield_count, uint usec, int ival, test_cdx_int_t sid_test_func)
Yield scheduler in loop until specified condition(s) are met or max loop count is exceeded.
Definition: thd.c:1616
void thd_set_main_pthread_attr(void)
Set/display main pthread attributes, to be inherited by all threads, unless otherwise noted.
Definition: thd.c:413
status_t thd_master_init(void)
Initialize all mutexes for use by threads.
Definition: thd.c:1506
thd_tcb_t * thd_find_tcb_by_type(thd_type_t type)
Find a TCB supporting the specified type.
Definition: thd.c:833
status_t thd_module_db_rdlock(thd_tcb_t *tcb)
Request read access to the module DB ("object tree")
Definition: thd.c:1344
thd_type_t
Thread types.
Definition: thd.h:161
void thd_clear_locks(thd_tcb_t *tcb)
Clear mutexes and RWLOCKs prior to termination.
Definition: thd.c:584
thd_tcb_t * thd_find_tcb_by_capability(uint cap)
Find a TCB supporting the specified capability.
Definition: thd.c:795
thd_tcb_state_t
Thread (tcb) states.
Definition: thd.h:149
int thd_lock_inreadyQ_mutex(ses_id_t sid)
Request mutex lock for access to the inreadyQ (pending input).
Definition: thd.c:1281
thd_tcb_t * thd_get_next_tcb(thd_tcb_t *tcb)
Get the next thread control block from the specified entry.
Definition: thd.c:2021
@ THD_TYPE_DUMMY
Dummy session TCB.
Definition: thd.h:167
@ THD_TYPE_NONE
not set
Definition: thd.h:162
@ THD_TYPE_SES
Session (rx_thread)
Definition: thd.h:166
@ THD_TYPE_CONN
Connection.
Definition: thd.h:164
@ THD_TYPE_BKGD
Background.
Definition: thd.h:163
@ THD_TYPE_TMR
Timer.
Definition: thd.h:165
@ THD_TCB_STATE_INACTIVE
Phase 1 cleanup done - ready to reuse.
Definition: thd.h:152
@ THD_TCB_STATE_NONE
not set
Definition: thd.h:150
@ THD_TCB_STATE_ACTIVE
active
Definition: thd.h:151
@ THD_TCB_STATE_DELETED
Phase 2 cleanup done - ready to free
Definition: thd.h:153
ncx_confirm_event_t
type of confirmEvent in the sysConfirmedCommit notification Used in confirmed-commit standard as well
Definition: ncxtypes.h:1515
#define NCX_CFGID_CNT
Should match CFG_NUM_STATIC.
Definition: ncxtypes.h:557
NETCONF Session Common definitions module.
Global error messages for status code enumerations.
Session Control Block.
Definition: ses.h:573
Thread control block (TCB)
Definition: thd.h:175
thd_tcb_state_t state
thread state
Definition: thd.h:178
int sleep
sleep amount
Definition: thd.h:191
ses_id_t sid
Session ID of thread.
Definition: thd.h:184
boolean module_db_locked
module with datastores loads
Definition: thd.h:217
struct agt_not_subscription_t_ * sub
sub is used for RFC 5277 notifications only There will be 0 or 1 of these fields used
Definition: thd.h:237
boolean snmp_notif
1 subscription allowed
Definition: thd.h:238
dlq_hdr_t qhdr
queue header
Definition: thd.h:177
ses_id_t killedby
kill session done
Definition: thd.h:223
int fd
I/O file descriptor.
Definition: thd.h:188
thd_type_t thd_type
thread type
Definition: thd.h:180
fd_set tcb_fd_set
FDs used by thread.
Definition: thd.h:190
boolean cancel_cc
cancel confirmed-commit flag
Definition: thd.h:229
ncx_confirm_event_t cancel_cc_event
confirm-commit event
Definition: thd.h:232
pthread_t handle
Internal thread handle.
Definition: thd.h:179
boolean kill_session
kill session requested
Definition: thd.h:220
int tid
thread ID
Definition: thd.h:186
const char * name
thread name
Definition: thd.h:182
ses_cb_t * dscb
Dummy SCB, if any.
Definition: thd.h:183
uint thd_cap
Capability bits.
Definition: thd.h:181
ses_term_reason_t termreason
termination reason
Definition: thd.h:226
int run
internal run state
Definition: thd.h:187
int sfd
Signal file descriptor.
Definition: thd.h:189