diff options
author | Douglas Gilbert <dgilbert@interlog.com> | 2014-08-29 20:21:51 +0000 |
---|---|---|
committer | Douglas Gilbert <dgilbert@interlog.com> | 2014-08-29 20:21:51 +0000 |
commit | bd4b51e9452bf0d5c22630cf5e21fa71477be34b (patch) | |
tree | a80711e4aa1bc4cb1c9be7d731a7a7efb1504323 /examples | |
parent | 5c18750d086d6f01c7ec167a3cde6bfe1de0acea (diff) | |
download | sg3_utils-bd4b51e9452bf0d5c22630cf5e21fa71477be34b.tar.gz |
sg_raw: with -vvv decode T10 CDB name; sg_lib: clean up service action string functions
git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@603 6180dd3e-e324-4e3e-922d-17de1ae2f315
Diffstat (limited to 'examples')
-rw-r--r-- | examples/sg_tst_async.cpp | 53 | ||||
-rw-r--r-- | examples/sg_tst_context.cpp | 86 | ||||
-rw-r--r-- | examples/sg_tst_excl.cpp | 131 | ||||
-rw-r--r-- | examples/sg_tst_excl2.cpp | 105 | ||||
-rw-r--r-- | examples/sg_tst_excl3.cpp | 89 |
5 files changed, 272 insertions, 192 deletions
diff --git a/examples/sg_tst_async.cpp b/examples/sg_tst_async.cpp index afda6185..fa3c3769 100644 --- a/examples/sg_tst_async.cpp +++ b/examples/sg_tst_async.cpp @@ -57,7 +57,7 @@ #include "sg_io_linux.h" #include "sg_unaligned.h" -static const char * version_str = "1.06 20140824"; +static const char * version_str = "1.07 20140827"; static const char * util_name = "sg_tst_async"; /* This is a test program for checking the async usage of the Linux sg @@ -268,16 +268,12 @@ static int pr2serr_lk(const char * fmt, ...) { int n; + va_list args; + lock_guard<mutex> lg(console_mutex); - console_mutex.lock(); - { - va_list args; - - va_start(args, fmt); - n = vfprintf(stderr, fmt, args); - va_end(args); - } - console_mutex.unlock(); + va_start(args, fmt); + n = vfprintf(stderr, fmt, args); + va_end(args); return n; } @@ -285,17 +281,13 @@ static void pr_errno_lk(int e_no, const char * fmt, ...) { char b[160]; - - console_mutex.lock(); - { va_list args; + lock_guard<mutex> lg(console_mutex); - va_start(args, fmt); - vsnprintf(b, sizeof(b), fmt, args); - fprintf(stderr, "%s: %s\n", b, strerror(e_no)); - va_end(args); - } - console_mutex.unlock(); + va_start(args, fmt); + vsnprintf(b, sizeof(b), fmt, args); + fprintf(stderr, "%s: %s\n", b, strerror(e_no)); + va_end(args); } static unsigned int @@ -304,8 +296,8 @@ get_urandom_uint(void) unsigned int res = 0; int n; unsigned char b[sizeof(unsigned int)]; + lock_guard<mutex> lg(rand_lba_mutex); - rand_lba_mutex.lock(); int fd = open(URANDOM_DEV, O_RDONLY); if (fd >= 0) { n = read(fd, b, sizeof(unsigned int)); @@ -313,7 +305,6 @@ get_urandom_uint(void) memcpy(&res, b, sizeof(unsigned int)); close(fd); } - rand_lba_mutex.unlock(); return res; } @@ -439,9 +430,10 @@ finish_sg3_cmd(int sg_fd, command2execute cmd2exe, int & pack_id, int wait_ms, ok = 1; break; default: /* won't bother decoding other categories */ - console_mutex.lock(); - sg_chk_n_print3(np, &pt, 1); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + sg_chk_n_print3(np, &pt, 1); + } break; } return ok ? 0 : -1; @@ -782,9 +774,10 @@ do_inquiry_prod_id(const char * dev_name, int block, char * b, int b_mlen) ok = 1; break; default: /* won't bother decoding other categories */ - console_mutex.lock(); - sg_chk_n_print3("INQUIRY command error", &pt, 1); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + sg_chk_n_print3("INQUIRY command error", &pt, 1); + } break; } if (ok) { @@ -844,15 +837,13 @@ do_read_capacity(const char * dev_name, int block, unsigned int * last_lba, } res = sg_err_category3(&io_hdr); if (SG_LIB_CAT_UNIT_ATTENTION == res) { - console_mutex.lock(); + lock_guard<mutex> lg(console_mutex); sg_chk_n_print3("read capacity", &io_hdr, 1); - console_mutex.unlock(); close(sg_fd); return 2; /* probably have another go ... */ } else if (SG_LIB_CAT_CLEAN != res) { - console_mutex.lock(); + lock_guard<mutex> lg(console_mutex); sg_chk_n_print3("read capacity", &io_hdr, 1); - console_mutex.unlock(); close(sg_fd); return -1; } diff --git a/examples/sg_tst_context.cpp b/examples/sg_tst_context.cpp index 86612bac..6ac99354 100644 --- a/examples/sg_tst_context.cpp +++ b/examples/sg_tst_context.cpp @@ -46,7 +46,7 @@ #include "sg_lib.h" #include "sg_pt.h" -static const char * version_str = "1.01 20131119"; +static const char * version_str = "1.02 20140828"; static const char * util_name = "sg_tst_context"; /* This is a test program for checking that file handles keep their @@ -165,9 +165,9 @@ pt_cat_no_good(int cat, struct sg_pt_base * ptp, const unsigned char * sbp) break; } if (cp) { - console_mutex.lock(); + lock_guard<mutex> lg(console_mutex); + fprintf(stderr, cp, b); - console_mutex.unlock(); } return -EIO /* -5 */; } @@ -191,11 +191,13 @@ do_tur(int pt_fd, int id) set_scsi_pt_sense(ptp, sense_buffer, sizeof(sense_buffer)); res = do_scsi_pt(ptp, pt_fd, 20 /* secs timeout */, 1); if (res) { - console_mutex.lock(); - fprintf(stderr, "TEST UNIT READY do_scsi_pt() submission error, " - "id=%d\n", id); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "TEST UNIT READY do_scsi_pt() submission error, " + "id=%d\n", id); + } res = pt_err(res); - console_mutex.unlock(); goto err; } cat = get_scsi_pt_result_category(ptp); @@ -206,10 +208,12 @@ do_tur(int pt_fd, int id) res = 1024; goto err; } - console_mutex.lock(); - fprintf(stderr, "TEST UNIT READY do_scsi_pt() category problem, " - "id=%d\n", id); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "TEST UNIT READY do_scsi_pt() category problem, " + "id=%d\n", id); + } res = pt_cat_no_good(cat, ptp, sense_buffer); goto err; } @@ -237,11 +241,13 @@ do_ssu(int pt_fd, int id, bool start) set_scsi_pt_sense(ptp, sense_buffer, sizeof(sense_buffer)); res = do_scsi_pt(ptp, pt_fd, 40 /* secs timeout */, 1); if (res) { - console_mutex.lock(); - fprintf(stderr, "START STOP UNIT do_scsi_pt() submission error, " - "id=%d\n", id); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "START STOP UNIT do_scsi_pt() submission error, " + "id=%d\n", id); + } res = pt_err(res); - console_mutex.unlock(); goto err; } cat = get_scsi_pt_result_category(ptp); @@ -252,10 +258,12 @@ do_ssu(int pt_fd, int id, bool start) res = 1024; goto err; } - console_mutex.lock(); - fprintf(stderr, "START STOP UNIT do_scsi_pt() category problem, " - "id=%d\n", id); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "START STOP UNIT do_scsi_pt() category problem, " + "id=%d\n", id); + } res = pt_cat_no_good(cat, ptp, sense_buffer); goto err; } @@ -278,10 +286,12 @@ work_thread(const char * dev_name, int id, int num, bool share, bool started = true; char ebuff[EBUFF_SZ]; - console_mutex.lock(); - cerr << "Enter work_thread id=" << id << " num=" << num << " share=" - << share << endl; - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + cerr << "Enter work_thread id=" << id << " num=" << num << " share=" + << share << endl; + } if (! share) { int open_flags = O_RDWR; @@ -301,9 +311,9 @@ work_thread(const char * dev_name, int id, int num, bool share, return; } if (thr_ebusy_count) { - count_mutex.lock(); + lock_guard<mutex> lg(count_mutex); + ebusy_count += thr_ebusy_count; - count_mutex.unlock(); } } for (k = 0; k < num; ++k) { @@ -329,18 +339,22 @@ work_thread(const char * dev_name, int id, int num, bool share, if (! share) scsi_pt_close_device(pt_fd); - count_mutex.lock(); - even_notreadys += thr_even_notreadys; - odd_notreadys += thr_odd_notreadys; - count_mutex.unlock(); + { + lock_guard<mutex> lg(count_mutex); - console_mutex.lock(); - if (k < num) - cerr << "thread id=" << id << " FAILed at iteration: " << k - << " [negated errno: " << res << "]\n"; - else - cerr << "thread id=" << id << " normal exit" << '\n'; - console_mutex.unlock(); + even_notreadys += thr_even_notreadys; + odd_notreadys += thr_odd_notreadys; + } + + { + lock_guard<mutex> lg(console_mutex); + + if (k < num) + cerr << "thread id=" << id << " FAILed at iteration: " << k + << " [negated errno: " << res << "]\n"; + else + cerr << "thread id=" << id << " normal exit" << '\n'; + } } diff --git a/examples/sg_tst_excl.cpp b/examples/sg_tst_excl.cpp index a4a85121..5b4e6956 100644 --- a/examples/sg_tst_excl.cpp +++ b/examples/sg_tst_excl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Douglas Gilbert. + * Copyright (c) 2013-2014 Douglas Gilbert. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -46,7 +46,7 @@ #include "sg_lib.h" #include "sg_io_linux.h" -static const char * version_str = "1.08 20140710"; +static const char * version_str = "1.09 20140828"; static const char * util_name = "sg_tst_excl"; /* This is a test program for checking O_EXCL on open() works. It uses @@ -212,17 +212,21 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, // queue up two READ_16s to same LBA if (write(sg_fd, &pt, sizeof(pt)) < 0) { - console_mutex.lock(); - perror("do_rd_inc_wr_twice: write(sg, READ_16)"); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + perror("do_rd_inc_wr_twice: write(sg, READ_16)"); + } close(sg_fd); return -1; } pt2 = pt; if (write(sg_fd, &pt2, sizeof(pt2)) < 0) { - console_mutex.lock(); - perror("do_rd_inc_wr_twice: write(sg, READ_16) 2"); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + perror("do_rd_inc_wr_twice: write(sg, READ_16) 2"); + } close(sg_fd); return -1; } @@ -238,9 +242,11 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, sleep(0); // process yield ?? } if (res < 0) { - console_mutex.lock(); - perror("do_rd_inc_wr_twice: read(sg, READ_16)"); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + perror("do_rd_inc_wr_twice: read(sg, READ_16)"); + } close(sg_fd); return -1; } @@ -251,15 +257,19 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, ok = 1; break; case SG_LIB_CAT_RECOVERED: - console_mutex.lock(); - fprintf(stderr, "Recovered error on READ_16, continuing\n"); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "Recovered error on READ_16, continuing\n"); + } ok = 1; break; default: /* won't bother decoding other categories */ - console_mutex.lock(); - sg_chk_n_print3("READ_16 command error", &pt, 1); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + sg_chk_n_print3("READ_16 command error", &pt, 1); + } break; } if (ok) { @@ -274,9 +284,11 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, sleep(0); // process yield ?? } if (res < 0) { - console_mutex.lock(); - perror("do_rd_inc_wr_twice: read(sg, READ_16) 2"); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + perror("do_rd_inc_wr_twice: read(sg, READ_16) 2"); + } close(sg_fd); return -1; } @@ -288,15 +300,20 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, ok = 1; break; case SG_LIB_CAT_RECOVERED: - console_mutex.lock(); - fprintf(stderr, "Recovered error on READ_16, continuing 2\n"); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "Recovered error on READ_16, continuing " + "2\n"); + } ok = 1; break; default: /* won't bother decoding other categories */ - console_mutex.lock(); - sg_chk_n_print3("READ_16 command error 2", &pt, 1); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + sg_chk_n_print3("READ_16 command error 2", &pt, 1); + } break; } } @@ -335,9 +352,11 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, pt.pack_id = id; if (ioctl(sg_fd, SG_IO, &pt) < 0) { - console_mutex.lock(); - perror("do_rd_inc_wr_twice: WRITE_16 SG_IO ioctl error"); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + perror("do_rd_inc_wr_twice: WRITE_16 SG_IO ioctl error"); + } close(sg_fd); return -1; } @@ -348,15 +367,19 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, ok = 1; break; case SG_LIB_CAT_RECOVERED: - console_mutex.lock(); - fprintf(stderr, "Recovered error on WRITE_16, continuing\n"); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "Recovered error on WRITE_16, continuing\n"); + } ok = 1; break; default: /* won't bother decoding other categories */ - console_mutex.lock(); - sg_chk_n_print3("WRITE_16 command error", &pt, 1); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + sg_chk_n_print3("WRITE_16 command error", &pt, 1); + } break; } if (! ok) { @@ -470,10 +493,12 @@ work_thread(const char * dev_name, unsigned int lba, int id, int block, unsigned int thr_eagain_count = 0; int k, res; - console_mutex.lock(); - cerr << "Enter work_thread id=" << id << " excl=" << excl << " block=" - << block << endl; - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + cerr << "Enter work_thread id=" << id << " excl=" << excl << " block=" + << block << endl; + } for (k = 0; k < num; ++k) { res = do_rd_inc_wr_twice(dev_name, lba, block, excl, wait_ms, k, thr_ebusy_count, thr_eagain_count); @@ -482,18 +507,22 @@ work_thread(const char * dev_name, unsigned int lba, int id, int block, if (res) ++thr_odd_count; } - console_mutex.lock(); - if (k < num) - cerr << "thread id=" << id << " FAILed at iteration: " << k << '\n'; - else - cerr << "thread id=" << id << " normal exit" << '\n'; - console_mutex.unlock(); - - odd_count_mutex.lock(); - odd_count += thr_odd_count; - ebusy_count += thr_ebusy_count; - eagain_count += thr_eagain_count; - odd_count_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + if (k < num) + cerr << "thread id=" << id << " FAILed at iteration: " << k << + '\n'; + else + cerr << "thread id=" << id << " normal exit" << '\n'; + } + { + lock_guard<mutex> lg(odd_count_mutex); + + odd_count += thr_odd_count; + ebusy_count += thr_ebusy_count; + eagain_count += thr_eagain_count; + } } diff --git a/examples/sg_tst_excl2.cpp b/examples/sg_tst_excl2.cpp index cec1c9e3..24d3677b 100644 --- a/examples/sg_tst_excl2.cpp +++ b/examples/sg_tst_excl2.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Douglas Gilbert. + * Copyright (c) 2013-2014 Douglas Gilbert. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -46,7 +46,7 @@ #include "sg_lib.h" #include "sg_pt.h" -static const char * version_str = "1.06 20131110"; +static const char * version_str = "1.07 20140828"; static const char * util_name = "sg_tst_excl2"; /* This is a test program for checking O_EXCL on open() works. It uses @@ -134,6 +134,7 @@ usage(void) "on lba (using its first 4 bytes).\n"); } +/* Assumed a lock (mutex) held when pt_err() is called */ static int pt_err(int res) { @@ -148,6 +149,7 @@ pt_err(int res) return -1; } +/* Assumed a lock (mutex) held when pt_cat_no_good() is called */ static int pt_cat_no_good(int cat, struct sg_pt_base * ptp, const unsigned char * sbp) { @@ -229,9 +231,11 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, if (sg_fd < 0) { snprintf(ebuff, EBUFF_SZ, "do_rd_inc_wr_twice: error opening file: %s", dev_name); - console_mutex.lock(); - perror(ebuff); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + perror(ebuff); + } return -1; } @@ -244,18 +248,22 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, set_scsi_pt_data_in(ptp, lb, READ16_REPLY_LEN); res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1); if (res) { - console_mutex.lock(); - fprintf(stderr, "READ_16 do_scsi_pt() submission error\n"); - res = pt_err(res); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "READ_16 do_scsi_pt() submission error\n"); + res = pt_err(res); + } goto err; } cat = get_scsi_pt_result_category(ptp); if (SCSI_PT_RESULT_GOOD != cat) { - console_mutex.lock(); - fprintf(stderr, "READ_16 do_scsi_pt() category problem\n"); - console_mutex.unlock(); - res = pt_cat_no_good(cat, ptp, sense_buffer); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "READ_16 do_scsi_pt() category problem\n"); + res = pt_cat_no_good(cat, ptp, sense_buffer); + } goto err; } @@ -283,18 +291,22 @@ do_rd_inc_wr_twice(const char * dev_name, unsigned int lba, int block, set_scsi_pt_data_out(ptp, lb, WRITE16_REPLY_LEN); res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1); if (res) { - console_mutex.lock(); - fprintf(stderr, "WRITE_16 do_scsi_pt() submission error\n"); - res = pt_err(res); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "WRITE_16 do_scsi_pt() submission error\n"); + res = pt_err(res); + } goto err; } cat = get_scsi_pt_result_category(ptp); if (SCSI_PT_RESULT_GOOD != cat) { - console_mutex.lock(); - fprintf(stderr, "WRITE_16 do_scsi_pt() category problem\n"); - console_mutex.unlock(); - res = pt_cat_no_good(cat, ptp, sense_buffer); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "WRITE_16 do_scsi_pt() category problem\n"); + res = pt_cat_no_good(cat, ptp, sense_buffer); + } goto err; } } @@ -352,14 +364,22 @@ do_inquiry_prod_id(const char * dev_name, int block, int wait_ms, set_scsi_pt_data_in(ptp, inqBuff, INQ_REPLY_LEN); res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1); if (res) { - fprintf(stderr, "INQUIRY do_scsi_pt() submission error\n"); - res = pt_err(res); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "INQUIRY do_scsi_pt() submission error\n"); + res = pt_err(res); + } goto err; } cat = get_scsi_pt_result_category(ptp); if (SCSI_PT_RESULT_GOOD != cat) { - fprintf(stderr, "INQUIRY do_scsi_pt() category problem\n"); - res = pt_cat_no_good(cat, ptp, sense_buffer); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "INQUIRY do_scsi_pt() category problem\n"); + res = pt_cat_no_good(cat, ptp, sense_buffer); + } goto err; } @@ -388,10 +408,12 @@ work_thread(const char * dev_name, unsigned int lba, int id, int block, unsigned int thr_ebusy_count = 0; int k, res; - console_mutex.lock(); - cerr << "Enter work_thread id=" << id << " excl=" << excl << " block=" - << block << endl; - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + cerr << "Enter work_thread id=" << id << " excl=" << excl << " block=" + << block << endl; + } for (k = 0; k < num; ++k) { res = do_rd_inc_wr_twice(dev_name, lba, block, excl, wait_ms, thr_ebusy_count); @@ -400,17 +422,22 @@ work_thread(const char * dev_name, unsigned int lba, int id, int block, if (res) ++thr_odd_count; } - console_mutex.lock(); - if (k < num) - cerr << "thread id=" << id << " FAILed at iteration: " << k << '\n'; - else - cerr << "thread id=" << id << " normal exit" << '\n'; - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + if (k < num) + cerr << "thread id=" << id << " FAILed at iteration: " << k << + '\n'; + else + cerr << "thread id=" << id << " normal exit" << '\n'; + } + + { + lock_guard<mutex> lg(odd_count_mutex); - odd_count_mutex.lock(); - odd_count += thr_odd_count; - ebusy_count += thr_ebusy_count; - odd_count_mutex.unlock(); + odd_count += thr_odd_count; + ebusy_count += thr_ebusy_count; + } } diff --git a/examples/sg_tst_excl3.cpp b/examples/sg_tst_excl3.cpp index c4052a90..6b6b880b 100644 --- a/examples/sg_tst_excl3.cpp +++ b/examples/sg_tst_excl3.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Douglas Gilbert. + * Copyright (c) 2013-2014 Douglas Gilbert. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -46,7 +46,7 @@ #include "sg_lib.h" #include "sg_pt.h" -static const char * version_str = "1.04 20131110"; +static const char * version_str = "1.05 20140828"; static const char * util_name = "sg_tst_excl3"; /* This is a test program for checking O_EXCL on open() works. It uses @@ -140,6 +140,7 @@ usage(void) "value is even.\n"); } +/* Assumed a lock (mutex) held when pt_err() is called */ static int pt_err(int res) { @@ -154,6 +155,7 @@ pt_err(int res) return -1; } +/* Assumed a lock (mutex) held when pt_cat_no_good() is called */ static int pt_cat_no_good(int cat, struct sg_pt_base * ptp, const unsigned char * sbp) { @@ -235,9 +237,11 @@ do_rd_inc_wr_twice(const char * dev_name, int read_only, unsigned int lba, if (sg_fd < 0) { snprintf(ebuff, EBUFF_SZ, "do_rd_inc_wr_twice: error opening file: %s", dev_name); - console_mutex.lock(); - perror(ebuff); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + perror(ebuff); + } return -1; } @@ -250,18 +254,22 @@ do_rd_inc_wr_twice(const char * dev_name, int read_only, unsigned int lba, set_scsi_pt_data_in(ptp, lb, READ16_REPLY_LEN); res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1); if (res) { - console_mutex.lock(); - fprintf(stderr, "READ_16 do_scsi_pt() submission error\n"); - res = pt_err(res); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "READ_16 do_scsi_pt() submission error\n"); + res = pt_err(res); + } goto err; } cat = get_scsi_pt_result_category(ptp); if (SCSI_PT_RESULT_GOOD != cat) { - console_mutex.lock(); - fprintf(stderr, "READ_16 do_scsi_pt() category problem\n"); - console_mutex.unlock(); - res = pt_cat_no_good(cat, ptp, sense_buffer); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "READ_16 do_scsi_pt() category problem\n"); + res = pt_cat_no_good(cat, ptp, sense_buffer); + } goto err; } @@ -292,18 +300,22 @@ do_rd_inc_wr_twice(const char * dev_name, int read_only, unsigned int lba, set_scsi_pt_data_out(ptp, lb, WRITE16_REPLY_LEN); res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1); if (res) { - console_mutex.lock(); - fprintf(stderr, "WRITE_16 do_scsi_pt() submission error\n"); - res = pt_err(res); - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "WRITE_16 do_scsi_pt() submission error\n"); + res = pt_err(res); + } goto err; } cat = get_scsi_pt_result_category(ptp); if (SCSI_PT_RESULT_GOOD != cat) { - console_mutex.lock(); - fprintf(stderr, "WRITE_16 do_scsi_pt() category problem\n"); - console_mutex.unlock(); - res = pt_cat_no_good(cat, ptp, sense_buffer); + { + lock_guard<mutex> lg(console_mutex); + + fprintf(stderr, "WRITE_16 do_scsi_pt() category problem\n"); + res = pt_cat_no_good(cat, ptp, sense_buffer); + } goto err; } } @@ -397,10 +409,12 @@ work_thread(const char * dev_name, unsigned int lba, int id, int block, int k, res; int reader = ((id > 0) || (all_readers)); - console_mutex.lock(); - cerr << "Enter work_thread id=" << id << " excl=" << excl << " block=" - << block << " reader=" << reader << endl; - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + cerr << "Enter work_thread id=" << id << " excl=" << excl << " block=" + << block << " reader=" << reader << endl; + } for (k = 0; k < num; ++k) { res = do_rd_inc_wr_twice(dev_name, reader, lba, block, excl, wait_ms, thr_ebusy_count); @@ -409,17 +423,22 @@ work_thread(const char * dev_name, unsigned int lba, int id, int block, if (res) ++thr_odd_count; } - console_mutex.lock(); - if (k < num) - cerr << "thread id=" << id << " FAILed at iteration: " << k << '\n'; - else - cerr << "thread id=" << id << " normal exit" << '\n'; - console_mutex.unlock(); + { + lock_guard<mutex> lg(console_mutex); + + if (k < num) + cerr << "thread id=" << id << " FAILed at iteration: " << k + << '\n'; + else + cerr << "thread id=" << id << " normal exit" << '\n'; + } - odd_count_mutex.lock(); - odd_count += thr_odd_count; - ebusy_count += thr_ebusy_count; - odd_count_mutex.unlock(); + { + lock_guard<mutex> lg(odd_count_mutex); + + odd_count += thr_odd_count; + ebusy_count += thr_ebusy_count; + } } |