LCOV - code coverage report
Current view: top level - src/core/support - thd_posix.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 20 21 95.2 %
Date: 2015-10-10 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : /* Posix implementation for gpr threads. */
      35             : 
      36             : #include <grpc/support/port_platform.h>
      37             : 
      38             : #ifdef GPR_POSIX_SYNC
      39             : 
      40             : #include <pthread.h>
      41             : #include <stdlib.h>
      42             : #include <string.h>
      43             : #include <grpc/support/alloc.h>
      44             : #include <grpc/support/log.h>
      45             : #include <grpc/support/thd.h>
      46             : #include <grpc/support/useful.h>
      47             : 
      48             : struct thd_arg {
      49             :   void (*body)(void *arg); /* body of a thread */
      50             :   void *arg;               /* argument to a thread */
      51             : };
      52             : 
      53             : /* Body of every thread started via gpr_thd_new. */
      54       12063 : static void *thread_body(void *v) {
      55       12063 :   struct thd_arg a = *(struct thd_arg *)v;
      56       12063 :   gpr_free(v);
      57       12067 :   (*a.body)(a.arg);
      58       12017 :   return NULL;
      59             : }
      60             : 
      61       12067 : int gpr_thd_new(gpr_thd_id *t, void (*thd_body)(void *arg), void *arg,
      62             :                 const gpr_thd_options *options) {
      63             :   int thread_started;
      64             :   pthread_attr_t attr;
      65             :   pthread_t p;
      66       12067 :   struct thd_arg *a = gpr_malloc(sizeof(*a));
      67       12067 :   a->body = thd_body;
      68       12067 :   a->arg = arg;
      69             : 
      70       12067 :   GPR_ASSERT(pthread_attr_init(&attr) == 0);
      71       12067 :   if (gpr_thd_options_is_detached(options)) {
      72        5799 :     GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) ==
      73             :                0);
      74             :   } else {
      75        6268 :     GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) ==
      76             :                0);
      77             :   }
      78       12067 :   thread_started = (pthread_create(&p, &attr, &thread_body, a) == 0);
      79       12067 :   GPR_ASSERT(pthread_attr_destroy(&attr) == 0);
      80       12067 :   if (!thread_started) {
      81           0 :     gpr_free(a);
      82             :   }
      83       12067 :   *t = (gpr_thd_id)p;
      84       12067 :   return thread_started;
      85             : }
      86             : 
      87       36271 : gpr_thd_id gpr_thd_currentid(void) { return (gpr_thd_id)pthread_self(); }
      88             : 
      89        6268 : void gpr_thd_join(gpr_thd_id t) { pthread_join((pthread_t)t, NULL); }
      90             : 
      91             : #endif /* GPR_POSIX_SYNC */

Generated by: LCOV version 1.10