Blame test/test-process-title-threadsafe.c

Packit b5b901
/* Copyright libuv project contributors. All rights reserved.
Packit b5b901
*
Packit b5b901
* Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
* of this software and associated documentation files (the "Software"), to
Packit b5b901
* deal in the Software without restriction, including without limitation the
Packit b5b901
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
* sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
* furnished to do so, subject to the following conditions:
Packit b5b901
*
Packit b5b901
* The above copyright notice and this permission notice shall be included in
Packit b5b901
* all copies or substantial portions of the Software.
Packit b5b901
*
Packit b5b901
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
* IN THE SOFTWARE.
Packit b5b901
*/
Packit b5b901
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "task.h"
Packit b5b901
Packit b5b901
#include <string.h>
Packit b5b901
Packit b5b901
#ifdef __APPLE__
Packit Service e08953
# define NUM_ITERATIONS 5
Packit b5b901
#else
Packit b5b901
# define NUM_ITERATIONS 50
Packit b5b901
#endif
Packit b5b901
Packit b5b901
static const char* titles[] = {
Packit b5b901
  "8L2NY0Kdj0XyNFZnmUZigIOfcWjyNr0SkMmUhKw99VLUsZFrvCQQC3XIRfNR8pjyMjXObllled",
Packit b5b901
  "jUAcscJN49oLSN8GdmXj2Wo34XX2T2vp2j5khfajNQarlOulp57cE130yiY53ipJFnPyTn5i82",
Packit b5b901
  "9niCI5icXGFS72XudhXqo5alftmZ1tpE7B3cwUmrq0CCDjC84FzBNB8XAHqvpNQfI2QAQG6ztT",
Packit b5b901
  "n8qXVXuG6IEHDpabJgTEiwtpY6LHMZ8MgznnMpdHARu5EywufA6hcBaQfetb0YhEsK0ykDd7JU"
Packit b5b901
};
Packit b5b901
Packit b5b901
static void getter_thread_body(void* arg) {
Packit b5b901
  char buffer[512];
Packit Service e08953
  size_t len;
Packit b5b901
Packit b5b901
  for (;;) {
Packit b5b901
    ASSERT(0 == uv_get_process_title(buffer, sizeof(buffer)));
Packit Service e08953
Packit Service e08953
    /* The maximum size of the process title on some platforms depends on
Packit Service e08953
     * the total size of the argv vector. It's therefore possible to read
Packit Service e08953
     * back a title that's shorter than what we submitted.
Packit Service e08953
     */
Packit Service e08953
    len = strlen(buffer);
Packit Service e08953
    ASSERT_GT(len, 0);
Packit Service e08953
Packit b5b901
    ASSERT(
Packit Service e08953
      0 == strncmp(buffer, titles[0], len) ||
Packit Service e08953
      0 == strncmp(buffer, titles[1], len) ||
Packit Service e08953
      0 == strncmp(buffer, titles[2], len) ||
Packit Service e08953
      0 == strncmp(buffer, titles[3], len));
Packit b5b901
Packit b5b901
    uv_sleep(0);
Packit b5b901
  }
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void setter_thread_body(void* arg) {
Packit b5b901
  int i;
Packit b5b901
Packit b5b901
  for (i = 0; i < NUM_ITERATIONS; i++) {
Packit b5b901
    ASSERT(0 == uv_set_process_title(titles[0]));
Packit b5b901
    ASSERT(0 == uv_set_process_title(titles[1]));
Packit b5b901
    ASSERT(0 == uv_set_process_title(titles[2]));
Packit b5b901
    ASSERT(0 == uv_set_process_title(titles[3]));
Packit b5b901
  }
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
TEST_IMPL(process_title_threadsafe) {
Packit b5b901
  uv_thread_t setter_threads[4];
Packit b5b901
  uv_thread_t getter_thread;
Packit b5b901
  int i;
Packit b5b901
Packit b5b901
#if defined(__sun) || defined(__CYGWIN__) || defined(__MSYS__) || \
Packit Service e08953
    defined(__MVS__) || defined(__PASE__)
Packit b5b901
  RETURN_SKIP("uv_(get|set)_process_title is not implemented.");
Packit Service e08953
#endif
Packit b5b901
Packit b5b901
  ASSERT(0 == uv_set_process_title(titles[0]));
Packit b5b901
  ASSERT(0 == uv_thread_create(&getter_thread, getter_thread_body, NULL));
Packit b5b901
Packit b5b901
  for (i = 0; i < (int) ARRAY_SIZE(setter_threads); i++)
Packit b5b901
    ASSERT(0 == uv_thread_create(&setter_threads[i], setter_thread_body, NULL));
Packit b5b901
Packit b5b901
  for (i = 0; i < (int) ARRAY_SIZE(setter_threads); i++)
Packit b5b901
    ASSERT(0 == uv_thread_join(&setter_threads[i]));
Packit b5b901
Packit b5b901
  return 0;
Packit b5b901
}