From ffa2c8986d204f4a3e7204258fd6906af4a57c93 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Mon, 24 Apr 2017 22:33:25 +0000 Subject: *: remove THREAD_ON macros, add nullity check The way thread.c is written, a caller who wishes to be able to cancel a thread or avoid scheduling it twice must keep a reference to the thread. Typically this is done with a long lived pointer whose value is checked for null in order to know if the thread is currently scheduled. The check-and-schedule idiom is so common that several wrapper macros in thread.h existed solely to provide it. This patch removes those macros and adds a new parameter to all thread_add_* functions which is a pointer to the struct thread * to store the result of a scheduling call. If the value passed is non-null, the thread will only be scheduled if the value is null. This helps with consistency. A Coccinelle spatch has been used to transform code of the form: if (t == NULL) t = thread_add_* (...) to the form thread_add_* (..., &t) The THREAD_ON macros have also been transformed to the underlying thread.c calls. Signed-off-by: Quentin Young --- ospf6d/ospf6_message.c | 102 +++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 58 deletions(-) (limited to 'ospf6d/ospf6_message.c') diff --git a/ospf6d/ospf6_message.c b/ospf6d/ospf6_message.c index 578b39a64..7ad3736d2 100644 --- a/ospf6d/ospf6_message.c +++ b/ospf6d/ospf6_message.c @@ -352,9 +352,9 @@ ospf6_hello_recv (struct in6_addr *src, struct in6_addr *dst, /* Schedule interface events */ if (backupseen) - thread_add_event (master, backup_seen, oi, 0); + thread_add_event (master, backup_seen, oi, 0, NULL); if (neighborchange) - thread_add_event (master, neighbor_change, oi, 0); + thread_add_event (master, neighbor_change, oi, 0, NULL); if (neighbor_ifindex_change && on->state == OSPF6_NEIGHBOR_FULL) OSPF6_ROUTER_LSA_SCHEDULE (oi->area); @@ -428,7 +428,7 @@ ospf6_dbdesc_recv_master (struct ospf6_header *oh, { if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Master/Slave bit mismatch"); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } @@ -436,7 +436,7 @@ ospf6_dbdesc_recv_master (struct ospf6_header *oh, { if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Initialize bit mismatch"); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } @@ -444,7 +444,7 @@ ospf6_dbdesc_recv_master (struct ospf6_header *oh, { if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Option field mismatch"); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } @@ -453,7 +453,7 @@ ospf6_dbdesc_recv_master (struct ospf6_header *oh, if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Sequence number mismatch (%#lx expected)", (u_long) on->dbdesc_seqnum); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } break; @@ -471,7 +471,7 @@ ospf6_dbdesc_recv_master (struct ospf6_header *oh, if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Not duplicate dbdesc in state %s", ospf6_neighbor_state_str[on->state]); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; default: @@ -517,7 +517,7 @@ ospf6_dbdesc_recv_master (struct ospf6_header *oh, if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("SeqNumMismatch (E-bit mismatch), discard"); ospf6_lsa_delete (his); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } @@ -549,19 +549,18 @@ ospf6_dbdesc_recv_master (struct ospf6_header *oh, on->dbdesc_seqnum ++; /* schedule send lsreq */ - if (on->request_list->count && (on->thread_send_lsreq == NULL)) - on->thread_send_lsreq = - thread_add_event (master, ospf6_lsreq_send, on, 0); + if (on->request_list->count) + thread_add_event (master, ospf6_lsreq_send, on, 0, &on->thread_send_lsreq); THREAD_OFF (on->thread_send_dbdesc); /* More bit check */ if (! CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MBIT) && ! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT)) - thread_add_event (master, exchange_done, on, 0); + thread_add_event (master, exchange_done, on, 0, NULL); else on->thread_send_dbdesc = - thread_add_event (master, ospf6_dbdesc_send_newone, on, 0); + thread_add_event (master, ospf6_dbdesc_send_newone, on, 0, NULL); /* save last received dbdesc */ memcpy (&on->dbdesc_last, dbdesc, sizeof (struct ospf6_dbdesc)); @@ -638,7 +637,7 @@ ospf6_dbdesc_recv_slave (struct ospf6_header *oh, zlog_debug ("Duplicated dbdesc causes retransmit"); THREAD_OFF (on->thread_send_dbdesc); on->thread_send_dbdesc = - thread_add_event (master, ospf6_dbdesc_send, on, 0); + thread_add_event (master, ospf6_dbdesc_send, on, 0, NULL); return; } @@ -646,7 +645,7 @@ ospf6_dbdesc_recv_slave (struct ospf6_header *oh, { if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Master/Slave bit mismatch"); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } @@ -654,7 +653,7 @@ ospf6_dbdesc_recv_slave (struct ospf6_header *oh, { if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Initialize bit mismatch"); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } @@ -662,7 +661,7 @@ ospf6_dbdesc_recv_slave (struct ospf6_header *oh, { if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Option field mismatch"); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } @@ -671,7 +670,7 @@ ospf6_dbdesc_recv_slave (struct ospf6_header *oh, if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Sequence number mismatch (%#lx expected)", (u_long) on->dbdesc_seqnum + 1); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } break; @@ -684,15 +683,14 @@ ospf6_dbdesc_recv_slave (struct ospf6_header *oh, if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Duplicated dbdesc causes retransmit"); THREAD_OFF (on->thread_send_dbdesc); - on->thread_send_dbdesc = - thread_add_event (master, ospf6_dbdesc_send, on, 0); + thread_add_event (master, ospf6_dbdesc_send, on, 0, &on->thread_send_dbdesc); return; } if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("Not duplicate dbdesc in state %s", ospf6_neighbor_state_str[on->state]); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; default: @@ -735,7 +733,7 @@ ospf6_dbdesc_recv_slave (struct ospf6_header *oh, if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV)) zlog_debug ("E-bit mismatch with LSA Headers"); ospf6_lsa_delete (his); - thread_add_event (master, seqnumber_mismatch, on, 0); + thread_add_event (master, seqnumber_mismatch, on, 0, NULL); return; } @@ -756,14 +754,11 @@ ospf6_dbdesc_recv_slave (struct ospf6_header *oh, on->dbdesc_seqnum = ntohl (dbdesc->seqnum); /* schedule send lsreq */ - if ((on->thread_send_lsreq == NULL) && - (on->request_list->count)) - on->thread_send_lsreq = - thread_add_event (master, ospf6_lsreq_send, on, 0); + if (on->request_list->count) + thread_add_event (master, ospf6_lsreq_send, on, 0, &on->thread_send_lsreq); THREAD_OFF (on->thread_send_dbdesc); - on->thread_send_dbdesc = - thread_add_event (master, ospf6_dbdesc_send_newone, on, 0); + thread_add_event (master, ospf6_dbdesc_send_newone, on, 0, &on->thread_send_dbdesc); /* save last received dbdesc */ memcpy (&on->dbdesc_last, dbdesc, sizeof (struct ospf6_dbdesc)); @@ -880,7 +875,7 @@ ospf6_lsreq_recv (struct in6_addr *src, struct in6_addr *dst, zlog_debug ("Can't find requested [%s Id:%s Adv:%s]", ospf6_lstype_name (e->type), id, adv_router); } - thread_add_event (master, bad_lsreq, on, 0); + thread_add_event (master, bad_lsreq, on, 0, NULL); return; } @@ -891,8 +886,7 @@ ospf6_lsreq_recv (struct in6_addr *src, struct in6_addr *dst, /* schedule send lsupdate */ THREAD_OFF (on->thread_send_lsupdate); - on->thread_send_lsupdate = - thread_add_event (master, ospf6_lsupdate_send_neighbor, on, 0); + thread_add_event (master, ospf6_lsupdate_send_neighbor, on, 0, &on->thread_send_lsupdate); } /* Verify, that the specified memory area contains exactly N valid IPv6 @@ -1532,7 +1526,7 @@ ospf6_receive (struct thread *thread) /* add next read thread */ sockfd = THREAD_FD (thread); - thread_add_read (master, ospf6_receive, NULL, sockfd); + thread_add_read (master, ospf6_receive, NULL, sockfd, NULL); /* initialize */ memset (&src, 0, sizeof (src)); @@ -1739,8 +1733,7 @@ ospf6_hello_send (struct thread *thread) } /* set next thread */ - oi->thread_send_hello = thread_add_timer (master, ospf6_hello_send, - oi, oi->hello_interval); + thread_add_timer (master, ospf6_hello_send, oi, oi->hello_interval, &oi->thread_send_hello); memset (sendbuf, 0, iobuflen); oh = (struct ospf6_header *) sendbuf; @@ -1804,9 +1797,9 @@ ospf6_dbdesc_send (struct thread *thread) /* set next thread if master */ if (CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT)) - on->thread_send_dbdesc = - thread_add_timer (master, ospf6_dbdesc_send, on, - on->ospf6_if->rxmt_interval); + thread_add_timer (master, ospf6_dbdesc_send, on, + on->ospf6_if->rxmt_interval, + &on->thread_send_dbdesc); memset (sendbuf, 0, iobuflen); oh = (struct ospf6_header *) sendbuf; @@ -1896,7 +1889,7 @@ ospf6_dbdesc_send_newone (struct thread *thread) if (! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT) && /* Slave */ ! CHECK_FLAG (on->dbdesc_last.bits, OSPF6_DBDESC_MBIT) && ! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT)) - thread_add_event (master, exchange_done, on, 0); + thread_add_event (master, exchange_done, on, 0, NULL); thread_execute (master, ospf6_dbdesc_send, on, 0); return 0; @@ -1927,7 +1920,7 @@ ospf6_lsreq_send (struct thread *thread) /* schedule loading_done if request list is empty */ if (on->request_list->count == 0) { - thread_add_event (master, loading_done, on, 0); + thread_add_event (master, loading_done, on, 0, NULL); return 0; } @@ -1979,8 +1972,8 @@ ospf6_lsreq_send (struct thread *thread) if (on->request_list->count != 0) { on->thread_send_lsreq = - thread_add_timer (master, ospf6_lsreq_send, on, - on->ospf6_if->rxmt_interval); + thread_add_timer (master, ospf6_lsreq_send, on, + on->ospf6_if->rxmt_interval, NULL); } return 0; @@ -2099,11 +2092,11 @@ ospf6_lsupdate_send_neighbor (struct thread *thread) if (on->lsupdate_list->count != 0) on->thread_send_lsupdate = - thread_add_event (master, ospf6_lsupdate_send_neighbor, on, 0); + thread_add_event (master, ospf6_lsupdate_send_neighbor, on, 0, NULL); else if (on->retrans_list->count != 0) on->thread_send_lsupdate = thread_add_timer (master, ospf6_lsupdate_send_neighbor, on, - on->ospf6_if->rxmt_interval); + on->ospf6_if->rxmt_interval, NULL); return 0; } @@ -2179,7 +2172,7 @@ ospf6_lsupdate_send_interface (struct thread *thread) if (oi->lsupdate_list->count > 0) { oi->thread_send_lsupdate = - thread_add_event (master, ospf6_lsupdate_send_interface, oi, 0); + thread_add_event (master, ospf6_lsupdate_send_interface, oi, 0, NULL); } return 0; @@ -2223,8 +2216,7 @@ ospf6_lsack_send_neighbor (struct thread *thread) /* if we run out of packet size/space here, better to try again soon. */ THREAD_OFF (on->thread_send_lsack); - on->thread_send_lsack = - thread_add_event (master, ospf6_lsack_send_neighbor, on, 0); + thread_add_event (master, ospf6_lsack_send_neighbor, on, 0, &on->thread_send_lsack); ospf6_lsdb_lsa_unlock (lsa); break; @@ -2248,11 +2240,8 @@ ospf6_lsack_send_neighbor (struct thread *thread) on->ospf6_if, oh); } - if (on->thread_send_lsack == NULL && on->lsack_list->count > 0) - { - on->thread_send_lsack = - thread_add_event (master, ospf6_lsack_send_neighbor, on, 0); - } + if (on->lsack_list->count > 0) + thread_add_event (master, ospf6_lsack_send_neighbor, on, 0, &on->thread_send_lsack); return 0; } @@ -2295,8 +2284,8 @@ ospf6_lsack_send_interface (struct thread *thread) /* if we run out of packet size/space here, better to try again soon. */ THREAD_OFF (oi->thread_send_lsack); - oi->thread_send_lsack = - thread_add_event (master, ospf6_lsack_send_interface, oi, 0); + thread_add_event (master, ospf6_lsack_send_interface, oi, 0, + &oi->thread_send_lsack); ospf6_lsdb_lsa_unlock (lsa); break; @@ -2324,11 +2313,8 @@ ospf6_lsack_send_interface (struct thread *thread) ospf6_send (oi->linklocal_addr, &alldrouters6, oi, oh); } - if (oi->thread_send_lsack == NULL && oi->lsack_list->count > 0) - { - oi->thread_send_lsack = - thread_add_event (master, ospf6_lsack_send_interface, oi, 0); - } + if (oi->lsack_list->count > 0) + thread_add_event (master, ospf6_lsack_send_interface, oi, 0, &oi->thread_send_lsack); return 0; } -- cgit v1.2.3