yumapro  21.10T-9
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 
45 #ifdef WINDOWS
46 #include <winsock2.h>
47 #include <windows.h>
48 #else
49 #include <sys/signal.h>
50 #endif
51 
52 #ifdef HAS_SIGNALFD
53 #include <sys/signalfd.h>
54 #endif
55 
56 #include "procdefs.h"
57 #include "status.h"
58 #include "ses.h"
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 /********************************************************************
65 * *
66 * M A C R O S *
67 * *
68 *********************************************************************/
69 
70 
71 /********************************************************************
72 * *
73 * C O N S T A N T S *
74 * *
75 *********************************************************************/
76 #undef DEBUG_THD
77 /*
78  * Not strictly necessary, but this gives us the flexibility to
79  * force thd debugs separately into a special (non-DEBUG) build.
80  */
81 #ifdef DEBUG
82 #define DEBUG_THD // DEBUG compile only thread-related debugs
83 #endif
84 
85 #define LOCK_BANNER "[LOCK]"
86 
87 /* For use with thd_cleanup() */
88 #define THD_CLEANUP_PHASE1 TRUE
89 #define THD_CLEANUP_PHASE2 FALSE
90 
91 /* For use with thd_find_tcb_by_sid() ('strict' param) */
92 #define THD_STRICT_LOOKUP TRUE
93 #define THD_SIMPLE_LOOKUP !THD_STRICT_LOOKUP
94 
95 #define NULL_PTHREAD (pthread_t)NULL
96 
97 #define INIT_TID (-1) /* For use during init */
98 #define INIT_SID (-1) /* For use during init */
99 
100 #define MAINLINE_TID 0 /* To distinguish between MAINLINE and thread */
101 #define MAINLINE_SID 0 /* To distinguish between MAINLINE and thread */
102 
103 #define NULL_TID 0 /* For use to show "none" */
104 #define NULL_SID SES_NULL_SID /* For use to show "none" */
105 
106 
107 /* Thread capability bits */
108 #define THD_CAP_CANCEL_CC bit0 /* Background thread for CC cancel */
109 #define THD_CAP_NEXT_AVAIL bit1
110 
111 #define THD_INC_THD_CNT ENTER_FAST_CS; /* ++++++++++++++++++++++++ */ \
112  thread_count++; \
113  EXIT_FAST_CS; /* ------------------------ */
114 
115 #define THD_DEC_THD_CNT ENTER_FAST_CS; /* ++++++++++++++++++++++++ */ \
116  thread_count--; \
117  EXIT_FAST_CS; /* ------------------------ */
118 
119 #ifdef HAS_SIGNALFD
120 #define THD_SIGNAL SIGVTALRM
121 #else
122 #define THD_SIGNAL SIGTERM
123 #endif
124 
125 #define THD_LOGROTATE_SIG SIGUSR2
126 
127 
128 /********************************************************************
129 * *
130 * T Y P E S *
131 * *
132 *********************************************************************/
133 
146 typedef boolean (*test_cdx_void_t) (void);
147 typedef boolean (*test_cdx_int_t) (int);
148 
150 typedef enum thd_tcb_state_t_ {
155  THD_TCB_STATE_LIMIT
157 
158 #define THD_TCB_STATE_MIN (THD_TCB_STATE_NONE + 1)
159 #define THD_TCB_STATE_MAX (THD_TCB_STATE_LIMIT - 1)
160 
162 typedef enum thd_type_t_ {
169  THD_TYPE_LIMIT
170 } thd_type_t;
171 
172 #define THD_TYPE_MIN (THD_TYPE_NONE + 1)
173 #define THD_TYPE_MAX (THD_TYPE_LIMIT - 1)
174 
176 typedef struct thd_tcb_t_ {
177 
178  dlq_hdr_t qhdr;
179  thd_tcb_state_t state;
180  pthread_t handle;
181  thd_type_t thd_type;
182  uint thd_cap;
183  const char *name;
186  // ses_cb_t *scb; /* NOT USED anymore, use SID instead*/
187  int tid;
188  int run;
189  int fd;
190  int sfd;
191  fd_set tcb_fd_set;
192  int sleep;
197  void *rwlock_locked[NCX_CFGID_CNT];
198 
200  boolean rwlock_wrlocked[NCX_CFGID_CNT];
201 
203  int rwlock_lock_level[NCX_CFGID_CNT];
204 
206  boolean rwlock_req_needed[NCX_CFGID_CNT];
207 
209  boolean rwlock_req_wrlock[NCX_CFGID_CNT];
210 
212  boolean rwlock_req_locked[NCX_CFGID_CNT];
213 
215  boolean rwlock_req_error[NCX_CFGID_CNT];
216 
219 
221  boolean kill_session;
222 
225 
228 
230  boolean cancel_cc;
231 
234 
238  struct agt_not_subscription_t_ *sub; /* back-ptr to subscription */
239  boolean snmp_notif;
241 } thd_tcb_t;
242 
243 
244 
245 /********************************************************************
246 * *
247 * V A R I A B L E S *
248 * *
249 *********************************************************************/
250 extern boolean thd_init_called;
251 extern boolean thd_init_successful;
252 
253 extern int thread_count;
254 extern boolean thd_cs_mutex_init;
255 
256 extern pthread_attr_t pthread_attr;
257 
258 
259 /********************************************************************
260 * *
261 * F U N C T I O N S *
262 * *
263 *********************************************************************/
264 
272 extern boolean
273  thd_rcv_signal_setup(sigset_t *sigmask,
274  thd_tcb_t *tcb);
275 
276 
287 extern void
288  thd_check_shutdown_signal (sigset_t *sigmask,
289  thd_tcb_t *tcb);
290 
302 extern boolean
303  thd_yield_for_cdx (int max_yield_count,
304  uint usec,
305  int ival,
306  test_cdx_int_t sid_test_func);
307 
308 
318 extern void thd_set_main_pthread_attr (void);
319 
320 
326 extern status_t thd_master_init ( void );
327 
328 
338 extern void thd_set_main_handle ( void );
339 
340 
348 extern thd_tcb_t *thd_new_tcb ( thd_type_t type );
349 
350 
356 extern void thd_clear_locks(thd_tcb_t *tcb);
357 
358 
368 extern void thd_mark_tcb_deleted( thd_tcb_t *tcb);
369 
370 
375 extern void thd_shutdown (void);
376 
377 
393 extern void thd_cleanup( boolean phase1 );
394 
395 
402 extern thd_tcb_t *thd_find_tcb_by_handle( pthread_t handle );
403 
404 
411 extern thd_tcb_t *thd_find_tcb_by_capability( uint cap );
412 
413 
420 extern thd_tcb_t *
421  thd_find_tcb_by_type (thd_type_t type);
422 
423 
429 extern int
430  thd_count_ses_threads (void);
431 
432 
440 extern thd_tcb_t *thd_find_tcb_by_sid( ses_id_t sid, boolean strict );
441 
442 
443 
453 extern ses_id_t thd_find_sess_id_by_handle( pthread_t handle );
454 
455 
464 extern ses_id_t thd_find_sess_id ( void );
465 
466 
475 extern int thd_find_thread_id ( void );
476 
477 
483 extern thd_tcb_t *thd_get_thread_tcb ( void );
484 
485 
494 extern void thd_get_ids (int *tid, ses_id_t *sid);
495 
496 
503 extern void thd_set_ready (void);
504 
505 
512 extern int thd_lock_recursive_log_cs_mutex (void);
513 
514 
521 extern int thd_unlock_recursive_log_cs_mutex (void);
522 
523 
530 extern int thd_lock_ses_sync_mutex (void);
531 
532 
539 extern int thd_unlock_ses_sync_mutex (void);
540 
541 
548 extern int thd_lock_inreadyQ_mutex (ses_id_t sid);
549 
550 
557 extern int thd_unlock_inreadyQ_mutex (ses_id_t sid);
558 
559 
569 
570 
580 
581 
588 
589 
596 extern thd_tcb_t *
597  thd_get_first_tcb (void);
598 
599 
607 extern thd_tcb_t *
609 
610 
614 #ifdef __cplusplus
615 } /* end extern 'C' */
616 #endif
617 
618 #endif /* _H_thd */
619 #endif // PTHREADS
int thd_unlock_ses_sync_mutex(void)
Unlock session sync mutex (enforce session synchronization)
Definition: thd.c:1115
Connection.
Definition: thd.h:165
void thd_mark_tcb_deleted(thd_tcb_t *tcb)
Cleanup (release resources associated with a TCB) and free it.
Definition: thd.c:661
ses_id_t thd_find_sess_id(void)
Find the session ID represented by the current thread.
Definition: thd.c:954
Phase 2 cleanup done - ready to free.
Definition: thd.h:154
ses_id_t thd_find_sess_id_by_handle(pthread_t handle)
Find the session ID represented by the thread handle.
Definition: thd.c:915
boolean module_db_locked
module with datastores loads
Definition: thd.h:218
thd_tcb_t * thd_find_tcb_by_capability(uint cap)
Find a TCB supporting the specified capability.
Definition: thd.c:798
status_t thd_module_db_wrlock(thd_tcb_t *tcb)
Request write access to the module DB ("object tree").
Definition: thd.c:1404
void thd_get_ids(int *tid, ses_id_t *sid)
Return the TID and SID of the current running thread.
Definition: thd.c:1028
NETCONF Session Common definitions module.
status_t thd_module_db_rdlock(thd_tcb_t *tcb)
Request read access to the module DB ("object tree")
Definition: thd.c:1347
status_t thd_module_db_unlock(thd_tcb_t *tcb)
Unlock (release access) to the module DB ("object tree").
Definition: thd.c:1460
thd_tcb_t * thd_get_first_tcb(void)
Get the first thread control block.
Definition: thd.c:2012
thd_tcb_t * thd_find_tcb_by_handle(pthread_t handle)
Find a thread by its OS pthread handle.
Definition: thd.c:760
Session (rx_thread)
Definition: thd.h:167
not set
Definition: thd.h:163
const char * name
thread name
Definition: thd.h:183
thd_tcb_t * thd_get_thread_tcb(void)
Return the TCB of the current thread.
Definition: thd.c:1001
uint32 ses_id_t
Session ID.
Definition: ses.h:306
boolean thd_rcv_signal_setup(sigset_t *sigmask, thd_tcb_t *tcb)
Init thread to receive omnibus signal.
Definition: thd.c:1719
Phase 1 cleanup done - ready to reuse.
Definition: thd.h:153
ses_term_reason_t
Session Termination reason.
Definition: ses.h:387
boolean cancel_cc
cancel confirmed-commit flag
Definition: thd.h:230
int thd_unlock_recursive_log_cs_mutex(void)
Unlock recursive logger CS (critical section) mutex.
Definition: thd.c:1255
Thread control block (TCB)
Definition: thd.h:176
thd_type_t thd_type
thread type
Definition: thd.h:181
ses_term_reason_t termreason
termination reason
Definition: thd.h:227
void thd_set_main_handle(void)
Record the thread handle of "main" (running initially before any threads are created) for reference...
Definition: thd.c:505
void thd_check_shutdown_signal(sigset_t *sigmask, thd_tcb_t *tcb)
The agt_shutdown_requested == TRUE at this point.
Definition: thd.c:1776
ses_cb_t * dscb
Dummy SCB, if any.
Definition: thd.h:184
Timer.
Definition: thd.h:166
active
Definition: thd.h:152
void thd_clear_locks(thd_tcb_t *tcb)
Clear mutexes and RWLOCKs prior to termination.
Definition: thd.c:587
thd_tcb_state_t state
thread state
Definition: thd.h:179
uint thd_cap
Capability bits.
Definition: thd.h:182
thd_tcb_state_t
Thread (tcb) states.
Definition: thd.h:150
not set
Definition: thd.h:151
int run
internal run state
Definition: thd.h:188
void thd_set_main_pthread_attr(void)
Set/display main pthread attributes, to be inherited by all threads, unless otherwise noted...
Definition: thd.c:416
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:1625
int tid
thread ID
Definition: thd.h:187
void thd_shutdown(void)
Begin thread ermination by signalling.
Definition: thd.c:1826
Dummy session TCB.
Definition: thd.h:168
Session Control Block.
Definition: ses.h:542
thd_type_t
Thread types.
Definition: thd.h:162
status_t
global error return code
Definition: status_enum.h:186
Background.
Definition: thd.h:164
#define NCX_CFGID_CNT
Should match CFG_NUM_STATIC.
Definition: ncxtypes.h:544
thd_tcb_t * thd_find_tcb_by_type(thd_type_t type)
Find a TCB supporting the specified type.
Definition: thd.c:836
thd_tcb_t * thd_find_tcb_by_sid(ses_id_t sid, boolean strict)
Lookup TCB by session ID.
Definition: thd.c:711
thd_tcb_t * thd_get_next_tcb(thd_tcb_t *tcb)
Get the next thread control block from the specified entry.
Definition: thd.c:2030
int thd_lock_recursive_log_cs_mutex(void)
Request lock on recursive logger CS (critical section) mutex.
Definition: thd.c:1222
int thd_lock_ses_sync_mutex(void)
Request lock on session sync mutex (enforce session synchronization)
Definition: thd.c:1075
ncx_confirm_event_t
type of confirmEvent in the sysConfirmedCommit notification Used in confirmed-commit standard as well...
Definition: ncxtypes.h:1377
int thd_lock_inreadyQ_mutex(ses_id_t sid)
Request mutex lock for access to the inreadyQ (pending input).
Definition: thd.c:1284
Global error messages for status code enumerations.
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:238
int thd_unlock_inreadyQ_mutex(ses_id_t sid)
Unlock mutex for access to the inreadyQ (pending input).
Definition: thd.c:1315
int fd
I/O file descriptor.
Definition: thd.h:189
int sfd
Signal file descriptor.
Definition: thd.h:190
ses_id_t killedby
kill session done
Definition: thd.h:224
int thd_count_ses_threads(void)
Count the number of session threads.
Definition: thd.c:874
boolean kill_session
kill session requested
Definition: thd.h:221
ncx_confirm_event_t cancel_cc_event
confirm-commit event
Definition: thd.h:233
void thd_cleanup(boolean phase1)
Terminate threads and deallocate associated resources, including thread control blocks.
Definition: thd.c:1873
thd_tcb_t * thd_new_tcb(thd_type_t type)
Allocate a new thread control block.
Definition: thd.c:551
status_t thd_master_init(void)
Initialize all mutexes for use by threads.
Definition: thd.c:1515
fd_set tcb_fd_set
FDs used by thread.
Definition: thd.h:191
boolean snmp_notif
1 subscription allowed
Definition: thd.h:239
pthread_t handle
Internal thread handle.
Definition: thd.h:180
void thd_set_ready(void)
Netconfd will set thd_ready when initialization is complete, but before creating threads.
Definition: thd.c:531
int thd_find_thread_id(void)
Find the thread ID represented by the current thread.
Definition: thd.c:975
int sleep
sleep amount
Definition: thd.h:192
ses_id_t sid
Session ID of thread.
Definition: thd.h:185