Print this page
8158 Want named threads API
9857 proc manpages should have LIBRARY section

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libc/port/threads/thr.c
          +++ new/usr/src/lib/libc/port/threads/thr.c
↓ open down ↓ 17 lines elided ↑ open up ↑
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright (c) 2016 by Delphix. All rights reserved.
  25   25   * Copyright (c) 2017 by The MathWorks, Inc. All rights reserved.
  26   26   */
  27   27  /*
  28      - * Copyright 2016 Joyent, Inc.
       28 + * Copyright 2018 Joyent, Inc.
  29   29   */
  30   30  
  31   31  #include "lint.h"
  32   32  #include "thr_uberdata.h"
  33   33  #include <pthread.h>
  34   34  #include <procfs.h>
  35   35  #include <sys/uio.h>
  36   36  #include <ctype.h>
  37   37  #include "libc.h"
  38   38  
↓ open down ↓ 514 lines elided ↑ open up ↑
 553  553          if (ulwp && ulwp->ul_dead) {
 554  554                  ulwp_unlock(ulwp, udp);
 555  555                  ulwp = NULL;
 556  556          }
 557  557  
 558  558          return (ulwp);
 559  559  }
 560  560  
 561  561  int
 562  562  _thrp_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
 563      -    long flags, thread_t *new_thread, size_t guardsize)
      563 +    long flags, thread_t *new_thread, size_t guardsize, const char *name)
 564  564  {
 565  565          ulwp_t *self = curthread;
 566  566          uberdata_t *udp = self->ul_uberdata;
 567  567          ucontext_t uc;
 568  568          uint_t lwp_flags;
 569  569          thread_t tid;
 570  570          int error;
 571  571          ulwp_t *ulwp;
 572  572  
 573  573          /*
↓ open down ↓ 134 lines elided ↑ open up ↑
 708  708          lmutex_unlock(&udp->link_lock);
 709  709  
 710  710          if (__td_event_report(self, TD_CREATE, udp)) {
 711  711                  self->ul_td_evbuf.eventnum = TD_CREATE;
 712  712                  self->ul_td_evbuf.eventdata = (void *)(uintptr_t)tid;
 713  713                  tdb_event(TD_CREATE, udp);
 714  714          }
 715  715  
 716  716          exit_critical(self);
 717  717  
      718 +        if (name != NULL)
      719 +                (void) pthread_setname_np(tid, name);
      720 +
 718  721          if (!(flags & THR_SUSPENDED))
 719  722                  (void) _thrp_continue(tid, TSTP_REGULAR);
 720  723  
 721  724          return (0);
 722  725  }
 723  726  
 724  727  int
 725  728  thr_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
 726  729      long flags, thread_t *new_thread)
 727  730  {
 728      -        return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 0));
      731 +        return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 0,
      732 +            NULL));
 729  733  }
 730  734  
 731  735  /*
 732  736   * A special cancellation cleanup hook for DCE.
 733  737   * cleanuphndlr, when it is not NULL, will contain a callback
 734  738   * function to be called before a thread is terminated in
 735  739   * thr_exit() as a result of being cancelled.
 736  740   */
 737  741  static void (*cleanuphndlr)(void) = NULL;
 738  742  
↓ open down ↓ 1652 lines elided ↑ open up ↑
2391 2395  {
2392 2396          return (MINSTACK);
2393 2397  }
2394 2398  
2395 2399  int
2396 2400  __nthreads(void)
2397 2401  {
2398 2402          return (curthread->ul_uberdata->nthreads);
2399 2403  }
2400 2404  
     2405 +/* "/proc/self/lwp/%u/lwpname" w/o stdio */
     2406 +static void
     2407 +lwpname_path(pthread_t tid, char *buf, size_t bufsize)
     2408 +{
     2409 +        (void) strlcpy(buf, "/proc/self/lwp/", bufsize);
     2410 +        ultos((uint64_t)tid, 10, buf + strlen(buf));
     2411 +        (void) strlcat(buf, "/lwpname", bufsize);
     2412 +}
     2413 +
     2414 +#pragma weak pthread_setname_np = thr_setname
     2415 +int
     2416 +thr_setname(pthread_t tid, const char *name)
     2417 +{
     2418 +        extern ssize_t __write(int, const void *, size_t);
     2419 +        char path[PATH_MAX];
     2420 +        int saved_errno;
     2421 +        size_t len;
     2422 +        ssize_t n;
     2423 +        int fd;
     2424 +
     2425 +        if (name == NULL)
     2426 +                name = "";
     2427 +
     2428 +        len = strlen(name) + 1;
     2429 +        if (len > THREAD_NAME_MAX)
     2430 +                return (ERANGE);
     2431 +
     2432 +        lwpname_path(tid, path, sizeof (path));
     2433 +
     2434 +        if ((fd = __open(path, O_WRONLY, 0)) < 0) {
     2435 +                if (errno == ENOENT)
     2436 +                        errno = ESRCH;
     2437 +                return (errno);
     2438 +        }
     2439 +
     2440 +        n = __write(fd, name, len);
     2441 +        saved_errno = errno;
     2442 +        (void) __close(fd);
     2443 +
     2444 +        if (n < 0)
     2445 +                return (saved_errno);
     2446 +        if (n != len)
     2447 +                return (EFAULT);
     2448 +        return (0);
     2449 +}
     2450 +
     2451 +#pragma weak pthread_getname_np = thr_getname
     2452 +int
     2453 +thr_getname(pthread_t tid, char *buf, size_t bufsize)
     2454 +{
     2455 +        extern ssize_t __read(int, void *, size_t);
     2456 +        char name[THREAD_NAME_MAX];
     2457 +        char path[PATH_MAX];
     2458 +        int saved_errno;
     2459 +        ssize_t n;
     2460 +        int fd;
     2461 +
     2462 +        if (buf == NULL)
     2463 +                return (EINVAL);
     2464 +
     2465 +        lwpname_path(tid, path, sizeof (path));
     2466 +
     2467 +        if ((fd = __open(path, O_RDONLY, 0)) < 0) {
     2468 +                if (errno == ENOENT)
     2469 +                        errno = ESRCH;
     2470 +                return (errno);
     2471 +        }
     2472 +
     2473 +        n = __read(fd, name, sizeof (name));
     2474 +        saved_errno = errno;
     2475 +        (void) __close(fd);
     2476 +
     2477 +        if (n < 0)
     2478 +                return (saved_errno);
     2479 +        if (n != sizeof (name))
     2480 +                return (EFAULT);
     2481 +        if (strlcpy(buf, name, bufsize) >= bufsize)
     2482 +                return (ERANGE);
     2483 +        return (0);
     2484 +}
     2485 +
2401 2486  /*
2402 2487   * XXX
2403 2488   * The remainder of this file implements the private interfaces to java for
2404 2489   * garbage collection.  It is no longer used, at least by java 1.2.
2405 2490   * It can all go away once all old JVMs have disappeared.
2406 2491   */
2407 2492  
2408 2493  int     suspendingallmutators;  /* when non-zero, suspending all mutators. */
2409 2494  int     suspendedallmutators;   /* when non-zero, all mutators suspended. */
2410 2495  int     mutatorsbarrier;        /* when non-zero, mutators barrier imposed. */
↓ open down ↓ 465 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX