• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *   Copyright © International Business Machines  Corp., 2006, 2008
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * NAME
20  *    sbrk-mutex.c
21  *
22  * DESCRIPTION
23  *    Create NUM_THREADS to walk through an array of malloc'd pthread mutexes.
24  *    Each thread holds up to NUM_CONCURRENT locks at a time.
25  *
26  * USAGE:
27  *    Use run_auto.sh script in current directory to build and run test.
28  *
29  * AUTHOR
30  *    Darren Hart <dvhltc@us.ibm.com>
31  *
32  * HISTORY
33  *    2006-02-28: Initial version by Darren Hart
34  *    2006-03-01: Changed mutexes to PTHREAD_MUTEX_ROBUST_NP type -Sripathi Kodi
35  *
36  *****************************************************************************/
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <signal.h>
41 #include <time.h>
42 #include <pthread.h>
43 #include <sched.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include "librttest.h"
47 
48 #define NUM_MUTEXES 5000
49 #define NUM_THREADS 50
50 #define NUM_CONCURRENT_LOCKS 50
51 #define DELAY 1000		/* how long to sleep in the worker thread in us */
52 
53 static pthread_mutex_t *mutexes[NUM_MUTEXES];
54 
usage(void)55 void usage(void)
56 {
57 	rt_help();
58 	printf("sbrk_mutex specific options:\n");
59 }
60 
parse_args(int c,char * v)61 int parse_args(int c, char *v)
62 {
63 
64 	int handled = 1;
65 	switch (c) {
66 	case 'h':
67 		usage();
68 		exit(0);
69 	default:
70 		handled = 0;
71 		break;
72 	}
73 	return handled;
74 }
75 
worker_thread(void * arg)76 void *worker_thread(void *arg)
77 {
78 	int i;
79 
80 	for (i = 0; i < NUM_MUTEXES + NUM_CONCURRENT_LOCKS; i++) {
81 		/* release prior lock */
82 		if (i >= NUM_CONCURRENT_LOCKS) {
83 			pthread_mutex_unlock(mutexes[i - NUM_CONCURRENT_LOCKS]);
84 		}
85 		/* grab a new lock */
86 		if (i < NUM_MUTEXES) {
87 			pthread_mutex_lock(mutexes[i]);
88 		}
89 
90 		usleep(DELAY);
91 
92 		if (_dbg_lvl)
93 			printf("thread %ld @ %d\n", (long)arg, i);
94 	}
95 	return NULL;
96 }
97 
main(int argc,char * argv[])98 int main(int argc, char *argv[])
99 {
100 	int m, ret, robust;
101 	intptr_t t;
102 	pthread_mutexattr_t mutexattr;
103 	setup();
104 
105 	rt_init("h", parse_args, argc, argv);
106 
107 	if (pthread_mutexattr_init(&mutexattr) != 0) {
108 		printf("Failed to init mutexattr\n");
109 	}
110 	if (pthread_mutexattr_setrobust(&mutexattr, PTHREAD_MUTEX_ROBUST)
111 	    != 0) {
112 		printf("Can't set mutexattr robust\n");
113 	}
114 	if (pthread_mutexattr_getrobust(&mutexattr, &robust) != 0) {
115 		printf("Can't get mutexattr robust\n");
116 	} else {
117 		printf("robust in mutexattr is %d\n", robust);
118 	}
119 
120 	/* malloc and initialize the mutexes */
121 	printf("allocating and initializing %d mutexes\n", NUM_MUTEXES);
122 	for (m = 0; m < NUM_MUTEXES; m++) {
123 		if (!(mutexes[m] = malloc(sizeof(pthread_mutex_t)))) {
124 			perror("malloc failed\n");
125 		}
126 		if ((ret = pthread_mutex_init(mutexes[m], &mutexattr))) {
127 			perror("pthread_mutex_init() failed\n");
128 		}
129 	}
130 	printf("mutexes allocated and initialized successfully\n");
131 
132 	/* start children threads to walk the array, grabbing the locks */
133 	for (t = 0; t < NUM_THREADS; t++) {
134 		create_fifo_thread(worker_thread, (void *)t,
135 				   sched_get_priority_min(SCHED_FIFO));
136 	}
137 	/* wait for the children to complete */
138 	printf("joining threads\n");
139 	join_threads();
140 	/* destroy all the mutexes */
141 	for (m = 0; m < NUM_MUTEXES; m++) {
142 		if (mutexes[m]) {
143 			if ((ret = pthread_mutex_destroy(mutexes[m])))
144 				perror("pthread_mutex_destroy() failed\n");
145 			free(mutexes[m]);
146 		}
147 	}
148 
149 	return 0;
150 }
151