watch: move the stdout locking to the top level

This reduces the concurrency slightly, but it guarantees that all output for
a single packet is contiguous.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
1 files changed, 10 insertions(+), 22 deletions(-)

M watch/watch.c
M watch/watch.c +10 -22
@@ 125,9 125,7 @@ static bool print_ann_qso(const struct t
 
 	xuuid_unparse(&uuid, uuid_str);
 
-	LOCK();
 	print(now, addr, "QSO %s %s\n", action, uuid_str);
-	UNLOCK();
 
 	return true;
 }

          
@@ 180,7 178,6 @@ static bool print_ann_loc(const struct t
 			break;
 	}
 
-	LOCK();
 	print(now, addr, "LOC %s ", action);
 	switch (mode) {
 		case 0:

          
@@ 195,7 192,6 @@ static bool print_ann_loc(const struct t
 			break;
 	}
 	printf("\n");
-	UNLOCK();
 
 	return true;
 }

          
@@ 215,14 211,12 @@ static bool print_ann_rig(const struct t
 			return false;
 	}
 
-	LOCK();
 	print(now, addr, "RIG tx %.6f %s @ %.3f W, rx %.6f %s\n",
 	      msg->u.rig.tx_freq / 1e6,
 	      hamlib_mode_name_raw(msg->u.rig.tx_mode),
 	      msg->u.rig.power / 1000.,
 	      msg->u.rig.rx_freq / 1e6,
 	      hamlib_mode_name_raw(msg->u.rig.rx_mode));
-	UNLOCK();
 
 	return true;
 }

          
@@ 234,19 228,16 @@ static bool print_ann_info(const struct 
 
 	val = val_unpack(msg->u.info, sizeof(msg->u.info), VF_CBOR);
 
-	LOCK();
 	if (IS_ERR(val)) {
 		print(now, addr, "Failed to unpack info CBOR payload\n");
+		return false;
 	} else {
 		print(now, addr, "Info:\n");
 		val_dump_file(stdout, val, 1);
-	}
-	UNLOCK();
-
-	if (!IS_ERR(val))
 		val_putref(val);
 
-	return !IS_ERR(val);
+		return true;
+	}
 }
 
 static void handle_msg(const union xsockaddr *addr, size_t addrlen, void *data,

          
@@ 264,31 255,27 @@ static void handle_msg(const union xsock
 	now_ts = gettime();
 	time_set(&now, now_ts);
 
+	LOCK();
+
 	if (addrlen > sizeof(*addr)) {
-		LOCK();
 		print(&now, NULL, "Received from address that's too large for "
 		      "xsockaddr (%zuB > %zuB)\n", addrlen, sizeof(*addr));
-		UNLOCK();
 
 		/* continue processing but without an address */
 		addr = NULL;
 	}
 
 	if (len != sizeof(*msg)) {
-		LOCK();
 		print(&now, addr, "Received announcement has wrong size "
 		      "(%zuB != %zuB)\n", len, sizeof(*msg));
-		UNLOCK();
-		return;
+		goto unlock;
 	}
 
 	magic = be32_to_cpu_unaligned(&msg->magic);
 	if (magic != ANNOUNCE_CURRENT_VERSION) {
-		LOCK();
 		print(&now, addr, "Received announcement has wrong magic "
 		      "(%#x != %#x)\n", magic, ANNOUNCE_CURRENT_VERSION);
-		UNLOCK();
-		return;
+		goto unlock;
 	}
 
 	orig = *msg;

          
@@ 325,7 312,6 @@ static void handle_msg(const union xsock
 		ns = msg->ts % 1000000000ul;
 		time_set(&t, msg->ts);
 
-		LOCK();
 		if (!known)
 			print(&now, addr, "Unknown announcement message: "
 			      "major %u, minor %u, inst %#x\n",

          
@@ 338,8 324,10 @@ static void handle_msg(const union xsock
 		       msg->ts / 1000000000ul, msg->ts % 1000000000ul,
 		       now_ts - msg->ts);
 		hexdump_msg(&orig, sizeof(orig));
-		UNLOCK();
 	}
+
+unlock:
+	UNLOCK();
 }
 
 int main(int argc, char **argv)