//bench/incr_rwlock1.c
#include	"unpipc.h"
#include	<synch.h>		/*    Solaris */

void	 Rw_wrlock(rwlock_t *rwptr);
void	 Rw_unlock(rwlock_t *rwptr);

#define	MAXNTHREADS	100

int		nloop;

struct {
  rwlock_t	rwlock;			/*   Solaris */
  long	counter;
} shared;					/*  0 -> USYNC_THREAD */

void	*incr(void *);

int
main(int argc, char **argv)
{
	int		i, nthreads;
	pthread_t	tid[MAXNTHREADS];

	if (argc != 3)
		err_quit("usage: incr_rwlock1 <#loops> <#threads>");
	nloop = atoi(argv[1]);
	nthreads = min(atoi(argv[2]), MAXNTHREADS);

		/*     */
	Rw_wrlock(&shared.rwlock);

		/*    */
	Set_concurrency(nthreads);
	for (i = 0; i < nthreads; i++) {
		Pthread_create(&tid[i], NULL, incr, NULL);
	}
		/*      */
	Start_time();
	Rw_unlock(&shared.rwlock);

		/*     */
	for (i = 0; i < nthreads; i++) {
		Pthread_join(tid[i], NULL);
	}
	printf("microseconds: %.0f usec\n", Stop_time());
	if (shared.counter != nloop * nthreads)
		printf("error: counter = %ld\n", shared.counter);

	exit(0);
}
