yumapro  20.10-12
YumaPro SDK
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 
35 date init comment
36 ----------------------------------------------------------------------
37 14-Jan-13 mts begun, borrowed from log.h: Pthread feature set
38 */
39 
40 #include <assert.h>
41 #include <stdio.h>
42 #include <xmlstring.h>
43 #include <pthread.h>
44 #include <sys/signal.h>
45 
46 #ifdef HAS_SIGNALFD
47 #include <sys/signalfd.h>
48 #endif
49 
50 #include "procdefs.h"
51 #include "status.h"
52 #include "ses.h"
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 /********************************************************************
59 * *
60 * M A C R O S *
61 * *
62 *********************************************************************/
63 
64 
65 /********************************************************************
66 * *
67 * C O N S T A N T S *
68 * *
69 *********************************************************************/
70 #undef DEBUG_THD
71 /*
72  * Not strictly necessary, but this gives us the flexibility to
73  * force thd debugs separately into a special (non-DEBUG) build.
74  */
75 #ifdef DEBUG
76 #define DEBUG_THD // DEBUG compile only thread-related debugs
77 #endif
78 
79 #define LOCK_BANNER "[LOCK]"
80 
81 /* For use with thd_cleanup() */
82 #define THD_CLEANUP_PHASE1 TRUE
83 #define THD_CLEANUP_PHASE2 FALSE
84 
85 /* For use with thd_find_tcb_by_sid() ('strict' param) */
86 #define THD_STRICT_LOOKUP TRUE
87 #define THD_SIMPLE_LOOKUP !THD_STRICT_LOOKUP
88 
89 #define NULL_PTHREAD (pthread_t)NULL
90 
91 #define INIT_TID -1 /* For use during init */
92 #define INIT_SID -1 /* For use during init */
93 
94 #define MAINLINE_TID 0 /* To distinguish between MAINLINE and thread */
95 #define MAINLINE_SID 0 /* To distinguish between MAINLINE and thread */
96 
97 #define NULL_TID 0 /* For use to show "none" */
98 #define NULL_SID SES_NULL_SID /* For use to show "none" */
99 
100 
101 /* Thread capability bits */
102 #define THD_CAP_CANCEL_CC bit0 /* Background thread for CC cancel */
103 #define THD_CAP_NEXT_AVAIL bit1
104 
105 #define THD_INC_THD_CNT ENTER_FAST_CS; /* ++++++++++++++++++++++++ */ \
106  thread_count++; \
107  EXIT_FAST_CS; /* ------------------------ */
108 
109 #define THD_DEC_THD_CNT ENTER_FAST_CS; /* ++++++++++++++++++++++++ */ \
110  thread_count--; \
111  EXIT_FAST_CS; /* ------------------------ */
112 
113 #define THD_SIGNAL SIGVTALRM
114 #define THD_LOGROTATE_SIG SIGUSR2
115 
116 
117 /********************************************************************
118 * *
119 * T Y P E S *
120 * *
121 *********************************************************************/
122 
135 typedef boolean (*test_cdx_void_t) (void);
136 typedef boolean (*test_cdx_int_t) (int);
137 
139 typedef enum thd_tcb_state_t_ {
144  THD_TCB_STATE_LIMIT
145 } thd_tcb_state_t;
146 
147 #define THD_TCB_STATE_MIN (THD_TCB_STATE_NONE + 1)
148 #define THD_TCB_STATE_MAX (THD_TCB_STATE_LIMIT - 1)
149 
151 typedef enum thd_type_t_ {
158  THD_TYPE_LIMIT
159 } thd_type_t;
160 
161 #define THD_TYPE_MIN (THD_TYPE_NONE + 1)
162 #define THD_TYPE_MAX (THD_TYPE_LIMIT - 1)
163 
165 typedef struct thd_tcb_t_ {
166 
167  dlq_hdr_t qhdr;
168  thd_tcb_state_t state;
169  pthread_t handle;
170  thd_type_t thd_type;
171  uint thd_cap;
172  const char *name;
173  ses_cb_t *dscb;
175  // ses_cb_t *scb; /* NOT USED anymore, use SID instead*/
176  int tid;
177  int run;
178  int fd;
179  int sfd;
180  fd_set tcb_fd_set;
181  int sleep;
187 
190 
193 
196 
199 
202 
205 
208 
210  boolean kill_session;
211 
214 
216  ses_term_reason_t termreason;
217 
219  boolean cancel_cc;
220 
222  ncx_confirm_event_t cancel_cc_event;
223 
227  struct agt_not_subscription_t_ *sub; /* back-ptr to subscription */
228  boolean snmp_notif;
230 } thd_tcb_t;
231 
232 
233 
234 /********************************************************************
235 * *
236 * V A R I A B L E S *
237 * *
238 *********************************************************************/
239 extern boolean thd_init_called;
240 extern boolean thd_init_successful;
241 
242 extern int thread_count;
243 extern boolean thd_cs_mutex_init;
244 
245 extern pthread_attr_t pthread_attr;
246 
247 
248 /********************************************************************
249 * *
250 * F U N C T I O N S *
251 * *
252 *********************************************************************/
253 
261 extern boolean
262  thd_rcv_signal_setup(sigset_t *sigmask,
263  thd_tcb_t *tcb);
264 
265 
276 extern void
277  thd_check_shutdown_signal (sigset_t *sigmask,
278  thd_tcb_t *tcb);
279 
291 extern boolean
292  thd_yield_for_cdx (int max_yield_count,
293  uint usec,
294  int ival,
295  test_cdx_int_t sid_test_func);
296 
297 
307 extern void thd_set_main_pthread_attr (void);
308 
309 
315 extern status_t thd_master_init ( void );
316 
317 
327 extern void thd_set_main_handle ( void );
328 
329 
337 extern thd_tcb_t *thd_new_tcb ( thd_type_t type );
338 
339 
345 extern void thd_clear_locks(thd_tcb_t *tcb);
346 
347 
357 extern void thd_mark_tcb_deleted( thd_tcb_t *tcb);
358 
359 
364 extern void thd_shutdown (void);
365 
366 
382 extern void thd_cleanup( boolean phase1 );
383 
384 
391 extern thd_tcb_t *thd_find_tcb_by_handle( pthread_t handle );
392 
393 
400 extern thd_tcb_t *thd_find_tcb_by_capability( uint cap );
401 
402 
409 extern thd_tcb_t *
410  thd_find_tcb_by_type (thd_type_t type);
411 
412 
418 extern int
419  thd_count_ses_threads (void);
420 
421 
429 extern thd_tcb_t *thd_find_tcb_by_sid( ses_id_t sid, boolean strict );
430 
431 
432 
442 extern ses_id_t thd_find_sess_id_by_handle( pthread_t handle );
443 
444 
453 extern ses_id_t thd_find_sess_id ( void );
454 
455 
464 extern int thd_find_thread_id ( void );
465 
466 
472 extern thd_tcb_t *thd_get_thread_tcb ( void );
473 
474 
483 extern void thd_get_ids (int *tid, ses_id_t *sid);
484 
485 
492 extern void thd_set_ready (void);
493 
494 
501 extern int thd_lock_recursive_log_cs_mutex (void);
502 
503 
510 extern int thd_unlock_recursive_log_cs_mutex (void);
511 
512 
519 extern int thd_lock_ses_sync_mutex (void);
520 
521 
528 extern int thd_unlock_ses_sync_mutex (void);
529 
530 
537 extern int thd_lock_inreadyQ_mutex (ses_id_t sid);
538 
539 
546 extern int thd_unlock_inreadyQ_mutex (ses_id_t sid);
547 
548 
557 extern status_t thd_module_db_rdlock (thd_tcb_t *tcb);
558 
559 
568 extern status_t thd_module_db_wrlock (thd_tcb_t *tcb);
569 
570 
576 extern status_t thd_module_db_unlock (thd_tcb_t *tcb);
577 
578 
585 extern thd_tcb_t *
586  thd_get_first_tcb (void);
587 
588 
596 extern thd_tcb_t *
597  thd_get_next_tcb (thd_tcb_t *tcb);
598 
599 
603 #ifdef __cplusplus
604 } /* end extern 'C' */
605 #endif
606 
607 #endif /* _H_thd */
608 #endif // PTHREADS
thd_unlock_inreadyQ_mutex
int thd_unlock_inreadyQ_mutex(ses_id_t sid)
Unlock mutex for access to the inreadyQ (pending input).
Definition: thd.c:1308
ses.h
NETCONF Session Common definitions module.
THD_TYPE_BKGD
@ THD_TYPE_BKGD
Background.
Definition: thd.h:153
cfg.h
THD_TCB_STATE_INACTIVE
@ THD_TCB_STATE_INACTIVE
Phase 1 cleanup done - ready to reuse.
Definition: thd.h:142
tstamp.h
Timestamp utilities.
thd_tcb_t_::dscb
ses_cb_t * dscb
Dummy SCB, if any.
Definition: thd.h:173
thd_tcb_t_::state
thd_tcb_state_t state
thread state
Definition: thd.h:168
thd_unlock_recursive_log_cs_mutex
int thd_unlock_recursive_log_cs_mutex(void)
Unlock recursive logger CS (critical section) mutex.
Definition: thd.c:1248
log_error
void log_error(const char *fstr,...) __attribute__((format(printf
Generate a new LOG_DEBUG_ERROR log entry.
xml_util.h
XML Utilities.
thd_check_shutdown_signal
void thd_check_shutdown_signal(sigset_t *sigmask, thd_tcb_t *tcb)
The agt_shutdown_requested == TRUE at this point.
Definition: thd.c:1769
thd_tcb_t_::rwlock_lock_level
int rwlock_lock_level[NCX_CFGID_CNT]
Nesting level of locking.
Definition: thd.h:192
thd_tcb_t_::killedby
ses_id_t killedby
kill session done
Definition: thd.h:213
log_vendor.h
Logging manager vendor API.
thd_tcb_t_::rwlock_req_error
boolean rwlock_req_error[NCX_CFGID_CNT]
read locks by datastore
Definition: thd.h:204
thd_type_t_
thd_type_t_
Thread types.
Definition: thd.h:151
thd_find_thread_id
int thd_find_thread_id(void)
Find the thread ID represented by the current thread.
Definition: thd.c:968
thd_set_ready
void thd_set_ready(void)
Netconfd will set thd_ready when initialization is complete, but before creating threads.
Definition: thd.c:524
thd_new_tcb
thd_tcb_t * thd_new_tcb(thd_type_t type)
Allocate a new thread control block.
Definition: thd.c:544
thd_clear_locks
void thd_clear_locks(thd_tcb_t *tcb)
Clear mutexes and RWLOCKs prior to termination.
Definition: thd.c:580
thd_tcb_t_::thd_type
thd_type_t thd_type
thread type
Definition: thd.h:170
ncxconst.h
Contains NCX constants.
THD_TCB_STATE_DELETED
@ THD_TCB_STATE_DELETED
Phase 2 cleanup done - ready to free
Definition: thd.h:143
THD_TCB_STATE_NONE
@ THD_TCB_STATE_NONE
not set
Definition: thd.h:140
thd_find_tcb_by_type
thd_tcb_t * thd_find_tcb_by_type(thd_type_t type)
Find a TCB supporting the specified type.
Definition: thd.c:829
ses_id_t
uint32 ses_id_t
Session ID.
Definition: ses.h:306
thd_yield_for_cdx
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:1618
log_info
void void void void void log_info(const char *fstr,...) __attribute__((format(printf
Generate a new LOG_DEBUG_INFO log entry.
thd_lock_ses_sync_mutex
int thd_lock_ses_sync_mutex(void)
Request lock on session sync mutex (enforce session synchronization)
Definition: thd.c:1068
thd_tcb_t_::name
const char * name
thread name
Definition: thd.h:172
NO_ERR
@ NO_ERR
000
Definition: status_enum.h:188
log_vendor_extern.h
Logging manager vendor API registration.
log_syslog.h
Syslog logging manager API.
thd_unlock_ses_sync_mutex
int thd_unlock_ses_sync_mutex(void)
Unlock session sync mutex (enforce session synchronization)
Definition: thd.c:1108
thd_tcb_t_::sub
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:227
cfg_rwlock_release_all_locks
status_t cfg_rwlock_release_all_locks(thd_tcb_t *tcb)
Release all RWLOCKs recorded in TCB lock state structure.
Definition: cfg.c:675
thd_get_next_tcb
thd_tcb_t * thd_get_next_tcb(thd_tcb_t *tcb)
Get the next thread control block from the specified entry.
Definition: thd.c:2023
dlq_createSQue
void dlq_createSQue(dlq_hdrT *queAddr)
create a static queue header
Definition: dlq.c:177
log_warn
void void void log_warn(const char *fstr,...) __attribute__((format(printf
Generate a new LOG_DEBUG_WARN log entry.
thd_tcb_t_::qhdr
dlq_hdr_t qhdr
queue header
Definition: thd.h:167
thd_mark_tcb_deleted
void thd_mark_tcb_deleted(thd_tcb_t *tcb)
Cleanup (release resources associated with a TCB) and free it.
Definition: thd.c:654
dlq_remove
void dlq_remove(void *nodeP)
remove the queue entry from its queue list entry MUST have been enqueued somehow before this function...
Definition: dlq.c:519
THD_TYPE_DUMMY
@ THD_TYPE_DUMMY
Dummy session TCB.
Definition: thd.h:157
thd_count_ses_threads
int thd_count_ses_threads(void)
Count the number of session threads.
Definition: thd.c:867
thd_tcb_t_::sid
ses_id_t sid
Session ID of thread.
Definition: thd.h:174
thd_tcb_t_::rwlock_req_locked
boolean rwlock_req_locked[NCX_CFGID_CNT]
write locks by datastore
Definition: thd.h:201
thd_module_db_wrlock
status_t thd_module_db_wrlock(thd_tcb_t *tcb)
Request write access to the module DB ("object tree").
Definition: thd.c:1397
thd_tcb_t_::termreason
ses_term_reason_t termreason
termination reason
Definition: thd.h:216
thd_cleanup
void thd_cleanup(boolean phase1)
Terminate threads and deallocate associated resources, including thread control blocks.
Definition: thd.c:1866
THD_TYPE_NONE
@ THD_TYPE_NONE
not set
Definition: thd.h:152
ERR_INTERNAL_PTR
@ ERR_INTERNAL_PTR
002
Definition: status_enum.h:192
thd_module_db_unlock
status_t thd_module_db_unlock(thd_tcb_t *tcb)
Unlock (release access) to the module DB ("object tree").
Definition: thd.c:1453
THD_TYPE_SES
@ THD_TYPE_SES
Session (rx_thread)
Definition: thd.h:156
thd_tcb_t_::cancel_cc
boolean cancel_cc
cancel confirmed-commit flag
Definition: thd.h:219
dlq_nextEntry
#define dlq_nextEntry(nodeP)
get the next queue entry after the current entry
Definition: dlq.h:265
thd_tcb_t_::fd
int fd
I/O file descriptor.
Definition: thd.h:178
thd_get_thread_tcb
thd_tcb_t * thd_get_thread_tcb(void)
Return the TCB of the current thread.
Definition: thd.c:994
thd_tcb_t_
Thread control block (TCB)
Definition: thd.h:165
thd_tcb_t_::handle
pthread_t handle
Internal thread handle.
Definition: thd.h:169
thd_tcb_t_::thd_cap
uint thd_cap
Capability bits.
Definition: thd.h:171
thd_master_init
status_t thd_master_init(void)
Initialize all mutexes for use by threads.
Definition: thd.c:1508
thd_get_first_tcb
thd_tcb_t * thd_get_first_tcb(void)
Get the first thread control block.
Definition: thd.c:2005
thd_tcb_state_t_
thd_tcb_state_t_
Thread (tcb) states.
Definition: thd.h:139
log.h
NCX System Logging Manager.
ERR_MUTEX_ERR
@ ERR_MUTEX_ERR
015
Definition: status_enum.h:205
THD_TCB_STATE_ACTIVE
@ THD_TCB_STATE_ACTIVE
active
Definition: thd.h:141
thd_find_sess_id
ses_id_t thd_find_sess_id(void)
Find the session ID represented by the current thread.
Definition: thd.c:947
dlq_deque
void * dlq_deque(dlq_hdrT *listP)
remove the first queue node from the queue list
Definition: dlq.c:286
thd_set_main_handle
void thd_set_main_handle(void)
Record the thread handle of "main" (running initially before any threads are created) for reference.
Definition: thd.c:498
THD_TYPE_CONN
@ THD_TYPE_CONN
Connection.
Definition: thd.h:154
thd_tcb_t_::rwlock_req_needed
boolean rwlock_req_needed[NCX_CFGID_CNT]
Lock request context : locks needed by datastore.
Definition: thd.h:195
thd.h
Thread support (for now limited to netconfd)
thd_tcb_t_::kill_session
boolean kill_session
kill session requested
Definition: thd.h:210
thd_tcb_t_::module_db_locked
boolean module_db_locked
module with datastores loads
Definition: thd.h:207
thd_tcb_t_::run
int run
internal run state
Definition: thd.h:177
dlq_firstEntry
#define dlq_firstEntry(listP)
get the first entry in the queue list
Definition: dlq.h:337
thd_tcb_t_::sleep
int sleep
sleep amount
Definition: thd.h:181
thd_tcb_t_::rwlock_req_wrlock
boolean rwlock_req_wrlock[NCX_CFGID_CNT]
Lock request context : write locks by datastore.
Definition: thd.h:198
thd_tcb_t_::rwlock_wrlocked
boolean rwlock_wrlocked[NCX_CFGID_CNT]
TRUE if cfg write locked.
Definition: thd.h:189
thd_shutdown
void thd_shutdown(void)
Begin thread ermination by signalling.
Definition: thd.c:1819
thd_find_sess_id_by_handle
ses_id_t thd_find_sess_id_by_handle(pthread_t handle)
Find the session ID represented by the thread handle.
Definition: thd.c:908
dlq_enque
void dlq_enque(REG void *newP, REG dlq_hdrT *listP)
add a queue node to the end of a queue list Add newP to listP
Definition: dlq.c:246
thd_tcb_t_::cancel_cc_event
ncx_confirm_event_t cancel_cc_event
confirm-commit event
Definition: thd.h:222
NCX_CFGID_CNT
#define NCX_CFGID_CNT
Should match CFG_NUM_STATIC.
Definition: ncxtypes.h:392
thd_set_main_pthread_attr
void thd_set_main_pthread_attr(void)
Set/display main pthread attributes, to be inherited by all threads, unless otherwise noted.
Definition: thd.c:410
SET_ERROR
#define SET_ERROR(E)
macro SET_ERROR
Definition: status_enum.h:103
thd_find_tcb_by_capability
thd_tcb_t * thd_find_tcb_by_capability(uint cap)
Find a TCB supporting the specified capability.
Definition: thd.c:791
thd_lock_recursive_log_cs_mutex
int thd_lock_recursive_log_cs_mutex(void)
Request lock on recursive logger CS (critical section) mutex.
Definition: thd.c:1215
thd_get_ids
void thd_get_ids(int *tid, ses_id_t *sid)
Return the TID and SID of the current running thread.
Definition: thd.c:1021
thd_find_tcb_by_handle
thd_tcb_t * thd_find_tcb_by_handle(pthread_t handle)
Find a thread by its OS pthread handle.
Definition: thd.c:753
thd_tcb_t_::sfd
int sfd
Signal file descriptor.
Definition: thd.h:179
thd_tcb_t_::rwlock_locked
void * rwlock_locked[NCX_CFGID_CNT]
Transaction (TXID) lock context: ptr to cfg, if locked by sess.
Definition: thd.h:186
thd_lock_inreadyQ_mutex
int thd_lock_inreadyQ_mutex(ses_id_t sid)
Request mutex lock for access to the inreadyQ (pending input).
Definition: thd.c:1277
thd_rcv_signal_setup
boolean thd_rcv_signal_setup(sigset_t *sigmask, thd_tcb_t *tcb)
Init thread to receive omnibus signal.
Definition: thd.c:1712
THD_TYPE_TMR
@ THD_TYPE_TMR
Timer.
Definition: thd.h:155
status.h
Global error messages for status code enumerations.
thd_tcb_t_::tid
int tid
thread ID
Definition: thd.h:176
thd_tcb_t_::snmp_notif
boolean snmp_notif
1 subscription allowed
Definition: thd.h:228
thd_module_db_rdlock
status_t thd_module_db_rdlock(thd_tcb_t *tcb)
Request read access to the module DB ("object tree")
Definition: thd.c:1340
thd_find_tcb_by_sid
thd_tcb_t * thd_find_tcb_by_sid(ses_id_t sid, boolean strict)
Lookup TCB by session ID.
Definition: thd.c:704
ERR_RWLOCK_ERR
@ ERR_RWLOCK_ERR
014
Definition: status_enum.h:204
thd_tcb_t_::tcb_fd_set
fd_set tcb_fd_set
FDs used by thread.
Definition: thd.h:180