Blame nptl/tst-memstream.c

Packit 6c4009
/* Test for open_memstream locking.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* This test checks if concurrent writes to a FILE created with
Packit 6c4009
   open_memstream are correctly interleaved without loss or corruption
Packit 6c4009
   of data.  Large number of concurrent fputc operations are used
Packit 6c4009
   and in the end the bytes written to the memstream buffer are
Packit 6c4009
   counted to see if they all got recorded.
Packit 6c4009
Packit 6c4009
   This is a regression test to see if the single threaded stdio
Packit 6c4009
   optimization broke multi-threaded open_memstream usage.  */
Packit 6c4009
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/xthread.h>
Packit 6c4009
Packit 6c4009
enum
Packit 6c4009
{
Packit 6c4009
  thread_count = 2,
Packit 6c4009
  byte_count = 1000000,
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
struct closure
Packit 6c4009
{
Packit 6c4009
  FILE *fp;
Packit 6c4009
  char b;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
thread_func (void *closure)
Packit 6c4009
{
Packit 6c4009
  struct closure *args = closure;
Packit 6c4009
Packit 6c4009
  for (int i = 0; i < byte_count; ++i)
Packit 6c4009
    fputc (args->b, args->fp);
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char *buffer = NULL;
Packit 6c4009
  size_t buffer_length = 0;
Packit 6c4009
  FILE *fp = open_memstream (&buffer, &buffer_length);
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    FAIL_RET ("open_memstream: %m");
Packit 6c4009
Packit 6c4009
  pthread_t threads[thread_count];
Packit 6c4009
  struct closure args[thread_count];
Packit 6c4009
Packit 6c4009
  for (int i = 0; i < thread_count; ++i)
Packit 6c4009
    {
Packit 6c4009
      args[i].fp = fp;
Packit 6c4009
      args[i].b = 'A' + i;
Packit 6c4009
      threads[i] = xpthread_create (NULL, thread_func, args + i);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (int i = 0; i < thread_count; ++i)
Packit 6c4009
    xpthread_join (threads[i]);
Packit 6c4009
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  if (buffer_length != thread_count * byte_count)
Packit 6c4009
    FAIL_RET ("unexpected number of written bytes: %zu (should be %d)",
Packit 6c4009
	      buffer_length, thread_count * byte_count);
Packit 6c4009
Packit 6c4009
  /* Verify that each thread written its unique character byte_count times.  */
Packit 6c4009
  size_t counts[thread_count] = { 0, };
Packit 6c4009
  for (size_t i = 0; i < buffer_length; ++i)
Packit 6c4009
    {
Packit 6c4009
      if (buffer[i] < 'A' || buffer[i] >= 'A' + thread_count)
Packit 6c4009
	FAIL_RET ("written byte at %zu out of range: %d", i, buffer[i] & 0xFF);
Packit 6c4009
      ++counts[buffer[i] - 'A'];
Packit 6c4009
    }
Packit 6c4009
  for (int i = 0; i < thread_count; ++i)
Packit 6c4009
    if (counts[i] != byte_count)
Packit 6c4009
      FAIL_RET ("incorrect write count for thread %d: %zu (should be %d)", i,
Packit 6c4009
		counts[i], byte_count);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TIMEOUT 100
Packit 6c4009
#include <support/test-driver.c>