test-process-title-threadsafe.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright libuv project contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <string.h>
  24. #ifdef __APPLE__
  25. # define NUM_ITERATIONS 5
  26. #else
  27. # define NUM_ITERATIONS 50
  28. #endif
  29. static const char* titles[] = {
  30. "8L2NY0Kdj0XyNFZnmUZigIOfcWjyNr0SkMmUhKw99VLUsZFrvCQQC3XIRfNR8pjyMjXObllled",
  31. "jUAcscJN49oLSN8GdmXj2Wo34XX2T2vp2j5khfajNQarlOulp57cE130yiY53ipJFnPyTn5i82",
  32. "9niCI5icXGFS72XudhXqo5alftmZ1tpE7B3cwUmrq0CCDjC84FzBNB8XAHqvpNQfI2QAQG6ztT",
  33. "n8qXVXuG6IEHDpabJgTEiwtpY6LHMZ8MgznnMpdHARu5EywufA6hcBaQfetb0YhEsK0ykDd7JU"
  34. };
  35. static void getter_thread_body(void* arg) {
  36. char buffer[512];
  37. size_t len;
  38. for (;;) {
  39. ASSERT(0 == uv_get_process_title(buffer, sizeof(buffer)));
  40. /* The maximum size of the process title on some platforms depends on
  41. * the total size of the argv vector. It's therefore possible to read
  42. * back a title that's shorter than what we submitted.
  43. */
  44. len = strlen(buffer);
  45. ASSERT_GT(len, 0);
  46. ASSERT(
  47. 0 == strncmp(buffer, titles[0], len) ||
  48. 0 == strncmp(buffer, titles[1], len) ||
  49. 0 == strncmp(buffer, titles[2], len) ||
  50. 0 == strncmp(buffer, titles[3], len));
  51. uv_sleep(0);
  52. }
  53. }
  54. static void setter_thread_body(void* arg) {
  55. int i;
  56. for (i = 0; i < NUM_ITERATIONS; i++) {
  57. ASSERT(0 == uv_set_process_title(titles[0]));
  58. ASSERT(0 == uv_set_process_title(titles[1]));
  59. ASSERT(0 == uv_set_process_title(titles[2]));
  60. ASSERT(0 == uv_set_process_title(titles[3]));
  61. }
  62. }
  63. TEST_IMPL(process_title_threadsafe) {
  64. uv_thread_t setter_threads[4];
  65. uv_thread_t getter_thread;
  66. int i;
  67. #if defined(__sun) || defined(__CYGWIN__) || defined(__MSYS__) || \
  68. defined(__MVS__) || defined(__PASE__)
  69. RETURN_SKIP("uv_(get|set)_process_title is not implemented.");
  70. #endif
  71. ASSERT(0 == uv_set_process_title(titles[0]));
  72. ASSERT(0 == uv_thread_create(&getter_thread, getter_thread_body, NULL));
  73. for (i = 0; i < (int) ARRAY_SIZE(setter_threads); i++)
  74. ASSERT(0 == uv_thread_create(&setter_threads[i], setter_thread_body, NULL));
  75. for (i = 0; i < (int) ARRAY_SIZE(setter_threads); i++)
  76. ASSERT(0 == uv_thread_join(&setter_threads[i]));
  77. return 0;
  78. }