xref: /netbsd-src/external/mit/libuv/dist/test/test-process-title-threadsafe.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
10e552da7Schristos /* Copyright libuv project contributors. All rights reserved.
20e552da7Schristos *
30e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos * of this software and associated documentation files (the "Software"), to
50e552da7Schristos * deal in the Software without restriction, including without limitation the
60e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos * furnished to do so, subject to the following conditions:
90e552da7Schristos *
100e552da7Schristos * The above copyright notice and this permission notice shall be included in
110e552da7Schristos * all copies or substantial portions of the Software.
120e552da7Schristos *
130e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos * IN THE SOFTWARE.
200e552da7Schristos */
210e552da7Schristos 
220e552da7Schristos 
230e552da7Schristos #include "uv.h"
240e552da7Schristos #include "task.h"
250e552da7Schristos 
260e552da7Schristos #include <string.h>
270e552da7Schristos 
280e552da7Schristos #ifdef __APPLE__
290e552da7Schristos # define NUM_ITERATIONS 5
300e552da7Schristos #else
310e552da7Schristos # define NUM_ITERATIONS 50
320e552da7Schristos #endif
330e552da7Schristos 
340e552da7Schristos static const char* titles[] = {
350e552da7Schristos   "8L2NY0Kdj0XyNFZnmUZigIOfcWjyNr0SkMmUhKw99VLUsZFrvCQQC3XIRfNR8pjyMjXObllled",
360e552da7Schristos   "jUAcscJN49oLSN8GdmXj2Wo34XX2T2vp2j5khfajNQarlOulp57cE130yiY53ipJFnPyTn5i82",
370e552da7Schristos   "9niCI5icXGFS72XudhXqo5alftmZ1tpE7B3cwUmrq0CCDjC84FzBNB8XAHqvpNQfI2QAQG6ztT",
380e552da7Schristos   "n8qXVXuG6IEHDpabJgTEiwtpY6LHMZ8MgznnMpdHARu5EywufA6hcBaQfetb0YhEsK0ykDd7JU"
390e552da7Schristos };
400e552da7Schristos 
getter_thread_body(void * arg)410e552da7Schristos static void getter_thread_body(void* arg) {
42*5f2f4271Schristos   uv_sem_t* getter_sem;
430e552da7Schristos   char buffer[512];
440e552da7Schristos   size_t len;
450e552da7Schristos 
46*5f2f4271Schristos   getter_sem = arg;
47*5f2f4271Schristos 
48*5f2f4271Schristos   while (UV_EAGAIN == uv_sem_trywait(getter_sem)) {
490e552da7Schristos     ASSERT(0 == uv_get_process_title(buffer, sizeof(buffer)));
500e552da7Schristos 
510e552da7Schristos     /* The maximum size of the process title on some platforms depends on
520e552da7Schristos      * the total size of the argv vector. It's therefore possible to read
530e552da7Schristos      * back a title that's shorter than what we submitted.
540e552da7Schristos      */
550e552da7Schristos     len = strlen(buffer);
560e552da7Schristos     ASSERT_GT(len, 0);
570e552da7Schristos 
580e552da7Schristos     ASSERT(
590e552da7Schristos       0 == strncmp(buffer, titles[0], len) ||
600e552da7Schristos       0 == strncmp(buffer, titles[1], len) ||
610e552da7Schristos       0 == strncmp(buffer, titles[2], len) ||
620e552da7Schristos       0 == strncmp(buffer, titles[3], len));
630e552da7Schristos 
640e552da7Schristos     uv_sleep(0);
650e552da7Schristos   }
660e552da7Schristos }
670e552da7Schristos 
680e552da7Schristos 
setter_thread_body(void * arg)690e552da7Schristos static void setter_thread_body(void* arg) {
700e552da7Schristos   int i;
710e552da7Schristos 
720e552da7Schristos   for (i = 0; i < NUM_ITERATIONS; i++) {
730e552da7Schristos     ASSERT(0 == uv_set_process_title(titles[0]));
740e552da7Schristos     ASSERT(0 == uv_set_process_title(titles[1]));
750e552da7Schristos     ASSERT(0 == uv_set_process_title(titles[2]));
760e552da7Schristos     ASSERT(0 == uv_set_process_title(titles[3]));
770e552da7Schristos   }
780e552da7Schristos }
790e552da7Schristos 
800e552da7Schristos 
TEST_IMPL(process_title_threadsafe)810e552da7Schristos TEST_IMPL(process_title_threadsafe) {
820e552da7Schristos   uv_thread_t setter_threads[4];
830e552da7Schristos   uv_thread_t getter_thread;
84*5f2f4271Schristos   uv_sem_t getter_sem;
850e552da7Schristos   int i;
860e552da7Schristos 
870e552da7Schristos #if defined(__sun) || defined(__CYGWIN__) || defined(__MSYS__) || \
880e552da7Schristos     defined(__MVS__) || defined(__PASE__)
890e552da7Schristos   RETURN_SKIP("uv_(get|set)_process_title is not implemented.");
900e552da7Schristos #endif
910e552da7Schristos 
920e552da7Schristos   ASSERT(0 == uv_set_process_title(titles[0]));
93*5f2f4271Schristos 
94*5f2f4271Schristos   ASSERT_EQ(0, uv_sem_init(&getter_sem, 0));
95*5f2f4271Schristos   ASSERT_EQ(0,
96*5f2f4271Schristos             uv_thread_create(&getter_thread, getter_thread_body, &getter_sem));
970e552da7Schristos 
980e552da7Schristos   for (i = 0; i < (int) ARRAY_SIZE(setter_threads); i++)
990e552da7Schristos     ASSERT(0 == uv_thread_create(&setter_threads[i], setter_thread_body, NULL));
1000e552da7Schristos 
1010e552da7Schristos   for (i = 0; i < (int) ARRAY_SIZE(setter_threads); i++)
1020e552da7Schristos     ASSERT(0 == uv_thread_join(&setter_threads[i]));
1030e552da7Schristos 
104*5f2f4271Schristos   uv_sem_post(&getter_sem);
105*5f2f4271Schristos   ASSERT_EQ(0, uv_thread_join(&getter_thread));
106*5f2f4271Schristos   uv_sem_destroy(&getter_sem);
107*5f2f4271Schristos 
1080e552da7Schristos   return 0;
1090e552da7Schristos }
110