public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] lib/tst_res.c: synchronize access to tst_res functions
@ 2014-02-18 13:53 Alexey Kodanev
  2014-02-25 13:19 ` chrubis
  2014-03-24 17:11 ` chrubis
  0 siblings, 2 replies; 4+ messages in thread
From: Alexey Kodanev @ 2014-02-18 13:53 UTC (permalink / raw)
  To: ltp-list; +Cc: vasily.isaenko, Alexey Kodanev

The patch adds synchronization mechanism which is required (tst_res.c
uses global variables e.g. T_exitval, tst_count, T_out, ...) when tst_res
functions are used in multi-threaded tests.
Note: it will only work if test is linked with pthread library, also some
functions include each other that is why recursive mutex is used (can be
locked several times by the same thread).

Signed-off-by: Alexey Kodanev <alexey.kodanev@oracle.com>
---
 lib/tst_res.c |   28 +++++++++++++++++++++++++---
 1 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/lib/tst_res.c b/lib/tst_res.c
index 2a3eff6..8faa5a9 100644
--- a/lib/tst_res.c
+++ b/lib/tst_res.c
@@ -93,6 +93,7 @@
 
 #define _GNU_SOURCE		/* for asprintf */
 
+#include <pthread.h>
 #include <assert.h>
 #include <errno.h>
 #include <stdio.h>
@@ -138,6 +139,8 @@ pid_t spawned_program_pid;
 	assert(strlen(buf) > 0);		\
 } while (0)
 
+static pthread_mutex_t tmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+
 /*
  * Define local function prototypes.
  */
@@ -226,6 +229,8 @@ const char *strttype(int ttype)
  */
 void tst_res(int ttype, const char *fname, const char *arg_fmt, ...)
 {
+	pthread_mutex_lock(&tmutex);
+
 	char tmesg[USERMESG];
 	int ttype_result = TTYPE_RESULT(ttype);
 
@@ -286,6 +291,7 @@ void tst_res(int ttype, const char *fname, const char *arg_fmt, ...)
 		tst_count++;
 	}
 
+	pthread_mutex_unlock(&tmutex);
 }
 
 /*
@@ -351,6 +357,8 @@ static void tst_condense(int tnum, int ttype, const char *tmesg)
  */
 void tst_flush(void)
 {
+	pthread_mutex_lock(&tmutex);
+
 #if DEBUG
 	printf("IN tst_flush\n");
 	fflush(stdout);
@@ -365,6 +373,8 @@ void tst_flush(void)
 	}
 
 	fflush(T_out);
+
+	pthread_mutex_unlock(&tmutex);
 }
 
 /*
@@ -537,6 +547,8 @@ static void check_env(void)
  */
 void tst_exit(void)
 {
+	pthread_mutex_lock(&tmutex);
+
 #if DEBUG
 	printf("IN tst_exit\n");
 	fflush(stdout);
@@ -568,10 +580,13 @@ pid_t tst_vfork(void)
  */
 int tst_environ(void)
 {
+	pthread_mutex_lock(&tmutex);
+	int ret = 0;
 	if ((T_out = fdopen(dup(fileno(stdout)), "w")) == NULL)
-		return -1;
-	else
-		return 0;
+		ret = -1;
+
+	pthread_mutex_unlock(&tmutex);
+	return ret;
 }
 
 /*
@@ -586,6 +601,8 @@ static int tst_brk_entered = 0;
  */
 void tst_brk(int ttype, const char *fname, void (*func) (void), const char *arg_fmt, ...)
 {
+	pthread_mutex_lock(&tmutex);
+
 	char tmesg[USERMESG];
 	int ttype_result = TTYPE_RESULT(ttype);
 
@@ -633,6 +650,7 @@ void tst_brk(int ttype, const char *fname, void (*func) (void), const char *arg_
 	if (tst_brk_entered == 0)
 		tst_exit();
 
+	pthread_mutex_unlock(&tmutex);
 }
 
 /*
@@ -659,6 +677,8 @@ void tst_resm(int ttype, const char *arg_fmt, ...)
  */
 void tst_resm_hexd(int ttype, const void *buf, size_t size, const char *arg_fmt, ...)
 {
+	pthread_mutex_lock(&tmutex);
+
 	char tmesg[USERMESG];
 
 #if DEBUG
@@ -692,6 +712,8 @@ void tst_resm_hexd(int ttype, const void *buf, size_t size, const char *arg_fmt,
 			pmesg = tmesg;
 		}
 	}
+
+	pthread_mutex_unlock(&tmutex);
 }
 
 /*
-- 
1.7.1


------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-03-24 17:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-02-18 13:53 [LTP] [PATCH] lib/tst_res.c: synchronize access to tst_res functions Alexey Kodanev
2014-02-25 13:19 ` chrubis
2014-03-11 15:11   ` chrubis
2014-03-24 17:11 ` chrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox