Blame gl/tests/test-thread_create.c

Packit Service 991b93
/* Test of gl_thread_create () macro.
Packit Service 991b93
   Copyright (C) 2011-2020 Free Software Foundation, Inc.
Packit Service 991b93
Packit Service 991b93
   This program is free software: you can redistribute it and/or modify
Packit Service 991b93
   it under the terms of the GNU General Public License as published by
Packit Service 991b93
   the Free Software Foundation; either version 3 of the License, or
Packit Service 991b93
   (at your option) any later version.
Packit Service 991b93
Packit Service 991b93
   This program is distributed in the hope that it will be useful,
Packit Service 991b93
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 991b93
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 991b93
   GNU General Public License for more details.
Packit Service 991b93
Packit Service 991b93
   You should have received a copy of the GNU General Public License
Packit Service 991b93
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service 991b93
Packit Service 991b93
/* Written by Bruno Haible <bruno@clisp.org>, 2011.  */
Packit Service 991b93
Packit Service 991b93
#include <config.h>
Packit Service 991b93
Packit Service 991b93
#include "glthread/thread.h"
Packit Service 991b93
Packit Service 991b93
#include <stdio.h>
Packit Service 991b93
#include <string.h>
Packit Service 991b93
Packit Service 991b93
#include "macros.h"
Packit Service 991b93
Packit Service 991b93
static gl_thread_t main_thread_before;
Packit Service 991b93
static gl_thread_t main_thread_after;
Packit Service 991b93
static gl_thread_t worker_thread;
Packit Service 991b93
Packit Service 991b93
static int dummy;
Packit Service 991b93
static volatile int work_done;
Packit Service 991b93
Packit Service 991b93
static void *
Packit Service 991b93
worker_thread_func (void *arg)
Packit Service 991b93
{
Packit Service 991b93
  work_done = 1;
Packit Service 991b93
  return &dummy;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
int
Packit Service 991b93
main ()
Packit Service 991b93
{
Packit Service 991b93
  main_thread_before = gl_thread_self ();
Packit Service 991b93
Packit Service 991b93
  if (glthread_create (&worker_thread, worker_thread_func, NULL) == 0)
Packit Service 991b93
    {
Packit Service 991b93
      void *ret;
Packit Service 991b93
Packit Service 991b93
      /* Check that gl_thread_self () has the same value before than after the
Packit Service 991b93
         first call to gl_thread_create ().  */
Packit Service 991b93
      main_thread_after = gl_thread_self ();
Packit Service 991b93
      ASSERT (memcmp (&main_thread_before, &main_thread_after,
Packit Service 991b93
                      sizeof (gl_thread_t))
Packit Service 991b93
              == 0);
Packit Service 991b93
Packit Service 991b93
      gl_thread_join (worker_thread, &ret;;
Packit Service 991b93
Packit Service 991b93
      /* Check the return value of the thread.  */
Packit Service 991b93
      ASSERT (ret == &dummy);
Packit Service 991b93
Packit Service 991b93
      /* Check that worker_thread_func () has finished executing.  */
Packit Service 991b93
      ASSERT (work_done);
Packit Service 991b93
Packit Service 991b93
      return 0;
Packit Service 991b93
    }
Packit Service 991b93
  else
Packit Service 991b93
    {
Packit Service 991b93
#if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
Packit Service 991b93
      fputs ("glthread_create failed\n", stderr);
Packit Service 991b93
      return 1;
Packit Service 991b93
#else
Packit Service 991b93
      fputs ("Skipping test: multithreading not enabled\n", stderr);
Packit Service 991b93
      return 77;
Packit Service 991b93
#endif
Packit Service 991b93
    }
Packit Service 991b93
}